test(agent): add coverage for drift gating and post-heal verification; remove bare except

This commit is contained in:
patrick committed 2026-02-10 22:04:57 -05:00
1 parent 461512f20a
commit 5fc5b8c114
3 files changed
+185 -37

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
import tempfile
import unittest
from datetime import datetime
from unittest.mock import patch
from agent import should_heal, execute_heal_and_verify
class FakeReporter:
def __init__(self):
self.update_calls = []
def update_status(self, is_synced, ban_count, last_error=None):
self.update_calls.append((is_synced, ban_count, last_error))
class FakeAuditor:
def __init__(self, heal_ok=True, reconcile_result=None):
self.heal_ok = heal_ok
self.reconcile_result = reconcile_result
def heal(self):
return self.heal_ok
def reconcile(self):
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))
class PostHealVerificationTests(unittest.TestCase):
@patch("agent.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([(True, 12, None)], reporter.update_calls)
@patch("agent.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([(False, 7, "Bouncer restart failed")], reporter.update_calls)
if __name__ == "__main__":
unittest.main()
+38
View File
@@ -0,0 +1,38 @@
import unittest
from auditor import FirewallAuditor
class AuditorDecisionExtractionTests(unittest.TestCase):
def test_extract_decisions_handles_flat_and_nested_payloads(self):
auditor = FirewallAuditor()
payload = [
{"value": "1.1.1.1", "duration": "4h", "origin": "lists"},
{
"decisions": [
{"value": "2.2.2.2", "duration": "8h", "origin": "crowdsec"},
{"value": "2.2.2.2", "duration": "8h", "origin": "crowdsec"},
]
},
{
"source": {
"decisions": [
{"value": "3.3.3.3", "duration": "12h", "origin": "manual"}
]
}
},
]
decisions = list(auditor._extract_decisions(payload))
decision_keys = {
(d.get("value"), d.get("duration"), d.get("origin")) for d in decisions
}
self.assertIn(("1.1.1.1", "4h", "lists"), decision_keys)
self.assertIn(("2.2.2.2", "8h", "crowdsec"), decision_keys)
self.assertIn(("3.3.3.3", "12h", "manual"), decision_keys)
self.assertEqual(3, len(decision_keys))
if __name__ == "__main__":
unittest.main()