"""get_lldp_neighbors: every neighbour of a port, its chassis MAC and its port (#58). lldpd's ``lldpctl -f keyvalue`` is parsed by napalm-device-types' ``parse_lldpctl_keyvalue``. Before, the driver grouped the lines by interface, so a second neighbour on a port overwrote the first; it looked for ``chassis.id.subtype``/``chassis.id.value`` and ``port.id.value``, keys lldpd never prints, so the chassis MAC was never reported and a port given by MAC or local id came back empty; and it cut ``eth0.100`` at its dot. """ from __future__ import annotations import pytest from napalm_linux.linux import LinuxDriver # 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. Not recorded from a device. On eth0 a phone and the # PC behind it (an unmanaged switch between them and the host), on the VLAN # interface eth0.100 a switch that names its port by a local id. LLDPCTL_KEYVALUE = """\ lldp.eth0.via=LLDP lldp.eth0.rid=1 lldp.eth0.age=0 day, 00:10:00 lldp.eth0.chassis.mac=00:11:22:33:44:AA lldp.eth0.chassis.name=desk-phone lldp.eth0.chassis.descr=SIP phone lldp.eth0.chassis.Tel.enabled=on lldp.eth0.port.mac=00:11:22:33:44:aa lldp.eth0.port.descr=WAN lldp.eth0.port.ttl=120 lldp.eth0.via=LLDP lldp.eth0.rid=2 lldp.eth0.age=0 day, 00:09:00 lldp.eth0.chassis.mac=00:11:22:33:44:bb lldp.eth0.chassis.name=desk-pc lldp.eth0.chassis.Station.enabled=on lldp.eth0.port.ifname=eno1 lldp.eth0.port.ttl=120 lldp.eth0.100.via=LLDP lldp.eth0.100.rid=3 lldp.eth0.100.age=1 day, 02:00:00 lldp.eth0.100.chassis.local=access-sw-01 lldp.eth0.100.chassis.name=access-sw lldp.eth0.100.chassis.Bridge.enabled=on lldp.eth0.100.port.local=gi1/0/7 lldp.eth0.100.port.descr=GigabitEthernet1/0/7 """ def _answer(driver: LinuxDriver, lldpctl: str, *, installed: bool = True): sent: list[str] = [] def send(command: str, read_timeout: float = 100) -> str: sent.append(command) if command.startswith("command -v lldpctl"): return "/usr/sbin/lldpctl" if installed else "" if "is-active lldpd" in command: return "active" if command.startswith("lldpctl"): return lldpctl raise AssertionError(f"unexpected command {command!r}") driver._send = send # type: ignore[method-assign] return sent @pytest.fixture() def driver() -> LinuxDriver: return LinuxDriver.__new__(LinuxDriver) class TestNeighbours: def test_every_neighbour_of_a_port_is_reported(self, driver): _answer(driver, LLDPCTL_KEYVALUE) result = driver.get_lldp_neighbors() assert [n["hostname"] for n in result["eth0"]] == ["desk-phone", "desk-pc"] def test_the_chassis_mac_is_reported_in_lower_case(self, driver): _answer(driver, LLDPCTL_KEYVALUE) phone, pc = driver.get_lldp_neighbors()["eth0"] assert phone["mac"] == "00:11:22:33:44:aa" assert pc["mac"] == "00:11:22:33:44:bb" def test_a_port_given_by_mac_or_local_id_is_reported(self, driver): _answer(driver, LLDPCTL_KEYVALUE) result = driver.get_lldp_neighbors() assert result["eth0"][0]["port"] == "00:11:22:33:44:aa" assert result["eth0"][1]["port"] == "eno1" assert result["eth0.100"][0]["port"] == "gi1/0/7" def test_a_dotted_interface_name_stays_whole(self, driver): _answer(driver, LLDPCTL_KEYVALUE) assert sorted(driver.get_lldp_neighbors()) == ["eth0", "eth0.100"] def test_a_chassis_not_named_by_a_mac_has_no_mac(self, driver): _answer(driver, LLDPCTL_KEYVALUE) assert driver.get_lldp_neighbors()["eth0.100"] == [ {"hostname": "access-sw", "port": "gi1/0/7"} ] def test_a_chassis_mac_that_is_no_mac_address_is_left_out(self, driver): _answer(driver, "lldp.eth0.via=LLDP\nlldp.eth0.chassis.mac=00:11\n") assert driver.get_lldp_neighbors() == {"eth0": [{"hostname": "", "port": ""}]} class TestWithoutLldpd: def test_without_lldpctl_there_are_no_neighbours(self, driver): sent = _answer(driver, LLDPCTL_KEYVALUE, installed=False) assert driver.get_lldp_neighbors() == {} assert not any(c.startswith("lldpctl") for c in sent) def test_lldpd_without_neighbours_lists_none(self, driver): _answer(driver, "") assert driver.get_lldp_neighbors() == {}