tenant-engine/tests/test_layer_conformance.py

107 lines
3.8 KiB
Python
Raw Normal View History

"""TEN-WP-0011-T01/T02: layer declaration and published PEP stance."""
from __future__ import annotations
import importlib.util
import subprocess
import sys
from pathlib import Path
import yaml
from tenant_engine.stance import published_stance, shipped_stance
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "scripts" / "check_layer_conformance.py"
def _run(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run([sys.executable, str(SCRIPT), *args], capture_output=True, text=True)
def test_layer_yaml_declares_engine_pip():
data = yaml.safe_load((ROOT / "layer.yaml").read_text())
assert data["repository"] == "tenant-engine"
assert data["layer"] == "engine"
assert data["role"] == "pip"
assert data["derived"] is True
assert data["derived_from"] == "INTENT.md"
assert data["tooling_contacts"] == []
assert data["pep_stance"] == "pep-stance.yaml"
assert data["pip_claims"] == "pip-claims.yaml"
ids = {c["id"] for c in data["non_tooling_clients"]}
assert "postgres-own-store" in ids
assert "sqlite-dev-store" in ids
assert "access-engine-check" in ids
assert "state-hub-work-records" in ids
def _intent() -> dict:
return yaml.safe_load((ROOT / "INTENT.md").read_text().split("---", 2)[1])
def _fold(value: object) -> str:
return str(value).translate(str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz"))
def test_intent_frontmatter_agrees_with_layer_yaml():
"""A11/A9: the derived form agrees with the governing one after a case-fold.
A fold, not an equality: INTENT.md says Engine and layer.yaml says engine,
and neither is re-spelled. A real divergence still fails.
"""
intent = _intent()
decl = yaml.safe_load((ROOT / "layer.yaml").read_text())
assert _fold(intent["layer"]) == _fold(decl["layer"])
assert _fold(intent["role"]) == _fold(decl["role"])
assert _fold(intent["layer"]) in {"taxonomy", "tooling", "engine", "staff"}
def test_no_declaration_carries_a_standard_version():
"""GH-DEC-2026-017 / A12: neither form carries a standard version."""
assert "standard_version" not in yaml.safe_load((ROOT / "layer.yaml").read_text())
assert "standard_version" not in _intent()
def test_checker_rejects_a_divergence_that_survives_the_fold(tmp_path, monkeypatch):
spec = importlib.util.spec_from_file_location("check_layer_conformance", SCRIPT)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
fake = tmp_path / "INTENT.md"
fake.write_text("---\nlayer: Staff\nrole: PIP\n---\n")
monkeypatch.setattr(module, "INTENT", fake)
try:
module.intent_frontmatter()
except SystemExit as exc:
assert exc.code == 2
else: # pragma: no cover
raise AssertionError("a Staff INTENT.md must not pass as Engine")
def test_checker_passes_on_the_real_tree():
result = _run()
assert result.returncode == 0, result.stderr + result.stdout
def test_checker_catches_an_undeclared_openbao_client(tmp_path, monkeypatch):
spec = importlib.util.spec_from_file_location("check_layer_conformance", SCRIPT)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
fake_src = tmp_path / "src" / "tenant_engine"
fake_src.mkdir(parents=True)
(fake_src / "vault.py").write_text("import hvac\n")
monkeypatch.setattr(module, "SRC", fake_src)
hits = module.scan()
assert hits
assert any(h[1] == "hvac" for h in hits)
def test_published_stance_equals_shipped_behaviour():
assert published_stance() == shipped_stance()
assert set(shipped_stance()) == {"unset", "unreachable", "non_allow", "unknown"}
assert set(shipped_stance().values()) == {"fail_closed"}