From a184a9cad1c826b00e8c82b89fbdbe83a18f2958 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:44:04 +0100 Subject: [PATCH] fix(sockets): raise when the listening-socket fallback fails When the root reading failed, get_listening_sockets ran the fallback without root and parsed its output without looking at the exit status. A failing sockstat or netstat returned an empty list, which a caller reads as every port closed. The fallback now goes through the reader check and raises with the command's error. Closes #11 --- CHANGELOG.md | 3 +++ napalm_bsd/base.py | 5 ++++- tests/test_drivers.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea29083..cbc575d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 3.12 venv no longer has. ### Fixed +- `get_listening_sockets()` raises when the reading without root fails as well + (`sockstat` on FreeBSD, `netstat` on OpenBSD). It returned an empty list, which + read as a host where every port had closed. - `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types' `ARP_AGE_UNKNOWN`). It reported the time until the entry *expires* (FreeBSD's `expires in N seconds`, OpenBSD's `Expire` column), which is no age: an entry diff --git a/napalm_bsd/base.py b/napalm_bsd/base.py index 89f9967..761dfe0 100644 --- a/napalm_bsd/base.py +++ b/napalm_bsd/base.py @@ -399,6 +399,9 @@ class BsdDriver(SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver): first, and without root when that brings nothing back, which the reading then says (``attributed: False``). BSD has no systemd units or container IDs to name. + + :raises RuntimeError: when the reading without root fails too: a list + that was never read must not read as every port closed. """ privileged = self.run_command(self.LISTENING_COMMAND, privileged=True) if privileged.exit_code == 0 and privileged.stdout.strip(): @@ -407,7 +410,7 @@ class BsdDriver(SnmpAgentMixin, EnsurePackageMixin, HostStatusMixin, OSDriver): "sockets": self._parse_listening(privileged.stdout, attributed=True), } command = self.LISTENING_FALLBACK_COMMAND or self.LISTENING_COMMAND - plain = self.run_command(command, privileged=False) + plain = self._read(command) return { "attributed": False, "sockets": self._parse_listening(plain.stdout, attributed=False), diff --git a/tests/test_drivers.py b/tests/test_drivers.py index cb9c003..edc0082 100644 --- a/tests/test_drivers.py +++ b/tests/test_drivers.py @@ -232,6 +232,20 @@ class TestListeningSockets: } assert calls == [("fstat -n", True), (OpenBSDDriver.LISTENING_FALLBACK_COMMAND, False)] + @pytest.mark.parametrize("cls", [FreeBSDDriver, OpenBSDDriver]) + def test_a_failed_fallback_raises_instead_of_reading_as_no_sockets(self, cls): + """Both readings failing is no empty host: every port would read as closed.""" + driver = _driver(cls, {}) + + def run(command, privileged=False, timeout=60, stdin=None): + if privileged: + return type("R", (), {"stdout": "", "stderr": "", "exit_code": 1})() + return type("R", (), {"stdout": "", "stderr": "not found", "exit_code": 127})() + + driver.run_command = run # type: ignore[method-assign] + with pytest.raises(RuntimeError, match="not found"): + driver.get_listening_sockets() + class _Result: def __init__(self, stdout: str = "", exit_code: int = 0, stderr: str = "") -> None: