"""deploy.sh runs remote/database.sh on every host (NetOrk/netork#1308). A normal deploy reconciles the bundled database (`reconcile`); only --infra-db lets it move to a new volume (`move`). It runs after .env is written and before the broker and the engine, and a refusal (exit 3: compose wants a new volume) stops the deploy there, with the old engine still running. The remote side is a fake `ssh` that records every command and what it was fed. """ from __future__ import annotations import os import subprocess from pathlib import Path DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" DATABASE = Path(__file__).resolve().parent.parent / "remote" / "database.sh" _FAKE_SSH = """#!/bin/sh for arg in "$@"; do cmd="$arg"; done case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac printf '%s\\n----\\n' "$cmd" >> "{state}/commands" case "$cmd" in *"bash -s -- "*) cat > "{state}/database-stdin"; exit {db_exit} ;; esac cat > /dev/null exit 0 """ def _deploy(tmp_path: Path, *args: str, db_exit: int = 0): bin_dir = tmp_path / "bin" bin_dir.mkdir() fake = bin_dir / "ssh" fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path)).replace("{db_exit}", str(db_exit))) fake.chmod(0o755) result = subprocess.run( ["bash", str(DEPLOY), *args, "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_a_normal_deploy_reconciles_the_database_first(tmp_path: Path) -> None: result, commands = _deploy(tmp_path) assert result.returncode == 0, result.stderr db = _index(commands, "bash -s -- reconcile netork-api") assert "NETORK_VERSION='latest-dev'" in commands[db] assert db < _index(commands, "--wait-timeout 120 redis") < _index(commands, "--force-recreate") assert (tmp_path / "database-stdin").read_text() == DATABASE.read_text() def test_infra_db_lets_it_move(tmp_path: Path) -> None: result, commands = _deploy(tmp_path, "--infra-db") assert result.returncode == 0, result.stderr assert any("bash -s -- move netork-api" in c for c in commands) assert not any("bash -s -- reconcile" in c for c in commands) def test_a_refusal_stops_the_deploy_before_the_engine(tmp_path: Path) -> None: result, commands = _deploy(tmp_path, db_exit=3) assert result.returncode != 0 assert not any("--force-recreate" in c for c in commands) assert not any("redis" in c and "up -d" in c for c in commands)