Files
napalm-ubiquiti/tests/test_sources.py
T
Christian Manivong f77524cd44 feat: device sources, through the controller or over SSH
ControllerSource reads a device's stat/device entry, its networks and port
profiles, the stations and the WLANs from its controller; SshSource reads the
device's own mca-dump and running.cfg. Both hand the drivers the same record,
read once per connection.
2026-12-05 16:57:16 +01:00

130 lines
4.4 KiB
Python

"""Where a switch's or AP's readings come from: its controller, or the device itself."""
from __future__ import annotations
import json
from typing import Any
import pytest
from conftest import load
from napalm.base.exceptions import CommandErrorException
from napalm_ubiquiti.client import UnifiError
from napalm_ubiquiti.sources import (
CONFIG_COMMAND,
DUMP_COMMAND,
ControllerSource,
SshSource,
)
MAC = "fc:ec:da:11:22:33"
class FakeClient:
def __init__(self, data: dict[str, list[Any]]) -> None:
self.data = data
self.asked: list[tuple[str, str]] = []
def classic_get(self, site: str, endpoint: str) -> list[Any]:
self.asked.append((site, endpoint))
return self.data.get(endpoint, [])
class FakeShell:
def __init__(self, answers: dict[str, str]) -> None:
self.answers = answers
self.ran: list[str] = []
def run(self, command: str) -> str:
self.ran.append(command)
if command not in self.answers:
raise CommandErrorException(f"{command}: not found")
return self.answers[command]
class TestController:
def test_the_record_is_the_devices_entry_by_mac(self, us16: dict[str, Any]) -> None:
client = FakeClient({f"stat/device/{MAC}": [us16]})
record = ControllerSource(client, "default", MAC).record()
assert record["model"] == "US16P150"
assert client.asked == [("default", f"stat/device/{MAC}")]
def test_the_record_is_read_once_per_connection(self, us16: dict[str, Any]) -> None:
client = FakeClient({f"stat/device/{MAC}": [us16]})
source = ControllerSource(client, "default", MAC)
source.record()
source.record()
assert len(client.asked) == 1
def test_a_device_the_controller_does_not_know(self) -> None:
with pytest.raises(UnifiError, match="does not manage"):
ControllerSource(FakeClient({}), "default", MAC).record()
def test_the_mac_is_normalised(self, us16: dict[str, Any]) -> None:
client = FakeClient({f"stat/device/{MAC}": [us16]})
ControllerSource(client, "default", "FC-EC-DA-11-22-33").record()
assert client.asked == [("default", f"stat/device/{MAC}")]
def test_vlans_come_from_networks_and_profiles(self, us16: dict[str, Any]) -> None:
networks = [
{"_id": "lan", "name": "Default", "purpose": "corporate", "attr_hidden_id": "LAN"}
]
client = FakeClient(
{f"stat/device/{MAC}": [us16], "rest/networkconf": networks, "rest/portconf": []}
)
vlans = ControllerSource(client, "default", MAC).vlans_detail()
assert len(vlans["1"]["untagged"]) == 18
assert ("default", "rest/portconf") in client.asked
def test_clients_and_wlans(self) -> None:
client = FakeClient(
{
"stat/sta": load("controller/stat-sta.json"),
"rest/wlanconf": load("controller/wlanconf.json"),
}
)
source = ControllerSource(client, "default", "80:2a:a8:00:01:02")
assert [c["mac"] for c in source.wireless_clients({})] == ["00:00:00:00:00:01"]
assert [w["name"] for w in source.wlans()] == ["SSID 1", "SSID 2"]
class TestSsh:
def test_the_record_is_the_devices_own_dump(self, us24_dump: dict[str, Any]) -> None:
shell = FakeShell({DUMP_COMMAND: json.dumps(us24_dump)})
record = SshSource(shell).record()
assert record["hostname"] == "usw-1"
assert shell.ran == [DUMP_COMMAND]
def test_a_dump_that_is_no_json(self) -> None:
with pytest.raises(CommandErrorException, match="mca-dump"):
SshSource(FakeShell({DUMP_COMMAND: "sh: mca-dump: not found"})).record()
def test_vlans_from_the_running_config(self, us24_dump: dict[str, Any], us24_cfg: str) -> None:
shell = FakeShell({DUMP_COMMAND: json.dumps(us24_dump), CONFIG_COMMAND: us24_cfg})
vlans = SshSource(shell).vlans_detail()
assert vlans["11"]["untagged"] == ["Port 2"]
def test_clients_come_from_the_vap_tables_and_there_are_no_wlans(self) -> None:
record = {
"vap_table": [
{"essid": "Lab", "radio": "ng", "sta_table": [{"mac": "aa:bb:cc:00:00:01"}]}
]
}
source = SshSource(FakeShell({}))
assert [c["mac"] for c in source.wireless_clients(record)] == ["aa:bb:cc:00:00:01"]
assert source.wlans() == []