Skip to content

Commit 6f96b54

Browse files
refactor: inspect_image test
1 parent c51d312 commit 6f96b54

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

tests/test_singularity.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests to find local Singularity image."""
22

3+
import json
34
import shutil
45
import subprocess
56
from pathlib import Path
@@ -202,10 +203,6 @@ def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPat
202203
repo_path = workdir / "container_repo"
203204
image_path = repo_path / "alpine"
204205

205-
# test path does not exists
206-
res_inspect = _inspect_singularity_image(str(image_path))
207-
assert res_inspect is False
208-
209206
# test image exists
210207
repo_path.mkdir()
211208
cmd = [
@@ -217,14 +214,36 @@ def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPat
217214
]
218215
build = subprocess.run(cmd, capture_output=True, text=True)
219216
if build.returncode == 0:
220-
# Verify the path is a container image
217+
# Verify the path is a correct container image
221218
res_inspect = _inspect_singularity_image(image_path)
222219
assert res_inspect is True
223220

224-
# test wrong subprocess call
221+
# test wrong json output
222+
def mock_subprocess_run(*args, **kwargs):
223+
class Result:
224+
returncode = 0
225+
stdout = "not-json"
226+
227+
return Result()
228+
229+
monkeypatch.setattr("cwltool.singularity.run", mock_subprocess_run)
230+
res_inspect = _inspect_singularity_image(image_path)
231+
assert res_inspect is False
232+
else:
233+
pytest.skip(f"singularity sandbox image build didn't worked: {build.stderr}")
234+
235+
236+
def test_singularity_sandbox_image_not_exists():
237+
image_path = "/tmp/not_existing/image"
238+
res_inspect = _inspect_singularity_image(image_path)
239+
assert res_inspect is False
240+
241+
242+
def test_inspect_image_wrong_sb_call(monkeypatch: pytest.MonkeyPatch):
243+
225244
def mock_failed_subprocess(*args, **kwargs):
226245
raise subprocess.CalledProcessError(returncode=1, cmd=args[0])
227-
246+
228247
monkeypatch.setattr("cwltool.singularity.run", mock_failed_subprocess)
229-
res_inspect = _inspect_singularity_image(str(image_path))
230-
assert res_inspect is False
248+
res_inspect = _inspect_singularity_image("/tmp/container_repo/alpine")
249+
assert res_inspect is False

tests/test_tmpdir.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_dockerfile_singularity_build(monkeypatch: pytest.MonkeyPatch, tmp_path:
288288
@needs_singularity
289289
def test_singularity_get_image_from_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
290290
"""Test that SingularityCommandLineJob.get_image correctly handle sandbox image."""
291-
291+
292292
(tmp_path / "out").mkdir(exist_ok=True)
293293
tmp_outdir_prefix = tmp_path / "out"
294294
(tmp_path / "3").mkdir(exist_ok=True)
@@ -330,7 +330,9 @@ def test_singularity_get_image_from_sandbox(monkeypatch: pytest.MonkeyPatch, tmp
330330
image_path.mkdir()
331331

332332
# directory exists but is not an image
333-
monkeypatch.setattr("cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: False)
333+
monkeypatch.setattr(
334+
"cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: False
335+
)
334336
req = {"class": "DockerRequirement", "dockerPull": f"{image_path}"}
335337
res = SingularityCommandLineJob(
336338
builder, {}, CommandLineTool.make_path_mapper, [], [], ""
@@ -344,9 +346,11 @@ def test_singularity_get_image_from_sandbox(monkeypatch: pytest.MonkeyPatch, tmp
344346
assert res is False
345347

346348
# directory exists and is an image:
347-
monkeypatch.setattr("cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: True)
349+
monkeypatch.setattr(
350+
"cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: True
351+
)
348352
req = {"class": "DockerRequirement", "dockerPull": f"{image_path}"}
349-
res = SingularityCommandLineJob(
353+
res = SingularityCommandLineJob(
350354
builder, {}, CommandLineTool.make_path_mapper, [], [], ""
351355
).get_image(
352356
req,

0 commit comments

Comments
 (0)