Files
deploy/tests/test_deploy_infra_db.py
T
Christian Manivong 6e3ecb8680
CI / check (pull_request) Failing after 28s
feat: the bundled database on every deploy, and --infra-db to move it (NetOrk/netork#1308)
remote/database.sh runs on the host (over ssh's stdin) after .env and before
broker and engine:

- no bundled database: nothing to do;
- the volume it already uses: plain `up -d --wait`, recreated only on a changed
  image or definition, so a new PostgreSQL minor release arrives with the next
  deploy;
- another volume (a new major version): a normal deploy stops there, nothing
  changed; with --infra-db the engine stops, the old database is counted and
  dumped (kept in ~/netork/backups), the new container comes up on the new
  volume, restore, ANALYZE, counts and Alembic revision must match. The old
  volume stays, and deploying the previous version with --infra-db switches
  back onto it without a restore.

Checked on .50 in reconcile mode: the running postgres:16 container stayed as
it was. README describes the steps and the way back.
2026-10-20 23:15:48 +02:00

85 lines
2.9 KiB
Python

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