diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c2605c..1ca5975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). ### Fixed +- dnf and yum hosts read pending updates and security advisories from the cached + metadata only (`--cacheonly`), as apt reads its local index (#59). With an expired + cache `check-update` downloaded the repositories' metadata first, and on an Oracle + Linux host on OCI that outlasted netOrk's whole poll, every time. Refreshing stays + `refresh_available_updates()` (`dnf makecache`). Without a cache the read fails at + once and says to refresh the index. Requires the napalm-device-types release with + NAPALM/napalm-device-types#76 for dnf's security query. - `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types' `ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": `ip neigh` has no age, and a STALE entry may be hours old (NetOrk/netork#1327). diff --git a/README.md b/README.md index 955a195..36bcd83 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ The SSH user needs read access to: | `systemctl list-unit-files`, `systemctl show` | unprivileged | | `systemctl start/stop/restart/enable/disable` | `root`, or `sudo` (with `sudo_password`, or passwordless) | | `apt list --upgradable` | may require `apt-get update` (root) | -| `dnf check-update` / `yum check-update` | unprivileged, but slower without cache | +| `dnf --cacheonly check-update` / `yum --cacheonly check-update` | `root` or `sudo`; reads the cache that `dnf makecache` (root) fills | For full functionality it is recommended to run as `root` or grant passwordless `sudo` for the above commands. diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 9b6269e..5ebb09c 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -117,9 +117,9 @@ _REFRESH = { } #: yum's counterpart of DNF_SECURITY_COMMAND, in the same shape: parse_dnf_security #: (napalm-device-types 4.7) needs the ``__DNF_RC=`` line, and behind ``| cat`` it is -#: the only place yum's own exit status survives. +#: the only place yum's own exit status survives. Cached metadata only, as there. _YUM_SECURITY_COMMAND = ( - "{ LC_ALL=C yum updateinfo list security -q 2>/dev/null; echo __DNF_RC=$?; } | cat" + "{ LC_ALL=C yum --cacheonly updateinfo list security -q 2>/dev/null; echo __DNF_RC=$?; } | cat" ) #: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report. _DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE) @@ -1466,17 +1466,17 @@ class LinuxDriver( return parse_apt_upgradable(self._send(APT_UPGRADABLE_COMMAND, read_timeout=60)) def _get_updates_rpm(self) -> list[UpdateDict]: - """dnf/yum check-update: exit 100 means updates, 0 none, anything else failed.""" + """dnf/yum check-update on the cached metadata only, as apt reads its local index + (#59): exit 100 means updates, 0 none, anything else failed.""" cmd = ( - "dnf check-update --quiet 2>/dev/null" + "dnf --cacheonly check-update --quiet 2>/dev/null" if self._pkg_manager == "dnf" - else "yum check-update -q 2>/dev/null" + else "yum --cacheonly check-update -q 2>/dev/null" ) output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 120)) if status not in (0, 100): - raise RuntimeError( - f"{self._pkg_manager} check-update failed (exit {status}): {output[-200:]}" - ) + reason = output[-200:] or "no cached metadata? refresh the package index" + raise RuntimeError(f"{self._pkg_manager} check-update failed (exit {status}): {reason}") security = self._rpm_security_names() updates: list[UpdateDict] = [] for line in output.splitlines(): diff --git a/tests/test_linux.py b/tests/test_linux.py index 57dcd33..fc176d8 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -1684,7 +1684,7 @@ class TestAvailableUpdates: # parse_dnf_security (napalm-device-types 4.7) needs the __DNF_RC marker, and # behind "| cat" the outer status no longer says whether yum failed. assert "echo __DNF_RC=$?" in _YUM_SECURITY_COMMAND - assert "yum updateinfo list security" in _YUM_SECURITY_COMMAND + assert "yum --cacheonly updateinfo list security" in _YUM_SECURITY_COMMAND def test_yum_marks_what_a_security_advisory_covers(self, driver): driver._pkg_manager = "yum" @@ -1749,6 +1749,32 @@ class TestAvailableUpdates: with pytest.raises(RuntimeError): driver.get_available_updates() + @pytest.mark.parametrize("manager", ["dnf", "yum"]) + def test_rpm_reads_only_the_cached_metadata(self, driver, manager): + """An expired cache made check-update download the repositories' metadata + inside the poll, which outlasted its whole budget (#59).""" + driver._pkg_manager = manager + driver._device.send_command.side_effect = [ + "0", + "__NETORK_RC=0", + "__DNF_RC=0\n__NETORK_RC=0", + ] + + driver.get_available_updates() + + sent = [c[0][0] for c in driver._device.send_command.call_args_list] + assert f"{manager} --cacheonly check-update" in sent[1] + assert f"{manager} --cacheonly updateinfo" in sent[2] + + def test_rpm_without_a_cache_says_to_refresh_it(self, driver): + # dnf --cacheonly fails at once without cached metadata; its stderr is + # discarded, so the reason has to come from here. + driver._pkg_manager = "dnf" + driver._device.send_command.side_effect = ["0", "__NETORK_RC=1"] + + with pytest.raises(RuntimeError, match="refresh the package index"): + driver.get_available_updates() + class TestRefreshAvailableUpdates: def _sent(self, driver) -> list: