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.
This commit is contained in:
1 parent
dc0d6345a8
commit
707b24748b
5 files changed
+123
-17
No files matched your search
@@ -4,14 +4,21 @@ 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, and the driver parses no log over SSH.
|
||||
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
|
||||
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 = [
|
||||
{
|
||||
@@ -117,8 +124,73 @@ class TestUpdateHistory:
|
||||
with pytest.raises(RuntimeError, match="timed out"):
|
||||
driver.get_update_history()
|
||||
|
||||
def test_an_older_node_has_no_history(self, driver, history):
|
||||
driver._api.version.get.return_value = {"version": "9.2.4", "release": "9.2"}
|
||||
with pytest.raises(NotImplementedError):
|
||||
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()
|
||||
Reference in new issue
Block a user