diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2cc9b..374c5e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 inherited `OSDriver`'s `linux`, so an application that shows or branches on the role treated them as Linux hosts. An application that keys on `linux` for every general-purpose host has to accept `bsd` as well. +- `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 4300e4f..769cc2d 100644 --- a/napalm_bsd/base.py +++ b/napalm_bsd/base.py @@ -402,6 +402,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(): @@ -410,7 +413,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 1358553..1b2fd88 100644 --- a/tests/test_drivers.py +++ b/tests/test_drivers.py @@ -238,6 +238,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: