A decision issued in the seconds between a heal and its verification is missing from the kernel until the bouncer's next poll, so verification reported the heal as failed even though it had fixed the drift it was started for. Seen on a node: a metadata heal fixed a stale TTL, a new ban arrived inside the 5 s window, and the heal was logged as failed; the next audit found the node in sync. reconcile() now records the decision values it saw (last_truth_values) and takes an optional baseline_truth. Post-heal verification passes the values the drift was found against; values whose LAPI membership changed since then are left out of the drift, logged with a count, returned as lapi_changed_values, and checked by the next regular audit. Drift on decisions that existed when the heal started still fails verification. The regular audit and the membership re-check are unchanged. Tests: baseline handling in reconcile, the verification call, and a replay of the logged sequence (failed before, success now). 78 tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
258 lines
10 KiB
Python
258 lines
10 KiB
Python
import tempfile
|
|
import unittest
|
|
from datetime import datetime
|
|
from unittest.mock import patch
|
|
|
|
from healing import should_heal, execute_heal_and_verify, recheck_membership_mismatch
|
|
|
|
|
|
class FakeReporter:
|
|
def __init__(self):
|
|
self.update_calls = []
|
|
|
|
def update_status(self, is_synced, ban_count, last_error=None, **kwargs):
|
|
self.update_calls.append(
|
|
{
|
|
"is_synced": is_synced,
|
|
"ban_count": ban_count,
|
|
"last_error": last_error,
|
|
**kwargs,
|
|
}
|
|
)
|
|
|
|
|
|
class FakeAuditor:
|
|
def __init__(self, heal_ok=True, reconcile_result=None, reconcile_exception=None):
|
|
self.heal_ok = heal_ok
|
|
self.reconcile_result = reconcile_result
|
|
self.reconcile_exception = reconcile_exception
|
|
self.last_heal_drift = None
|
|
|
|
def heal(self):
|
|
return self.heal_ok
|
|
|
|
def heal_with_drift(self, drift):
|
|
self.last_heal_drift = drift
|
|
return self.heal_ok
|
|
|
|
def reconcile(self, **kwargs):
|
|
self.reconcile_kwargs = kwargs
|
|
if self.reconcile_exception:
|
|
raise self.reconcile_exception
|
|
return self.reconcile_result
|
|
|
|
|
|
class HealGateTests(unittest.TestCase):
|
|
def test_should_heal_suppresses_identical_recent_fingerprint(self):
|
|
now = datetime.now().isoformat()
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
|
|
tmp.write(
|
|
f"{now} - HEAL: drift_fingerprint=abc123 heal_result=success post_drift_fingerprint=\n"
|
|
)
|
|
path = tmp.name
|
|
|
|
self.assertFalse(should_heal(path, "abc123", min_interval=900))
|
|
|
|
def test_should_heal_allows_different_fingerprint(self):
|
|
now = datetime.now().isoformat()
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
|
|
tmp.write(
|
|
f"{now} - HEAL: drift_fingerprint=abc123 heal_result=success post_drift_fingerprint=\n"
|
|
)
|
|
path = tmp.name
|
|
|
|
self.assertTrue(should_heal(path, "xyz999", min_interval=900))
|
|
|
|
def test_should_heal_reads_recent_heal_from_tail(self):
|
|
now = datetime.now().isoformat()
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
|
|
for idx in range(4000):
|
|
tmp.write(f"noise line {idx}\n")
|
|
tmp.write(
|
|
f"{now} - HEAL: drift_fingerprint=tail123 heal_result=success post_drift_fingerprint=\n"
|
|
)
|
|
path = tmp.name
|
|
|
|
self.assertFalse(should_heal(path, "tail123", min_interval=900))
|
|
|
|
def test_should_heal_handles_timezone_aware_history_timestamp(self):
|
|
now_utc = datetime.now().astimezone().astimezone().isoformat()
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
|
|
tmp.write(
|
|
f"{now_utc} - HEAL: drift_fingerprint=aware123 heal_result=success post_drift_fingerprint=\n"
|
|
)
|
|
path = tmp.name
|
|
|
|
self.assertFalse(should_heal(path, "aware123", min_interval=900))
|
|
|
|
|
|
class PostHealVerificationTests(unittest.TestCase):
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_execute_heal_and_verify_success(self, _sleep):
|
|
reporter = FakeReporter()
|
|
post_drift = {
|
|
"reason": "in_sync",
|
|
"missing_count": 0,
|
|
"extra_count": 0,
|
|
"metadata_drift_count": 0,
|
|
"fingerprint": "",
|
|
}
|
|
auditor = FakeAuditor(heal_ok=True, reconcile_result=(True, 12, post_drift))
|
|
|
|
heal_result, returned_post_drift = execute_heal_and_verify(
|
|
auditor,
|
|
reporter,
|
|
count=12,
|
|
drift={"fingerprint": "pre123"},
|
|
heal_verify_delay=0,
|
|
)
|
|
|
|
self.assertEqual("success", heal_result)
|
|
self.assertEqual(post_drift, returned_post_drift)
|
|
self.assertEqual(1, len(reporter.update_calls))
|
|
self.assertTrue(reporter.update_calls[0]["is_synced"])
|
|
self.assertEqual(12, reporter.update_calls[0]["ban_count"])
|
|
self.assertEqual("success", reporter.update_calls[0]["heal_result"])
|
|
self.assertEqual("", reporter.update_calls[0]["last_drift_fingerprint"])
|
|
self.assertEqual({"fingerprint": "pre123"}, auditor.last_heal_drift)
|
|
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_execute_heal_and_verify_restart_failed(self, _sleep):
|
|
reporter = FakeReporter()
|
|
auditor = FakeAuditor(heal_ok=False, reconcile_result=(True, 0, {}))
|
|
|
|
heal_result, returned_post_drift = execute_heal_and_verify(
|
|
auditor,
|
|
reporter,
|
|
count=7,
|
|
drift={"fingerprint": "pre123"},
|
|
heal_verify_delay=0,
|
|
)
|
|
|
|
self.assertEqual("restart_failed", heal_result)
|
|
self.assertEqual("not_verified", returned_post_drift["reason"])
|
|
self.assertEqual(1, len(reporter.update_calls))
|
|
self.assertFalse(reporter.update_calls[0]["is_synced"])
|
|
self.assertEqual(7, reporter.update_calls[0]["ban_count"])
|
|
self.assertEqual("Bouncer restart failed", reporter.update_calls[0]["last_error"])
|
|
self.assertEqual("restart_failed", reporter.update_calls[0]["heal_result"])
|
|
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_execute_heal_and_verify_verify_exception(self, _sleep):
|
|
reporter = FakeReporter()
|
|
auditor = FakeAuditor(heal_ok=True, reconcile_exception=RuntimeError("boom"))
|
|
|
|
heal_result, returned_post_drift = execute_heal_and_verify(
|
|
auditor,
|
|
reporter,
|
|
count=7,
|
|
drift={"fingerprint": "pre123"},
|
|
heal_verify_delay=0,
|
|
)
|
|
|
|
self.assertEqual("verify_error", heal_result)
|
|
self.assertEqual("verify_error", returned_post_drift["reason"])
|
|
self.assertEqual(1, len(reporter.update_calls))
|
|
self.assertFalse(reporter.update_calls[0]["is_synced"])
|
|
self.assertEqual(7, reporter.update_calls[0]["ban_count"])
|
|
self.assertIn("Post-heal verify error: boom", reporter.update_calls[0]["last_error"])
|
|
self.assertEqual("verify_error", reporter.update_calls[0]["heal_result"])
|
|
|
|
|
|
class PostHealBaselineTests(unittest.TestCase):
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_verification_is_judged_against_the_pre_heal_truth(self, _sleep):
|
|
auditor = FakeAuditor(heal_ok=True, reconcile_result=(True, 3, {"reason": "in_sync"}))
|
|
auditor.last_truth_values = frozenset({"192.0.2.1"})
|
|
|
|
execute_heal_and_verify(auditor, FakeReporter(), count=3, drift={}, heal_verify_delay=0)
|
|
|
|
self.assertEqual({"baseline_truth": frozenset({"192.0.2.1"})}, auditor.reconcile_kwargs)
|
|
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_auditor_without_baseline_support_is_verified_as_before(self, _sleep):
|
|
auditor = FakeAuditor(heal_ok=True, reconcile_result=(True, 3, {"reason": "in_sync"}))
|
|
|
|
execute_heal_and_verify(auditor, FakeReporter(), count=3, drift={}, heal_verify_delay=0)
|
|
|
|
self.assertEqual({}, auditor.reconcile_kwargs)
|
|
|
|
@patch("healing.time.sleep", return_value=None)
|
|
def test_decision_issued_during_verification_window_does_not_fail_the_heal(self, _sleep):
|
|
# Replays a logged metadata heal: the heal fixed the stale TTL, but a new
|
|
# decision arrived in the 5 s before verification, before the bouncer's next
|
|
# poll. That used to be recorded as heal_result=failed.
|
|
from auditor import FirewallAuditor
|
|
|
|
long_ban = {"duration": "335h", "until_epoch": None}
|
|
truth = {"192.0.2.10": long_ban, "192.0.2.11": long_ban}
|
|
kernel = {"192.0.2.10": 13780, "192.0.2.11": 335 * 3600}
|
|
auditor = FirewallAuditor()
|
|
auditor.get_remote_lapi_truth = lambda: dict(truth)
|
|
auditor.get_kernel_state = lambda: dict(kernel)
|
|
|
|
def heal(_drift):
|
|
kernel["192.0.2.10"] = 335 * 3600 # the restarted bouncer re-adds with the full TTL
|
|
truth["203.0.113.70"] = dict(long_ban) # new decision, not yet applied
|
|
return True
|
|
|
|
auditor.heal_with_drift = heal
|
|
is_synced, count, drift = auditor.reconcile()
|
|
self.assertEqual("metadata_drift", drift["reason"])
|
|
|
|
reporter = FakeReporter()
|
|
heal_result, post_drift = execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay=0)
|
|
|
|
self.assertEqual("success", heal_result)
|
|
self.assertEqual(["203.0.113.70"], post_drift["lapi_changed_values"])
|
|
self.assertTrue(reporter.update_calls[-1]["is_synced"])
|
|
|
|
|
|
class MembershipRecheckTests(unittest.TestCase):
|
|
MISMATCH = (False, 10, {"reason": "membership_mismatch", "missing_count": 1})
|
|
IN_SYNC = (True, 11, {"reason": "in_sync"})
|
|
|
|
def test_recheck_clears_transient_membership_mismatch(self):
|
|
auditor = FakeAuditor(reconcile_result=self.IN_SYNC)
|
|
waits = []
|
|
|
|
result = recheck_membership_mismatch(
|
|
auditor, self.MISMATCH, 30, wait=waits.append
|
|
)
|
|
|
|
self.assertEqual(self.IN_SYNC, result)
|
|
self.assertEqual([30], waits)
|
|
|
|
def test_recheck_returns_persisting_drift(self):
|
|
persisted = (False, 10, {"reason": "membership_mismatch", "missing_count": 2})
|
|
auditor = FakeAuditor(reconcile_result=persisted)
|
|
|
|
result = recheck_membership_mismatch(
|
|
auditor, self.MISMATCH, 30, wait=lambda _delay: None
|
|
)
|
|
|
|
self.assertEqual(persisted, result)
|
|
|
|
def test_recheck_skipped_for_metadata_drift_and_when_disabled(self):
|
|
auditor = FakeAuditor(reconcile_exception=AssertionError("must not re-audit"))
|
|
metadata = (False, 10, {"reason": "metadata_drift"})
|
|
waits = []
|
|
|
|
self.assertEqual(
|
|
metadata,
|
|
recheck_membership_mismatch(auditor, metadata, 30, wait=waits.append),
|
|
)
|
|
self.assertEqual(
|
|
self.MISMATCH,
|
|
recheck_membership_mismatch(auditor, self.MISMATCH, 0, wait=waits.append),
|
|
)
|
|
self.assertEqual(
|
|
self.IN_SYNC,
|
|
recheck_membership_mismatch(auditor, self.IN_SYNC, 30, wait=waits.append),
|
|
)
|
|
self.assertEqual([], waits)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|