From 211d0c02eb3ae0464ca6b8491e149f5d72a67224 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 14 Nov 2026 23:45:38 +0100 Subject: [PATCH] feat(updates): PackageIndexMissing, for a host without a package index OpenWrt keeps its package index in RAM and loses it on every reboot. get_available_updates() raises then, rightly, but only as a bare RuntimeError, so a caller cannot tell it from a read that failed and reports a communication error for a state a refresh cures. PackageIndexMissing (a RuntimeError, so existing callers still catch it) names that case; refresh_available_updates() is the way out. Refs NetOrk/netork#2527 --- CHANGELOG.md | 6 ++++++ napalm_device_types/__init__.py | 3 ++- napalm_device_types/updates.py | 11 +++++++++++ tests/test_updates.py | 20 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_updates.py diff --git a/CHANGELOG.md b/CHANGELOG.md index dd825d3..a46f88a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `PackageIndexMissing` (a `RuntimeError`): what `get_available_updates()` raises when + the host holds no package index to read from -- OpenWrt's, which lives in RAM, after + every reboot. `refresh_available_updates()` loads it again, so a caller can tell this + from a failed read and refresh instead of reporting an error (NetOrk/netork#2527). + ## [4.38.0] - 2026-11-13 ### Added diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index f1abe2d..69af84b 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -335,7 +335,7 @@ from napalm_device_types.time_sync import ( check_ntp_servers, ) from napalm_device_types.time_sync_posix import PosixTimeSyncMixin, parse_time_sync -from napalm_device_types.updates import UpdateMixin +from napalm_device_types.updates import PackageIndexMissing, UpdateMixin from napalm_device_types.virtual_ips import ( VIRTUAL_IPS_COMMAND, VirtualIpsMixin, @@ -505,6 +505,7 @@ __all__ = [ "SystemdServicesMixin", "SystemdUnavailable", "parse_systemd_services", + "PackageIndexMissing", "UpdateMixin", "VIRTUAL_IPS_COMMAND", "VirtualIpsMixin", diff --git a/napalm_device_types/updates.py b/napalm_device_types/updates.py index 1d5d44e..91c25c4 100644 --- a/napalm_device_types/updates.py +++ b/napalm_device_types/updates.py @@ -17,6 +17,15 @@ from typing import TYPE_CHECKING, Any from napalm_device_types.models import ApplyUpdatesResultDict, UpdateDict, UpdateProgressDict +class PackageIndexMissing(RuntimeError): + """The host holds no package index to read pending updates from. + + Not a fault: OpenWrt keeps its index in RAM and loses it on every reboot. + :meth:`UpdateMixin.refresh_available_updates` loads it again, so a caller + that sees this can refresh instead of reporting a failed read. + """ + + class UpdateMixin: if TYPE_CHECKING: @@ -37,6 +46,8 @@ class UpdateMixin: read -- no package index yet, an API that did not answer -- raises instead: netOrk keeps "pending since" per package, and an empty list for "don't know" would reset every one of those clocks. + Without a package index it raises :exc:`PackageIndexMissing`, which + :meth:`refresh_available_updates` cures. Example:: diff --git a/tests/test_updates.py b/tests/test_updates.py new file mode 100644 index 0000000..c891041 --- /dev/null +++ b/tests/test_updates.py @@ -0,0 +1,20 @@ +"""The contract of :class:`~napalm_device_types.updates.UpdateMixin`.""" + +from __future__ import annotations + +import napalm_device_types +from napalm_device_types.updates import PackageIndexMissing + + +def test_a_missing_index_is_its_own_failure(): + """A reader can tell "no index to read from" from any other failure, and a + caller that only knows failures still catches it as one.""" + exc = PackageIndexMissing("The package index is not loaded (run apk update first)") + + assert isinstance(exc, RuntimeError) + assert str(exc) == "The package index is not loaded (run apk update first)" + + +def test_it_is_exported_from_the_package(): + assert napalm_device_types.PackageIndexMissing is PackageIndexMissing + assert "PackageIndexMissing" in napalm_device_types.__all__