From 89c70d85be165b79ee33b20c2deb5e26368f5e18 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:46:51 +0100 Subject: [PATCH] fix(lldp): keep every neighbour of a port apart lldpcli prints one Interface block per neighbour. The summary parser reused the port's last entry, so a second neighbour overwrote the first, and a neighbour without SysName was dropped. Each block now starts an entry of its own. Closes #22 --- CHANGELOG.md | 3 ++ napalm_proxmox/lldp_mixin.py | 24 ++++++------- tests/test_lldp.py | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 tests/test_lldp.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 96c89c7..a640423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `create_vm_from_cloud_init(ssh_public_keys=...)` installs every key, one per line, instead of only the first; a key containing `/` or `+` is no longer refused by Proxmox (#23). +- `get_lldp_neighbors()` lists every neighbour of a port: a second neighbour on + the same port no longer overwrites the first, and a neighbour that sends no + system name is listed with an empty `hostname` instead of being dropped (#22). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/lldp_mixin.py b/napalm_proxmox/lldp_mixin.py index cd10613..c8eae80 100644 --- a/napalm_proxmox/lldp_mixin.py +++ b/napalm_proxmox/lldp_mixin.py @@ -26,26 +26,26 @@ class ProxmoxLLDPMixin: """Mixin providing LLDP NAPALM methods.""" def get_lldp_neighbors(self) -> dict[str, list[_JsonDict]]: - """Return LLDP neighbours (requires lldpd on the Proxmox node).""" + """Return LLDP neighbours (requires lldpd on the Proxmox node). + + lldpcli prints one ``Interface:`` block per neighbour, so a port with + several neighbours appears once for each of them. + """ result: dict[str, list[_JsonDict]] = {} raw = self._exec_ssh_command("lldpcli show neighbors summary 2>/dev/null || true") - current_iface = "" + current: _JsonDict | None = None for line in raw.splitlines(): m_iface = re.match(r"^\s*Interface:\s+(\S+?),?\s", line) if m_iface: - current_iface = m_iface.group(1) - result.setdefault(current_iface, []) + current = {"hostname": "", "port": ""} + result.setdefault(m_iface.group(1), []).append(current) continue m_sys = re.match(r"^\s*SysName:\s+(.+)", line) m_port = re.match(r"^\s*PortID:\s+\S+\s+(.+)", line) - if m_sys and current_iface: - hostname = m_sys.group(1).strip() - if result[current_iface]: - result[current_iface][-1]["hostname"] = hostname - else: - result[current_iface].append({"hostname": hostname, "port": ""}) - if m_port and current_iface and result[current_iface]: - result[current_iface][-1]["port"] = m_port.group(1).strip() + if m_sys and current is not None: + current["hostname"] = m_sys.group(1).strip() + if m_port and current is not None: + current["port"] = m_port.group(1).strip() return result def get_lldp_neighbors_detail(self, interface: str = "") -> dict[str, list[_JsonDict]]: diff --git a/tests/test_lldp.py b/tests/test_lldp.py new file mode 100644 index 0000000..f48ab62 --- /dev/null +++ b/tests/test_lldp.py @@ -0,0 +1,65 @@ +"""LLDP neighbours read from lldpd's ``lldpcli show neighbors``.""" + +from __future__ import annotations + +from napalm_proxmox import ProxmoxDriver + +# lldpd 1.0 output: one "Interface:" block per neighbour, so a port with two +# neighbours (a switch and a phone behind it) appears twice. The phone sends +# no SysName. +TWO_NEIGHBOURS_ON_ONE_PORT = """\ +------------------------------------------------------------------------------- +LLDP neighbors: +------------------------------------------------------------------------------- +Interface: eno1, via: LLDP, RID: 1, Time: 0 day, 00:10:12 + Chassis: + ChassisID: mac 00:11:22:33:44:55 + SysName: sw1 + Port: + PortID: ifname gi1/0/1 + PortDescr: GigabitEthernet1/0/1 +------------------------------------------------------------------------------- +Interface: eno1, via: LLDP, RID: 2, Time: 0 day, 00:01:02 + Chassis: + ChassisID: mac aa:bb:cc:dd:ee:ff + Port: + PortID: mac aa:bb:cc:dd:ee:ff +------------------------------------------------------------------------------- +Interface: eno2, via: LLDP, RID: 3, Time: 0 day, 00:10:12 + Chassis: + ChassisID: mac 00:11:22:33:44:66 + SysName: sw2 + Port: + PortID: ifname gi1/0/2 +------------------------------------------------------------------------------- +""" + + +def _driver(output: str) -> ProxmoxDriver: + d = ProxmoxDriver("pve", "root@pam", "pw") + d._exec_ssh_command = lambda command: output + 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"}, + {"hostname": "", "port": "aa:bb:cc:dd:ee:ff"}, + ], + "eno2": [{"hostname": "sw2", "port": "gi1/0/2"}], + } + + def test_no_lldpd_output_is_no_neighbour(self): + assert _driver("").get_lldp_neighbors() == {} + + +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"