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
This commit is contained in:
Christian Manivong committed 2026-11-01 21:44:26 +01:00
1 parent 16e0fa9d5e
commit c211ba0d9e
3 files changed
+68 -5

No files matched your search

+2
View File
@@ -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
+15 -5
View File
@@ -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
+51
View File
@@ -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"]