114 lines
4.6 KiB
Python
114 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from aios_single_owner_governance.evidence import (
|
|
ActivationEvidenceClosureBuilder,
|
|
ApprovalMirrorStatusVerifier,
|
|
)
|
|
from aios_single_owner_governance.service import main, runtime_state
|
|
from aios_single_owner_governance.status import (
|
|
DormantStatusProducer,
|
|
MachineStatusProducer,
|
|
)
|
|
|
|
from .helpers import HEAD
|
|
|
|
|
|
class EvidenceStatusServiceTests(unittest.TestCase):
|
|
def test_complete_evidence_set_builds_canonical_closure(self):
|
|
evidence = {
|
|
"activation_prerequisites_commit_oid": "a" * 40,
|
|
"activation_prerequisites_tree_oid": "b" * 40,
|
|
"artifact_sha256": "c" * 64,
|
|
"rollback_artifact_sha256": "d" * 64,
|
|
"validator_sha256": "e" * 64,
|
|
"state_machine_sha256": "f" * 64,
|
|
"branch_protection_adapter_sha256": "0" * 64,
|
|
"rollback_configuration_sha256": "1" * 64,
|
|
"test_evidence_sha256": "2" * 64,
|
|
"deployment_manifest_sha256": "3" * 64,
|
|
"dormant_unit_manifest_sha256": "4" * 64,
|
|
"dormant_deployment_evidence_sha256": "5" * 64,
|
|
}
|
|
closure = ActivationEvidenceClosureBuilder().build(evidence)
|
|
self.assertEqual(len(closure.closure_sha256), 64)
|
|
with self.assertRaises(ValueError):
|
|
ActivationEvidenceClosureBuilder().build({})
|
|
|
|
def test_mirror_verifier_requires_all_four_refs_and_fsck(self):
|
|
verifier = ApprovalMirrorStatusVerifier()
|
|
self.assertTrue(verifier.verify(
|
|
HEAD, gitea_oid=HEAD, quarantine_oid=HEAD,
|
|
authoritative_oid=HEAD, controller_visible_oid=HEAD,
|
|
fsck_connectivity="PASS",
|
|
))
|
|
self.assertFalse(verifier.verify(
|
|
HEAD, gitea_oid=HEAD, quarantine_oid=HEAD,
|
|
authoritative_oid="b" * 40, controller_visible_oid=HEAD,
|
|
fsck_connectivity="PASS",
|
|
))
|
|
|
|
def test_machine_status_is_fixture_only_and_dormant(self):
|
|
with self.assertRaises(DormantStatusProducer):
|
|
MachineStatusProducer().produce("aios/machine-gates", "success", HEAD)
|
|
with self.assertRaises(DormantStatusProducer):
|
|
MachineStatusProducer(enabled=True, fixture=True).produce(
|
|
"aios/machine-gates", "success", HEAD
|
|
)
|
|
record = MachineStatusProducer(fixture=True).produce(
|
|
"aios/evidence-closure", "success", HEAD
|
|
)
|
|
self.assertEqual(record.head_oid, HEAD)
|
|
|
|
def test_service_self_check_is_dormant_and_activate_is_rejected(self):
|
|
output = io.StringIO()
|
|
with contextlib.redirect_stdout(output):
|
|
self.assertEqual(main(["--component", "owner-review-adapter", "--self-check"]), 0)
|
|
state = json.loads(output.getvalue())
|
|
self.assertFalse(state["active"])
|
|
self.assertFalse(state["production_mutation"])
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
with self.assertRaises(SystemExit):
|
|
main(["--component", "owner-review-adapter", "--activate"])
|
|
|
|
def test_write_flags_cannot_be_enabled(self):
|
|
for name in (
|
|
"AIOS_SINGLE_OWNER_GOVERNANCE_ENABLED",
|
|
"AIOS_SINGLE_OWNER_ACTIVATION_ACTIVE",
|
|
"AIOS_GITOPS_WRITES_ENABLED",
|
|
):
|
|
with self.subTest(name=name), patch.dict(os.environ, {name: "true"}, clear=False):
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
with self.assertRaises(SystemExit):
|
|
main(["--component", "machine-status-producer", "--self-check"])
|
|
|
|
def test_service_entrypoint_restart_recovery_across_processes(self):
|
|
with tempfile.TemporaryDirectory(prefix="aios-governance-fixture-") as root:
|
|
command = [
|
|
sys.executable, "-m", "aios_single_owner_governance.service",
|
|
"--component", "owner-review-adapter", "--self-check",
|
|
"--checkpoint-dir", root,
|
|
]
|
|
first = subprocess.run(command, text=True, capture_output=True, check=True)
|
|
second = subprocess.run(command, text=True, capture_output=True, check=True)
|
|
first_state = json.loads(first.stdout)
|
|
second_state = json.loads(second.stdout)
|
|
self.assertFalse(first_state["restart_recovery"])
|
|
self.assertTrue(second_state["restart_recovery"])
|
|
self.assertEqual(second_state["prior_invocations"], 1)
|
|
self.assertFalse(second_state["production_mutation"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|