From 3d2de279881142efc9ba488cc5d17d629e1f0013 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:44:15 +0100 Subject: [PATCH 1/3] fix(lldp): list every neighbour of a port, with its chassis MAC lldpctl -f keyvalue was grouped by interface, so a second neighbour on a port overwrote the first. The driver looked for chassis.id.value and port.id.value, keys lldpd never prints (it names an id by its type, chassis.mac, port.local), so the chassis MAC was never reported and a port given by MAC or local id came back empty; and eth0.100 was cut at its dot. The output now goes through napalm-device-types' parse_lldpctl_keyvalue, which splits neighbours at their via line and keeps the interface name whole; the floor on napalm-device-types rises to 4.24.0 for it. Closes #58 --- CHANGELOG.md | 7 ++ napalm_linux/linux.py | 41 ++++-------- pyproject.toml | 2 +- tests/quality_baseline.json | 2 +- tests/test_lldp.py | 127 ++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 29 deletions(-) create mode 100644 tests/test_lldp.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ca5975..1143b87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,8 +43,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). +- The floor on napalm-device-types is 4.24.0, for its shared `lldpctl` parser. ### Fixed +- `get_lldp_neighbors()` lists every neighbour of a port (#58): a phone and the PC + behind it, or the devices behind an unmanaged switch, used to merge into one entry + and only the last one was left. A neighbour whose chassis id is a MAC address now + carries it as `mac` (it never did), a port given as a MAC address or local id is + reported instead of coming back empty, and an interface with a dot in its name + (`eth0.100`) keeps its whole name. - dnf and yum hosts read pending updates and security advisories from the cached metadata only (`--cacheonly`), as apt reads its local index (#59). With an expired cache `check-update` downloaded the repositories' metadata first, and on an Oracle diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 5ebb09c..c2a4ac9 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -50,6 +50,7 @@ from napalm_device_types import ( open_stream_on_transport, parse_apt_upgradable, parse_dnf_security, + parse_lldpctl_keyvalue, run_fix_steps, run_on_transport, strip_terminal_codes, @@ -127,6 +128,10 @@ _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE) #: Restart the host two seconds later, detached from this session: the launcher's #: exit status comes back before the host goes down, and closing the session #: cannot take the restart with it. +#: lldpd's neighbours; a failure prints nothing and reads as none, as before (#58). +_LLDPCTL_COMMAND = "lldpctl -f keyvalue 2>/dev/null || true" +#: A chassis id lldpd names ``mac`` that really is one. +_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$") _REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) /dev/null 2>&1 &'" #: Why ``run_command`` refuses stdin to a privileged command while a sudo @@ -763,7 +768,9 @@ class LinuxDriver( def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]: """Return LLDP neighbors if lldpd is installed and currently running. - Uses ``lldpctl -f keyvalue``. Returns an empty dict when lldpd is + Uses ``lldpctl -f keyvalue``, parsed by napalm-device-types: every + neighbour of a port is its own entry, and one whose chassis id is a MAC + address carries it as ``mac``. Returns an empty dict when lldpd is absent or stopped — does NOT attempt to start the daemon. """ # Check lldpctl is available @@ -779,33 +786,13 @@ class LinuxDriver( if "active" not in running: return {} - output = self._send("lldpctl -f keyvalue 2>/dev/null || true") + output = self._send(_LLDPCTL_COMMAND) neighbors: dict[str, list[dict[str, Any]]] = {} - entries: dict[str, dict[str, str]] = {} - - for line in output.splitlines(): - line = line.strip() - if "=" not in line: - continue - key, _, value = line.partition("=") - parts = key.split(".") - if len(parts) < 3 or parts[0] != "lldp": - continue - iface = parts[1] - subkey = ".".join(parts[2:]) - entries.setdefault(iface, {})[subkey] = value - - for iface, data in entries.items(): - entry = { - "hostname": data.get("chassis.name", ""), - "port": data.get("port.ifname", data.get("port.id.value", "")), - } - if data.get("chassis.id.subtype") == "mac": - mac = data.get("chassis.id.value", "") - if re.match(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$", mac): - entry["mac"] = mac.lower() - neighbors.setdefault(iface, []).append(entry) - + for row in parse_lldpctl_keyvalue(output): + entry: dict[str, Any] = {"hostname": row["system_name"], "port": row["port_id"]} + if row["chassis_id_type"] == "mac" and _MAC_RE.match(row["chassis_id"]): + entry["mac"] = row["chassis_id"].lower() + neighbors.setdefault(row["local_port"], []).append(entry) return neighbors def get_interfaces(self) -> dict[str, Any]: diff --git a/pyproject.toml b/pyproject.toml index 2d54d07..0ce252a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.22.0", + "napalm-device-types>=4.24.0", "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index f468186..b0037df 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -21,7 +21,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2142 + "napalm_linux/linux.py": 2129 }, "untyped_public": [] } diff --git a/tests/test_lldp.py b/tests/test_lldp.py new file mode 100644 index 0000000..f7c711f --- /dev/null +++ b/tests/test_lldp.py @@ -0,0 +1,127 @@ +"""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() == {} From a3d95d72b9b18d4ca3cabf4ff4af6b2f81ca05de Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:52:10 +0100 Subject: [PATCH 2/3] refactor(lldp): take the neighbour MAC from the shared projection napalm-device-types' lldp_neighbors now carries the chassis MAC as mac when the chassis id is a MAC address, the rule this driver applied itself. The getter uses the projection, so linux and openwrt report neighbours the same way. --- napalm_linux/linux.py | 15 ++++----------- tests/quality_baseline.json | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index c2a4ac9..8fa5c34 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -38,6 +38,7 @@ from napalm_device_types import ( HostStatusMixin, KernelFactsMixin, ListeningSocketsMixin, + LldpNeighborDict, OpenSshServerMixin, OSDriver, ServiceClustersMixin, @@ -47,6 +48,7 @@ from napalm_device_types import ( SystemdUnavailable, SystemIdentityMixin, VirtualIpsMixin, + lldp_neighbors, open_stream_on_transport, parse_apt_upgradable, parse_dnf_security, @@ -130,8 +132,6 @@ _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE) #: cannot take the restart with it. #: lldpd's neighbours; a failure prints nothing and reads as none, as before (#58). _LLDPCTL_COMMAND = "lldpctl -f keyvalue 2>/dev/null || true" -#: A chassis id lldpd names ``mac`` that really is one. -_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$") _REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) /dev/null 2>&1 &'" #: Why ``run_command`` refuses stdin to a privileged command while a sudo @@ -765,7 +765,7 @@ class LinuxDriver( except (IndexError, ValueError): return 0 - def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]: + def get_lldp_neighbors(self) -> dict[str, list[LldpNeighborDict]]: """Return LLDP neighbors if lldpd is installed and currently running. Uses ``lldpctl -f keyvalue``, parsed by napalm-device-types: every @@ -786,14 +786,7 @@ class LinuxDriver( if "active" not in running: return {} - output = self._send(_LLDPCTL_COMMAND) - neighbors: dict[str, list[dict[str, Any]]] = {} - for row in parse_lldpctl_keyvalue(output): - entry: dict[str, Any] = {"hostname": row["system_name"], "port": row["port_id"]} - if row["chassis_id_type"] == "mac" and _MAC_RE.match(row["chassis_id"]): - entry["mac"] = row["chassis_id"].lower() - neighbors.setdefault(row["local_port"], []).append(entry) - return neighbors + return lldp_neighbors(parse_lldpctl_keyvalue(self._send(_LLDPCTL_COMMAND))) def get_interfaces(self) -> dict[str, Any]: """Return interface operational data.""" diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index b0037df..804a6f3 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -21,7 +21,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2129 + "napalm_linux/linux.py": 2122 }, "untyped_public": [] } From d3189806d36c544d4ebbf3314292913339d699bf Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:53:52 +0100 Subject: [PATCH 3/3] fix(lldp): recognise a stopped lldpd and raise on a failed read The running check looked for "active" anywhere in the answer, and systemctl answers "inactive" for a stopped lldpd, so lldpctl was asked anyway. The check now prints one word, running or stopped, and is compared whole. With lldpd running, lldpctl runs as napalm-device-types' LLDPCTL_KEYVALUE_COMMAND: a failure (socket out of the login's reach, a daemon restarting) raises with lldpctl's message instead of reading as a host without neighbours. A missing or stopped lldpd still answers {}. Closes #66 --- CHANGELOG.md | 5 ++ napalm_linux/linux.py | 22 ++++---- tests/test_lldp.py | 113 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 125 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1143b87..d26f45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The floor on napalm-device-types is 4.24.0, for its shared `lldpctl` parser. ### Fixed +- `get_lldp_neighbors()` raises when lldpd runs but lldpctl fails (its socket out of + the login's reach, a daemon restarting), with lldpctl's message, instead of reporting + no neighbours (#66). A stopped lldpd is recognised as stopped: systemd's `inactive` + used to pass the running check, as it contains the word "active". A host without + lldpd, or with lldpd stopped, still reports no neighbours. - `get_lldp_neighbors()` lists every neighbour of a port (#58): a phone and the PC behind it, or the devices behind an unmanaged switch, used to merge into one entry and only the last one was left. A neighbour whose chassis id is a MAC address now diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 8fa5c34..eb48530 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -28,6 +28,7 @@ from napalm.base.netmiko_helpers import netmiko_args from napalm_device_types import ( APT_UPGRADABLE_COMMAND, DNF_SECURITY_COMMAND, + LLDPCTL_KEYVALUE_COMMAND, AptProxyMixin, ByteStream, CommandResult, @@ -130,8 +131,11 @@ _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE) #: Restart the host two seconds later, detached from this session: the launcher's #: exit status comes back before the host goes down, and closing the session #: cannot take the restart with it. -#: lldpd's neighbours; a failure prints nothing and reads as none, as before (#58). -_LLDPCTL_COMMAND = "lldpctl -f keyvalue 2>/dev/null || true" +#: ``running`` when systemd has lldpd active or a process of that name runs, else ``stopped``. +_LLDPD_STATE_COMMAND = ( + "if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; " + "then echo running; else echo stopped; fi" +) _REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) /dev/null 2>&1 &'" #: Why ``run_command`` refuses stdin to a privileged command while a sudo @@ -772,21 +776,17 @@ class LinuxDriver( neighbour of a port is its own entry, and one whose chassis id is a MAC address carries it as ``mac``. Returns an empty dict when lldpd is absent or stopped — does NOT attempt to start the daemon. + + :raises ValueError: when lldpd runs but lldpctl fails (socket out of reach). """ - # Check lldpctl is available if not self._send("command -v lldpctl 2>/dev/null").strip(): return {} - # Check lldpd is active (systemd or fallback to pgrep) - running = self._send( - "systemctl is-active lldpd 2>/dev/null || " - "service lldpd status 2>/dev/null | grep -q running && echo active || " - "pgrep -x lldpd >/dev/null 2>&1 && echo active || true" - ).strip() - if "active" not in running: + # The whole state: systemctl's "inactive" holds the word "active" (#66). + if self._send(_LLDPD_STATE_COMMAND).strip() != "running": return {} - return lldp_neighbors(parse_lldpctl_keyvalue(self._send(_LLDPCTL_COMMAND))) + return lldp_neighbors(parse_lldpctl_keyvalue(self._send(LLDPCTL_KEYVALUE_COMMAND))) def get_interfaces(self) -> dict[str, Any]: """Return interface operational data.""" diff --git a/tests/test_lldp.py b/tests/test_lldp.py index f7c711f..a8b4e0a 100644 --- a/tests/test_lldp.py +++ b/tests/test_lldp.py @@ -6,14 +6,22 @@ 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. + +#66: the running check took systemctl's ``inactive`` for running, and a failed +lldpctl read as no neighbours. """ from __future__ import annotations +import subprocess + import pytest +from napalm_device_types import LLDPCTL_KEYVALUE_COMMAND from napalm_linux.linux import LinuxDriver +_RC = "__LLDPCTL_RC" + # 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 @@ -49,17 +57,67 @@ lldp.eth0.100.port.descr=GigabitEthernet1/0/7 """ -def _answer(driver: LinuxDriver, lldpctl: str, *, installed: bool = True): +#: Shell stubs for the host's lldpd state: ``systemctl`` and ``service`` as a +#: systemd host answers them, ``pgrep`` as procps does. +_RUNNING = ( + "systemctl() { case $* in *--quiet*) ;; *) echo active ;; esac; return 0; }; " + "service() { echo ' Active: active (running)'; return 0; }; " + "pgrep() { return 0; }; " +) +_STOPPED = ( + "systemctl() { case $* in *--quiet*) ;; *) echo inactive ;; esac; return 3; }; " + "service() { echo ' Active: inactive (dead)'; return 3; }; " + "pgrep() { return 1; }; " +) +_FAILED = ( + "systemctl() { case $* in *--quiet*) ;; *) echo failed ;; esac; return 3; }; " + "service() { echo ' Active: failed (Result: exit-code)'; return 3; }; " + "pgrep() { return 1; }; " +) +#: No systemd, lldpd started by its init script: only pgrep finds it. +_RUNNING_WITHOUT_SYSTEMD = ( + "systemctl() { return 127; }; service() { return 1; }; pgrep() { return 0; }; " +) + + +def _shell(stubs: str, command: str) -> str: + """What *command* prints in ``sh`` with the state commands stubbed out.""" + done = subprocess.run( # a local sh, no network + ["sh", "-c", stubs + command], capture_output=True, text=True, check=False, timeout=10 + ) + return done.stdout.strip() + + +def _answer( + driver: LinuxDriver, + lldpctl: str, + *, + installed: bool = True, + state: str = _RUNNING, + lldpctl_error: str = "", + lldpctl_status: int = 0, +): + """Answer ``_send`` the way the host's shell would. + + lldpctl prints *lldpctl* on stdout, *lldpctl_error* on stderr and exits with + *lldpctl_status*; the redirections and the status echo of the command sent + decide what of that comes back. + """ 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 "lldpd" in command and "lldpctl" not in command: + return _shell(state, command) if command.startswith("lldpctl"): - return lldpctl + out = lldpctl + if "2>&1" in command: + out += lldpctl_error + if f"echo {_RC}" in command: + out += f"\n{_RC}={lldpctl_status}" + return out.strip() raise AssertionError(f"unexpected command {command!r}") driver._send = send # type: ignore[method-assign] @@ -125,3 +183,50 @@ class TestWithoutLldpd: _answer(driver, "") assert driver.get_lldp_neighbors() == {} + + +class TestLldpdState: + """Only a running lldpd is asked; a stopped one is no neighbours (#66).""" + + @pytest.mark.parametrize("state", [_STOPPED, _FAILED], ids=["inactive", "failed"]) + def test_a_stopped_lldpd_is_not_asked(self, driver, state): + sent = _answer(driver, LLDPCTL_KEYVALUE, state=state) + + assert driver.get_lldp_neighbors() == {} + assert not any(c.startswith("lldpctl") for c in sent) + + @pytest.mark.parametrize( + "state", [_RUNNING, _RUNNING_WITHOUT_SYSTEMD], ids=["systemd", "without-systemd"] + ) + def test_a_running_lldpd_is_asked(self, driver, state): + _answer(driver, LLDPCTL_KEYVALUE, state=state) + + assert sorted(driver.get_lldp_neighbors()) == ["eth0", "eth0.100"] + + +class TestFailedRead: + """With lldpd running, a failed lldpctl raises instead of reading as none (#66).""" + + def test_lldpctl_is_asked_with_its_exit_status(self, driver): + sent = _answer(driver, LLDPCTL_KEYVALUE) + + driver.get_lldp_neighbors() + + assert sent[-1] == LLDPCTL_KEYVALUE_COMMAND + + def test_an_unreachable_lldpd_raises(self, driver): + _answer( + driver, + "", + lldpctl_error="[WARN/control] unable to connect to socket /run/lldpd.socket: " + "Permission denied", + lldpctl_status=1, + ) + + with pytest.raises(ValueError, match="Permission denied"): + driver.get_lldp_neighbors() + + def test_lldpctl_without_neighbours_lists_none(self, driver): + _answer(driver, "") + + assert driver.get_lldp_neighbors() == {}