Files
deploy/tests/test_deploy_env_before_up.py
T
Christian Manivong 7e4fff8025
CI / check (pull_request) Failing after 2m28s
feat: reconcile the broker before the engine (NetOrk/netork#1308)
redis was in no list: the engine is recreated with --no-deps, and the
infrastructure reconcile left databases out on purpose. A changed definition
-- Valkey in place of redis:7-alpine -- therefore never reached a host.

The broker is now brought up with a plain `up -d --wait` right before the
engine is recreated: recreated only when its definition changed, so an
unchanged broker keeps running and keeps its queue, and the engine comes up
against a healthy broker. If the broker does not get healthy within 120 s, the
deploy stops with the old engine still running.
2026-10-20 22:26:25 +02:00

103 lines
3.6 KiB
Python

"""The containers start with the version being deployed in `~/netork/.env` (NetOrk/deploy#6).
The services read `~/netork/.env` through `env_file: .env`, so whatever the file
says when `docker compose up -d` creates a container ends up in that container's
environment. The script used to record `NETORK_VERSION` there only after the
containers had been started, so every container carried the *previous* deploy's
version: a host moved from a feature tag to `latest-dev` ran the `latest-dev`
image (the tag comes from the inline variable) but `docker exec <api> env` still
said `NETORK_VERSION=feature-container-engine-api`.
The remote side is a fake `ssh`. The `.env` step (`bash -s` with a heredoc) runs
for real against a fake home directory; every `up -d` notes what `.env` says at
that moment.
"""
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
# The host lock's holder runs for real, here (test_deploy_host_lock.py).
case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac
# The .env step runs for real, in the fake home.
[ "$cmd" = "bash -s" ] && exec bash -s
cat > /dev/null
case "$cmd" in
*" pull "*)
if [ -n "{fail_pull}" ]; then echo "Error: manifest unknown"; exit 1; fi
;;
*"up -d"*)
grep '^NETORK_VERSION=' "$HOME/netork/.env" >> "{state}/env-at-up" || echo none >> "{state}/env-at-up"
;;
esac
exit 0
"""
PREVIOUS = "NETORK_VERSION=feature-previous"
def _deploy(tmp_path: Path, *, fail_pull: bool = False) -> subprocess.CompletedProcess[str]:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
fake = bin_dir / "ssh"
fake.write_text(
_FAKE_SSH.replace("{state}", str(tmp_path)).replace("{fail_pull}", "x" if fail_pull else "")
)
fake.chmod(0o755)
netork = tmp_path / "netork"
netork.mkdir()
(netork / ".env").write_text(
f"POSTGRES_PASSWORD=secret\nREGISTRY_HOST=old.example\n{PREVIOUS}\n"
)
return subprocess.run(
["bash", str(DEPLOY), "--version=latest-dev", "testhost"],
env={
"PATH": f"{bin_dir}:{os.environ['PATH']}",
"HOME": str(tmp_path),
"DEPLOY_ENV_FILE": "/dev/null",
"REGISTRY_HOST": "registry.example",
},
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
timeout=60,
check=False,
)
def test_every_up_sees_the_version_being_deployed(tmp_path: Path) -> None:
result = _deploy(tmp_path)
assert result.returncode == 0, result.stderr
seen = (tmp_path / "env-at-up").read_text().splitlines()
# The broker, the engine services' recreate, then the infrastructure reconcile.
assert len(seen) == 3, seen
assert seen == ["NETORK_VERSION=latest-dev"] * 3, (
"a container was started while .env still named the previous deploy's version"
)
def test_the_record_keeps_the_rest_of_env(tmp_path: Path) -> None:
result = _deploy(tmp_path)
assert result.returncode == 0, result.stderr
assert (tmp_path / "netork" / ".env").read_text() == (
"POSTGRES_PASSWORD=secret\nREGISTRY_HOST=registry.example\nNETORK_VERSION=latest-dev\n"
)
def test_a_failed_pull_leaves_env_alone(tmp_path: Path) -> None:
"""A tag the registry does not have must not end up recorded on the host
(the reason the step once moved behind the image check)."""
result = _deploy(tmp_path, fail_pull=True)
assert result.returncode != 0
assert PREVIOUS in (tmp_path / "netork" / ".env").read_text()
assert not (tmp_path / "env-at-up").exists(), "containers were started after a failed pull"