Files
deploy/tests/test_deploy_broker.py
T
2026-10-20 22:38:11 +02:00

87 lines
2.8 KiB
Python

"""The broker is reconciled before the engine is recreated (NetOrk/netork#1308).
`redis` was in neither list: the engine is recreated with --no-deps, and the
infrastructure reconcile left databases out on purpose. A change to its compose
definition -- Valkey in place of redis:7-alpine -- therefore never reached a
host. A plain `up -d` recreates it only when its definition changed, so an
unchanged broker keeps running. It comes before the engine, which cannot work
without it, and waits until it is healthy; when it does not come up, the deploy
stops there.
The remote side is a fake `ssh` that records every command it is given.
"""
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
cat > /dev/null
printf '%s\\n----\\n' "$cmd" >> "{state}/commands"
case "$cmd" in *"up -d --no-deps --wait"*redis*) exit {broker_exit} ;; esac
exit 0
"""
def _deploy(
tmp_path: Path, broker_exit: int = 0
) -> 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)).replace("{broker_exit}", str(broker_exit))
)
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, c in enumerate(commands) if needle in c)
def test_the_broker_is_reconciled_before_the_engine(tmp_path: Path) -> None:
result, commands = _deploy(tmp_path)
assert result.returncode == 0, result.stderr
broker = _index(commands, "up -d --no-deps --wait --wait-timeout 120 redis")
engine = _index(commands, "up -d --no-deps --force-recreate")
assert broker < engine
def test_the_broker_is_never_force_recreated(tmp_path: Path) -> None:
_, commands = _deploy(tmp_path)
for c in commands:
if "--force-recreate" in c:
assert " redis" not in c.replace("--no-deps", "")
def test_a_broker_that_does_not_come_up_stops_the_deploy(tmp_path: Path) -> None:
result, commands = _deploy(tmp_path, broker_exit=1)
assert result.returncode != 0
assert not any("--force-recreate" in c for c in commands)