driver / lint (pull_request) Successful in 12s
driver / standard (pull_request) Successful in 15s
driver / lowest (pull_request) Successful in 33s
driver / types (pull_request) Successful in 54s
driver / test (3.12) (pull_request) Successful in 1m3s
driver / test (3.13) (pull_request) Successful in 55s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 1m44s
driver / publish (pull_request) Skipped
OPNsenseDriver implemented get_dhcp_leases without declaring napalm-device-types' DhcpLeasesMixin, so contract tests and isinstance checks missed it and nothing checked it against the declared signature. It now lives in OPNsenseDhcpLeasesMixin(DhcpLeasesMixin), under strict mypy and typed as DhcpLeaseDict; the floor is napalm-device-types 4.54.0. Every row has the declared shape: the MAC normalised to lower-case colon form, a numeric Kea state kept as its text, and a Kea answer that is a plain list read instead of failing. The ARP fallback keeps its state "arp" rows.
131 lines
4.2 KiB
Python
131 lines
4.2 KiB
Python
"""``get_dhcp_leases()`` keeps napalm-device-types' ``DhcpLeasesMixin`` contract (#53).
|
|
|
|
The driver declares the mixin, so a contract test or an ``isinstance`` check finds it,
|
|
and every row it returns -- from Kea, from ISC DHCP and from the ARP fallback -- has
|
|
the declared ``DhcpLeaseDict`` shape: the declared keys, each of its declared type, the
|
|
MAC lower-case and colon-separated.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
import re
|
|
from typing import Any
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from napalm_device_types.dhcp_leases import DhcpLeaseDict, DhcpLeasesMixin
|
|
|
|
from napalm_opnsense.opnsense import OPNsenseDriver
|
|
|
|
KEA = "/api/kea/leases4/search"
|
|
ISC = "/api/dhcpv4/leases/searchlease"
|
|
ARP = "/api/diagnostics/interface/get_arp"
|
|
|
|
#: Each source as OPNsense answers it, with the forms a MAC and an end come in.
|
|
KEA_ROWS = [
|
|
{
|
|
"hwaddr": "AA-BB-CC-DD-EE-01",
|
|
"address": "10.0.0.11",
|
|
"hostname": "nas",
|
|
"expire": 1700000000,
|
|
"state": 0,
|
|
},
|
|
{"hw-address": "aa:bb:cc:dd:ee:02", "ip-address": "10.0.0.12", "hostname": None, "expire": "x"},
|
|
]
|
|
ISC_ROWS = [
|
|
{"mac": "aa:bb:cc:dd:ee:03", "address": "10.0.0.13", "ends": "1700000000", "state": "active"}
|
|
]
|
|
ARP_ROWS = [{"intf": "igb0", "mac": "AA:BB:CC:DD:EE:04", "ip": "10.0.0.14", "expires": "1200"}]
|
|
|
|
TYPES = {"mac": str, "ip": str, "hostname": str, "lease_end": int, "state": str}
|
|
MAC = re.compile(r"^[0-9a-f]{2}(:[0-9a-f]{2}){5}$")
|
|
|
|
|
|
def _driver(kea: Any = None, isc: Any = None, arp: Any = None) -> OPNsenseDriver:
|
|
def answer(value: Any) -> Any:
|
|
if isinstance(value, Exception):
|
|
raise value
|
|
return value
|
|
|
|
driver = OPNsenseDriver("gw-01", "user", "secret")
|
|
driver._get = MagicMock(side_effect=lambda path: answer({KEA: kea, ARP: arp}[path])) # type: ignore[method-assign]
|
|
driver._post = MagicMock(side_effect=lambda path, data=None: answer({ISC: isc}[path])) # type: ignore[method-assign]
|
|
return driver
|
|
|
|
|
|
def test_the_driver_declares_the_mixin() -> None:
|
|
assert issubclass(OPNsenseDriver, DhcpLeasesMixin)
|
|
assert isinstance(OPNsenseDriver("gw-01", "user", "secret"), DhcpLeasesMixin)
|
|
|
|
|
|
def test_the_method_takes_what_the_mixin_declares() -> None:
|
|
assert list(inspect.signature(OPNsenseDriver.get_dhcp_leases).parameters) == ["self"]
|
|
|
|
|
|
def test_the_types_here_are_the_declared_ones() -> None:
|
|
assert set(TYPES) == DhcpLeaseDict.__required_keys__ | DhcpLeaseDict.__optional_keys__
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"driver",
|
|
[
|
|
_driver(kea={"rows": KEA_ROWS}),
|
|
_driver(kea=RuntimeError("404"), isc={"rows": ISC_ROWS}),
|
|
# The ARP fallback keeps its ``state: "arp"``: netOrk never stores those as leases.
|
|
_driver(kea=RuntimeError("404"), isc=RuntimeError("404"), arp=ARP_ROWS),
|
|
],
|
|
ids=["kea", "isc", "arp-fallback"],
|
|
)
|
|
def test_every_row_has_the_declared_shape(driver: OPNsenseDriver) -> None:
|
|
leases = driver.get_dhcp_leases()
|
|
|
|
assert leases
|
|
for lease in leases:
|
|
assert set(lease) == set(TYPES)
|
|
assert all(type(lease[key]) is kind for key, kind in TYPES.items()), lease # type: ignore[literal-required]
|
|
assert MAC.match(lease["mac"]), lease["mac"]
|
|
|
|
|
|
def test_the_values_kea_gives_in_other_forms() -> None:
|
|
leases = _driver(kea={"rows": KEA_ROWS}).get_dhcp_leases()
|
|
|
|
assert leases == [
|
|
{
|
|
"mac": "aa:bb:cc:dd:ee:01",
|
|
"ip": "10.0.0.11",
|
|
"hostname": "nas",
|
|
"lease_end": 1700000000,
|
|
"state": "0",
|
|
},
|
|
{
|
|
"mac": "aa:bb:cc:dd:ee:02",
|
|
"ip": "10.0.0.12",
|
|
"hostname": "",
|
|
"lease_end": 0,
|
|
"state": "",
|
|
},
|
|
]
|
|
|
|
|
|
def test_the_arp_fallback_rows_stay_as_they_are() -> None:
|
|
leases = _driver(
|
|
kea=RuntimeError("404"), isc=RuntimeError("404"), arp=ARP_ROWS
|
|
).get_dhcp_leases()
|
|
|
|
assert leases == [
|
|
{
|
|
"mac": "aa:bb:cc:dd:ee:04",
|
|
"ip": "10.0.0.14",
|
|
"hostname": "",
|
|
"lease_end": 0,
|
|
"state": "arp",
|
|
}
|
|
]
|
|
|
|
|
|
def test_a_kea_answer_that_is_a_plain_list_is_read() -> None:
|
|
leases = _driver(kea=KEA_ROWS).get_dhcp_leases()
|
|
|
|
assert [lease["mac"] for lease in leases] == ["aa:bb:cc:dd:ee:01", "aa:bb:cc:dd:ee:02"]
|