Files
computer-playbook/tests/unit/cli/deploy/development/test_matrix_deploy.py
T
kevinveenbirkenbachandClaude Opus 4.7 3f2021b07d refactor: collapse INFINITO_DISTRO/INFINITO_CONTAINER to a single SPOT and align all consumers
Follow-up to d002796a5. The formula `infinito_nexus_${INFINITO_DISTRO}` is
now defined in exactly one place — `scripts/meta/env/defaults.sh:18`,
exported outside the load-once guard so it tracks INFINITO_DISTRO across
matrix iterations (caller re-sources defaults.sh after distro changes).
Every other site reads INFINITO_CONTAINER strictly from env; nothing
re-derives the formula, no lazy/colon-dash fallbacks, no per-call defaults.

Production code aligned:

- cli/deploy/development/common.py: add resolve_container() (strict env
  reader, raises SystemExit on unset). make_compose() calls it as an
  early-fail guard so a missing defaults.sh source surfaces here instead
  of as a confusing compose.yml interpolation error later.
- cli/deploy/development/deploy.py: stop re-deriving from compose.distro;
  use resolve_container().
- compose.yml: container_name reads ${INFINITO_CONTAINER:?...} strictly,
  so callers that bypass defaults.sh fail fast with a clear hint.
- scripts/meta/resolve/image/local.sh: drop ${INFINITO_DISTRO:-debian}
  fallback; require INFINITO_DISTRO via :? error.
- scripts/tests/deploy/ci/dedicated.sh: drop local _container fallback.
- scripts/tests/deploy/local/deploy/fresh-kept-app.sh: drop redundant
  ${INFINITO_DISTRO:-debian} reassignment after sourcing env/all.sh
  (defaults.sh already defaults the value).
- scripts/tests/deploy/local/deploy/fresh-purged-app.sh: drop
  target_container fallback; INFINITO_CONTAINER is already in env.
- scripts/tests/deploy/local/exec/container.sh: drop the lazy
  ${INFINITO_CONTAINER:=infinito_nexus_...} default; defaults.sh source
  in env/all.sh already guarantees the value.
- scripts/tests/dns.sh: source defaults.sh so callers only need to set
  INFINITO_DISTRO (the implicit dependency was previously a footgun).
- scripts/system/tls/trust/{wsl2,linux}.sh: same — source defaults.sh,
  drop the ${INFINITO_CONTAINER:-infinito_nexus_debian} fallback in
  linux.sh.
- .github/workflows/test-dns.yml: drop redundant
  INFINITO_CONTAINER: infinito_nexus_${{ matrix.distro }} from per-step
  env blocks (3 sites). The env now flows from INFINITO_DISTRO via the
  defaults.sh sourced inside the scripts.
- tests/unit/cli/deploy/development/test_matrix_deploy.py: replace the
  module-level os.environ.setdefault('INFINITO_CONTAINER', 'infinito_nexus_arch')
  shim — which itself encoded the formula — with a module-level patch of
  resolve_container that returns an opaque test-fixture string. Drop the
  now-unused distro="arch" field from the _args() Namespace fixture
  (deploy.handler stopped consuming it after the --distro arg removal).

Documentation aligned:

- docs/contributing/environment/variables.md: rewrite the INFINITO_CONTAINER
  row to spell out the SPOT contract — set automatically by defaults.sh,
  never re-derived elsewhere, callers re-source defaults.sh after
  changing INFINITO_DISTRO mid-process.
- docs/contributing/artefact/files/compose.yml.md: replace the
  INFINITO_DISTRO row (which described the now-removed
  `infinito_nexus_${INFINITO_DISTRO:-debian}` interpolation) with an
  INFINITO_CONTAINER row noting the strict ${INFINITO_CONTAINER:?...} read.

Verified:
  - Formula derivation: 1 production hit (the SPOT in defaults.sh:18).
  - INFINITO_CONTAINER fallback patterns (:-/:=): 0.
  - --distro CLI arg refs: 0 (one comment explaining the removal).
  - make test green: 1553 unit + 168 integration tests, no skipped suites.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 12:21:35 +02:00

425 lines
17 KiB
Python

"""Unit tests for the deploy-side matrix iteration in
`cli.deploy.development.deploy.handler`.
The init step builds N inventory folders (one per round, with the
per-app variant data baked in); the deploy handler re-derives the same
plan via `plan_dev_inventory_matrix` and deploys against each folder in
turn, purging only the apps whose variant changed between rounds. These
tests pin both the iteration order and the cleanup decisions."""
from __future__ import annotations
import argparse
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch
# deploy.handler reads the running container name strictly via
# cli.deploy.development.common.resolve_container (which is sourced from
# scripts/meta/env/defaults.sh — the single SPOT for the formula). Patch
# that resolver for the whole test module instead of duplicating the
# formula or hard-coding INFINITO_CONTAINER in os.environ. The tests
# below do not assert on the returned value; the sentinel is purely an
# opaque fixture string.
_RESOLVE_CONTAINER_PATCHER = patch(
"cli.deploy.development.deploy.resolve_container",
return_value="<unit-test fixture>",
)
_RESOLVE_CONTAINER_PATCHER.start()
from cli.deploy.development.deploy import handler # noqa: E402
def _args(
*,
apps: list[str] | None = None,
variant: int | None = None,
full_cycle: bool = False,
) -> argparse.Namespace:
# `distro` is intentionally absent: deploy.handler no longer consumes
# it (the --distro arg was retired in favour of resolve_distro() reading
# INFINITO_DISTRO env strictly).
return argparse.Namespace(
inventory_dir="/srv/inv",
apps=None,
id=apps,
debug=False,
variant=variant,
full_cycle=full_cycle,
ansible_args=[],
)
def _make_compose_mock() -> MagicMock:
compose = MagicMock()
compose.repo_root = Path("/tmp/infinito-nexus")
return compose
class TestHandlerMatrixDeploy(unittest.TestCase):
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_single_round_skips_matrix_loop(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv", {"web-app-jira": 0, "web-app-keycloak": 0}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-jira", "web-app-keycloak"]))
self.assertEqual(rc, 0)
# Single deploy against the unsuffixed folder, no cleanup.
run_deploy_mock.assert_called_once()
self.assertEqual(run_deploy_mock.call_args.kwargs["inventory_dir"], "/srv/inv")
purge_mock.assert_not_called()
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_round_one_only_deploys_apps_with_real_variant_one(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
# User-spec example: variant 0 covers ALL apps, variant 1 only
# exists for WordPress. Round 1 MUST therefore deploy ONLY
# web-app-wordpress, NOT keycloak (which has no variant 1 and
# would only fall back to variant 0 -- already deployed in round 0).
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-wordpress": 0, "web-app-keycloak": 0}),
(1, "/srv/inv-1", {"web-app-wordpress": 1, "web-app-keycloak": 0}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-wordpress", "web-app-keycloak"]))
self.assertEqual(rc, 0)
self.assertEqual(run_deploy_mock.call_count, 2)
round_one_deploy_ids = run_deploy_mock.call_args_list[1].kwargs["deploy_ids"]
self.assertEqual(round_one_deploy_ids, ["web-app-wordpress"])
# Keycloak MUST NOT be in the round-1 deploy_ids; it stays at the
# state round 0 produced.
self.assertNotIn("web-app-keycloak", round_one_deploy_ids)
# Purge mirrors the deploy filter: only WP gets purged before
# round 1 (Keycloak is not being re-deployed).
purge_mock.assert_called_once()
self.assertEqual(purge_mock.call_args.kwargs["app_ids"], ["web-app-wordpress"])
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_two_round_plan_deploys_each_folder_in_order(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0, "web-app-keycloak": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1, "web-app-keycloak": 0}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-multi", "web-app-keycloak"]))
self.assertEqual(rc, 0)
self.assertEqual(run_deploy_mock.call_count, 2)
self.assertEqual(
[c.kwargs["inventory_dir"] for c in run_deploy_mock.call_args_list],
["/srv/inv-0", "/srv/inv-1"],
)
# Per the spec ("der 1. eintrag auch im 2. durchlauf"): keycloak
# stays on variant 0 in round 2, so it MUST NOT be purged. Only
# `web-app-multi` flipped variants between rounds.
purge_mock.assert_called_once()
self.assertEqual(purge_mock.call_args.kwargs["app_ids"], ["web-app-multi"])
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_three_round_plan_only_re_deploys_apps_with_real_variant(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
# WordPress 2 variants, Discourse 3 variants, Keycloak 1 variant.
# Round 0 deploys the full include set as the baseline.
# Round 1: WP and Discourse have a real variant 1 -> both re-deploy.
# Round 2: only Discourse has a real variant 2 -> only it re-deploys.
# WordPress stays at variant 1 from round 1 (it has no variant 2);
# Keycloak stays at variant 0 from round 0 (no later variants).
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(
0,
"/srv/inv-0",
{"web-app-wordpress": 0, "web-app-discourse": 0, "web-app-keycloak": 0},
),
(
1,
"/srv/inv-1",
{"web-app-wordpress": 1, "web-app-discourse": 1, "web-app-keycloak": 0},
),
(
2,
"/srv/inv-2",
{"web-app-wordpress": 0, "web-app-discourse": 2, "web-app-keycloak": 0},
),
]
run_deploy_mock.return_value = 0
rc = handler(
_args(
apps=[
"web-app-wordpress",
"web-app-discourse",
"web-app-keycloak",
]
)
)
self.assertEqual(rc, 0)
self.assertEqual(run_deploy_mock.call_count, 3)
per_round_deploy_ids = [
c.kwargs["deploy_ids"] for c in run_deploy_mock.call_args_list
]
# Round 0: full baseline (mirrors `--id` order from _args).
self.assertEqual(
per_round_deploy_ids[0],
["web-app-wordpress", "web-app-discourse", "web-app-keycloak"],
)
# Round 1: WP + Discourse (their real variant index for the round).
self.assertEqual(
per_round_deploy_ids[1],
["web-app-discourse", "web-app-wordpress"],
)
# Round 2: only Discourse has variant 2.
self.assertEqual(per_round_deploy_ids[2], ["web-app-discourse"])
# Purge mirrors the per-round deploy filter:
# Round 1 -> purge {WP, Discourse} (both flipped 0->1).
# Round 2 -> purge {Discourse} (1->2). WP is NOT purged because it
# is not being re-deployed this round; its round-1 state stays.
self.assertEqual(purge_mock.call_count, 2)
self.assertEqual(
purge_mock.call_args_list[0].kwargs["app_ids"],
["web-app-discourse", "web-app-wordpress"],
)
self.assertEqual(
purge_mock.call_args_list[1].kwargs["app_ids"],
["web-app-discourse"],
)
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_failure_in_round_one_aborts_before_round_two(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
run_deploy_mock.return_value = 17 # failure exit code
rc = handler(_args(apps=["web-app-multi"]))
self.assertEqual(rc, 17)
# Round 1 ran, round 2 must NOT have been attempted.
run_deploy_mock.assert_called_once()
purge_mock.assert_not_called()
class TestHandlerVariantPin(unittest.TestCase):
"""`--variant <idx>` (or VARIANT env-var) pins the deploy to one
specific round's folder, skipping inter-round cleanup. Use case:
redeploying one variant without iterating the whole matrix."""
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_variant_one_runs_only_that_round_no_cleanup(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-multi"], variant=1))
self.assertEqual(rc, 0)
# Only the picked round runs; no cleanup because there is no
# previous round to diff against in single-round mode.
run_deploy_mock.assert_called_once()
self.assertEqual(
run_deploy_mock.call_args.kwargs["inventory_dir"], "/srv/inv-1"
)
purge_mock.assert_not_called()
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_variant_out_of_range_exits_with_clean_message(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
_purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
with self.assertRaisesRegex(SystemExit, "variant 7 out of range"):
handler(_args(apps=["web-app-multi"], variant=7))
run_deploy_mock.assert_not_called()
class TestHandlerFullCycle(unittest.TestCase):
"""`--full-cycle` runs an async re-deploy IMMEDIATELY after each
round's sync deploy (Pass 2 stays co-located with Pass 1 on the
same variant). Without `--full-cycle` only Pass 1 runs per round."""
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_full_cycle_runs_pass2_after_each_round(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-multi"], full_cycle=True))
self.assertEqual(rc, 0)
# Two rounds, two passes each = 4 deploy calls.
self.assertEqual(run_deploy_mock.call_count, 4)
sequence = [
(call.kwargs["inventory_dir"], call.kwargs.get("extra_ansible_vars"))
for call in run_deploy_mock.call_args_list
]
# Per-variant interleave: round-0 sync, round-0 async, then
# round-1 sync, round-1 async.
self.assertEqual(
sequence,
[
("/srv/inv-0", None),
("/srv/inv-0", {"ASYNC_ENABLED": True}),
("/srv/inv-1", None),
("/srv/inv-1", {"ASYNC_ENABLED": True}),
],
)
# Cleanup still runs once between rounds (variant changed 0->1).
purge_mock.assert_called_once()
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_full_cycle_aborts_when_pass1_fails_skipping_pass2(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
_purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
# PASS 1 of round 0 fails. PASS 2 of round 0 and the entire round
# 1 must be skipped to surface the failure cleanly.
run_deploy_mock.return_value = 11
rc = handler(_args(apps=["web-app-multi"], full_cycle=True))
self.assertEqual(rc, 11)
run_deploy_mock.assert_called_once()
@patch("cli.deploy.development.deploy._purge_app_entities", autospec=True)
@patch("cli.deploy.development.deploy._run_deploy", autospec=True)
@patch("cli.deploy.development.deploy.plan_dev_inventory_matrix", autospec=True)
@patch("cli.deploy.development.deploy.make_compose", autospec=True)
def test_full_cycle_with_variant_pin_runs_only_one_round_with_both_passes(
self,
make_compose_mock: MagicMock,
plan_mock: MagicMock,
run_deploy_mock: MagicMock,
purge_mock: MagicMock,
) -> None:
make_compose_mock.return_value = _make_compose_mock()
plan_mock.return_value = [
(0, "/srv/inv-0", {"web-app-multi": 0}),
(1, "/srv/inv-1", {"web-app-multi": 1}),
]
run_deploy_mock.return_value = 0
rc = handler(_args(apps=["web-app-multi"], variant=1, full_cycle=True))
self.assertEqual(rc, 0)
self.assertEqual(run_deploy_mock.call_count, 2)
sequence = [
(call.kwargs["inventory_dir"], call.kwargs.get("extra_ansible_vars"))
for call in run_deploy_mock.call_args_list
]
self.assertEqual(
sequence,
[
("/srv/inv-1", None),
("/srv/inv-1", {"ASYNC_ENABLED": True}),
],
)
purge_mock.assert_not_called()
if __name__ == "__main__":
unittest.main()