Files
napalm-ubiquiti/tests/test_role_drivers.py
T
Christian Manivong e08f5bb28e feat: unifi_switch and unifi_ap drivers
UnifiSwitchDriver adds PoE and VLANs (with their ports, and NAPALM's
get_vlans from them); UnifiAccessPointDriver adds SSIDs, radios and wireless
clients. Discovery knows Ubiquiti's OUIs from the IEEE registry.
2026-12-05 17:02:49 +01:00

112 lines
4.5 KiB
Python

"""The switch and access point drivers: their role, and the readings their role adds.
A driver's source is set directly (opening one is tested in test_device_driver).
"""
from __future__ import annotations
import json
from typing import Any
import pytest
from conftest import load
from fakes import FakeClient, FakeShell
from napalm_device_types import AccessPointDriver, SwitchDriver
from napalm_device_types.roles import primary_role_of
from napalm_ubiquiti import UnifiAccessPointDriver, UnifiSwitchDriver
from napalm_ubiquiti.parsers.wireless import parse_radios, parse_ssids
from napalm_ubiquiti.sources import CONFIG_COMMAND, DUMP_COMMAND, ControllerSource, SshSource
NETWORKS = [
{"_id": "lan", "name": "Default", "purpose": "corporate", "attr_hidden_id": "LAN"},
{"_id": "iot", "name": "IoT", "purpose": "corporate", "vlan_enabled": True, "vlan": 20},
]
def _through_controller(driver: Any, record: dict[str, Any], **data: list[Any]) -> FakeClient:
client = FakeClient({f"stat/device/{record['mac']}": [record], **data})
driver.source = ControllerSource(client, "default", record["mac"])
return client
class TestSwitch:
def test_its_role(self) -> None:
assert issubclass(UnifiSwitchDriver, SwitchDriver)
assert primary_role_of(UnifiSwitchDriver) == "switch"
assert UnifiSwitchDriver.DRIVER_NAME == "unifi_switch"
assert "FC:EC:DA" in UnifiSwitchDriver.OUI_PREFIXES
def test_poe(self, us16: dict[str, Any]) -> None:
driver = UnifiSwitchDriver("192.0.2.20", "", "")
_through_controller(driver, us16)
poe = driver.get_poe_status()
assert poe["total_power_budget"] == 135.0
assert len(poe["ports"]) == 16
def test_vlans_through_the_controller(self, us16: dict[str, Any]) -> None:
driver = UnifiSwitchDriver("192.0.2.20", "", "")
_through_controller(driver, us16, **{"rest/networkconf": NETWORKS, "rest/portconf": []})
detail = driver.get_vlans_detail()
vlans = driver.get_vlans()
assert detail["1"]["name"] == "Default"
assert vlans[1]["name"] == "Default"
assert vlans[1]["interfaces"] == sorted(detail["1"]["untagged"] + detail["1"]["tagged"])
assert vlans[20]["name"] == "IoT"
def test_vlans_over_ssh(self, us24_dump: dict[str, Any], us24_cfg: str) -> None:
driver = UnifiSwitchDriver("192.0.2.30", "ubnt", "pw")
shell = FakeShell({DUMP_COMMAND: json.dumps(us24_dump), CONFIG_COMMAND: us24_cfg})
driver.source = SshSource(shell)
assert "Port 2" in driver.get_vlans()[11]["interfaces"]
class TestAccessPoint:
def test_its_role(self) -> None:
assert issubclass(UnifiAccessPointDriver, AccessPointDriver)
assert primary_role_of(UnifiAccessPointDriver) == "access_point"
assert UnifiAccessPointDriver.DRIVER_NAME == "unifi_ap"
def test_ssids_carry_the_controllers_wlan_settings(self, uap: dict[str, Any]) -> None:
wlans = load("controller/wlanconf.json")
driver = UnifiAccessPointDriver("192.0.2.4", "", "")
_through_controller(driver, uap, **{"rest/wlanconf": wlans})
assert driver.get_ssids() == parse_ssids(uap, wlans)
def test_radios(self, uap: dict[str, Any]) -> None:
driver = UnifiAccessPointDriver("192.0.2.4", "", "")
_through_controller(driver, uap)
assert driver.get_radio_status() == parse_radios(uap)
def test_clients_through_the_controller_are_this_aps_only(self, uap: dict[str, Any]) -> None:
driver = UnifiAccessPointDriver("192.0.2.4", "", "")
_through_controller(driver, uap, **{"stat/sta": load("controller/stat-sta.json")})
assert [c["mac"] for c in driver.get_wireless_clients()] == ["00:00:00:00:00:01"]
def test_over_ssh_clients_come_from_the_device(self) -> None:
record = {
"mac": "80:2a:a8:00:01:02",
"vap_table": [
{"essid": "Lab", "radio": "na", "sta_table": [{"mac": "aa:bb:cc:00:00:01"}]}
],
}
driver = UnifiAccessPointDriver("192.0.2.4", "ubnt", "pw")
driver.source = SshSource(FakeShell({DUMP_COMMAND: json.dumps(record)}))
assert [c["mac"] for c in driver.get_wireless_clients()] == ["aa:bb:cc:00:00:01"]
assert list(driver.get_ssids()) == ["Lab"]
@pytest.mark.parametrize("driver_class", [UnifiSwitchDriver, UnifiAccessPointDriver])
def test_both_read_over_ssh_and_through_the_controller(driver_class: Any) -> None:
assert driver_class.USES_SSH is True
assert driver_class.VENDOR == "Ubiquiti"