feat: apt's errors come through, unreadable sources raise PackageSourcesUnreadable, 4.57.0 (NetOrk/netork#3087)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / lowest (pull_request) Successful in 45s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m19s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 2m14s
driver / publish (pull_request) Skipped

One bad apt source line, two Signed-By for one repository, makes apt refuse
every source. The update read then failed with "exit status 100" and nothing
else, because APT_UPGRADABLE_COMMAND threw stderr away.

The command now passes apt's E: lines on, marked __APT_E:, and still keeps
its warnings out. parse_apt_upgradable raises PackageSourcesUnreadable, a
ValueError, with apt's errors when apt could not read its sources, and names
them after the exit status for any other failure.
This commit is contained in:
christianmanivong committed 2026-12-03 13:03:41 +01:00
1 parent 6eec94830a
commit 9efc3275b1
6 files changed
+111 -7

No files matched your search

+61
View File
@@ -12,10 +12,12 @@ Fixture lines are from real hosts (Ubuntu 24.04, Debian 13 / OMV, Proxmox VE 9).
from __future__ import annotations
import os
import shutil
import subprocess
import pytest
from napalm_device_types import PackageSourcesUnreadable
from napalm_device_types.package_updates import (
APT_UPGRADABLE_COMMAND,
DNF_INSTALLED_CVES_COMMAND,
@@ -126,6 +128,65 @@ class TestAptUpgradable:
with pytest.raises(ValueError, match="100"):
parse_apt_upgradable("E: Could not get lock\n__APT_RC=100\n")
def test_a_failed_apt_says_what_apt_said(self):
"""Its errors come through marked; the exit status alone names no cause."""
output = "__APT_RC=100\n__APT_E: Could not open lock file /var/lib/apt/lists/lock\n"
with pytest.raises(ValueError, match="100") as caught:
parse_apt_upgradable(output)
assert "Could not open lock file /var/lib/apt/lists/lock" in str(caught.value)
assert not isinstance(caught.value, PackageSourcesUnreadable)
def test_unreadable_sources_raise_their_own_error(self):
"""Two Signed-By for one source make apt refuse every source (netOrk #3087)."""
output = (
"__APT_RC=100\n"
"__APT_E: Conflicting values set for option Signed-By regarding source"
" http://HTTPS///download.docker.com/linux/ubuntu/ noble:"
" /etc/apt/keyrings/docker.gpg != /etc/apt/keyrings/docker.asc\n"
"__APT_E: The list of sources could not be read.\n"
)
with pytest.raises(PackageSourcesUnreadable) as caught:
parse_apt_upgradable(output)
assert isinstance(caught.value, ValueError) # what a caller already catches
assert str(caught.value) == (
"Conflicting values set for option Signed-By regarding source"
" http://HTTPS///download.docker.com/linux/ubuntu/ noble:"
" /etc/apt/keyrings/docker.gpg != /etc/apt/keyrings/docker.asc;"
" The list of sources could not be read."
)
def test_errors_of_a_read_that_worked_are_no_failure(self):
output = "__APT_E: The list of sources could not be read.\n" + UBUNTU
assert len(parse_apt_upgradable(output)) == 2
def test_unreadable_sources_through_the_real_command(self, tmp_path):
"""The command's own plumbing, in sh: apt's errors arrive marked, its
exit status is apt's, and its warnings stay out."""
if not shutil.which("apt"):
pytest.skip("apt is not installed")
parts = tmp_path / "sources.list.d"
parts.mkdir()
(parts / "docker.list").write_text(
"deb [signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu noble stable\n"
"deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu noble stable\n"
)
config = tmp_path / "apt.conf"
config.write_text(f'Dir::Etc::sourceparts "{parts}";\nDir::Etc::sourcelist "/dev/null";\n')
env = {**os.environ, "APT_CONFIG": str(config)}
out = subprocess.run(
["sh", "-c", APT_UPGRADABLE_COMMAND], capture_output=True, text=True, env=env
).stdout
with pytest.raises(PackageSourcesUnreadable, match="Signed-By"):
parse_apt_upgradable(out)
assert "WARNING" not in out
def test_terminal_codes_around_the_status_are_dropped(self):
"""What a pseudo-terminal left on a Raspberry Pi OS host."""
raw = "Listing... 0%\n\x1b[?1h\x1b=\n" + UBUNTU.replace("__APT_RC=0", "\x1b>__APT_RC=0")