1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
"""Test sandboxjs.py and related code."""
import os
import shutil
import threading
from pathlib import Path
from typing import Any
import pytest
from cwl_utils import expression, sandboxjs
from .util import needs_podman, needs_singularity, needs_udocker
node_versions = [
("v0.8.26\n", False),
("v0.10.25\n", False),
("v0.10.26\n", True),
("v4.4.2\n", True),
("v7.7.3\n", True),
]
@pytest.mark.parametrize("version,supported", node_versions)
def test_node_version(version: str, supported: bool, mocker: Any) -> None:
mocked_subprocess = mocker.patch("cwl_utils.sandboxjs.subprocess")
mocked_subprocess.check_output = mocker.Mock(return_value=version)
assert sandboxjs.check_js_threshold_version("node") == supported
def test_value_from_two_concatenated_expressions() -> None:
js_engine = sandboxjs.get_js_engine()
js_engine.have_node_slim = False # type: ignore[attr-defined]
js_engine.localdata = threading.local() # type: ignore[attr-defined]
assert (
expression.do_eval(
'$("a ")$("string")',
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
)
== "a string"
)
def hide_nodejs(temp_dir: Path) -> str:
"""Generate a new PATH that hides node{js,}."""
paths: list[str] = os.environ.get("PATH", "").split(":")
names: list[str] = []
if "/bin" in paths:
bin_path = Path("/bin")
if (
bin_path.is_symlink()
and os.readlink(bin_path) == "usr/bin"
and "/usr/bin" in paths
):
paths.remove("/bin")
for name in ("nodejs", "node"):
path = shutil.which(name, path=":".join(paths))
if path:
names.append(path)
for name in names:
dirname = os.path.dirname(name)
if dirname in paths:
paths.remove(dirname)
new_dir = temp_dir / os.path.basename(dirname)
new_dir.mkdir()
for entry in os.listdir(dirname):
if entry not in ("nodejs", "node"):
os.symlink(os.path.join(dirname, entry), new_dir / entry)
paths.append(str(new_dir))
return ":".join(paths)
@needs_podman
def test_value_from_two_concatenated_expressions_podman(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Javascript test using podman."""
new_paths = hide_nodejs(tmp_path)
with monkeypatch.context() as m:
m.setenv("PATH", new_paths)
js_engine = sandboxjs.get_js_engine()
js_engine.have_node_slim = False # type: ignore[attr-defined]
js_engine.localdata = threading.local() # type: ignore[attr-defined]
assert (
expression.do_eval(
'$("a ")$("string")',
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
container_engine="podman",
)
== "a string"
)
@needs_udocker
def test_value_from_two_concatenated_expressions_udocker(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Javascript test using udocker."""
new_paths = hide_nodejs(tmp_path)
with monkeypatch.context() as m:
m.setenv("PATH", new_paths)
js_engine = sandboxjs.get_js_engine()
js_engine.have_node_slim = False # type: ignore[attr-defined]
js_engine.localdata = threading.local() # type: ignore[attr-defined]
assert (
expression.do_eval(
'$("a ")$("string")',
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
container_engine="udocker",
)
== "a string"
)
@needs_singularity
def test_value_from_two_concatenated_expressions_singularity(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Javascript test using Singularity."""
new_paths = hide_nodejs(tmp_path)
with monkeypatch.context() as m:
m.setenv("PATH", new_paths)
js_engine = sandboxjs.get_js_engine()
js_engine.have_node_slim = False # type: ignore[attr-defined]
js_engine.localdata = threading.local() # type: ignore[attr-defined]
assert (
expression.do_eval(
'$("a ")$("string")',
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
container_engine="singularity",
)
== "a string"
)
@needs_singularity
def test_singularity_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Affirm that CWL_SINGULARIT_CACHE is respected."""
bin_path = tmp_path / "no_nodejs"
bin_path.mkdir()
new_paths = hide_nodejs(bin_path)
cache_path = tmp_path / "singularity_cache"
cache_path.mkdir()
with monkeypatch.context() as m:
m.setenv("PATH", new_paths)
m.setenv("CWL_SINGULARITY_CACHE", str(cache_path))
js_engine = sandboxjs.get_js_engine()
js_engine.localdata = threading.local() # type: ignore[attr-defined]
js_engine.have_node_slim = False # type: ignore[attr-defined]
assert (
expression.do_eval(
"$(42*23)",
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
container_engine="singularity",
)
== 42 * 23
)
assert (cache_path / "node_alpine.sif").exists()
def test_caches_js_processes(mocker: Any) -> None:
sandboxjs.exec_js_process("7", context="{}")
mocked_new_js_proc = mocker.patch("cwl_utils.sandboxjs.new_js_proc")
sandboxjs.exec_js_process("7", context="{}")
mocked_new_js_proc.assert_not_called()
|