import unittest from inventory_collector.models import HostRecord from inventory_collector.engine import DeduplicationEngine class TestDeduplicationEngine(unittest.TestCase): def setUp(self): self.engine = DeduplicationEngine(conflict_policy="runtime_over_static") def test_exact_duplicate_merge(self): h1 = HostRecord( name="k3s-master-0", ansible_host="192.168.5.70", ansible_user="lain", groups=["k3s_master"], tags=["control-plane"], source="static:sample.ini" ) h2 = HostRecord( name="k3s-master-0", ansible_host="192.168.5.70", ansible_user="lain", groups=["proxmox_vms", "pve_node_node0"], tags=["proxmox"], variables={"proxmox_vmid": 100}, source="proxmox:qemu" ) inv, warnings = self.engine.process([h1, h2]) self.assertEqual(len(inv.hosts), 1) merged = inv.get_host("k3s-master-0") self.assertIsNotNone(merged) self.assertEqual(merged.source, "hybrid") self.assertEqual(sorted(merged.groups), ["k3s_master", "proxmox_vms", "pve_node_node0"]) self.assertEqual(sorted(merged.tags), ["control-plane", "proxmox"]) self.assertEqual(merged.variables.get("proxmox_vmid"), 100) def test_ip_discrepancy_runtime_over_static(self): h_static = HostRecord( name="tool-server-1", ansible_host="192.168.5.61", ansible_user="lain", source="static:sample.ini" ) h_runtime = HostRecord( name="tool-server-1", ansible_host="192.168.5.99", ansible_user="lain", source="proxmox:qemu" ) inv, warnings = self.engine.process([h_static, h_runtime]) merged = inv.get_host("tool-server-1") self.assertEqual(merged.ansible_host, "192.168.5.99") # Runtime wins self.assertTrue(any("IP discrepancy" in w and "tool-server-1" in w for w in warnings)) def test_ip_collision_different_names(self): h1 = HostRecord(name="nas", ansible_host="192.168.0.2", source="static") h2 = HostRecord(name="synology-storage", ansible_host="192.168.0.2", source="proxmox") inv, warnings = self.engine.process([h1, h2]) self.assertEqual(len(inv.hosts), 2) self.assertIn("nas", inv.hosts) self.assertIn("synology-storage", inv.hosts) self.assertTrue(any("IP collision" in w and "192.168.0.2" in w for w in warnings)) def test_user_conflict_static_over_default(self): h1 = HostRecord(name="node0", ansible_host="192.168.0.101", ansible_user="root", source="static") h2 = HostRecord(name="node0", ansible_host="192.168.0.101", ansible_user="lain", source="proxmox:node") inv, warnings = self.engine.process([h1, h2]) merged = inv.get_host("node0") self.assertEqual(merged.ansible_user, "root") if __name__ == "__main__": unittest.main()