fix(sockets): raise when the listening-socket fallback fails
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m18s
driver / test (3.13) (pull_request) Successful in 1m7s
driver / lowest (pull_request) Successful in 33s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m17s
CI / driver (pull_request) Successful in 3m6s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m18s
driver / test (3.13) (pull_request) Successful in 1m7s
driver / lowest (pull_request) Successful in 33s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m17s
CI / driver (pull_request) Successful in 3m6s
driver / publish (pull_request) Skipped
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
This commit is contained in:
1 parent
405239d5df
commit
a184a9cad1
3 files changed
+21
-1
No files matched your search
@@ -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
|
||||
|
||||
+4
-1
@@ -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),
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user