From 9106871bfa78f7d83d1ae2bc2bad82987aa74071 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 5 Dec 2026 14:10:49 +0100 Subject: [PATCH] fix: the apt read ends on its own status echo, 4.59.1 (NetOrk/netork#3139) APT_UPGRADABLE_COMMAND printed __APT_PHASED (4.58.1) and __APT_E (4.57.0) after __APT_RC. A screen-scraping transport reads until the command's last status echo and the prompt after it, so on a host where apt defers a package for phasing, or reports an error, the read never ended and ran into its timeout. __APT_END is now echoed once everything else is written. --- CHANGELOG.md | 10 ++++++++ napalm_device_types/package_updates.py | 9 +++++-- pyproject.toml | 2 +- tests/test_package_updates.py | 35 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 246ac30..1513eea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ recorded here; their history is the git log. ## [Unreleased] +## [4.59.1] - 2026-12-05 + +### Fixed +- `APT_UPGRADABLE_COMMAND` ends on its own status echo, `__APT_END=`, written once + everything else is. A screen-scraping transport reads until the command's last status + echo and the prompt after it. The `__APT_PHASED:` lines (4.58.1) and the `__APT_E:` lines + (4.57.0) came after `__APT_RC`, so on a host where apt defers a package for phasing, or + reports an error, the read never ended and ran into its timeout (NetOrk/netork#3139). + The parse does not read the new line. + ## [4.59.0] - 2026-12-05 ### Added diff --git a/napalm_device_types/package_updates.py b/napalm_device_types/package_updates.py index 86a1df8..37df2e4 100644 --- a/napalm_device_types/package_updates.py +++ b/napalm_device_types/package_updates.py @@ -38,12 +38,17 @@ from napalm_device_types.updates import PackageSourcesUnreadable #: 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. +#: +#: Its last line is ``__APT_END=``, echoed once everything else is written. A +#: screen-scraping transport reads until the command's last status echo and the +#: prompt after it; ``__APT_PHASED`` and ``__APT_E`` came after ``__APT_RC``, and +#: the read never ended (netOrk #3139). The parse does not read it. APT_UPGRADABLE_COMMAND = ( - "{ { LC_ALL=C apt list --upgradable 2>&1 1>&3 3>&-; echo __APT_RC=$? 1>&3; }" + "{ { { LC_ALL=C apt list --upgradable 2>&1 1>&3 3>&-; echo __APT_RC=$? 1>&3; }" " | 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" + " 3>&1; echo __APT_END=$?; } | cat" ) #: apt's last word when it cannot read its sources, in ``LC_ALL=C``. diff --git a/pyproject.toml b/pyproject.toml index 50dce04..e7e3d42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.59.0" +version = "4.59.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 30ac089..e329e6e 100644 --- a/tests/test_package_updates.py +++ b/tests/test_package_updates.py @@ -12,6 +12,7 @@ Fixture lines are from real hosts (Ubuntu 24.04, Debian 13 / OMV, Proxmox VE 9). from __future__ import annotations import os +import re import shutil import subprocess @@ -230,6 +231,11 @@ Conf openssl (3.0.13-0ubuntu3.6 Ubuntu:24.04/noble-updates [amd64]) """ +#: The marker of the command's last ``echo __NAME=$?``: a screen-scraping +#: transport reads until it, with a number, and the prompt line after it. +_LAST_STATUS_ECHO = re.escape(re.findall(r"echo\s+(__[A-Z_]+=)\$\?", APT_UPGRADABLE_COMMAND)[-1]) + + 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" @@ -290,6 +296,35 @@ class TestPhasedUpdates: assert len(parse_apt_upgradable(out)) == 3 + def test_the_command_ends_on_its_last_status_echo(self, tmp_path): + """A screen-scraping transport reads until the command's last status echo + and the prompt after it (napalm-linux's ``expect_for``). What apt-get + defers came after ``__APT_RC``, so the read never ended on a host with a + phased package and ran into its timeout (netOrk #3139).""" + 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 re.fullmatch(_LAST_STATUS_ECHO + r"\d+", out.splitlines()[-1]) + assert [u["name"] for u in parse_apt_upgradable(out)] == ["openssl"] + + def test_apts_errors_come_before_the_last_status_echo(self, tmp_path): + """``sed`` writes apt's marked errors when apt is done, after ``__APT_RC``.""" + env = _fake_apt(tmp_path, "") + (tmp_path / "bin" / "apt").write_text( + "#!/bin/sh\necho 'E: The list of sources could not be read.' >&2\nexit 100\n" + ) + + out = subprocess.run( + ["sh", "-c", APT_UPGRADABLE_COMMAND], capture_output=True, text=True, env=env + ).stdout + + assert re.fullmatch(_LAST_STATUS_ECHO + r"\d+", out.splitlines()[-1]) + with pytest.raises(PackageSourcesUnreadable): + parse_apt_upgradable(out) + 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 -- 2.54.1