95 lines
3.7 KiB
Python
Executable File
95 lines
3.7 KiB
Python
Executable File
"""Assignment state persistence, locking and audit log."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from proxdomain.errors import ProxdomainError
|
|
from proxdomain.state import Assignment, StateStore
|
|
|
|
|
|
def assignment(fqdn: str = "web01.example.com", vmid: int = 101) -> Assignment:
|
|
return Assignment(fqdn=fqdn, vmid=vmid, node="pve", kind="qemu", name="web01", ip="10.0.0.5")
|
|
|
|
|
|
class StateTests(unittest.TestCase):
|
|
def test_roundtrip(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = str(Path(tmp) / "state.json")
|
|
store = StateStore(path, audit_path=str(Path(tmp) / "audit.jsonl"))
|
|
store.load()
|
|
store.upsert(assignment())
|
|
store.flush()
|
|
|
|
reloaded = StateStore(path)
|
|
items = reloaded.all()
|
|
self.assertEqual(len(items), 1)
|
|
self.assertEqual(items[0].fqdn, "web01.example.com")
|
|
self.assertEqual(items[0].ip, "10.0.0.5")
|
|
self.assertTrue(items[0].created_at)
|
|
|
|
def test_created_at_is_preserved(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = str(Path(tmp) / "state.json")
|
|
store = StateStore(path)
|
|
first = store.upsert(assignment())
|
|
created = first.created_at
|
|
first.port = 8080
|
|
again = store.upsert(first)
|
|
self.assertEqual(again.created_at, created)
|
|
self.assertGreaterEqual(again.updated_at, created)
|
|
|
|
def test_remove(self) -> None:
|
|
store = StateStore(":memory:")
|
|
store.upsert(assignment())
|
|
self.assertTrue(store.remove("web01.example.com"))
|
|
self.assertFalse(store.remove("web01.example.com"))
|
|
self.assertEqual(store.all(), [])
|
|
|
|
def test_corrupt_state_is_reported(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "state.json"
|
|
path.write_text("{not json", encoding="utf-8")
|
|
with self.assertRaises(ProxdomainError) as ctx:
|
|
StateStore(str(path)).load()
|
|
self.assertIn("cannot read state file", str(ctx.exception))
|
|
|
|
def test_unknown_version_is_rejected(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "state.json"
|
|
path.write_text(json.dumps({"version": 99, "assignments": {}}), encoding="utf-8")
|
|
with self.assertRaises(ProxdomainError):
|
|
StateStore(str(path)).load()
|
|
|
|
def test_audit_appends_json_lines(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
audit = Path(tmp) / "audit.jsonl"
|
|
store = StateStore(str(Path(tmp) / "state.json"), audit_path=str(audit))
|
|
store.audit("assign", fqdn="web01.example.com", vmid=101)
|
|
store.audit("remove", fqdn="web01.example.com")
|
|
lines = [json.loads(line) for line in audit.read_text(encoding="utf-8").splitlines()]
|
|
self.assertEqual([line["event"] for line in lines], ["assign", "remove"])
|
|
self.assertEqual(lines[0]["vmid"], 101)
|
|
|
|
def test_second_lock_times_out(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
store = StateStore(str(Path(tmp) / "state.json"), lock_timeout=0.2)
|
|
with store.locked():
|
|
with self.assertRaises(ProxdomainError) as ctx:
|
|
with store.locked():
|
|
pass
|
|
self.assertIn("locked", str(ctx.exception))
|
|
|
|
def test_memory_only(self) -> None:
|
|
store = StateStore(":memory:")
|
|
store.upsert(assignment())
|
|
store.flush()
|
|
self.assertEqual(len(store.all()), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|