Files
napalm-proxmox/tests/test_lldp.py
T
Christian Manivong f93daa381e
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Failing after 29s
driver / test (3.12) (pull_request) Failing after 20s
driver / test (3.13) (pull_request) Failing after 17s
driver / lowest (pull_request) Failing after 18s
driver / security (pull_request) Failing after 45s
driver / build (pull_request) Successful in 35s
CI / driver (pull_request) Failing after 1m43s
driver / publish (pull_request) Skipped
fix(lldp): read lldpd through the shared parser and raise on failure
lldpcli's text was read with 2>/dev/null || true, so lldpd not running,
lldpctl missing or the command not running at all came back as a node
without neighbours. Capabilities were lldpd's symbols lower-cased (tel,
wlan, docsis) rather than NAPALM's names.

The node now runs napalm-device-types' LLDPCTL_KEYVALUE_COMMAND, whose
output carries lldpctl's exit status; parse_lldpctl_keyvalue raises on a
failure, an empty answer raises too, and the lldp_neighbors projections
give NAPALM's capability names and the chassis MAC. The module is under
strict mypy now; the floor on napalm-device-types rises to 4.24.0.

Closes #44
2026-11-02 08:55:59 +01:00

146 lines
5.5 KiB
Python

"""LLDP neighbours read from lldpd's ``lldpctl -f keyvalue`` (#44).
The output is parsed by napalm-device-types' ``parse_lldpctl_keyvalue`` and
projected by its ``lldp_neighbors`` / ``lldp_neighbors_detail``. Before, the
driver parsed ``lldpcli show neighbors`` text with ``2>/dev/null || true``, so
a failed read came back as no neighbours, and capabilities were lldpd's own
symbols lower-cased (``tel``, ``wlan``) instead of NAPALM's names.
"""
from __future__ import annotations
import pytest
from napalm_device_types import LLDPCTL_KEYVALUE_COMMAND
from napalm_proxmox import ProxmoxDriver
# Laid out the way lldpd's keyvalue writer prints ``show neighbors details``
# (src/client/display.c, kv_writer.c): every neighbour opens with ``via``, an id
# is printed under its type, a capability as ``chassis.<Symbol>.enabled``. Not
# recorded from a device. On eno1 a switch and a phone behind it (the phone
# sends no SysName), on eno2 a second switch; lldpctl's exit status follows, as
# LLDPCTL_KEYVALUE_COMMAND prints it.
TWO_NEIGHBOURS_ON_ONE_PORT = """\
lldp.eno1.via=LLDP
lldp.eno1.rid=1
lldp.eno1.age=0 day, 00:10:12
lldp.eno1.chassis.mac=00:11:22:33:44:55
lldp.eno1.chassis.name=sw1
lldp.eno1.chassis.descr=Switch OS 2.4
lldp.eno1.chassis.Bridge.enabled=on
lldp.eno1.chassis.Router.enabled=off
lldp.eno1.port.ifname=gi1/0/1
lldp.eno1.port.descr=GigabitEthernet1/0/1
lldp.eno1.via=LLDP
lldp.eno1.rid=2
lldp.eno1.age=0 day, 00:01:02
lldp.eno1.chassis.mac=AA:BB:CC:DD:EE:FF
lldp.eno1.chassis.Bridge.enabled=off
lldp.eno1.chassis.Tel.enabled=on
lldp.eno1.port.mac=aa:bb:cc:dd:ee:ff
lldp.eno2.via=LLDP
lldp.eno2.rid=3
lldp.eno2.age=0 day, 00:10:12
lldp.eno2.chassis.mac=00:11:22:33:44:66
lldp.eno2.chassis.name=sw2
lldp.eno2.chassis.Wlan.enabled=on
lldp.eno2.port.ifname=gi1/0/2
__LLDPCTL_RC=0
"""
def _driver(output: str, sent: list[str] | None = None) -> ProxmoxDriver:
d = ProxmoxDriver("pve", "root@pam", "pw")
def run(command: str) -> str:
if sent is not None:
sent.append(command)
return output
d._exec_ssh_command = run # type: ignore[method-assign]
return d
class TestGetLldpNeighbors:
def test_every_neighbour_on_a_port_is_its_own_entry(self):
result = _driver(TWO_NEIGHBOURS_ON_ONE_PORT).get_lldp_neighbors()
assert result == {
"eno1": [
{"hostname": "sw1", "port": "gi1/0/1", "mac": "00:11:22:33:44:55"},
{"hostname": "", "port": "aa:bb:cc:dd:ee:ff", "mac": "aa:bb:cc:dd:ee:ff"},
],
"eno2": [{"hostname": "sw2", "port": "gi1/0/2", "mac": "00:11:22:33:44:66"}],
}
def test_lldpctl_without_neighbours_lists_none(self):
assert _driver("__LLDPCTL_RC=0").get_lldp_neighbors() == {}
@pytest.mark.parametrize("getter", ["get_lldp_neighbors", "get_lldp_neighbors_detail"])
def test_lldpctl_is_asked_with_its_exit_status(self, getter):
sent: list[str] = []
getattr(_driver(TWO_NEIGHBOURS_ON_ONE_PORT, sent), getter)()
assert sent == [LLDPCTL_KEYVALUE_COMMAND]
class TestGetLldpNeighborsDetail:
def test_every_neighbour_on_a_port_is_its_own_entry(self):
result = _driver(TWO_NEIGHBOURS_ON_ONE_PORT).get_lldp_neighbors_detail()
assert [e["remote_system_name"] for e in result["eno1"]] == ["sw1", ""]
assert result["eno1"][1]["remote_chassis_id"] == "AA:BB:CC:DD:EE:FF"
def test_capabilities_carry_napalm_names(self):
result = _driver(TWO_NEIGHBOURS_ON_ONE_PORT).get_lldp_neighbors_detail()
phone = result["eno1"][1]
assert phone["remote_system_capab"] == ["bridge", "telephone"]
assert phone["remote_system_enable_capab"] == ["telephone"]
assert result["eno2"][0]["remote_system_capab"] == ["wlan-access-point"]
def test_the_fields_of_a_neighbour(self):
switch = _driver(TWO_NEIGHBOURS_ON_ONE_PORT).get_lldp_neighbors_detail()["eno1"][0]
assert switch == {
"parent_interface": "",
"remote_port": "gi1/0/1",
"remote_port_description": "GigabitEthernet1/0/1",
"remote_chassis_id": "00:11:22:33:44:55",
"remote_system_name": "sw1",
"remote_system_description": "Switch OS 2.4",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["bridge"],
}
def test_one_interface_only(self):
result = _driver(TWO_NEIGHBOURS_ON_ONE_PORT).get_lldp_neighbors_detail("eno2")
assert list(result) == ["eno2"]
class TestFailedRead:
"""A failed read raises; ``{}`` only when lldpctl ran and listed nobody."""
@pytest.mark.parametrize("getter", ["get_lldp_neighbors", "get_lldp_neighbors_detail"])
def test_lldpctl_failing_raises(self, getter):
output = (
"[WARN/control] unable to connect to socket /run/lldpd.socket: "
"No such file or directory\n__LLDPCTL_RC=1"
)
with pytest.raises(ValueError, match="unable to connect"):
getattr(_driver(output), getter)()
def test_a_missing_lldpctl_raises(self):
with pytest.raises(ValueError, match="not found"):
_driver("sh: 1: lldpctl: not found\n__LLDPCTL_RC=127").get_lldp_neighbors()
@pytest.mark.parametrize("getter", ["get_lldp_neighbors", "get_lldp_neighbors_detail"])
def test_no_answer_at_all_raises(self, getter):
# lldpctl's exit status is always printed; nothing at all means the
# command never ran on the node.
with pytest.raises(ValueError, match="no answer"):
getattr(_driver(""), getter)()