67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from reporter import LocalStateReporter
|
|
|
|
|
|
class ReporterStateTests(unittest.TestCase):
|
|
def test_update_status_writes_drift_and_fingerprint_fields(self):
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
state_file = os.path.join(tmp_dir, "state.json")
|
|
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
|
|
|
|
reporter.update_status(
|
|
False,
|
|
10,
|
|
last_error="boom",
|
|
missing_count=2,
|
|
extra_count=3,
|
|
metadata_drift_count=1,
|
|
last_drift_fingerprint="abc123",
|
|
last_heal_result="failed",
|
|
)
|
|
|
|
with open(state_file, "r") as f:
|
|
payload = json.load(f)
|
|
|
|
self.assertEqual("n1", payload["node_name"])
|
|
self.assertEqual("DESYNC_DETECTED", payload["status"])
|
|
self.assertEqual(10, payload["kernel_ban_count"])
|
|
self.assertEqual(2, payload["missing_count"])
|
|
self.assertEqual(3, payload["extra_count"])
|
|
self.assertEqual(1, payload["metadata_drift_count"])
|
|
self.assertEqual("abc123", payload["last_drift_fingerprint"])
|
|
self.assertEqual("failed", payload["last_heal_result"])
|
|
|
|
def test_update_status_preserves_last_heal_result_when_omitted(self):
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
state_file = os.path.join(tmp_dir, "state.json")
|
|
reporter = LocalStateReporter(state_file=state_file, node_name="n1")
|
|
|
|
reporter.update_status(
|
|
False,
|
|
7,
|
|
last_heal_result="success",
|
|
last_drift_fingerprint="oldfp",
|
|
)
|
|
reporter.update_status(
|
|
True,
|
|
7,
|
|
missing_count=0,
|
|
extra_count=0,
|
|
metadata_drift_count=0,
|
|
last_drift_fingerprint="",
|
|
)
|
|
|
|
with open(state_file, "r") as f:
|
|
payload = json.load(f)
|
|
|
|
self.assertEqual("success", payload["last_heal_result"])
|
|
self.assertEqual("", payload["last_drift_fingerprint"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|