fix(packages): leave removed packages with leftover config out of apt

dpkg-query -W also lists packages that were removed but whose
configuration dpkg keeps (state rc), and the driver marked every line
installed: old kernels and a removed agent showed up as installed
software. The query now asks for db:Status-Abbrev and drops packages
that are not installed or only left their configuration; held, unpacked
and half-configured packages keep counting, their files are on disk.

The dpkg query and its parse move to napalm_linux/_dpkg.py, a pure
function under strict mypy, which keeps linux.py inside its line count.

Closes #17
This commit is contained in:
Christian Manivong committed 2026-11-01 21:48:05 +01:00
1 parent 54f6844d84
commit 024f68c543
5 files changed
+108 -51

No files matched your search

+28 -4
View File
@@ -259,17 +259,41 @@ def test_parse_uptime_invalid(driver):
#: assertion noticed. A fixture simpler than the data cannot fail the way the
#: data does.
APT_PKG_OUTPUT = (
"openssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2"
"ii \topenssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2"
"\tsecure shell server\n"
"curl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5"
"ii \tcurl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5"
"\tcommand line tool for transferring data\n"
# The shape that matters: a binary package whose own upstream version has
# nothing to do with its source package's. ldb 2.11.0 is built from samba
# 4.22.11, and OSV states Debian ranges in source versions.
"libldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba"
"ii \tlibldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba"
"\t2:4.22.11+dfsg-0+deb13u1\tLDB shared library\n"
)
#: Removed packages whose configuration dpkg still keeps (state ``rc``), next to
#: a held one and one whose postinst failed -- both still on disk (#17).
APT_PKG_STATES = (
"rc \twazuh-agent\t4.14.7-1\t\twazuh-agent\t4.14.7-1\tWazuh agent\n"
"rc \tlinux-image-6.8.0-117-generic\t6.8.0-117.117\t\tlinux-signed\t6.8.0-117.117\tSigned kernel image generic\n"
"hi \tdocker-ce\t5:28.5.1-1~debian.13~trixie\t104858\tdocker-ce\t5:28.5.1-1~debian.13~trixie\tDocker: the open-source application container engine\n"
"iU \tzabbix-agent\t1:7.0.10-1\t1550\tzabbix\t1:7.0.10-1\tZabbix network monitoring solution - agent\n"
"ii \tcurl\t8.14.1-2\t512\tcurl\t8.14.1-2\tcommand line tool for transferring data with URL syntax\n"
)
def test_get_packages_apt_reads_the_package_state(driver):
driver._pkg_manager = "apt"
with patch.object(driver, "_send", return_value=APT_PKG_STATES) as send:
driver.get_packages()
assert send.call_args[0][0].startswith("dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}")
def test_get_packages_apt_leaves_out_what_only_left_its_configuration(driver):
driver._pkg_manager = "apt"
with patch.object(driver, "_send", return_value=APT_PKG_STATES):
names = [p["name"] for p in driver.get_packages()]
assert names == ["docker-ce", "zabbix-agent", "curl"]
def test_get_packages_apt(driver):
driver._pkg_manager = "apt"
@@ -309,7 +333,7 @@ def test_get_packages_apt_falls_back_when_dpkg_gives_no_source(driver):
"""`source:Package` is empty for a package whose source name equals its own.
Older dpkg builds leave `source:Version` empty in that case too."""
driver._pkg_manager = "apt"
with patch.object(driver, "_send", return_value="curl\t7.88.1-10\t1024\t\t\ttool\n"):
with patch.object(driver, "_send", return_value="ii \tcurl\t7.88.1-10\t1024\t\t\ttool\n"):
(pkg,) = driver.get_packages()
assert pkg["source_package"] == "curl"