Skip to content

Commit 7d2e1ef

Browse files
committed
Support using udocker to run nodejs
Fixes: #153
1 parent 9453f0b commit 7d2e1ef

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

cwl_utils/sandboxjs.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __del__(self) -> None:
118118
try:
119119
with open(cidfile[0]) as inp_stream:
120120
p = subprocess.Popen( # nosec
121-
["docker", "kill", inp_stream.read()],
121+
[args[0], "kill", inp_stream.read()],
122122
shell=False, # nosec
123123
)
124124
try:
@@ -318,6 +318,8 @@ def new_js_proc(
318318
nodeimg = "docker.io/node:alpine"
319319
if container_engine == "singularity":
320320
nodeimg = f"docker://{nodeimg}"
321+
elif container_engine == "podman":
322+
nodeimg = "docker.io/library/node:alpine"
321323

322324
if not self.have_node_slim:
323325
singularity_cache: Optional[str] = None
@@ -338,6 +340,16 @@ def new_js_proc(
338340
)
339341
if singularityimgs:
340342
nodeimg = singularityimgs[0]
343+
elif container_engine == "udocker":
344+
matches = re.search(
345+
re.escape(nodeimg),
346+
subprocess.check_output( # nosec
347+
[container_engine, "images"],
348+
universal_newlines=True,
349+
),
350+
)
351+
if matches:
352+
dockerimgs = matches[0]
341353
else:
342354
raise Exception(
343355
f"Unknown container_engine: {container_engine}."
@@ -367,7 +379,10 @@ def new_js_proc(
367379
)
368380
self.have_node_slim = True
369381
nodejs_commands = [container_engine]
370-
if container_engine != "singularity":
382+
if (
383+
container_engine != "singularity"
384+
and "udocker" not in container_engine
385+
):
371386
nodejs_commands.extend(
372387
[
373388
"run",
@@ -379,7 +394,7 @@ def new_js_proc(
379394
"--rm",
380395
]
381396
)
382-
else:
397+
elif "singularity" in container_engine:
383398
nodejs_commands.extend(
384399
[
385400
"exec",
@@ -389,6 +404,15 @@ def new_js_proc(
389404
"--userns" if singularity_supports_userns() else "--pid",
390405
]
391406
)
407+
elif "udocker" in container_engine:
408+
nodejs_commands.extend(
409+
[
410+
"run",
411+
"--device=/dev/stdin",
412+
"--device=/dev/stdout",
413+
"--device=/dev/stderr",
414+
]
415+
)
392416
nodejs_commands.extend(
393417
[
394418
nodeimg,

tests/test_js_sandbox.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from cwl_utils import expression, sandboxjs
1111

12-
from .util import needs_podman, needs_singularity
12+
from .util import needs_podman, needs_singularity, needs_udocker
1313

1414
node_versions = [
1515
("v0.8.26\n", False),
@@ -101,6 +101,32 @@ def test_value_from_two_concatenated_expressions_podman(
101101
)
102102

103103

104+
@needs_udocker
105+
def test_value_from_two_concatenated_expressions_udocker(
106+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
107+
) -> None:
108+
"""Javascript test using udocker."""
109+
new_paths = hide_nodejs(tmp_path)
110+
with monkeypatch.context() as m:
111+
m.setenv("PATH", new_paths)
112+
js_engine = sandboxjs.get_js_engine()
113+
js_engine.have_node_slim = False # type: ignore[attr-defined]
114+
js_engine.localdata = threading.local() # type: ignore[attr-defined]
115+
assert (
116+
expression.do_eval(
117+
'$("a ")$("string")',
118+
{},
119+
[{"class": "InlineJavascriptRequirement"}],
120+
None,
121+
None,
122+
{},
123+
cwlVersion="v1.0",
124+
container_engine="udocker",
125+
)
126+
== "a string"
127+
)
128+
129+
104130
@needs_singularity
105131
def test_value_from_two_concatenated_expressions_singularity(
106132
tmp_path: Path, monkeypatch: pytest.MonkeyPatch

tests/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def get_data(filename: str) -> str:
3333
not bool(shutil.which("podman")),
3434
reason="Requires the podman executable on the system path.",
3535
)
36+
37+
needs_udocker = pytest.mark.skipif(
38+
not bool(shutil.which("udocker")),
39+
reason="Requires the udocker executable on the system path.",
40+
)

0 commit comments

Comments
 (0)