"""Actual startup Warm state and exact authenticated target rejection are independent proofs.""" from contextlib import contextmanager from copy import deepcopy from pathlib import Path import sys import unittest from unittest.mock import Mock sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import scenarios def warm_state(node, role, generation=3): return {"nodeId": node + ":4053", "mode": "Warm", "isWarm": True, "warmRole": {"role": role, "generation": generation}, "serving": {"nodeId": node + ":4053", "role": "Primary" if role == "Active" else "Standby", "incarnation": "warm-" + node, "catalogEpoch": 4, "authorityEpoch": 4, "servingGeneration": generation}, "process": {"id": 11 if node == "source-1" else 12, "startedUtc": "2026-11-17T00:00:00Z"}} def rejection(node="source-1"): return {"readiness": "Unavailable", "selectionRejection": "TargetIneligible", "failurePhase": "GetCapabilities", "failureEvidence": {"category": "TargetIneligible", "truncated": False, "chain": [{"code": "NoReadyEndpoint"}, {"code": "TargetIneligible"}]}, "target": {"mode": "Node", "expectedClusterId": "SOURCE", "expectedNodeId": node + ":4053", "consistency": "ReadyReplica"}} class WarmStartupFixtureTests(unittest.TestCase): def test_actual_roles_select_warm_standby_instead_of_old_hot_secondary(self): states = {"source-1": warm_state("source-1", "Standby"), "source-2": warm_state("source-2", "Active")} self.assertEqual(scenarios.select_warm_pair(states), ("source-2", "source-1")) for change in ({"mode": "Unset"}, {"isWarm": False}, {"warmRole": {"role": "Standby", "generation": 0}}, {"nodeId": "other:4053"}): bad = deepcopy(states) bad["source-1"].update(change) with self.subTest(change=change), self.assertRaises(scenarios.ScenarioError): scenarios.select_warm_pair(bad) def test_only_known_target_ineligible_chain_and_exact_source_standby_proof_pass(self): actual, source = rejection(), warm_state("source-1", "Standby") self.assertIs(scenarios.validate_warm_target_rejection(actual, source, expected_node_id="source-1:4053"), actual) def test_tls_transport_rpc_backoff_truncation_and_wrong_target_cannot_pass(self): cases = [dict(rejection(), failurePhase="Read"), dict(rejection(), sample={"value": 42}), dict(rejection(), target=dict(rejection()["target"], expectedNodeId="source-2:4053")), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": True, "chain": []}), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": False, "chain": [{"code": "NoReadyEndpoint"}, {"code": "TransportFailure"}]}), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": False, "chain": [{"code": "NoReadyEndpoint"}, {"code": "RpcFailure", "rpcStatus": "Unavailable"}]}), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": False, "chain": [{"code": "DiscoveryBackoff"}]}), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": False, "chain": [{"code": "NoReadyEndpoint"}, {"code": "TlsFailure"}]}), dict(rejection(), failureEvidence={"category": "TargetIneligible", "truncated": False, "chain": [{"code": "TargetIneligible"}] * 9})] for actual in cases: with self.subTest(actual=actual), self.assertRaises(scenarios.ScenarioError): scenarios.validate_warm_target_rejection(actual, warm_state("source-1", "Standby"), expected_node_id="source-1:4053") def test_d09_uses_real_startup_override_actual_standby_and_restores_before_outer_baseline(self): runner = object.__new__(scenarios.ScenarioRunner) runner.manifest = {"nodes": {"source-1": {}, "source-2": {}, "consumer-1": {}}} runner.primary = Mock(side_effect=lambda pair: pair + "-1") runner.node_id = Mock(side_effect=lambda node: node + ":4053") hot = {"nodeId": "source-2:4053", "role": "Secondary", "incarnation": "hot", "servingGeneration": 1} runner.matrix_probe = Mock(return_value={"serving": hot}) events, reads = [], [10] runner.device = Mock(side_effect=lambda: {"effect_total": 0, "read_total": reads[0]}) runner.check = Mock(side_effect=lambda valid, message: self.assertTrue(valid, message)) runner.rig = Mock() runner.rig.load.return_value = runner.manifest runner.rig.seed_helper.side_effect = AssertionError("no global resolver inside Warm startup context") @contextmanager def configuration(name, nodes, environment): self.assertEqual(nodes, ("source-1", "source-2")) self.assertEqual(environment, {"OpcUa__Redundancy__Mode": "Warm"}) events.append("startup Warm") try: yield finally: events.append("startup restored") runner.rig._configuration_experiment.side_effect = configuration runner.mutate = Mock(side_effect=lambda **fields: events.append(fields)) states = {"source-1": warm_state("source-1", "Standby"), "source-2": warm_state("source-2", "Active")} runner.wait_warm_pair = Mock(return_value=states) runner.wait_warm_state = Mock(side_effect=lambda node, role: warm_state(node, role, 4 if role == "Active" else 3)) runner.wait_connection_ready, runner.save = Mock(), Mock() runner.remaining = Mock(side_effect=lambda maximum: maximum) runner.baseline = Mock(side_effect=AssertionError("outer baseline must follow startup restoration")) @contextmanager def stopped(node): self.assertEqual(node, "source-2") events.append("stop Warm Active") yield runner.stopped = stopped def probe(node, operation, realm="raw", **fields): if fields.get("expected_state") == "TargetIneligible": return rejection() active = warm_state("source-1", "Active", 4) reads[0] += 1 return {"serving": active["serving"], "status": "Good", "resultReadiness": "Ready", "maximumAgeMilliseconds": 5000, "ageBounds": {"lowerMilliseconds": 0, "upperMilliseconds": 1}, "sample": {"value": 42, "serving": active["serving"]}} runner.probe = Mock(side_effect=probe) runner.scenario_D09() self.assertEqual(runner.matrix_probe.call_count, 4) self.assertLess(events.index("startup Warm"), events.index({"source_redundancy_mode": "Warm"})) self.assertIn({"target_mode": "Node", "target_node_id": "source-1:4053", "consistency": "ReadyReplica"}, events) self.assertEqual(events[-1], "startup restored") runner.baseline.assert_not_called() for realm in ("raw", "uns"): runner.probe.assert_any_call("consumer-1", "read", realm, expected_state="TargetIneligible", expectedNodeId="source-1:4053") runner.probe.assert_any_call("consumer-1", "read", realm, expected_state="Good", expectedNodeId="source-1:4053", expectedRole="Primary") if __name__ == "__main__": unittest.main()