feat(updates): the update history of an apt host from apt's own log (NetOrk/netork#2728)

get_update_history through napalm-device-types' AptHistoryMixin, over run_command
as the login user. Another package manager raises NotImplementedError. Requires
napalm-device-types 4.47.0.
This commit is contained in:
Christian Manivong committed 2026-11-26 23:05:44 +01:00
1 parent 670e62e0f0
commit 66b6068ebf
5 files changed
+126 -6

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
"""The host's past package transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728).
On an apt host the reading and its parse are napalm-device-types' ``AptHistoryMixin``:
apt's history log over ``run_command``, as the login user, root only for a file
the login user may not read. Another package manager has no history this driver
reads yet: ``NotImplementedError``, so a caller knows not to ask, not an empty list.
"""
from __future__ import annotations
import pytest
from napalm_device_types import APT_HISTORY_COMMAND, CommandResult
from napalm_linux import LinuxDriver
REPORT = """\
### apt-history zone /usr/share/zoneinfo/Etc/UTC
### apt-history offset +0000
### apt-history file /var/log/apt/history.log
Start-Date: 2026-11-26 09:00:00
Commandline: apt-get -y upgrade
Requested-By: admin (1000)
Upgrade: openssh-server:amd64 (1:10.0p1-7, 1:10.0p1-7+deb13u1)
End-Date: 2026-11-26 09:01:00
### apt-history status 0
### apt-history end
"""
def _driver(pkg_manager: str | None) -> tuple[LinuxDriver, list[tuple[str, bool]]]:
driver = LinuxDriver("host", "user", "pw", optional_args={"sudo_password": "pw"})
driver._pkg_manager = pkg_manager
calls: list[tuple[str, bool]] = []
def run_command(command, *, privileged=False, timeout=60, stdin=None): # type: ignore[no-untyped-def]
calls.append((command, privileged))
return CommandResult(REPORT, "", 0)
driver.run_command = run_command # type: ignore[method-assign]
return driver, calls
def test_an_apt_host_reads_apts_log_as_the_login_user():
driver, calls = _driver("apt")
assert driver.get_update_history() == [
{
"id": "1795683600-0",
"start": 1795683600,
"end": 1795683660,
"packages": [
{
"name": "openssh-server",
"action": "upgrade",
"old_version": "1:10.0p1-7",
"new_version": "1:10.0p1-7+deb13u1",
}
],
"requested_by": "admin",
"command": "apt-get -y upgrade",
"error": None,
}
]
assert calls == [(APT_HISTORY_COMMAND, False)]
def test_since_is_passed_on():
driver, _ = _driver("apt")
assert driver.get_update_history(since=1795683601) == []
@pytest.mark.parametrize("pkg_manager", ["dnf", "yum", "apk", "pacman", None])
def test_another_package_manager_has_no_history_here(pkg_manager):
driver, calls = _driver(pkg_manager)
with pytest.raises(NotImplementedError):
driver.get_update_history()
assert calls == []