Files
napalm-openwrt/tests/unit/test_updates.py
T
Christian Manivong ec88cfd734
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 16s
driver / types (pull_request) Successful in 46s
driver / test (3.13) (pull_request) Successful in 44s
driver / test (3.12) (pull_request) Successful in 51s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Successful in 1m55s
driver / publish (pull_request) Skipped
feat(updates): raise PackageIndexMissing when the index is not in RAM
After a reboot apk and opkg have no package index. get_available_updates()
raised a bare RuntimeError for that, which netOrk could not tell from a
read that failed. It now raises napalm-device-types' PackageIndexMissing,
so netOrk loads the index instead of reporting a communication error.

Needs napalm-device-types 4.39.0.

Refs NetOrk/netork#2527
2026-11-14 23:46:09 +01:00

167 lines
5.7 KiB
Python

"""Pending updates on OpenWrt: never "nothing pending" when the index is gone.
OpenWrt keeps its package index in RAM. After a reboot ``apk version`` only
warns that it cannot open the cached index and exits 0, and ``opkg
list-upgradable`` prints nothing: both looked like "no updates". netOrk keeps
"pending since" per package, so a reader that cannot read raises instead, and
``refresh_available_updates`` loads the index (netOrk MVP 5).
The apk warning is the real output of an OpenWrt 25.12.5 access point after a
reboot.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from napalm_device_types import PackageIndexMissing
from napalm_openwrt.openwrt import OpenWrtDriver
APK_INDEX_MISSING = (
"WARNING: opening from cache https://downloads.openwrt.org/releases/25.12.5/targets/"
"ath79/generic/packages/packages.adb: No such file or directory\n"
"WARNING: opening from cache https://downloads.openwrt.org/releases/25.12.5/packages/"
"mips_24kc/base/packages.adb: No such file or directory\n"
"__OWRT_RC=0\n"
)
APK_PENDING = (
"Installed: Available:\n"
"dnsmasq-2.90-r4 < 2.90-r5\n"
"hostapd-common-2024.09.15-r2 < 2024.09.15-r3\n"
"__OWRT_RC=0\n"
)
OPKG_PENDING = "dnsmasq - 2.90-4 - 2.90-5\n"
@pytest.fixture
def driver():
with patch("napalm_openwrt.openwrt.ConnectHandler"):
drv = OpenWrtDriver(hostname="192.0.2.1", username="root", password="")
drv.device = MagicMock()
yield drv
def _answer(driver, pm: str, reply: str):
"""Answer the package-manager probe with *pm* and everything else with *reply*."""
sent: list = []
def send(cmd, **kwargs):
sent.append(cmd)
if cmd.startswith("command -v apk"):
return "/usr/bin/apk" if pm == "apk" else "__no_apk__"
return reply
driver._send_command = send
return sent
class TestAvailableUpdates:
def test_apk_without_its_index_raises_instead_of_reporting_nothing(self, driver):
_answer(driver, "apk", APK_INDEX_MISSING)
with pytest.raises(PackageIndexMissing, match="apk update"):
driver.get_available_updates()
def test_apk_with_its_index_lists_what_is_pending(self, driver):
_answer(driver, "apk", APK_PENDING)
updates = driver.get_available_updates()
assert [u["name"] for u in updates] == ["dnsmasq", "hostapd-common"]
assert updates[0]["current_version"] == "2.90-r4"
assert updates[0]["new_version"] == "2.90-r5"
def test_apk_that_failed_raises(self, driver):
_answer(driver, "apk", "ERROR: unable to lock database\n__OWRT_RC=99\n")
with pytest.raises(RuntimeError) as raised:
driver.get_available_updates()
assert not isinstance(raised.value, PackageIndexMissing)
def test_opkg_without_its_lists_raises(self, driver):
_answer(driver, "opkg", "__NO_INDEX__\n")
with pytest.raises(PackageIndexMissing, match="opkg update"):
driver.get_available_updates()
def test_opkg_with_its_lists(self, driver):
_answer(driver, "opkg", OPKG_PENDING)
assert driver.get_available_updates() == [
{"name": "dnsmasq", "current_version": "2.90-4", "new_version": "2.90-5"}
]
class TestRefresh:
def test_apk_loads_its_index(self, driver):
sent = _answer(driver, "apk", "OK: 8912 distinct packages available\n__OWRT_RC=0\n")
assert driver.refresh_available_updates()["success"] is True
assert sent[-1].startswith("apk update")
def test_opkg_loads_its_lists(self, driver):
sent = _answer(driver, "opkg", "Updated list of available packages\n__OWRT_RC=0\n")
assert driver.refresh_available_updates()["success"] is True
assert sent[-1].startswith("opkg update")
def test_a_failed_refresh_says_why(self, driver):
_answer(driver, "apk", "ERROR: network unreachable\n__OWRT_RC=1\n")
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"]