driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m17s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / lowest (pull_request) Successful in 36s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m7s
CI / driver (pull_request) Successful in 2m56s
driver / publish (pull_request) Skipped
create_dhcp_reservation and _kea_subnet_uuid_for each walked the searchSubnet rows for the one holding an address. Both now call the pure subnet_containing(), which brings create_dhcp_reservation under the complexity limit and back below its recorded length after #40; the quality baseline records the shrunk entries.
91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
"""Matching Kea reservations and leases on address and MAC (#26)."""
|
|
|
|
from napalm_opnsense.kea_reservations import (
|
|
lease_held,
|
|
reservation_unchanged,
|
|
split_by_mac,
|
|
subnet_containing,
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
|
class TestSubnetContaining:
|
|
ROWS = [
|
|
{"uuid": "s-broken", "subnet": "not-a-cidr"},
|
|
{"uuid": "s-nocidr"},
|
|
{"uuid": "s-8", "subnet": " 172.22.8.0/24 "},
|
|
{"uuid": "s-wide", "subnet": "172.22.0.0/16"},
|
|
]
|
|
|
|
def test_first_subnet_holding_the_address_wins(self):
|
|
assert subnet_containing(self.ROWS, "172.22.8.253") == self.ROWS[2]
|
|
|
|
def test_rows_without_a_readable_subnet_are_skipped(self):
|
|
assert subnet_containing(self.ROWS, "172.22.9.1") == self.ROWS[3]
|
|
|
|
def test_none_when_no_subnet_holds_the_address(self):
|
|
assert subnet_containing(self.ROWS, "10.99.99.99") is None
|