diff --git a/CHANGELOG.md b/CHANGELOG.md index 9783de3..e17f201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ recorded here; their history is the git log. ## [Unreleased] +## [4.58.1] - 2026-12-03 + +### Fixed +- `APT_UPGRADABLE_COMMAND` / `parse_apt_upgradable`: a build Ubuntu is still phasing in + is no longer pending where apt holds it back (NetOrk/netork#3110). `apt list + --upgradable` lists it on every host, `apt upgrade` installs it only on the share of + hosts it has reached. The command now also runs a simulated `apt-get upgrade` (no + root, no lock) and marks what it defers for phasing `__APT_PHASED:`; the parser leaves + those packages out. A simulation that fails marks nothing, and the listing stands. + ## [4.58.0] - 2026-12-03 ### Added diff --git a/napalm_device_types/package_updates.py b/napalm_device_types/package_updates.py index 57ef304..86a1df8 100644 --- a/napalm_device_types/package_updates.py +++ b/napalm_device_types/package_updates.py @@ -10,6 +10,12 @@ apt: the suites a candidate comes from are its ``origin``; any suite ending in ``-security`` makes it a security update. A security fix that a later ``-updates`` build superseded shows only ``-updates`` and counts as not security -- netOrk's CVE matching is what catches those. + +apt also lists a build Ubuntu is still phasing in -- handing it to a share of +its hosts first -- on every host, while ``apt upgrade`` holds it back where the +host is not yet in the share. Such a package is not pending there, and an +update would not install it, so the read leaves out what apt defers for +phasing (netOrk #3110). """ from __future__ import annotations @@ -28,9 +34,16 @@ from napalm_device_types.updates import PackageSourcesUnreadable #: ``__APT_E:``, so a failed read says why (netOrk #3087); the rest of what it #: writes to stderr, its warnings, stays out. Plain ``sh``: stdout goes round #: the pipe on descriptor 3. +#: +#: After it, a simulated ``apt-get upgrade`` -- no root, no lock -- names what apt +#: defers for phasing, each line of its list marked ``__APT_PHASED:``. Where the +#: simulation fails it names nothing, and the listing stands as it is. APT_UPGRADABLE_COMMAND = ( "{ { LC_ALL=C apt list --upgradable 2>&1 1>&3 3>&-; echo __APT_RC=$? 1>&3; }" - " | sed -n 's/^E: /__APT_E: /p'; } 3>&1 | cat" + " | sed -n 's/^E: /__APT_E: /p';" + " LC_ALL=C apt-get -s -o Debug::NoLocking=1 upgrade 2>/dev/null" + " | sed -n '/deferred due to phasing:$/,/^[^ ]/{/^ /s/^ */__APT_PHASED: /p;}'; }" + " 3>&1 | cat" ) #: apt's last word when it cannot read its sources, in ``LC_ALL=C``. @@ -63,6 +76,7 @@ _APT_LINE = re.compile(r"^(\S+)/(\S+)\s+(\S+)\s+\S+\s+\[upgradable from:\s+(\S+) _SECURITY_SUITE = "-security" _APT_STATUS = re.compile(r"^__APT_RC=(\d+)\s*$", re.MULTILINE) _APT_ERROR = re.compile(r"^__APT_E: (.*?)\s*$", re.MULTILINE) +_APT_PHASED = re.compile(r"^__APT_PHASED: (.*?)\s*$", re.MULTILINE) _DNF_STATUS = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE) _CVE_ID = re.compile(r"^CVE-\d{4}-\d{4,}$") @@ -103,13 +117,20 @@ def parse_apt_upgradable(output: str) -> list[UpdateDict]: A package listed for several architectures (``libc6`` for amd64 and i386) is one entry; it counts as a security update if any of its lines does. + A package apt defers for phasing (``__APT_PHASED:``) is left out: apt would + not upgrade it on this host. + :raises PackageSourcesUnreadable: when apt could not read its list of sources. :raises ValueError: when apt failed otherwise or its exit status never arrived. """ + listing = _listing(output, _APT_STATUS, "apt list --upgradable") + deferred = { + name.split(":")[0] for names in _APT_PHASED.findall(listing) for name in names.split() + } by_name: dict[str, UpdateDict] = {} - for line in _joined_lines(_listing(output, _APT_STATUS, "apt list --upgradable")): + for line in _joined_lines(listing): match = _APT_LINE.match(line) - if not match: + if not match or match.group(1) in deferred: continue name, listed, new_version, current_version = match.groups() suites = list(dict.fromkeys(listed.split(","))) # apt may list a suite twice diff --git a/pyproject.toml b/pyproject.toml index ec441c7..1da2458 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.58.0" +version = "4.58.1" description = "Abstract device-type base classes for NAPALM drivers" readme = "README.md" requires-python = ">=3.12" diff --git a/tests/test_package_updates.py b/tests/test_package_updates.py index 038ea84..30ac089 100644 --- a/tests/test_package_updates.py +++ b/tests/test_package_updates.py @@ -201,6 +201,101 @@ class TestAptUpgradable: assert "sudo" not in APT_UPGRADABLE_COMMAND +#: ``apt list --upgradable`` on an Ubuntu 24.04 host on 2026-12-03, while nftables +#: 1.0.9-1ubuntu0.2 was out to 10 % of hosts (Phased-Update-Percentage: 10). +PHASING = """\ +libnftables1/noble-updates 1.0.9-1ubuntu0.2 amd64 [upgradable from: 1.0.9-1ubuntu0.1] +nftables/noble-updates 1.0.9-1ubuntu0.2 amd64 [upgradable from: 1.0.9-1ubuntu0.1] +openssl/noble-updates,noble-security 3.0.13-0ubuntu3.6 amd64 [upgradable from: 3.0.13-0ubuntu3.5] +__APT_RC=0 +""" + +#: What ``apt-get -s upgrade`` says on that host: apt holds the phased ones back. +APT_GET_UPGRADE = """\ +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Keep also in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Reading package lists... +Building dependency tree... +Reading state information... +Calculating upgrade... +The following upgrades have been deferred due to phasing: + libnftables1 nftables +The following packages will be upgraded: + openssl +1 upgraded, 0 newly installed, 0 to remove and 2 not upgraded. +Inst openssl [3.0.13-0ubuntu3.5] (3.0.13-0ubuntu3.6 Ubuntu:24.04/noble-updates [amd64]) +Conf openssl (3.0.13-0ubuntu3.6 Ubuntu:24.04/noble-updates [amd64]) +""" + + +def _fake_apt(tmp_path, apt_get: str, apt_get_rc: int = 0) -> dict[str, str]: + """A PATH whose ``apt`` lists :data:`PHASING` and whose ``apt-get`` says *apt_get*.""" + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + listing = PHASING.replace("__APT_RC=0\n", "") + (bin_dir / "apt").write_text(f"#!/bin/sh\ncat <<'EOF'\n{listing}EOF\n") + (bin_dir / "apt-get").write_text(f"#!/bin/sh\ncat <<'EOF'\n{apt_get}EOF\nexit {apt_get_rc}\n") + for tool in ("apt", "apt-get"): + (bin_dir / tool).chmod(0o755) + return {**os.environ, "PATH": f"{bin_dir}:{os.environ['PATH']}"} + + +class TestPhasedUpdates: + """Ubuntu hands a new build to a share of its hosts first (phased updates). + ``apt list --upgradable`` lists it on every host, but ``apt upgrade`` holds it + back where the host is not yet in the share: it is not pending there, and an + update run would not install it (netOrk #3110).""" + + def test_a_package_apt_defers_for_phasing_is_not_pending(self): + output = PHASING + "__APT_PHASED: libnftables1 nftables\n" + + updates = parse_apt_upgradable(output) + + assert [u["name"] for u in updates] == ["openssl"] + + def test_deferred_names_over_several_lines_are_all_left_out(self): + output = PHASING + "__APT_PHASED: libnftables1\n__APT_PHASED: nftables\n" + + assert [u["name"] for u in parse_apt_upgradable(output)] == ["openssl"] + + def test_a_foreign_architecture_is_named_with_it(self): + """apt names a package of a foreign architecture as ``name:arch``.""" + output = PHASING + "__APT_PHASED: libnftables1:i386 nftables\n" + + assert [u["name"] for u in parse_apt_upgradable(output)] == ["openssl"] + + def test_without_a_deferred_list_every_package_is_pending(self): + assert len(parse_apt_upgradable(PHASING)) == 3 + + def test_the_command_marks_what_apt_get_defers(self, tmp_path): + env = _fake_apt(tmp_path, APT_GET_UPGRADE) + + out = subprocess.run( + ["sh", "-c", APT_UPGRADABLE_COMMAND], capture_output=True, text=True, env=env + ).stdout + + assert "__APT_PHASED: libnftables1 nftables" in out + assert "openssl\n" not in out.split("__APT_RC=0")[1] + assert [u["name"] for u in parse_apt_upgradable(out)] == ["openssl"] + + def test_a_failed_simulation_leaves_the_listing_as_it_is(self, tmp_path): + """Where apt-get cannot simulate, nothing is left out: the listing stands.""" + env = _fake_apt(tmp_path, "E: Could not open lock file\n", apt_get_rc=100) + + out = subprocess.run( + ["sh", "-c", APT_UPGRADABLE_COMMAND], capture_output=True, text=True, env=env + ).stdout + + assert len(parse_apt_upgradable(out)) == 3 + + def test_the_simulation_needs_no_root_and_no_lock(self): + assert "apt-get -s" in APT_UPGRADABLE_COMMAND + assert "Debug::NoLocking=1" in APT_UPGRADABLE_COMMAND + assert "sudo" not in APT_UPGRADABLE_COMMAND + + class TestDnfSecurity: ADVISORIES = """\ FEDORA-2024-1a2b3c4d5e Important/Sec. openssl-libs-1:3.1.4-2.fc40.x86_64