Files
napalm-proxmox/tests/test_update_history.py
T
Christian Manivong 707b24748b feat(updates): the update history before PVE 9.3 from apt's log over SSH (NetOrk/netork#2728)
Below 9.3 get_update_history reads apt's history log over the node's SSH session
with napalm-device-types' AptHistoryMixin, which names a transaction as
GET /nodes/{node}/apt/history does, so the ids stay when the node moves to 9.3.
A node without an SSH login still raises NotImplementedError. Requires
napalm-device-types 4.47.0.
2026-11-26 23:07:22 +01:00

197 lines
7.2 KiB
Python

"""The node's past APT transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728).
PVE 9.3 reads apt's history log for ``GET /nodes/{node}/apt/history``, newest
first. The payload below follows its schema: a transaction is identified by
``start-time`` with ``start-index`` (default 0) among those that started in the
same second; ``end-time`` is missing while it runs or after it was interrupted.
An older node has no such endpoint: the driver reads the same log over its SSH
session with napalm-device-types' ``AptHistoryMixin``, which names a transaction
the same way, and a node without an SSH login has no history.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from napalm.base.exceptions import ConnectionException
from napalm_device_types import APT_HISTORY_COMMAND, CommandResult
from napalm_proxmox.driver import ProxmoxDriver
from tests.conftest import _build_mock_api
HISTORY = [
{
"start-time": 1764158400,
"start-index": 1,
"end-time": 1764158460,
"command-line": "apt-get -y dist-upgrade",
"requested-by": "admin",
"requested-by-uid": 1000,
"packages": [
{
"package": "openssh-server",
"action": "upgrade",
"arch": "amd64",
"old-version": "1:10.0p1-7",
"new-version": "1:10.0p1-7+deb13u1",
},
{
"package": "proxmox-kernel-6.17.2-1-pve-signed",
"action": "install",
"new-version": "6.17.2-1",
"automatic": 1,
},
{"package": "ifupdown", "action": "purge", "old-version": "0.8.44"},
],
},
{
# unattended-upgrades, interrupted: no end, no user, the error dpkg left.
"start-time": 1764072000,
"packages": [
{
"package": "libssl3t64",
"action": "upgrade",
"old-version": "3.5.1-1",
"new-version": "3.5.1-1+deb13u1",
}
],
"error": "Sub-process /usr/bin/dpkg returned an error code (1)",
"running": 0,
},
]
@pytest.fixture
def history(driver) -> MagicMock:
driver._api.version.get.return_value = {"version": "9.3.0", "release": "9.3"}
getter = driver._node_api().apt.history.get
getter.return_value = HISTORY
return getter
class TestUpdateHistory:
def test_transactions_newest_first(self, driver, history):
newest, older = driver.get_update_history()
assert newest == {
"id": "1764158400-1",
"start": 1764158400,
"end": 1764158460,
"packages": [
{
"name": "openssh-server",
"action": "upgrade",
"old_version": "1:10.0p1-7",
"new_version": "1:10.0p1-7+deb13u1",
},
{
"name": "proxmox-kernel-6.17.2-1-pve-signed",
"action": "install",
"old_version": None,
"new_version": "6.17.2-1",
},
{
"name": "ifupdown",
"action": "purge",
"old_version": "0.8.44",
"new_version": None,
},
],
"requested_by": "admin",
"command": "apt-get -y dist-upgrade",
"error": None,
}
assert older["id"] == "1764072000-0"
history.assert_called_once_with()
def test_what_the_log_does_not_keep_is_none(self, driver, history):
older = driver.get_update_history()[1]
assert older["end"] is None
assert older["requested_by"] is None
assert older["command"] is None
assert older["error"] == "Sub-process /usr/bin/dpkg returned an error code (1)"
def test_since_is_passed_on(self, driver, history):
driver.get_update_history(since=1764100000)
history.assert_called_once_with(since=1764100000)
def test_no_transaction_is_an_empty_list(self, driver, history):
history.return_value = []
assert driver.get_update_history() == []
def test_a_failed_read_raises(self, driver, history):
history.side_effect = Exception("596 Connection timed out")
with pytest.raises(RuntimeError, match="timed out"):
driver.get_update_history()
#: The node's log as AptHistoryMixin's command prints it: the transaction the API
#: names 1764158400-1 is the second to start in that second (12:00:00 UTC) of the file.
LOG = """\
### apt-history zone /usr/share/zoneinfo/Etc/UTC
### apt-history offset +0000
### apt-history file /var/log/apt/history.log
Start-Date: 2025-11-26 12:00:00
Commandline: apt-get update-kernel
Install: proxmox-kernel-6.17.2-1-pve-signed:amd64 (6.17.2-1, automatic)
End-Date: 2025-11-26 12:00:00
Start-Date: 2025-11-26 12:00:00
Commandline: apt-get -y dist-upgrade
Requested-By: admin (1000)
Upgrade: openssh-server:amd64 (1:10.0p1-7, 1:10.0p1-7+deb13u1)
Purge: ifupdown:amd64 (0.8.44)
End-Date: 2025-11-26 12:01:00
### apt-history status 0
### apt-history end
"""
@pytest.fixture
def older(driver, history) -> MagicMock:
"""A 9.2 node whose SSH session prints :data:`LOG`."""
driver._api.version.get.return_value = {"version": "9.2.4", "release": "9.2"}
run = MagicMock(return_value=CommandResult(LOG, "", 0))
driver.run_command = run
return run
class TestBeforeNineThree:
def test_the_log_is_read_over_ssh(self, driver, history, older):
newest, first = driver.get_update_history()
assert newest["id"] == "1764158400-1" # the API's id for the same transaction
assert newest["end"] == 1764158460
assert newest["requested_by"] == "admin"
assert [p["action"] for p in newest["packages"]] == ["upgrade", "purge"]
assert first["id"] == "1764158400-0"
older.assert_called_once()
assert older.call_args.args == (APT_HISTORY_COMMAND,)
history.assert_not_called()
def test_since_filters_the_log(self, driver, history, older):
assert driver.get_update_history(since=1764158401) == []
def test_a_failed_ssh_read_raises(self, driver, history, older):
older.side_effect = ConnectionException("Cannot run a command on pve1 over SSH: reset")
with pytest.raises(RuntimeError, match="reset"):
driver.get_update_history()
def test_a_log_cut_short_raises(self, driver, history, older):
older.return_value = CommandResult(LOG.replace("### apt-history end\n", ""), "", 0)
with pytest.raises(RuntimeError, match="cut short"):
driver.get_update_history()
def test_a_node_without_ssh_login_has_no_history(self):
drv = ProxmoxDriver(
"pve1", "netork", "", optional_args={"token_name": "poll", "token_value": "s3cret"}
)
api = _build_mock_api(version={"version": "9.2.4", "release": "9.2"})
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api):
drv.open()
drv.run_command = MagicMock()
with pytest.raises(NotImplementedError, match="SSH"):
drv.get_update_history()
drv.run_command.assert_not_called()