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") def test_inventory_sort_by_name(self): from inventory_collector.models import InventoryData inv = InventoryData() inv.add_host(HostRecord(name="zebra", ansible_host="10.0.0.1")) inv.add_host(HostRecord(name="alpha", ansible_host="10.0.0.2")) inv.add_host(HostRecord(name="beta", ansible_host="10.0.0.3")) sorted_inv = inv.sort(sort_by="name") self.assertEqual(list(sorted_inv.hosts.keys()), ["alpha", "beta", "zebra"]) rev_inv = inv.sort(sort_by="name", reverse=True) self.assertEqual(list(rev_inv.hosts.keys()), ["zebra", "beta", "alpha"]) def test_inventory_sort_by_ip(self): from inventory_collector.models import InventoryData inv = InventoryData() inv.add_host(HostRecord(name="host-101", ansible_host="192.168.0.101")) inv.add_host(HostRecord(name="host-2", ansible_host="192.168.0.2")) inv.add_host(HostRecord(name="host-20", ansible_host="192.168.0.20")) inv.add_host(HostRecord(name="host-no-ip", ansible_host="")) # In string sorting, "192.168.0.101" < "192.168.0.2". # But numerical octet sorting correctly places .2 before .20 before .101! sorted_inv = inv.sort(sort_by="ip") self.assertEqual(list(sorted_inv.hosts.keys()), ["host-2", "host-20", "host-101", "host-no-ip"]) rev_inv = inv.sort(sort_by="ip", reverse=True) self.assertEqual(list(rev_inv.hosts.keys()), ["host-no-ip", "host-101", "host-20", "host-2"]) if __name__ == "__main__": unittest.main()