"""A deploy removes the images it replaced, and nothing else (NetOrk/kb-server#157). It ended with `docker image prune -af`: every image no container used at that moment, on a host that may run other things too. Now it records the image IDs of netOrk's own containers before the swap and, once the swap is verified and the migration ran, hands them to remote/remove_replaced_images.sh. The remote side is a fake `ssh` that records every command and plays a host whose netOrk containers run one image. """ from __future__ import annotations import os import subprocess from pathlib import Path DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" _FAKE_SSH = """#!/bin/sh for arg in "$@"; do cmd="$arg"; done case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac case "$cmd" in *"bash -s"*) cat > "{state}/stdin-$(date +%s%N)" ;; *) cat > /dev/null ;; esac printf '%s\\n----\\n' "$cmd" >> "{state}/commands" case "$cmd" in *"xargs -r docker inspect"*) echo sha256:old ;; esac exit 0 """ def _deploy(tmp_path: Path) -> tuple[subprocess.CompletedProcess[str], list[str]]: bin_dir = tmp_path / "bin" bin_dir.mkdir() fake = bin_dir / "ssh" fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path))) fake.chmod(0o755) result = subprocess.run( ["bash", str(DEPLOY), "testhost"], env={ "PATH": f"{bin_dir}:{os.environ['PATH']}", "HOME": str(tmp_path), "DEPLOY_ENV_FILE": "/dev/null", "REGISTRY_HOST": "registry.example", "NETORK_VERSION": "latest-dev", }, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=60, check=False, ) commands = (tmp_path / "commands").read_text().split("\n----\n") return result, commands def _index(commands: list[str], needle: str) -> int: return next(i for i, cmd in enumerate(commands) if needle in cmd) def test_no_prune_of_everything(tmp_path: Path) -> None: result, commands = _deploy(tmp_path) assert result.returncode == 0, result.stderr assert not [cmd for cmd in commands if "image prune -af" in cmd] def test_the_replaced_images_are_recorded_before_the_swap_and_removed_after( tmp_path: Path, ) -> None: result, commands = _deploy(tmp_path) assert result.returncode == 0, result.stderr recorded = _index(commands, "xargs -r docker inspect") swap = _index(commands, "--force-recreate") migrate = _index(commands, "alembic upgrade head") removal = _index(commands, "sha256:old") assert recorded < swap < migrate < removal assert "bash -s" in commands[removal] sent = [p.read_text() for p in tmp_path.glob("stdin-*")] assert any("remove_replaced_images.sh" in text for text in sent), result.stdout