"""Matching Kea reservations and leases on address and MAC (#26).""" from napalm_opnsense.kea_reservations import lease_held, reservation_unchanged, split_by_mac def _row(mac, uuid="u-1"): return {"uuid": uuid, "ip_address": "172.22.8.253", "hw_address": mac} class TestSplitByMac: def test_no_rows_is_neither_own_nor_other(self): assert split_by_mac([], "02:aa:bb:cc:dd:ee") == (None, "") def test_own_row_matches_whatever_the_mac_spelling(self): row = _row("02-AA-BB-CC-DD-EE") assert split_by_mac([row], "02:aa:bb:cc:dd:ee") == (row, "") def test_another_hosts_row_is_reported_by_its_mac(self): assert split_by_mac([_row("02-11-22-33-44-55")], "02:aa:bb:cc:dd:ee") == ( None, "02:11:22:33:44:55", ) def test_a_row_without_mac_is_no_ones(self): """It cannot be shown to be the caller's, so it is never taken as such.""" row = {"uuid": "u-1", "ip_address": "172.22.8.253"} assert split_by_mac([row], "02:aa:bb:cc:dd:ee") == (None, "") def test_another_hosts_mac_is_found_behind_a_row_without_one(self): rows = [{"uuid": "u-0", "ip_address": "172.22.8.253"}, _row("02:11:22:33:44:55")] assert split_by_mac(rows, "02:aa:bb:cc:dd:ee") == (None, "02:11:22:33:44:55") class TestLeaseHeld: def test_os_kea_lease_of_the_mac(self): rows = [{"address": "172.22.8.253", "hwaddr": "02:AA:BB:CC:DD:EE"}] assert lease_held(rows, "172.22.8.253", "02:aa:bb:cc:dd:ee") is True def test_plain_kea_lease_of_the_mac(self): rows = [{"ip-address": "172.22.8.253", "hw-address": "02:aa:bb:cc:dd:ee"}] assert lease_held(rows, "172.22.8.253", "02:aa:bb:cc:dd:ee") is True def test_lease_of_another_mac(self): rows = [{"address": "172.22.8.253", "hwaddr": "02:11:22:33:44:55"}] assert lease_held(rows, "172.22.8.253", "02:aa:bb:cc:dd:ee") is False def test_lease_of_another_address(self): rows = [{"address": "172.22.8.254", "hwaddr": "02:aa:bb:cc:dd:ee"}] assert lease_held(rows, "172.22.8.253", "02:aa:bb:cc:dd:ee") is False class TestReservationUnchanged: WANTED = { "subnet": "subnet-uuid", "ip_address": "172.22.8.253", "hw_address": "02:aa:bb:cc:dd:ee", "hostname": "new-vm", "description": "[netork]", } def test_a_different_mac_is_a_change(self): row = dict(self.WANTED, hw_address="02:11:22:33:44:55") assert reservation_unchanged(row, self.WANTED, "172.22.8.0/24") is False def test_the_same_reservation_is_unchanged(self): row = dict(self.WANTED, hw_address="02-AA-BB-CC-DD-EE", subnet="172.22.8.0/24") assert reservation_unchanged(row, self.WANTED, "172.22.8.0/24") is True