Files
napalm-ubiquiti/tests/test_parse_device.py
T

275 lines
9.3 KiB
Python

"""Facts, ports, LLDP, MAC table and PoE from a device record.
A controller's ``stat/device`` entry and a device's own ``mca-dump`` carry the same
record; each parser is run against both where the fixture allows.
"""
from __future__ import annotations
from typing import Any
import pytest
from napalm_ubiquiti.parsers.device import (
MANAGEMENT,
parse_facts,
parse_interfaces,
parse_interfaces_ip,
parse_lldp_rows,
parse_mac_table,
parse_poe,
port_name,
)
class TestFacts:
def test_controller_record(self, us16: dict[str, Any]) -> None:
facts = parse_facts(us16)
assert facts["vendor"] == "Ubiquiti"
assert facts["model"] == "US16P150"
assert facts["hostname"] == "Switch 16"
assert facts["fqdn"] == "Switch 16"
assert facts["os_version"] == "4.0.66.10832"
assert facts["serial_number"] == "FCECDA112233"
assert facts["uptime"] == 3971865.0
assert facts["interface_list"][:2] == ["Port 1", "Port 2"]
assert facts["interface_list"][-1] == MANAGEMENT
assert len(facts["interface_list"]) == 19
def test_mca_dump_prefers_the_product_name_and_upper_cases_the_serial(
self, us24_dump: dict[str, Any]
) -> None:
facts = parse_facts(us24_dump)
assert facts["model"] == "US-24-G1"
assert facts["hostname"] == "usw-1"
assert facts["serial_number"] == "64F846000010"
def test_a_record_without_a_name_uses_its_mac(self) -> None:
facts = parse_facts({"mac": "f4:92:bf:00:00:01", "model": "U6LR", "version": "6.6.55"})
assert facts["hostname"] == "f4:92:bf:00:00:01"
assert facts["uptime"] == 0.0
assert facts["serial_number"] == ""
class TestInterfaces:
def test_ports_are_named_by_index_and_keep_their_label(self, us16: dict[str, Any]) -> None:
interfaces = parse_interfaces(us16)
assert interfaces["Port 1"] == {
"is_up": True,
"is_enabled": True,
"description": "",
"last_flapped": -1.0,
"mtu": 1500,
"speed": 1000.0,
"mac_address": "",
}
assert interfaces["Port 2"]["is_up"] is False
def test_a_renamed_port_keeps_its_name_as_description(self, uap: dict[str, Any]) -> None:
interfaces = parse_interfaces(uap)
assert interfaces["Port 1"]["description"] == "Main"
def test_management_carries_the_device_mac(self, us16: dict[str, Any]) -> None:
mgmt = parse_interfaces(us16)[MANAGEMENT]
assert mgmt["mac_address"] == "fc:ec:da:11:22:33"
assert mgmt["is_up"] is True
def test_a_disabled_port_and_jumbo_frames(self, us24_dump: dict[str, Any]) -> None:
record = dict(us24_dump)
record["port_table"] = [dict(us24_dump["port_table"][0], jumbo=True)]
port = parse_interfaces(record)["Port 1"]
assert port["is_enabled"] is False
assert port["mtu"] == 9216
def test_a_port_without_an_index_is_left_out(self) -> None:
record = {"mac": "78:8a:20:33:44:55", "port_table": [{"name": "wan", "up": True}]}
assert list(parse_interfaces(record)) == [MANAGEMENT]
def test_port_name(self) -> None:
assert port_name(7) == "Port 7"
class TestInterfacesIp:
def test_controller_record_takes_the_netmask_of_its_uplink(self, us16: dict[str, Any]) -> None:
assert parse_interfaces_ip(us16) == {
MANAGEMENT: {"ipv4": {"192.168.0.57": {"prefix_length": 24}}}
}
def test_mca_dump_has_its_own_netmask(self, us24_dump: dict[str, Any]) -> None:
assert parse_interfaces_ip(us24_dump) == {
MANAGEMENT: {"ipv4": {"10.0.0.10": {"prefix_length": 24}}}
}
def test_no_address_no_entry(self) -> None:
assert parse_interfaces_ip({"mac": "f4:92:bf:00:00:01"}) == {}
def test_an_unusable_netmask_leaves_32(self) -> None:
record = {"ip": "192.0.2.5", "uplink": {"netmask": "0.0.0.0"}}
assert parse_interfaces_ip(record) == {
MANAGEMENT: {"ipv4": {"192.0.2.5": {"prefix_length": 32}}}
}
class TestLldp:
def test_old_firmware_lists_neighbours_per_port(self, us8: dict[str, Any]) -> None:
rows = parse_lldp_rows(us8)
assert [(r["local_port"], r["system_name"], r["port_id"]) for r in rows] == [
("Port 2", "USG", "eth1"),
("Port 5", "Switch16", "0/1"),
("Port 6", "UAPACPRO", "80:2A:A8:55:44:33"),
]
def test_a_chassis_id_that_is_a_mac_is_typed_and_lower_cased(self, us8: dict[str, Any]) -> None:
row = parse_lldp_rows(us8)[1]
assert row["chassis_id"] == "fc:ec:da:33:22:44"
assert row["chassis_id_type"] == "mac"
def test_a_chassis_id_that_is_no_mac_stays_as_sent(self, us16: dict[str, Any]) -> None:
"""The anonymised fixture carries a seven-octet id: not a MAC, kept verbatim."""
row = parse_lldp_rows(us16)[0]
assert row["chassis_id"] == "F0:9F:C2:1A:2B:3C:4E"
assert row["chassis_id_type"] == "local"
def test_new_firmware_lists_them_at_the_top(self, us24_dump: dict[str, Any]) -> None:
rows = parse_lldp_rows(us24_dump)
assert rows == [
{
"local_port": "Port 22",
"chassis_id": "f0:9f:c2:00:00:01",
"chassis_id_type": "mac",
"port_id": "Port 1",
"port_id_type": "local",
"port_description": "",
"system_name": "",
"system_description": "",
"management_addresses": [],
"capabilities": [],
"enabled_capabilities": [],
}
]
def test_both_shapes_of_one_neighbour_count_once(self, usmini: dict[str, Any]) -> None:
record = dict(usmini)
record["port_table"] = [
dict(
usmini["port_table"][0],
lldp_table=[
{
"lldp_chassis_id": "FC:EC:00:11:22:33",
"lldp_port_id": "Port ",
"lldp_system_name": "sw",
}
],
)
]
rows = parse_lldp_rows(record)
assert len(rows) == 1
assert rows[0]["system_name"] == "sw"
def test_no_table_no_rows(self) -> None:
assert parse_lldp_rows({"port_table": [{"port_idx": 1}]}) == []
class TestMacTable:
def test_mca_dump_lists_what_each_port_learned(self, us24_dump: dict[str, Any]) -> None:
table = parse_mac_table(us24_dump)
assert len(table) == 12
first = table[0]
assert first["mac"] == "02:00:00:00:00:02"
assert first["vlan"] == 8
assert first["interface"].startswith("Port ")
assert first["static"] is False
assert first["active"] is True
assert first["moves"] == 0
assert first["last_move"] == -1.0
def test_a_controller_record_without_tables_is_empty(self, us16: dict[str, Any]) -> None:
assert parse_mac_table(us16) == []
class TestPoe:
def test_controller_record(self, us16: dict[str, Any]) -> None:
poe = parse_poe(us16)
assert poe["total_power_budget"] == 135.0
assert len(poe["ports"]) == 16
assert poe["ports"]["Port 1"] == {
"enabled": True,
"status": "searching",
"poe_class": "unknown",
"power_draw": 0.0,
"power_budget": 0.0,
"voltage": 0.0,
"current": 0.0,
}
@pytest.mark.parametrize(
("port", "status"),
[
({"poe_mode": "off"}, "disabled"),
({"poe_mode": "auto", "poe_good": True, "poe_power": "4.30"}, "delivering"),
({"poe_mode": "pasv24", "poe_good": True}, "delivering"),
({"poe_mode": "auto", "poe_good": False}, "searching"),
],
)
def test_status(self, port: dict[str, Any], status: str) -> None:
record = {"port_table": [dict(port, port_idx=1, port_poe=True)]}
assert parse_poe(record)["ports"]["Port 1"]["status"] == status
def test_drawn_power_adds_up(self) -> None:
record = {
"total_max_power": 60,
"port_table": [
{
"port_idx": 1,
"port_poe": True,
"poe_mode": "auto",
"poe_good": True,
"poe_power": "4.30",
"poe_class": "Class 2",
"poe_voltage": "53.10",
"poe_current": "81.00",
},
{
"port_idx": 2,
"port_poe": True,
"poe_mode": "auto",
"poe_good": True,
"poe_power": "2.20",
},
{"port_idx": 3, "port_poe": False},
],
}
poe = parse_poe(record)
assert poe["total_power_draw"] == pytest.approx(6.5)
assert poe["ports"]["Port 1"]["poe_class"] == "Class 2"
assert poe["ports"]["Port 1"]["current"] == 81.0
assert set(poe["ports"]) == {"Port 1", "Port 2"}
def test_unreadable_numbers_read_as_zero(self, us8: dict[str, Any]) -> None:
port = parse_poe(us8)["ports"]["Port 8"]
assert port["power_draw"] == 0.0
assert port["voltage"] == 48.0
assert port["poe_class"] == "unknown"