|
8 | 8 | import pytest |
9 | 9 | from click.testing import CliRunner |
10 | 10 |
|
| 11 | +from localci.cli.images import _cleanup_targets |
11 | 12 | from localci.cli.main import cli |
12 | 13 | from localci.utils.paths import ( |
13 | 14 | IMAGES_CAPY_REL, |
@@ -250,3 +251,295 @@ def fake_run(cmd: list[str]) -> MagicMock: |
250 | 251 |
|
251 | 252 | assert result.exit_code == 0 |
252 | 253 | assert captured[0][1] == str(images_dir / "build-all.sh") |
| 254 | + |
| 255 | + |
| 256 | +def _docker_run_side_effect( |
| 257 | + installed_tags: list[str], |
| 258 | + *, |
| 259 | + captured_rmi: list[list[str]] | None = None, |
| 260 | +): |
| 261 | + """Stub _run for clean tests: fake image ls output, record rmi argv.""" |
| 262 | + |
| 263 | + def fake_run(cmd: list[str]) -> MagicMock: |
| 264 | + if "image" in cmd and "ls" in cmd: |
| 265 | + stdout = "\n".join(installed_tags) |
| 266 | + if stdout: |
| 267 | + stdout += "\n" |
| 268 | + return MagicMock(returncode=0, stdout=stdout, stderr="") |
| 269 | + if "rmi" in cmd: |
| 270 | + if captured_rmi is not None: |
| 271 | + captured_rmi.append(cmd) |
| 272 | + return MagicMock(returncode=0, stdout="", stderr="") |
| 273 | + return MagicMock(returncode=0, stdout="", stderr="") |
| 274 | + |
| 275 | + return fake_run |
| 276 | + |
| 277 | + |
| 278 | +class TestCleanupTargets: |
| 279 | + def test_intersection_sorted(self) -> None: |
| 280 | + installed = ["b:tag", "a:tag", "other:tag"] |
| 281 | + registry = {"a:tag", "b:tag", "registry-only:tag"} |
| 282 | + assert _cleanup_targets(installed, registry) == ["a:tag", "b:tag"] |
| 283 | + |
| 284 | + def test_empty_when_no_match(self) -> None: |
| 285 | + assert _cleanup_targets(["other:tag"], {"ubuntu-latest-gcc15:latest"}) == [] |
| 286 | + |
| 287 | + |
| 288 | +class TestImagesClean: |
| 289 | + def test_clean_generic_tag_removes_registry_match_only( |
| 290 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 291 | + ) -> None: |
| 292 | + repo = tmp_path / "repo" |
| 293 | + registry = _write_registry_with_image( |
| 294 | + repo, docker_tag="ubuntu-latest-gcc15:latest" |
| 295 | + ) |
| 296 | + outside = tmp_path / "outside" |
| 297 | + outside.mkdir() |
| 298 | + monkeypatch.chdir(outside) |
| 299 | + |
| 300 | + captured_rmi: list[list[str]] = [] |
| 301 | + fake_dm = MagicMock() |
| 302 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 303 | + |
| 304 | + with ( |
| 305 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 306 | + patch( |
| 307 | + "localci.cli.images._run", |
| 308 | + side_effect=_docker_run_side_effect( |
| 309 | + ["ubuntu-latest-gcc15:latest", "other:tag"], |
| 310 | + captured_rmi=captured_rmi, |
| 311 | + ), |
| 312 | + ), |
| 313 | + ): |
| 314 | + result = runner.invoke( |
| 315 | + cli, |
| 316 | + ["images", "clean", "--all", "--registry", str(registry)], |
| 317 | + ) |
| 318 | + |
| 319 | + assert result.exit_code == 0 |
| 320 | + assert len(captured_rmi) == 1 |
| 321 | + assert captured_rmi[0][-1] == "ubuntu-latest-gcc15:latest" |
| 322 | + assert "other:tag" not in captured_rmi[0] |
| 323 | + |
| 324 | + def test_clean_capy_tag_regression( |
| 325 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 326 | + ) -> None: |
| 327 | + repo = tmp_path / "repo" |
| 328 | + registry = _write_registry_with_image( |
| 329 | + repo, docker_tag="capy-ubuntu-25.04-gcc15:latest" |
| 330 | + ) |
| 331 | + outside = tmp_path / "outside" |
| 332 | + outside.mkdir() |
| 333 | + monkeypatch.chdir(outside) |
| 334 | + |
| 335 | + captured_rmi: list[list[str]] = [] |
| 336 | + fake_dm = MagicMock() |
| 337 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 338 | + |
| 339 | + with ( |
| 340 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 341 | + patch( |
| 342 | + "localci.cli.images._run", |
| 343 | + side_effect=_docker_run_side_effect( |
| 344 | + [ |
| 345 | + "capy-ubuntu-25.04-gcc15:latest", |
| 346 | + "capy-ubuntu-orphan:latest", |
| 347 | + "unrelated:tag", |
| 348 | + ], |
| 349 | + captured_rmi=captured_rmi, |
| 350 | + ), |
| 351 | + ), |
| 352 | + ): |
| 353 | + result = runner.invoke( |
| 354 | + cli, |
| 355 | + ["images", "clean", "--all", "--registry", str(registry)], |
| 356 | + ) |
| 357 | + |
| 358 | + assert result.exit_code == 0 |
| 359 | + assert len(captured_rmi) == 1 |
| 360 | + assert captured_rmi[0][-1] == "capy-ubuntu-25.04-gcc15:latest" |
| 361 | + assert "capy-ubuntu-orphan:latest" not in captured_rmi[0] |
| 362 | + assert "unrelated:tag" not in captured_rmi[0] |
| 363 | + |
| 364 | + def test_clean_dry_run_lists_targets_without_rmi( |
| 365 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 366 | + ) -> None: |
| 367 | + repo = tmp_path / "repo" |
| 368 | + registry = _write_registry_with_image( |
| 369 | + repo, docker_tag="ubuntu-latest-gcc15:latest" |
| 370 | + ) |
| 371 | + outside = tmp_path / "outside" |
| 372 | + outside.mkdir() |
| 373 | + monkeypatch.chdir(outside) |
| 374 | + |
| 375 | + captured_rmi: list[list[str]] = [] |
| 376 | + fake_dm = MagicMock() |
| 377 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 378 | + |
| 379 | + with ( |
| 380 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 381 | + patch( |
| 382 | + "localci.cli.images._run", |
| 383 | + side_effect=_docker_run_side_effect( |
| 384 | + ["ubuntu-latest-gcc15:latest", "other:tag"], |
| 385 | + captured_rmi=captured_rmi, |
| 386 | + ), |
| 387 | + ), |
| 388 | + ): |
| 389 | + result = runner.invoke( |
| 390 | + cli, |
| 391 | + [ |
| 392 | + "images", |
| 393 | + "clean", |
| 394 | + "--all", |
| 395 | + "--dry-run", |
| 396 | + "--registry", |
| 397 | + str(registry), |
| 398 | + ], |
| 399 | + ) |
| 400 | + |
| 401 | + assert result.exit_code == 0 |
| 402 | + assert "ubuntu-latest-gcc15:latest" in result.output |
| 403 | + assert "other:tag" not in result.output |
| 404 | + assert "Dry-run" in result.output |
| 405 | + assert captured_rmi == [] |
| 406 | + |
| 407 | + def test_clean_without_all_does_not_invoke_docker( |
| 408 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 409 | + ) -> None: |
| 410 | + outside = tmp_path / "outside" |
| 411 | + outside.mkdir() |
| 412 | + monkeypatch.chdir(outside) |
| 413 | + |
| 414 | + with patch("localci.cli.images._run") as mock_run: |
| 415 | + result = runner.invoke(cli, ["images", "clean"]) |
| 416 | + |
| 417 | + mock_run.assert_not_called() |
| 418 | + assert result.exit_code == 0 |
| 419 | + assert "Nothing to clean" in result.output |
| 420 | + |
| 421 | + def test_clean_empty_intersection_reports_no_images( |
| 422 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 423 | + ) -> None: |
| 424 | + repo = tmp_path / "repo" |
| 425 | + registry = _write_registry_with_image( |
| 426 | + repo, docker_tag="ubuntu-latest-gcc15:latest" |
| 427 | + ) |
| 428 | + outside = tmp_path / "outside" |
| 429 | + outside.mkdir() |
| 430 | + monkeypatch.chdir(outside) |
| 431 | + |
| 432 | + captured_rmi: list[list[str]] = [] |
| 433 | + fake_dm = MagicMock() |
| 434 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 435 | + |
| 436 | + with ( |
| 437 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 438 | + patch( |
| 439 | + "localci.cli.images._run", |
| 440 | + side_effect=_docker_run_side_effect( |
| 441 | + ["other:tag"], |
| 442 | + captured_rmi=captured_rmi, |
| 443 | + ), |
| 444 | + ), |
| 445 | + ): |
| 446 | + result = runner.invoke( |
| 447 | + cli, |
| 448 | + ["images", "clean", "--all", "--registry", str(registry)], |
| 449 | + ) |
| 450 | + |
| 451 | + assert result.exit_code == 0 |
| 452 | + assert "No localci images found" in result.output |
| 453 | + assert captured_rmi == [] |
| 454 | + |
| 455 | + def test_clean_uses_explicit_registry( |
| 456 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 457 | + ) -> None: |
| 458 | + repo = tmp_path / "repo" |
| 459 | + registry = _write_registry_with_image(repo, docker_tag="explicit:tag") |
| 460 | + outside = tmp_path / "outside" |
| 461 | + outside.mkdir() |
| 462 | + monkeypatch.chdir(outside) |
| 463 | + |
| 464 | + captured_rmi: list[list[str]] = [] |
| 465 | + fake_dm = MagicMock() |
| 466 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 467 | + |
| 468 | + with ( |
| 469 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 470 | + patch( |
| 471 | + "localci.cli.images._run", |
| 472 | + side_effect=_docker_run_side_effect( |
| 473 | + ["explicit:tag"], |
| 474 | + captured_rmi=captured_rmi, |
| 475 | + ), |
| 476 | + ), |
| 477 | + ): |
| 478 | + result = runner.invoke( |
| 479 | + cli, |
| 480 | + ["images", "clean", "--all", "--registry", str(registry)], |
| 481 | + ) |
| 482 | + |
| 483 | + assert result.exit_code == 0 |
| 484 | + assert captured_rmi[0][-1] == "explicit:tag" |
| 485 | + |
| 486 | + def test_clean_discovers_registry_from_cwd( |
| 487 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 488 | + ) -> None: |
| 489 | + repo = tmp_path / "repo" |
| 490 | + nested = repo / "examples" / "validation-project" |
| 491 | + nested.mkdir(parents=True) |
| 492 | + _write_registry_with_image(repo, docker_tag="ubuntu-latest-gcc15:latest") |
| 493 | + monkeypatch.chdir(nested) |
| 494 | + |
| 495 | + captured_rmi: list[list[str]] = [] |
| 496 | + fake_dm = MagicMock() |
| 497 | + fake_dm.build_cmd.side_effect = lambda *args: ["docker", *args] |
| 498 | + |
| 499 | + with ( |
| 500 | + patch("localci.cli.images.DockerManager", return_value=fake_dm), |
| 501 | + patch( |
| 502 | + "localci.cli.images._run", |
| 503 | + side_effect=_docker_run_side_effect( |
| 504 | + ["ubuntu-latest-gcc15:latest", "other:tag"], |
| 505 | + captured_rmi=captured_rmi, |
| 506 | + ), |
| 507 | + ), |
| 508 | + ): |
| 509 | + result = runner.invoke(cli, ["images", "clean", "--all"]) |
| 510 | + |
| 511 | + assert result.exit_code == 0 |
| 512 | + assert len(captured_rmi) == 1 |
| 513 | + assert captured_rmi[0][-1] == "ubuntu-latest-gcc15:latest" |
| 514 | + assert "other:tag" not in captured_rmi[0] |
| 515 | + |
| 516 | + def test_clean_all_without_discoverable_registry_fails_before_docker( |
| 517 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 518 | + ) -> None: |
| 519 | + site_module = ( |
| 520 | + tmp_path |
| 521 | + / "venv" |
| 522 | + / "lib" |
| 523 | + / "python3.10" |
| 524 | + / "site-packages" |
| 525 | + / "localci" |
| 526 | + / "cli" |
| 527 | + / "images.py" |
| 528 | + ) |
| 529 | + site_module.parent.mkdir(parents=True, exist_ok=True) |
| 530 | + site_module.write_text("# installed copy\n", encoding="utf-8") |
| 531 | + outside = tmp_path / "outside" |
| 532 | + outside.mkdir() |
| 533 | + monkeypatch.chdir(outside) |
| 534 | + |
| 535 | + with ( |
| 536 | + patch("localci.cli.images._IMAGES_MODULE", site_module), |
| 537 | + patch("localci.cli.images.DockerManager") as mock_dm, |
| 538 | + patch("localci.cli.images._run") as mock_run, |
| 539 | + ): |
| 540 | + result = runner.invoke(cli, ["images", "clean", "--all"]) |
| 541 | + |
| 542 | + assert result.exit_code == 1 |
| 543 | + assert REGISTRY_FILENAME in result.output |
| 544 | + mock_dm.assert_not_called() |
| 545 | + mock_run.assert_not_called() |
0 commit comments