Skip to content

Commit 35a7346

Browse files
refactor: inspect_image test
1 parent c51d312 commit 35a7346

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

tests/test_singularity.py

Lines changed: 24 additions & 8 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
@@ -194,18 +195,13 @@ def test_singularity_local_sandbox_image(tmp_path: Path):
194195
else:
195196
pytest.skip(f"Failed to build the singularity image: {build.stderr}")
196197

197-
198198
@needs_singularity
199199
def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
200200
workdir = tmp_path / "working_dir"
201201
workdir.mkdir()
202202
repo_path = workdir / "container_repo"
203203
image_path = repo_path / "alpine"
204204

205-
# test path does not exists
206-
res_inspect = _inspect_singularity_image(str(image_path))
207-
assert res_inspect is False
208-
209205
# test image exists
210206
repo_path.mkdir()
211207
cmd = [
@@ -217,14 +213,34 @@ def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPat
217213
]
218214
build = subprocess.run(cmd, capture_output=True, text=True)
219215
if build.returncode == 0:
220-
# Verify the path is a container image
216+
# Verify the path is a correct container image
221217
res_inspect = _inspect_singularity_image(image_path)
222218
assert res_inspect is True
223219

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

228244
monkeypatch.setattr("cwltool.singularity.run", mock_failed_subprocess)
229-
res_inspect = _inspect_singularity_image(str(image_path))
245+
res_inspect = _inspect_singularity_image("/tmp/container_repo/alpine")
230246
assert res_inspect is False

0 commit comments

Comments
 (0)