Files
cc-ci/tests/unit/test_canonical.py
autonomic-bot b5f2b104e6
Some checks failed
continuous-integration/drone/push Build is failing
fix(keycloak): key warm state by stack namespace, not bare recipe (F-redfix-4)
The M2 keycloak enrollment made the canonical collision-free at the DOMAIN layer
(warm-canon-keycloak vs warm-keycloak) but warm STATE stayed keyed by bare recipe:
warmsnap.app_dir("keycloak") resolved both the live-warm reconciler's last_good and
the data-warm canonical's canonical.json + snapshot/ into /var/lib/ci-warm/keycloak/.
snapshot() atomically REPLACES that slot, so the two deployments destroyed each
other's known-good; restore() then raised SnapshotError (fails closed, no cross-stack
data write). Worst case: a sweep promote landing inside the reconciler's
snapshot->wait_healthy window makes its rollback restore() raise after
abra.undeploy(live), leaving the shared OIDC provider undeployed.

Fix: canonical.canonical_ns() is now the single namespace from which BOTH the
canonical's domain and its warm-state slot derive, so they cannot drift apart. A
live-warm provider gets ns "canon-<recipe>": domain warm-canon-keycloak (unchanged)
and slot /var/lib/ci-warm/canon-keycloak/. Every other recipe keeps ns "<recipe>" —
zero on-disk change for the 15 existing canonicals, and no migration on cc-ci
(keycloak's canonical was never seeded: its dir holds only last_good).

- warmsnap: functions take a SLOT, not a recipe; add live_slot(); meta records "slot".
- warmsnap: _assert_slot_not_foreign() refuses to snapshot/restore a slot recorded
  against a different domain -- defence in depth, naming-scheme-independent, fails
  before the destructive swap rather than at the next restore.
- canonical: registry_path/seed_canonical/prune_stale go through canonical_slot().
- prune_stale: the "reconciler dirs are never pruned" invariant is now STRUCTURAL --
  <recipe>/ never gains a canonical.json, so de-enrolling keycloak can no longer
  rmtree the reconciler's last_good (consequence 4).
- warm_reconcile: last_good + snapshot/restore go through warmsnap.live_slot().
- run_recipe_ci: canonical rollback restores from canonical_slot(recipe).
- Correct the two comments that claimed the deployments "can never touch each other".

Tests: 10 new (slot disjointness for every WARM_DOMAINS recipe, slot<->stack 1:1,
registry not in the reconciler dir, prune spares last_good, foreign-slot refusal in
both snapshot and restore). Unit suite 315 -> 325, no regressions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FS8p1esg57UAC69riNvuBX
2026-07-09 00:11:28 +00:00

174 lines
8.6 KiB
Python

"""Unit tests for the WC2 canonical registry (runner/harness/canonical.py).
Pure parts: enrollment (recipe_meta.WARM_CANONICAL), stable domain, registry read/write. The
data-warm lifecycle (deploy/undeploy-keep-volume/seed) is integration, proven live on a real recipe
canonical (W1).
"""
from __future__ import annotations
import json
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "runner"))
from harness import canonical, warm, warmsnap # noqa: E402
from harness import meta as harness_meta # noqa: E402
def test_canonical_domain():
assert canonical.canonical_domain("custom-html") == "warm-custom-html.ci.commoninternet.net"
assert warm.stable_domain("keycloak") == "warm-keycloak.ci.commoninternet.net"
# stable_domain matches the live-warm keycloak's mapping (no divergence)
assert warm.stable_domain("keycloak") == warm.WARM_DOMAINS["keycloak"]
def test_is_enrolled_missing_meta_false(tmp_path, monkeypatch):
# A recipe with no recipe_meta.py is not enrolled.
assert canonical.is_enrolled("definitely-not-a-recipe-xyz") is False
def test_is_enrolled_reads_flag(tmp_path, monkeypatch):
# Point the module's tests/<recipe>/ lookup at a temp recipe by monkeypatching __file__ dir.
recipe = "tmpwarm"
tests_dir = tmp_path / "tests" / recipe
tests_dir.mkdir(parents=True)
(tests_dir / "recipe_meta.py").write_text("WARM_CANONICAL = True\n")
# is_enrolled reads through the single meta loader (rcust P1); point its tests/ root at the
# temp layout.
monkeypatch.setattr(harness_meta, "TESTS_DIR", str(tmp_path / "tests"))
assert canonical.is_enrolled(recipe) is True
(tests_dir / "recipe_meta.py").write_text("WARM_CANONICAL = False\n")
assert canonical.is_enrolled(recipe) is False
(tests_dir / "recipe_meta.py").write_text("DEPS = ['keycloak']\n") # flag absent
assert canonical.is_enrolled(recipe) is False
def test_registry_roundtrip(tmp_path, monkeypatch):
monkeypatch.setenv("CCCI_WARM_ROOT", str(tmp_path))
assert canonical.read_registry("custom-html") is None
rec = canonical.write_registry(
"custom-html", version="1.10.0+x", commit="abc123", status="idle"
)
assert rec["domain"] == "warm-custom-html.ci.commoninternet.net"
assert rec["version"] == "1.10.0+x" and rec["commit"] == "abc123" and rec["status"] == "idle"
back = canonical.read_registry("custom-html")
assert back == rec
# atomic overwrite to a new status
canonical.write_registry("custom-html", version="1.10.0+x", commit="abc123", status="warm")
assert canonical.read_registry("custom-html")["status"] == "warm"
# the file is valid JSON on disk
with open(canonical.registry_path("custom-html")) as f:
assert json.load(f)["status"] == "warm"
def test_enrolled_recipes_scans_meta(tmp_path, monkeypatch):
# enrolled_recipes() lists recipes whose tests/<r>/recipe_meta.py sets WARM_CANONICAL=True.
monkeypatch.setattr(harness_meta, "TESTS_DIR", str(tmp_path / "tests"))
for name, body in (
("aaa", "WARM_CANONICAL = True\n"),
("bbb", "DEPS=['x']\n"),
("ccc", "WARM_CANONICAL = True\n"),
):
d = tmp_path / "tests" / name
d.mkdir(parents=True)
(d / "recipe_meta.py").write_text(body)
(tmp_path / "tests" / "ddd").mkdir(parents=True) # no recipe_meta.py at all
assert canonical.enrolled_recipes() == ["aaa", "ccc"]
def test_prune_stale_drops_deenrolled_only(tmp_path, monkeypatch):
# prune_stale removes <recipe>/ dirs that have a canonical.json but aren't enrolled; keeps
# enrolled canonicals, reconciler dirs (no canonical.json), and alerts/.
monkeypatch.setenv("CCCI_WARM_ROOT", str(tmp_path))
monkeypatch.setattr(canonical, "enrolled_recipes", lambda: ["keepme"])
monkeypatch.setattr(canonical.warmsnap, "stack_volumes", lambda d: []) # no docker in unit
# enrolled canonical (keep), de-enrolled canonical (prune), reconciler dir (keep), alerts (keep)
for name in ("keepme", "gone"):
(tmp_path / name).mkdir()
(tmp_path / name / "canonical.json").write_text(f'{{"recipe":"{name}"}}')
(tmp_path / "keycloak").mkdir()
(tmp_path / "keycloak" / "last_good").write_text("v1") # reconciler
(tmp_path / "alerts").mkdir()
pruned = canonical.prune_stale()
assert pruned == ["gone"]
assert not (tmp_path / "gone").exists()
assert (tmp_path / "keepme").exists()
assert (tmp_path / "keycloak").exists() # no canonical.json → not a canonical → kept
assert (tmp_path / "alerts").exists()
# ------------------------------------------- F-redfix-4: canonical ns/slot vs live-warm provider
def test_canonical_ns_and_domain_for_live_warm_provider():
# keycloak is a live-warm provider → its canonical is namespaced apart on BOTH axes.
assert canonical.canonical_ns("keycloak") == "canon-keycloak"
assert canonical.canonical_domain("keycloak") == "warm-canon-keycloak.ci.commoninternet.net"
assert canonical.canonical_slot("keycloak") == "canon-keycloak"
# ...and an ordinary recipe is untouched (zero blast radius on existing canonicals).
assert canonical.canonical_ns("cryptpad") == "cryptpad"
assert canonical.canonical_domain("cryptpad") == "warm-cryptpad.ci.commoninternet.net"
assert canonical.canonical_slot("cryptpad") == "cryptpad"
def test_live_and_canonical_slots_are_disjoint():
"""F-redfix-4: for EVERY live-warm provider, the reconciler's slot and the canonical's slot must
differ — they are two deployments and each `snapshot()` atomically replaces its slot."""
for recipe in warm.WARM_DOMAINS:
assert canonical.canonical_slot(recipe) != warmsnap.live_slot(recipe)
# every non-provider recipe keeps one slot (there is only one deployment)
assert canonical.canonical_slot("cryptpad") == warmsnap.live_slot("cryptpad")
def test_slot_maps_1to1_to_its_stack():
# The invariant warmsnap relies on: distinct slot ⇔ distinct stack (domain = warm-<ns>).
for recipe in warm.WARM_DOMAINS:
assert warm.stable_domain(canonical.canonical_slot(recipe)) == canonical.canonical_domain(
recipe
)
assert warm.stable_domain(warmsnap.live_slot(recipe)) == warm.WARM_DOMAINS[recipe]
def test_registry_path_of_live_warm_provider_is_not_the_reconciler_dir(tmp_path, monkeypatch):
"""The canonical registry must NOT land in `<recipe>/`, where the reconciler keeps last_good."""
monkeypatch.setenv("CCCI_WARM_ROOT", str(tmp_path))
assert canonical.registry_path("keycloak") == str(
tmp_path / "canon-keycloak" / "canonical.json"
)
canonical.write_registry("keycloak", version="10.7.1+26.6.2", commit=None, status="idle")
assert (tmp_path / "canon-keycloak" / "canonical.json").is_file()
assert not (tmp_path / "keycloak").exists() # reconciler dir untouched
def test_prune_stale_keeps_enrolled_provider_canonical_and_reconciler(tmp_path, monkeypatch):
"""Enrolled keycloak: `canon-keycloak/` is its canonical slot (keep), `keycloak/` is the
reconciler's (keep — no canonical.json)."""
monkeypatch.setenv("CCCI_WARM_ROOT", str(tmp_path))
monkeypatch.setattr(canonical, "enrolled_recipes", lambda: ["keycloak"])
monkeypatch.setattr(canonical.warmsnap, "stack_volumes", lambda d: [])
(tmp_path / "canon-keycloak").mkdir()
(tmp_path / "canon-keycloak" / "canonical.json").write_text('{"recipe":"keycloak"}')
(tmp_path / "keycloak").mkdir()
(tmp_path / "keycloak" / "last_good").write_text("10.7.1+26.6.2")
assert canonical.prune_stale() == []
assert (tmp_path / "canon-keycloak").exists()
assert (tmp_path / "keycloak" / "last_good").read_text() == "10.7.1+26.6.2"
def test_prune_stale_deenrolled_provider_spares_reconciler_last_good(tmp_path, monkeypatch):
"""F-redfix-4 consequence 4: de-enrolling keycloak must drop `canon-keycloak/` ONLY — never the
reconciler's `keycloak/last_good`. It also targets the canon stack's volumes, not the live ones."""
monkeypatch.setenv("CCCI_WARM_ROOT", str(tmp_path))
monkeypatch.setattr(canonical, "enrolled_recipes", lambda: []) # de-enrolled
seen = []
monkeypatch.setattr(canonical.warmsnap, "stack_volumes", lambda d: seen.append(d) or [])
(tmp_path / "canon-keycloak").mkdir()
(tmp_path / "canon-keycloak" / "canonical.json").write_text('{"recipe":"keycloak"}')
(tmp_path / "keycloak").mkdir()
(tmp_path / "keycloak" / "last_good").write_text("10.7.1+26.6.2")
assert canonical.prune_stale() == ["canon-keycloak"]
assert not (tmp_path / "canon-keycloak").exists()
assert (tmp_path / "keycloak" / "last_good").read_text() == "10.7.1+26.6.2"
assert seen == ["warm-canon-keycloak.ci.commoninternet.net"] # never the LIVE provider's stack