From 037a725b746c04730a410a81c808010605bc0e72 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:48:48 +0100 Subject: [PATCH] fix(updates): report a failed apply_updates as a failure apt-get ran with "|| true" and the result was always success, so a failed upgrade read as done. The exit status is read now and a failure names it. Closes #20 --- CHANGELOG.md | 2 ++ napalm_proxmox/system_mixin.py | 14 ++++++++++---- tests/test_updates.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd9e856..b40655f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `set_hostname()` renames only the node's own names in `/etc/hosts`: other hosts whose names start with the old one (`pve-backup`) and comments stay as they are, and the old FQDN becomes the new one instead of a mix of both (#21). +- `apply_updates()` reports a failed upgrade as `success: False`, with apt-get's + exit status in `error`; it reported success whatever apt-get did (#20). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 7f63931..53ea748 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -474,11 +474,17 @@ class ProxmoxSystemMixin: if not re.match(r"^[a-zA-Z0-9_\-\+\.]+$", pkg): raise ValueError(f"Invalid package name: {pkg!r}") pkg_args = " ".join(packages) - output = self._exec_ssh_command( - "DEBIAN_FRONTEND=noninteractive apt-get install --only-upgrade -y " - f"{pkg_args} 2>&1 || true" + output, status = self._rc( + f"DEBIAN_FRONTEND=noninteractive apt-get install --only-upgrade -y {pkg_args}" ) - return {"success": True, "output": output} + if status == 0: + return {"success": True, "output": output} + error = ( + f"apt-get exited with status {status}" + if status >= 0 + else "apt-get's exit status could not be read" + ) + return {"success": False, "output": output, "error": error} # ------------------------------------------------------------------ # # SNMP / Health diff --git a/tests/test_updates.py b/tests/test_updates.py index 1ca67d5..41cac64 100644 --- a/tests/test_updates.py +++ b/tests/test_updates.py @@ -104,6 +104,35 @@ class TestRefresh: assert result == {"success": False, "output": "403 Permission check failed"} +class TestApplyUpdates: + def test_a_clean_upgrade_succeeds(self, driver): + with patch.object(driver, "_exec_ssh_command", return_value="Done.\n__PVE_RC=0") as exec_: + result = driver.apply_updates(["curl"]) + + assert "apt-get install --only-upgrade -y curl" in exec_.call_args[0][0] + assert result == {"success": True, "output": "Done."} + + def test_a_failed_upgrade_is_a_failure(self, driver): + out = "E: Could not get lock /var/lib/dpkg/lock-frontend\n__PVE_RC=100" + with patch.object(driver, "_exec_ssh_command", return_value=out): + result = driver.apply_updates(["curl"]) + + assert result["success"] is False + assert result["output"] == "E: Could not get lock /var/lib/dpkg/lock-frontend" + assert result["error"] == "apt-get exited with status 100" + + def test_an_unread_exit_status_is_no_success(self, driver): + with patch.object(driver, "_exec_ssh_command", return_value=""): + result = driver.apply_updates(["curl"]) + + assert result["success"] is False + assert result["error"] == "apt-get's exit status could not be read" + + def test_a_name_that_is_no_package_name_is_refused(self, driver): + with pytest.raises(ValueError): + driver.apply_updates(["curl;reboot"]) + + class TestHostStatus: def test_the_node_is_read_over_the_exec_path(self, driver): report = (