Skip to content

Commit 08cd438

Browse files
test: better test conditions
1 parent 2dbcfc8 commit 08cd438

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

tests/test_singularity.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_singularity_local_sandbox_image(tmp_path: Path):
175175
"singularity",
176176
"build",
177177
"--sandbox",
178-
container_path / "alpine",
178+
str(container_path / "alpine"),
179179
"docker://alpine:latest",
180180
]
181181

@@ -196,25 +196,35 @@ def test_singularity_local_sandbox_image(tmp_path: Path):
196196

197197

198198
@needs_singularity
199-
def test_singularity_inspect_image(tmp_path: Path):
199+
def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
200200
workdir = tmp_path / "working_dir"
201201
workdir.mkdir()
202-
with working_directory(workdir):
203-
# build a sandbox image
204-
repo_path = workdir / "container_repo"
205-
image_path = repo_path / "alpine"
206-
res_inspect = _inspect_singularity_image(str(image_path))
207-
assert res_inspect is False
208-
repo_path.mkdir()
209-
cmd = [
210-
"singularity",
211-
"build",
212-
"--sandbox",
213-
image_path,
214-
"docker://alpine:latest",
215-
]
202+
repo_path = workdir / "container_repo"
203+
image_path = repo_path / "alpine"
204+
205+
# test path does not exists
206+
res_inspect = _inspect_singularity_image(str(image_path))
207+
assert res_inspect is False
208+
209+
# test image exists
210+
repo_path.mkdir()
211+
cmd = [
212+
"singularity",
213+
"build",
214+
"--sandbox",
215+
str(image_path),
216+
"docker://alpine:latest",
217+
]
216218
build = subprocess.run(cmd, capture_output=True, text=True)
217219
if build.returncode == 0:
218220
# Verify the path is a container image
219221
res_inspect = _inspect_singularity_image(image_path)
220222
assert res_inspect is True
223+
224+
# test wrong subprocess call
225+
def mock_failed_subprocess(*args, **kwargs):
226+
raise subprocess.CalledProcessError(returncode=1, cmd=args[0])
227+
228+
monkeypatch.setattr("cwltool.singularity.run", mock_failed_subprocess)
229+
res_inspect = _inspect_singularity_image(str(image_path))
230+
assert res_inspect is False

tests/test_tmpdir.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,39 +324,52 @@ def test_singularity_get_image_from_sandbox(monkeypatch: pytest.MonkeyPatch, tmp
324324

325325
workdir = tmp_path / "working_dir"
326326
workdir.mkdir()
327-
# build a sandbox image
328327
repo_path = workdir / "container_repo"
329328
repo_path.mkdir()
330329
image_path = repo_path / "alpine"
331330
image_path.mkdir()
332331

333332
# directory exists but is not an image
334333
monkeypatch.setattr("cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: False)
335-
assert not SingularityCommandLineJob(
334+
req = {"class": "DockerRequirement", "dockerPull": f"{image_path}"}
335+
res = SingularityCommandLineJob(
336336
builder, {}, CommandLineTool.make_path_mapper, [], [], ""
337337
).get_image(
338-
{
339-
"class": "DockerRequirement",
340-
"dockerPull": f"{image_path}",
341-
},
338+
req,
342339
pull_image=False,
343340
tmp_outdir_prefix=str(tmp_outdir_prefix),
344341
force_pull=False,
345342
)
343+
assert req["dockerPull"].startswith("docker://")
344+
assert res is False
346345

347346
# directory exists and is an image:
348347
monkeypatch.setattr("cwltool.singularity._inspect_singularity_image", lambda *args, **kwargs: True)
349-
assert SingularityCommandLineJob(
348+
req = {"class": "DockerRequirement", "dockerPull": f"{image_path}"}
349+
res = SingularityCommandLineJob(
350350
builder, {}, CommandLineTool.make_path_mapper, [], [], ""
351351
).get_image(
352-
{
353-
"class": "DockerRequirement",
354-
"dockerPull": f"{image_path}",
355-
},
352+
req,
353+
pull_image=False,
354+
tmp_outdir_prefix=str(tmp_outdir_prefix),
355+
force_pull=False,
356+
)
357+
assert req["dockerPull"] == str(image_path)
358+
assert req["dockerImageId"] == str(image_path)
359+
assert res
360+
361+
# test that dockerImageId is set and image exists:
362+
req = {"class": "DockerRequirement", "dockerImageId": f"{image_path}"}
363+
res = SingularityCommandLineJob(
364+
builder, {}, CommandLineTool.make_path_mapper, [], [], ""
365+
).get_image(
366+
req,
356367
pull_image=False,
357368
tmp_outdir_prefix=str(tmp_outdir_prefix),
358369
force_pull=False,
359370
)
371+
assert req["dockerImageId"] == str(image_path)
372+
assert res
360373

361374

362375
def test_docker_tmpdir_prefix(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)