Files
deploy/tests/test_deploy_env_before_up.py
T
Christian Manivong 3a15c311a0
CI / check (pull_request) Successful in 19s
fix: record the version in .env before starting the containers
The services read ~/netork/.env through env_file, so whatever it says
when `up -d` creates a container ends up in that container's
environment. The settings were recorded only after the swap, so every
container carried the previous deploy's NETORK_VERSION: the image was
right, `docker exec <api> env` was not.

Record them once the pull has brought the images onto the host and
before the first `up -d`. A failed pull still leaves .env alone. If the
swap fails afterwards, .env names the tag whose compose files and
images are already on the host, which is what a hand-typed
`docker compose up -d` there should start.

Also say which branches a feature-<branch> tag comes from: feature/<name>
and feat/<name> become feature-<name>, and fix/<name> now becomes
feature-fix-<name> (NetOrk/netork#208), because the registry keeps only
feature-* branch tags. The accepted forms do not change; the comment, the
refusal message and the README now say so, and a test holds that
feature-fix-<name> passes and a bare fix-<name> does not.

Closes #6
2026-10-18 18:26:24 +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 engine services' recreate, then the infrastructure reconcile.
assert len(seen) == 2, seen
assert seen == ["NETORK_VERSION=latest-dev"] * 2, (
"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"