Files
deploy/tests/test_deploy_replaced_images.py
Christian Manivong 476afe3982
CI / check (pull_request) Successful in 19s
fix: remove only the images the deploy replaced, never every unused one
The deploy ended with docker image prune -af: every image no container
used at that moment. On a host that runs other things too, that takes
their rollback images; the KB's copy of this line did exactly that on
compute-01 (NetOrk/kb-server#157).

deploy.sh now records the image IDs netOrk's own containers use before
the swap and, after the verification and the migration, hands them to
remote/remove_replaced_images.sh. It removes each by its tags, or by its
ID once a moving tag left it without one, and keeps any image a
container still uses: a redeploy of the same tag replaces nothing.
2026-10-23 12:06:08 +02:00

81 lines
2.7 KiB
Python

"""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