feat(log-sources): which logs the host writes, through LogSourcesMixin
driver / standard (pull_request) Failing after 18s
driver / lint (pull_request) Successful in 16s
driver / test (3.12) (pull_request) Failing after 46s
driver / types (pull_request) Failing after 1m3s
driver / test (3.13) (pull_request) Failing after 40s
driver / lowest (pull_request) Failing after 23s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Failing after 1m1s
CI / driver (pull_request) Failing after 2m22s
driver / publish (pull_request) Skipped

The driver mixes in napalm-device-types' LogSourcesMixin (4.30.0) and
carries its command: as root through sudo where it can, else what the
login user may see. OpenMediaVault and QNAP inherit it.

Refs NetOrk/netork#2049
This commit is contained in:
Christian Manivong committed 2026-11-05 00:24:32 +01:00
1 parent 249046512d
commit 2b153cd61c
4 files changed
+68 -1

No files matched your search

+52
View File
@@ -1955,6 +1955,58 @@ class TestGetListeningSockets:
assert plain.startswith("sh -c '")
# ---------------------------------------------------------------------------
# Log sources: journald units and the log files processes hold open (#2049)
# ---------------------------------------------------------------------------
_LOGS = (
"LOGS_BEGIN\n[journald]\nnginx.service\n"
"[open]\n/proc/900/fd/5 /var/log/nginx/access.log\n"
"[stat]\n/proc/900/fd/5 2048 1762250200\n"
"[cgroups]\n900 0::/system.slice/nginx.service\n"
"[comm]\n900 nginx\n"
"LOGS_END\n"
)
class TestGetLogSources:
def _sent(self, driver) -> list[str]:
return [c[0][0] for c in driver._device.send_command.call_args_list]
def test_it_reads_with_the_shared_command_as_root(self, driver):
from napalm_device_types import LogSourcesMixin
assert isinstance(driver, LogSourcesMixin)
driver._root = True
driver._device.send_command.return_value = _LOGS
reading = driver.get_log_sources()
assert (reading["attributed"], reading["journald"]) == (True, True)
access = [s for s in reading["sources"] if s["kind"] == "file"]
assert [(s["path"], s["unit"]) for s in access] == [("/var/log/nginx/access.log", "nginx")]
assert self._sent(driver)[0].startswith("sh -c '")
def test_without_a_sudo_password_the_whole_script_runs_under_sudo_n(self, driver):
driver._root = False
driver._device.send_command.return_value = _LOGS
driver.get_log_sources()
assert self._sent(driver)[0].startswith("sudo -n sh -c '")
def test_when_sudo_refuses_it_reads_what_the_user_may_see(self, driver):
driver._root = False
driver._device.send_command.side_effect = ["sudo: a password is required", _LOGS]
reading = driver.get_log_sources()
assert reading["attributed"] is False
refused, plain = self._sent(driver)
assert refused.startswith("sudo -n sh -c '")
assert plain.startswith("sh -c '")
# ---------------------------------------------------------------------------
# get_system_identity (napalm-device-types SystemIdentityMixin)
# ---------------------------------------------------------------------------