|
1 | 1 | """Tests to find local Singularity image.""" |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import shutil |
| 5 | +import subprocess |
4 | 6 | from pathlib import Path |
5 | 7 |
|
6 | 8 | import pytest |
7 | 9 |
|
8 | 10 | from cwltool.main import main |
| 11 | +from cwltool.singularity import _inspect_singularity_image |
9 | 12 |
|
10 | 13 | from .util import ( |
11 | 14 | get_data, |
@@ -159,3 +162,116 @@ def test_singularity3_docker_image_id_in_tool(tmp_path: Path) -> None: |
159 | 162 | ] |
160 | 163 | ) |
161 | 164 | assert result_code1 == 0 |
| 165 | + |
| 166 | + |
| 167 | +@needs_singularity |
| 168 | +def test_singularity_local_sandbox_image(tmp_path: Path): |
| 169 | + workdir = tmp_path / "working_dir" |
| 170 | + workdir.mkdir() |
| 171 | + with working_directory(workdir): |
| 172 | + # build a sandbox image |
| 173 | + container_path = workdir / "container_repo" |
| 174 | + container_path.mkdir() |
| 175 | + cmd = [ |
| 176 | + "singularity", |
| 177 | + "build", |
| 178 | + "--sandbox", |
| 179 | + str(container_path / "alpine"), |
| 180 | + "docker://alpine:latest", |
| 181 | + ] |
| 182 | + |
| 183 | + build = subprocess.run(cmd, capture_output=True, text=True) |
| 184 | + if build.returncode == 0: |
| 185 | + result_code, stdout, stderr = get_main_output( |
| 186 | + [ |
| 187 | + "--singularity", |
| 188 | + "--disable-pull", |
| 189 | + get_data("tests/sing_local_sandbox_test.cwl"), |
| 190 | + "--message", |
| 191 | + "hello", |
| 192 | + ] |
| 193 | + ) |
| 194 | + assert result_code == 0 |
| 195 | + result_code, stdout, stderr = get_main_output( |
| 196 | + [ |
| 197 | + "--singularity", |
| 198 | + "--disable-pull", |
| 199 | + get_data("tests/sing_local_sandbox_img_id_test.cwl"), |
| 200 | + "--message", |
| 201 | + "hello", |
| 202 | + ] |
| 203 | + ) |
| 204 | + assert result_code == 0 |
| 205 | + else: |
| 206 | + pytest.skip(f"Failed to build the singularity image: {build.stderr}") |
| 207 | + |
| 208 | + |
| 209 | +@needs_singularity |
| 210 | +def test_singularity_inspect_image(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 211 | + """Test inspect a real image works.""" |
| 212 | + workdir = tmp_path / "working_dir" |
| 213 | + workdir.mkdir() |
| 214 | + repo_path = workdir / "container_repo" |
| 215 | + image_path = repo_path / "alpine" |
| 216 | + |
| 217 | + # test image exists |
| 218 | + repo_path.mkdir() |
| 219 | + cmd = [ |
| 220 | + "singularity", |
| 221 | + "build", |
| 222 | + "--sandbox", |
| 223 | + str(image_path), |
| 224 | + "docker://alpine:latest", |
| 225 | + ] |
| 226 | + build = subprocess.run(cmd, capture_output=True, text=True) |
| 227 | + if build.returncode == 0: |
| 228 | + # Verify the path is a correct container image |
| 229 | + res_inspect = _inspect_singularity_image(image_path) |
| 230 | + assert res_inspect is True |
| 231 | + else: |
| 232 | + pytest.skip(f"singularity sandbox image build didn't worked: {build.stderr}") |
| 233 | + |
| 234 | +def _make_run_result(returncode: int, stdout: str): |
| 235 | + """Mock subprocess.run returning returncode and stdout.""" |
| 236 | + class DummyResult: |
| 237 | + def __init__(self, rc, out): |
| 238 | + self.returncode = rc |
| 239 | + self.stdout = out |
| 240 | + |
| 241 | + def _runner(*args, **kwargs): |
| 242 | + return DummyResult(returncode, stdout) |
| 243 | + |
| 244 | + return _runner |
| 245 | + |
| 246 | +def test_json_decode_error_branch(monkeypatch): |
| 247 | + """Test json can't decode inspect result.""" |
| 248 | + monkeypatch.setattr("cwltool.singularity.run", _make_run_result(0, "not-a-json")) |
| 249 | + |
| 250 | + def _raise_json_error(s): |
| 251 | + # construct and raise an actual JSONDecodeError |
| 252 | + raise json.JSONDecodeError("Expecting value", s, 0) |
| 253 | + |
| 254 | + # Patch json.loads so it raises JSONDecodeError when called |
| 255 | + monkeypatch.setattr("json.loads", _raise_json_error) |
| 256 | + |
| 257 | + assert _inspect_singularity_image("/tmp/image") is False |
| 258 | + |
| 259 | +def test_singularity_sandbox_image_not_exists(): |
| 260 | + image_path = "/tmp/not_existing/image" |
| 261 | + res_inspect = _inspect_singularity_image(image_path) |
| 262 | + assert res_inspect is False |
| 263 | + |
| 264 | +def test_singularity_sandbox_not_an_image(tmp_path: Path): |
| 265 | + image_path = tmp_path / "image" |
| 266 | + image_path.mkdir() |
| 267 | + res_inspect = _inspect_singularity_image(image_path) |
| 268 | + assert res_inspect is False |
| 269 | + |
| 270 | +def test_inspect_image_wrong_sb_call(monkeypatch: pytest.MonkeyPatch): |
| 271 | + |
| 272 | + def mock_failed_subprocess(*args, **kwargs): |
| 273 | + raise subprocess.CalledProcessError(returncode=1, cmd=args[0]) |
| 274 | + |
| 275 | + monkeypatch.setattr("cwltool.singularity.run", mock_failed_subprocess) |
| 276 | + res_inspect = _inspect_singularity_image("/tmp/container_repo/alpine") |
| 277 | + assert res_inspect is False |
0 commit comments