From c211ba0d9e862dadb3d82e507fe7b594f48eb1c2 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:44:26 +0100 Subject: [PATCH] fix(updates): only the exit status decides apply_updates apply_updates ran apk/opkg upgrade and returned success whatever happened, so a failed upgrade was reported as done. The command now prints its exit status after the marker get_available_updates already uses; a non-zero or missing status is a failure, named in "error". Closes #11 --- CHANGELOG.md | 2 ++ napalm_openwrt/system_mixin.py | 20 +++++++++---- tests/unit/test_updates.py | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25e6730..6f82b57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ recorded here; their history is the git log. - `manage_service()` reports a failed action or a missing init script as failed: only the init script's exit status decides, where any action used to come back as a success (#3). +- `apply_updates()` reports a failed upgrade as failed, with the package manager's + exit status in `error`; it used to report every upgrade as done (#11). ## [0.7.0] - 2026-10-23 diff --git a/napalm_openwrt/system_mixin.py b/napalm_openwrt/system_mixin.py index a491345..84053a2 100644 --- a/napalm_openwrt/system_mixin.py +++ b/napalm_openwrt/system_mixin.py @@ -480,7 +480,10 @@ class OpenWrtSystemMixin: return {"success": status == 0, "output": output} def apply_updates(self, packages: list[str]) -> dict[str, Any]: - """Upgrade the given packages using the device's package manager.""" + """Upgrade the given packages using the device's package manager. + + Only the package manager's exit status decides ``success``. + """ import re as _re for pkg in packages: @@ -489,11 +492,18 @@ class OpenWrtSystemMixin: pm = self._pm_type() pkg_args = " ".join(packages) if pm == "apk": - cmd = f"apk upgrade {pkg_args} 2>&1" + cmd = f"apk upgrade {pkg_args}" else: - cmd = f"opkg upgrade {pkg_args} 2>&1" - output = self._send_command(cmd) - return {"success": True, "output": output} + cmd = f"opkg upgrade {pkg_args}" + output, status = _split_status(self._send_command(f"{cmd} 2>&1; echo {_RC_MARKER}$?")) + if status == 0: + return {"success": True, "output": output} + error = ( + f"{cmd.strip()} exited with status {status}" + if status is not None + else "No exit status came back from the device." + ) + return {"success": False, "output": output, "error": error} # ------------------------------------------------------------------ # NTP diff --git a/tests/unit/test_updates.py b/tests/unit/test_updates.py index b86b3fb..98d3558 100644 --- a/tests/unit/test_updates.py +++ b/tests/unit/test_updates.py @@ -111,3 +111,54 @@ class TestRefresh: result = driver.refresh_available_updates() assert result == {"success": False, "output": "ERROR: network unreachable"} + + +class TestApplyUpdates: + """Only the package manager's exit status decides whether an upgrade succeeded (#11).""" + + def test_apk_upgrade_that_worked(self, driver): + sent = _answer( + driver, "apk", "(1/1) Upgrading dnsmasq (2.90-r4 -> 2.90-r5)\nOK\n__OWRT_RC=0\n" + ) + + result = driver.apply_updates(["dnsmasq"]) + + assert result == { + "success": True, + "output": "(1/1) Upgrading dnsmasq (2.90-r4 -> 2.90-r5)\nOK", + } + assert sent[-1].startswith("apk upgrade dnsmasq 2>&1") + + def test_apk_upgrade_that_failed_is_no_success(self, driver): + _answer( + driver, + "apk", + "ERROR: unable to select packages:\n nope (no such package)\n__OWRT_RC=1\n", + ) + + result = driver.apply_updates(["nope"]) + + assert result["success"] is False + assert "unable to select packages" in result["output"] + assert "status 1" in result["error"] + + def test_opkg_upgrade_that_failed_is_no_success(self, driver): + sent = _answer( + driver, + "opkg", + " * opkg_download: Failed to download\nCollected errors:\n__OWRT_RC=255\n", + ) + + result = driver.apply_updates(["dnsmasq"]) + + assert result["success"] is False + assert "status 255" in result["error"] + assert sent[-1].startswith("opkg upgrade dnsmasq 2>&1") + + def test_output_without_a_status_is_no_success(self, driver): + _answer(driver, "opkg", "") + + result = driver.apply_updates(["dnsmasq"]) + + assert result["success"] is False + assert result["error"]