fix(lldp): recognise a stopped lldpd and raise on a failed read
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / test (3.12) (pull_request) Failing after 57s
driver / types (pull_request) Failing after 1m7s
driver / test (3.13) (pull_request) Failing after 50s
driver / lowest (pull_request) Failing after 23s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Failing after 1m9s
CI / driver (pull_request) Failing after 2m35s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / test (3.12) (pull_request) Failing after 57s
driver / types (pull_request) Failing after 1m7s
driver / test (3.13) (pull_request) Failing after 50s
driver / lowest (pull_request) Failing after 23s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Failing after 1m9s
CI / driver (pull_request) Failing after 2m35s
driver / publish (pull_request) Skipped
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
This commit is contained in:
1 parent
a3d95d72b9
commit
d3189806d3
3 files changed
+125
-15
No files matched your search
@@ -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
|
||||
|
||||
+11
-11
@@ -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 >/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."""
|
||||
|
||||
+109
-4
@@ -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() == {}
|
||||
Reference in new issue
Block a user