diff --git a/CHANGELOG.md b/CHANGELOG.md index e4a2f33..1e0958c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `get_host_status()` tells an interrupted package manager on hosts without dpkg too, and + says which one it read in the new `package_manager` field (`dpkg`, `dnf`, `yum`, `apk`, + `opkg`, or None). dnf and yum: a package installed in two versions (not the kernels and + other packages dnf keeps side by side) as state `duplicate`, or a transaction that never + ended (dnf 4's history, yum's `transaction-all` journal). apk: a package apk marked with + broken files or a broken script (`broken-files`, `broken-script`), the ones `apk fix` would + take. opkg: a package left `unpacked`, `half-configured` or `half-installed`, or flagged for + reinstallation. All read without root; while one of them runs the answer is unknown + (NetOrk/netork#851). + ## [4.32.0] - 2026-11-07 ### Added diff --git a/README.md b/README.md index 2289e26..45181c5 100644 --- a/README.md +++ b/README.md @@ -174,9 +174,12 @@ A driver supplies `_run_apt_command`, `_run_apt_root_command` and `_write_apt_fi `HostStatusMixin` (`get_host_status`) is mixed in the same way: whether a Linux host needs a reboot to finish an update (`/var/run/reboot-required`, `needs-restarting -r`, or a newer kernel of the running flavour installed), whether it patches itself (unattended-upgrades, -dnf-automatic) and when that last succeeded (apt's `upgrade-stamp`), and whether dpkg was -left halfway (a journal in `/var/lib/dpkg/updates` or a half-configured package; unknown -while a package manager is running), and how full `/`, `/boot` and `/var` are (`df -Pk`, +dnf-automatic) and when that last succeeded (apt's `upgrade-stamp`), and whether its package +manager was left halfway (unknown while one is running): dpkg (a journal in +`/var/lib/dpkg/updates` or a half-configured package), dnf or yum (two versions of a package +in the rpm database, a transaction that never ended), apk (a package marked with broken files +or a broken script) or opkg (a package left `unpacked`), with `package_manager` naming which, +and how full `/`, `/boot` and `/var` are (`df -Pk`, each filesystem once, in bytes; how full is too full is the caller's decision). `package_updates` holds the shared apt and dnf parsers: apt's suites become an update's `origin`, a `-security` suite makes it a security update, and dnf's security advisories do the same. diff --git a/docs/STRUCTURE.md b/docs/STRUCTURE.md index ffd6bda..d3d671e 100644 --- a/docs/STRUCTURE.md +++ b/docs/STRUCTURE.md @@ -151,6 +151,7 @@ the driver. | Module | Knows | |---|---| | `host_status` | apt, dnf, dpkg, systemd timers, `df` | +| `package_managers` | rpm, dnf and yum history, apk's and opkg's databases, `/proc//comm` | | `kernel` | `/proc`, `/lib/modules`, gzip, base64 | | `listening` | `ss`, `netstat`, `/proc//cgroup` | | `systemd` | `systemctl` | diff --git a/napalm_device_types/host_status.py b/napalm_device_types/host_status.py index 68307fe..1ba460a 100644 --- a/napalm_device_types/host_status.py +++ b/napalm_device_types/host_status.py @@ -43,7 +43,9 @@ exits quietly every day (NetOrk/netork#738). While dpkg, apt, aptitude or unattended-upgrade is running, packages are halfway on purpose -- also between two of apt's dpkg calls -- so the answer is then ``None``, as on a host without dpkg. unattended-upgrade is told from Ubuntu's idle shutdown helper by its -command line (:data:`BUSY_CHECK`). +command line (:data:`BUSY_CHECK`). On a host without dpkg, dnf or yum, apk and opkg +are read the same way (:mod:`napalm_device_types.package_managers`, +NetOrk/netork#851), and ``package_manager`` names the one the answer is about. **Filesystems** are how full ``/``, ``/boot`` and ``/var`` are, from POSIX ``df -Pk``: where an upgrade unpacks a kernel, builds its initrd and keeps the @@ -65,6 +67,11 @@ from napalm_device_types.models import ( HostStatusDict, InterruptedPackageDict, ) +from napalm_device_types.package_managers import ( + PACKAGE_MANAGERS_COMMAND, + PackageManagerReading, + parse_package_managers, +) from napalm_device_types.terminal import strip_terminal_codes _BEGIN = "HSTAT_BEGIN" @@ -117,6 +124,7 @@ HOST_STATUS_COMMAND = ( f"echo '[dpkg-journal]'; ls -1 {_DPKG_JOURNAL} 2>/dev/null | head -n 20; " "echo '[dpkg-audit]'; dpkg-query -W -f='${db:Status-Abbrev} ${Package}\\n' 2>/dev/null " "| awk 'substr($0, 2, 1) ~ /[HUFWt]/ || substr($0, 3, 1) == \"R\"' | head -n 50; fi; " + f"{PACKAGE_MANAGERS_COMMAND}" f"echo '[filesystems]'; df -Pk {_FILESYSTEMS}; " f"echo '[timers]'; for u in {_APT_TIMER} {' '.join(_DNF_TIMERS)}; do " 'printf \'%s %s\\n\' "$u" "$(systemctl is-enabled "$u" 2>/dev/null)"; done; ' @@ -287,12 +295,24 @@ def _halfway(line: str) -> InterruptedPackageDict | None: return {"name": name, "state": state} if name and state else None -def _dpkg(sections: dict[str, list[str]]) -> tuple[bool | None, list[InterruptedPackageDict]]: - if "dpkg-audit" not in sections or "dpkg-busy" in sections: - return None, [] +def _dpkg(sections: dict[str, list[str]]) -> PackageManagerReading | None: + if "dpkg-audit" not in sections: + return None + if "dpkg-busy" in sections: + return PackageManagerReading("dpkg", None, []) packages = [p for p in map(_halfway, sections["dpkg-audit"]) if p is not None] journal = any(_JOURNAL_ENTRY.fullmatch(f) for f in sections.get("dpkg-journal") or []) - return bool(packages) or journal, packages + return PackageManagerReading("dpkg", bool(packages) or journal, packages) + + +def _package_manager(sections: dict[str, list[str]]) -> PackageManagerReading: + """The reading that says most: one left halfway, then one that is not, then a busy one.""" + readings = [r for r in (_dpkg(sections), *parse_package_managers(sections)) if r is not None] + for interrupted in (True, False): + found = next((r for r in readings if r.interrupted is interrupted), None) + if found is not None: + return found + return readings[0] if readings else PackageManagerReading(None, None, []) def _filesystems(sections: dict[str, list[str]]) -> list[FilesystemUsageDict] | None: @@ -318,15 +338,16 @@ def parse_host_status(output: str) -> HostStatusDict: """ sections = _sections(output) required, reason = _reboot(sections) - interrupted, packages = _dpkg(sections) + manager = _package_manager(sections) return { "reboot_required": required, "reboot_reason": reason, "auto_updates": _auto_updates(sections), "auto_updates_last_success": _last_success(sections), "auto_updates_interval": _interval(sections), - "package_manager_interrupted": interrupted, - "interrupted_packages": packages, + "package_manager": manager.manager, + "package_manager_interrupted": manager.interrupted, + "interrupted_packages": manager.packages, "filesystems": _filesystems(sections), } @@ -356,6 +377,7 @@ class HostStatusMixin: * auto_updates (bool or None) * auto_updates_last_success (Unix time or None) * auto_updates_interval (seconds or None) + * package_manager (dpkg, dnf, yum, apk, opkg, or None) * package_manager_interrupted (bool or None) * interrupted_packages (list of {name, state}) * filesystems (list of {mountpoint, total, used, available} in bytes, or None) diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index b2d4302..94f9b30 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -85,8 +85,8 @@ class InterruptedPackageDict(TypedDict): """A package an interrupted package manager run left halfway.""" name: str - #: dpkg's word for it: "half-installed", "unpacked", "half-configured", - #: "triggers-awaited", "triggers-pending", or "reinstall-required". + #: The manager's word for it: dpkg's and opkg's ("unpacked", "half-configured", ...), + #: rpm's "duplicate", apk's "broken-files" and "broken-script" (``package_managers``). state: str @@ -111,9 +111,9 @@ class HostStatusDict(TypedDict): reboot_reason: str | None #: True when the host installs updates on its own (unattended-upgrades, dnf-automatic). auto_updates: bool | None - #: True when a package manager run was cut off and has to be finished - #: (``dpkg --configure -a``) before anything else installs; None when it - #: cannot tell, or a package manager is running right now. + #: The package manager read (dpkg, dnf, yum, apk, opkg; None: none) and whether a run + #: of it was cut off, left halfway (None: cannot tell, or one is running right now). + package_manager: NotRequired[str | None] package_manager_interrupted: NotRequired[bool | None] #: The packages that run left halfway; empty when only its journal says so. interrupted_packages: NotRequired[list[InterruptedPackageDict]] diff --git a/napalm_device_types/package_managers.py b/napalm_device_types/package_managers.py new file mode 100644 index 0000000..2ad621a --- /dev/null +++ b/napalm_device_types/package_managers.py @@ -0,0 +1,194 @@ +"""Whether dnf or yum, apk or opkg was left halfway: the host status's answer for +a host without dpkg (NetOrk/netork#851). + +Read the same way as dpkg's state (:mod:`napalm_device_types.host_status`): in the +one host status command, without root, and changing nothing. Each manager is asked +only where it exists, and while one of its processes runs -- from +``/proc//comm``, which every Linux has and BusyBox's ``ps`` cannot narrow to -- +its packages are halfway on purpose and the answer is unknown. + +**dnf and yum** (rpm). A transaction cut off after rpm installed the new version +of a package and before it erased the old one leaves both in the rpm database: +what ``dnf check --duplicates`` and ``package-cleanup --dupes`` report. Packages +dnf installs side by side on purpose do not count: its default ``installonlypkgs`` +(``kernel``, ``installonlypkg(kernel)`` and the rest), plus any the host adds in +``dnf.conf``, asked of rpm by what they provide; and ``gpg-pubkey``, one entry +per imported key. Also interrupted is a transaction that never ended: dnf 4's +history (libdnf's ``trans`` table) holds its row with ``state`` 0 until +``finish()`` writes 1 (done) or 2 (error), so the newest row at 0 is one cut off; +yum leaves a ``transaction-all.*`` journal in ``/var/lib/yum``, which +``yum-complete-transaction`` reads (one renamed ``.disabled`` is given up). The +history is read with Python's sqlite3 module in read-only mode, as a stock EL host +has no ``sqlite3`` program and a ``dnf`` command loads every plugin and may reach +for its repositories. dnf 5's history lives elsewhere and is not read; its +duplicates are. + +**apk** (Alpine, OpenWrt from 25.12). apk marks an installed package whose files +or scripts failed with ``f:`` in ``/lib/apk/db/installed`` -- ``f`` broken files, +``s`` a broken script; ``x`` (xattrs) and ``S`` (the checksum kind) are no fault -- +and those are exactly the packages ``apk fix`` without arguments picks. A run +killed outright leaves no such mark; its next run usually repeats what it left. + +**opkg** (OpenWrt before 25.12). ``/usr/lib/opkg/status`` holds +``Status: `` per package. A run cut off, or a postinst that +failed, leaves ``unpacked`` (or ``half-configured``, ``half-installed``), and the +``reinstreq`` flag asks for a reinstall; ``opkg configure`` finishes every unpacked +package. + +What to do about it is not decided here. +""" + +from __future__ import annotations + +from typing import NamedTuple + +from napalm_device_types.models import InterruptedPackageDict + +#: dnf's default ``installonlypkgs``: names and what such packages provide. +_INSTALLONLY = ( + "kernel", + "kernel-PAE", + "installonlypkg(kernel)", + "installonlypkg(kernel-module)", + "installonlypkg(vm)", + "multiversion(kernel)", +) +#: Where a host adds its own installonly packages, dnf's then yum's. +_DNF_CONF = "/etc/dnf/dnf.conf /etc/yum.conf" +DNF_HISTORY = "/var/lib/dnf/history.sqlite" +YUM_DIR = "/var/lib/yum" +APK_INSTALLED = "/lib/apk/db/installed" +OPKG_STATUS = "/usr/lib/opkg/status" + +#: manager -> the process names that leave its packages halfway while they run. +_RUNNING = { + "rpm": frozenset({"rpm", "dnf", "dnf5", "yum", "dnf-automatic", "packagekitd"}), + "apk": frozenset({"apk"}), + "opkg": frozenset({"opkg"}), +} +_UNFINISHED = "0" +_YUM_JOURNAL = "transaction-all." +_YUM_GIVEN_UP = ".disabled" +_APK_FLAGS = (("f", "broken-files"), ("s", "broken-script")) +_OPKG_HALFWAY = frozenset({"unpacked", "half-configured", "half-installed"}) +_OPKG_REINSTALL = "reinstreq" + +#: The newest transaction in dnf's history as `` ``; nothing when unreadable. +#: One literal, with :data:`DNF_HISTORY` spelt out: a fixed query, nothing put into it. +_DNF_HISTORY_PY = ( + "import sqlite3; " + "c = sqlite3.connect('file:/var/lib/dnf/history.sqlite?mode=ro', uri=True); " + "r = c.execute('SELECT id, state FROM trans ORDER BY id DESC LIMIT 1').fetchone(); " + "r and print(*r)" +) + +_RUNNING_PART = ( + "echo '[pm-running]'; cat /proc/[0-9]*/comm 2>/dev/null | grep -xE '" + + "|".join(sorted(set().union(*_RUNNING.values()))) + + "' | sort -u; " +) +_RPM_PART = ( + "if command -v rpm >/dev/null 2>&1 && { command -v dnf || command -v yum; } >/dev/null 2>&1; " + "then echo '[rpm-frontend]'; { command -v dnf || command -v yum; } | sed 's#.*/##'; " + f"echo '[rpm-installonly]'; io=\"{' '.join(_INSTALLONLY)} " + f"$(sed -n 's/^[[:space:]]*installonlypkgs[[:space:]]*=//p' {_DNF_CONF} 2>/dev/null " + "| tr ',' ' ')\"; rpm -q --whatprovides $io --qf '%{NAME}\\n' 2>/dev/null " + "| grep -v ' ' | sort -u; " + "echo '[rpm-dupes]'; rpm -qa --qf '%{NAME} %{ARCH}\\n' 2>/dev/null " + "| grep -v '^gpg-pubkey ' | sort | uniq -d | head -n 50; " + f"echo '[yum-journal]'; ls -1 {YUM_DIR} 2>/dev/null | grep '^transaction-all\\.' " + "| head -n 20; " + "echo '[dnf-history]'; for py in /usr/libexec/platform-python python3; do " + f'if command -v $py >/dev/null 2>&1; then $py -c "{_DNF_HISTORY_PY}" 2>/dev/null; ' + "break; fi; done; fi; " +) +_APK_PART = ( + f"if command -v apk >/dev/null 2>&1 && [ -r {APK_INSTALLED} ]; then echo '[apk-audit]'; " + "awk '/^P:/ {p = substr($0, 3)} /^f:/ {f = substr($0, 3)} " + '/^$/ {if (f ~ /[fs]/) print f, p; p = ""; f = ""} END {if (f ~ /[fs]/) print f, p}\' ' + f"{APK_INSTALLED} | head -n 50; fi; " +) +_OPKG_PART = ( + f"if command -v opkg >/dev/null 2>&1 && [ -r {OPKG_STATUS} ]; then echo '[opkg-audit]'; " + "awk '/^Package:/ {p = $2} /^Status:/ && " + "($4 ~ /^(unpacked|half-configured|half-installed)$/ || $3 ~ /reinstreq/) " + f"{{print $3, $4, p}}' {OPKG_STATUS} | head -n 50; fi; " +) + +#: The part of the host status command that reads them; POSIX ``sh``, BusyBox too. +PACKAGE_MANAGERS_COMMAND = _RUNNING_PART + _RPM_PART + _APK_PART + _OPKG_PART + + +class PackageManagerReading(NamedTuple): + """One package manager's state: halfway or not, None while it is running.""" + + manager: str | None + interrupted: bool | None + packages: list[InterruptedPackageDict] + + +def _unfinished_transaction(sections: dict[str, list[str]]) -> bool: + newest = (sections.get("dnf-history") or [""])[0].split() + if len(newest) == 2 and newest[1] == _UNFINISHED: + return True + return any( + name.startswith(_YUM_JOURNAL) and not name.endswith(_YUM_GIVEN_UP) + for name in sections.get("yum-journal") or [] + ) + + +def _rpm(sections: dict[str, list[str]], running: set[str]) -> PackageManagerReading | None: + if "rpm-dupes" not in sections: + return None + manager = (sections.get("rpm-frontend") or ["dnf"])[0] + if running & _RUNNING["rpm"]: + return PackageManagerReading(manager, None, []) + installonly = set(sections.get("rpm-installonly") or []) + names = dict.fromkeys(line.split()[0] for line in sections["rpm-dupes"]) + packages: list[InterruptedPackageDict] = [ + {"name": name, "state": "duplicate"} for name in names if name not in installonly + ] + return PackageManagerReading( + manager, bool(packages) or _unfinished_transaction(sections), packages + ) + + +def _apk(sections: dict[str, list[str]], running: set[str]) -> PackageManagerReading | None: + if "apk-audit" not in sections: + return None + if running & _RUNNING["apk"]: + return PackageManagerReading("apk", None, []) + packages: list[InterruptedPackageDict] = [] + for line in sections["apk-audit"]: + flags, _, name = line.partition(" ") + name = name.strip() + packages += [ + {"name": name, "state": state} for flag, state in _APK_FLAGS if name and flag in flags + ] + return PackageManagerReading("apk", bool(packages), packages) + + +def _opkg_package(line: str) -> InterruptedPackageDict | None: + parts = line.split() + if len(parts) != 3: + return None + flags, state, name = parts + if _OPKG_REINSTALL in flags.split(","): + return {"name": name, "state": "reinstall-required"} + return {"name": name, "state": state} if state in _OPKG_HALFWAY else None + + +def _opkg(sections: dict[str, list[str]], running: set[str]) -> PackageManagerReading | None: + if "opkg-audit" not in sections: + return None + if running & _RUNNING["opkg"]: + return PackageManagerReading("opkg", None, []) + packages = [p for p in map(_opkg_package, sections["opkg-audit"]) if p is not None] + return PackageManagerReading("opkg", bool(packages), packages) + + +def parse_package_managers(sections: dict[str, list[str]]) -> list[PackageManagerReading]: + """What :data:`PACKAGE_MANAGERS_COMMAND` found, one reading per manager it asked.""" + running = set(sections.get("pm-running") or []) + readings = (read(sections, running) for read in (_rpm, _apk, _opkg)) + return [reading for reading in readings if reading is not None] diff --git a/tests/test_host_status.py b/tests/test_host_status.py index 2465e46..4813756 100644 --- a/tests/test_host_status.py +++ b/tests/test_host_status.py @@ -13,6 +13,8 @@ from __future__ import annotations import os import re +import shutil +import sqlite3 import subprocess import pytest @@ -25,6 +27,12 @@ from napalm_device_types.host_status import ( kernel_reboot_pending, parse_host_status, ) +from napalm_device_types.package_managers import ( + APK_INSTALLED, + DNF_HISTORY, + OPKG_STATUS, + PACKAGE_MANAGERS_COMMAND, +) def _wire( @@ -570,3 +578,371 @@ class TestFilesystems: mountpoints = [fs["mountpoint"] for fs in parse_host_status(out.stdout)["filesystems"]] assert "/" in mountpoints + + +def _other(*sections: tuple[str, tuple[str, ...]], running: tuple[str, ...] = ()) -> str: + """A report from a host without dpkg: the given sections before ``[timers]``.""" + lines = ["[pm-running]", *running] + for name, body in sections: + lines += [f"[{name}]", *body] + return _wire().replace("[timers]", "\n".join([*lines, "[timers]"])) + + +class TestInterruptedRpm: + """dnf and yum hosts: a transaction rpm was cut off in leaves the old and the + new version of a package both installed (``dnf check --duplicates``, + ``package-cleanup --dupes``), and dnf's history records a transaction that + never ended. netOrk only reports it: there is no one repair that is safe on + every host (NetOrk/netork#851, decided 2026-11-07). + + The outputs are shaped after the documented formats -- ``rpm -qa --qf``, + libdnf's ``trans`` table (``state`` 0 = UNKNOWN until ``finish()`` sets DONE + 1 or ERROR 2), yum's ``transaction-all.*`` journal that + ``yum-complete-transaction`` reads -- and still have to be checked against a + real Oracle Linux 9 host. + """ + + INSTALLONLY = ("kernel-core", "kernel-modules", "kernel-uek-core") + #: An upgrade of glibc and openssl-libs cut off after the new versions were in. + CUT_OFF = ("glibc x86_64", "kernel-uek-core x86_64", "openssl-libs x86_64") + + def _rpm(self, dupes=(), history=("42 1",), journal=(), frontend="dnf", running=()): + return _other( + ("rpm-frontend", (frontend,)), + ("rpm-installonly", self.INSTALLONLY), + ("rpm-dupes", dupes), + ("yum-journal", journal), + ("dnf-history", history), + running=running, + ) + + def test_a_clean_rpm_database_is_not_interrupted(self): + status = parse_host_status(self._rpm()) + + assert status["package_manager_interrupted"] is False + assert status["interrupted_packages"] == [] + assert status["package_manager"] == "dnf" + + def test_two_versions_of_a_package_left_by_a_cut_off_transaction(self): + status = parse_host_status(self._rpm(dupes=self.CUT_OFF)) + + assert status["package_manager_interrupted"] is True + assert status["interrupted_packages"] == [ + {"name": "glibc", "state": "duplicate"}, + {"name": "openssl-libs", "state": "duplicate"}, + ] + + def test_kernels_are_installed_side_by_side_on_purpose(self): + """dnf's installonly packages: several versions are the rule, not a fault.""" + status = parse_host_status(self._rpm(dupes=("kernel-uek-core x86_64",))) + + assert status["package_manager_interrupted"] is False + + def test_a_package_duplicated_in_two_arches_is_named_once(self): + status = parse_host_status(self._rpm(dupes=("glibc i686", "glibc x86_64"))) + + assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}] + + def test_the_last_transaction_never_finished(self): + status = parse_host_status(self._rpm(history=("43 0",))) + + assert status["package_manager_interrupted"] is True + assert status["interrupted_packages"] == [] + + @pytest.mark.parametrize("history", [("44 1",), ("45 2",), ()]) + def test_a_finished_or_unreadable_history_says_nothing(self, history): + """A failed transaction (ERROR) did end; an unreadable history is no answer.""" + assert parse_host_status(self._rpm(history=history))["package_manager_interrupted"] is False + + def test_yum_left_its_transaction_journal(self): + journal = ("transaction-all.2026-11-01.10:12.33", "transaction-done.2026-11-01.10:12.33") + status = parse_host_status(self._rpm(frontend="yum", history=(), journal=journal)) + + assert status["package_manager_interrupted"] is True + assert status["package_manager"] == "yum" + + def test_a_journal_yum_gave_up_on_is_none(self): + """yum-complete-transaction skips a journal it renamed ``.disabled``.""" + journal = ("transaction-all.2026-11-01.10:12.33.disabled",) + status = parse_host_status(self._rpm(frontend="yum", history=(), journal=journal)) + + assert status["package_manager_interrupted"] is False + + @pytest.mark.parametrize("name", ["rpm", "dnf", "dnf5", "yum", "dnf-automatic", "packagekitd"]) + def test_a_running_transaction_makes_it_unknown(self, name): + status = parse_host_status(self._rpm(dupes=self.CUT_OFF, running=(name,))) + + assert status["package_manager_interrupted"] is None + assert status["interrupted_packages"] == [] + assert status["package_manager"] == "dnf" + + def test_another_managers_run_does_not(self): + status = parse_host_status(self._rpm(dupes=self.CUT_OFF, running=("opkg",))) + + assert status["package_manager_interrupted"] is True + + def test_the_command_asks_rpm_only_where_dnf_or_yum_exists(self): + assert "command -v rpm" in HOST_STATUS_COMMAND + assert "rpm -qa --qf" in HOST_STATUS_COMMAND + assert f"file:{DNF_HISTORY}?mode=ro" in HOST_STATUS_COMMAND + + def test_it_never_repairs(self): + for word in ("distro-sync", "--cleandupes", "remove", "yum-complete-transaction", "rpm -e"): + assert word not in HOST_STATUS_COMMAND + + +class TestInterruptedApk: + """Alpine and OpenWrt 25.12: apk marks a package whose files or scripts failed + in ``/lib/apk/db/installed`` (``f:`` with ``f`` broken files, ``s`` a broken + script; ``x`` xattrs and ``S`` the checksum kind are no fault), and ``apk fix`` + with no arguments is what picks exactly those (apk-tools ``app_fix.c``). + netOrk only reports it (NetOrk/netork#851). Shaped after apk-tools' + ``database.c``; to be checked against a real OpenWrt 25.12 host.""" + + def _apk(self, audit=(), running=()): + return _other(("apk-audit", audit), running=running) + + def test_a_clean_database_is_not_interrupted(self): + status = parse_host_status(self._apk()) + + assert status["package_manager_interrupted"] is False + assert status["package_manager"] == "apk" + + @pytest.mark.parametrize( + ("flags", "states"), + [ + ("s", ["broken-script"]), + ("f", ["broken-files"]), + ("fsS", ["broken-files", "broken-script"]), + ], + ) + def test_a_package_apk_marked_broken(self, flags, states): + status = parse_host_status(self._apk(audit=(f"{flags} busybox",))) + + assert status["package_manager_interrupted"] is True + assert status["interrupted_packages"] == [ + {"name": "busybox", "state": state} for state in states + ] + + def test_a_running_apk_makes_it_unknown(self): + status = parse_host_status(self._apk(audit=("s busybox",), running=("apk",))) + + assert status["package_manager_interrupted"] is None + + def test_the_command_reads_the_installed_database(self): + assert "/lib/apk/db/installed" in HOST_STATUS_COMMAND + assert "apk fix" not in HOST_STATUS_COMMAND + + +class TestInterruptedOpkg: + """OpenWrt before 25.12: opkg writes ``Status: `` per + package to ``/usr/lib/opkg/status``; a package whose run was cut off or whose + postinst failed stays ``unpacked``, and ``opkg configure`` finishes every + unpacked package (opkg-lede ``opkg_cmd.c``, ``pkg.c``). netOrk's update run + does that first (NetOrk/netork#851). To be checked against a real router.""" + + def _opkg(self, audit=(), running=()): + return _other(("opkg-audit", audit), running=running) + + def test_a_clean_status_file_is_not_interrupted(self): + status = parse_host_status(self._opkg()) + + assert status["package_manager_interrupted"] is False + assert status["package_manager"] == "opkg" + + @pytest.mark.parametrize( + ("line", "state"), + [ + ("user unpacked luci-app-sqm", "unpacked"), + ("ok half-configured luci-app-sqm", "half-configured"), + ("ok half-installed luci-app-sqm", "half-installed"), + ("reinstreq,user installed luci-app-sqm", "reinstall-required"), + ], + ) + def test_every_state_an_interrupted_run_leaves(self, line, state): + status = parse_host_status(self._opkg(audit=(line,))) + + assert status["package_manager_interrupted"] is True + assert status["interrupted_packages"] == [{"name": "luci-app-sqm", "state": state}] + + def test_a_running_opkg_makes_it_unknown(self): + status = parse_host_status(self._opkg(audit=("ok unpacked x",), running=("opkg",))) + + assert status["package_manager_interrupted"] is None + + def test_the_command_reads_the_status_file_and_never_configures(self): + assert "/usr/lib/opkg/status" in HOST_STATUS_COMMAND + assert "opkg configure" not in HOST_STATUS_COMMAND + + +class TestPackageManager: + def test_dpkg_says_so(self): + assert parse_host_status(_wire(dpkg=()))["package_manager"] == "dpkg" + + def test_a_busy_dpkg_still_says_which(self): + status = parse_host_status(_wire(dpkg=(), busy=True)) + + assert (status["package_manager"], status["package_manager_interrupted"]) == ( + "dpkg", + None, + ) + + def test_no_package_manager_read_is_none(self): + status = parse_host_status(_wire()) + + assert status["package_manager"] is None + assert status["package_manager_interrupted"] is None + + +#: An rpm that answers the two queries the command asks, as on an EL host whose +#: glibc upgrade was cut off; and a dnf that only has to exist. +_RPM_STUB = """#!/bin/sh +case "$*" in + *--whatprovides*) printf '%s\\n' kernel-core kernel-core 'no package provides kernel-PAE' ;; + -qa*) printf '%s\\n' 'glibc x86_64' 'kernel-core x86_64' 'gpg-pubkey (none)' \\ + 'glibc x86_64' 'kernel-core x86_64' 'gpg-pubkey (none)' 'bash x86_64' ;; +esac +""" + +#: /lib/apk/db/installed shaped after apk-tools' database.c: busybox's script failed. +_APK_DB = """C:Q1abc= +P:alpine-baselayout +V:3.6.8-r1 +A:x86_64 +F:etc +R:fstab + +C:Q1def= +P:busybox +V:1.37.0-r12 +A:x86_64 +f:s +F:bin +R:busybox + +C:Q1ghi= +P:musl +V:1.2.5-r9 +f:S +""" + +#: /usr/lib/opkg/status as opkg writes it: luci-app-sqm's postinst never ran. +_OPKG_STATUS = """Package: libc +Version: 1.2.4-4 +Depends: libgcc1 +Status: install hold installed +Architecture: mipsel_24kc +Installed-Time: 1717000000 + +Package: luci-app-sqm +Version: 1.5.3-r1 +Depends: libc, sqm-scripts +Status: install user unpacked +Architecture: all +Installed-Time: 1730000000 +""" + + +def _stubs(tmp_path, *names: str, rpm: bool = False) -> dict: + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + for name in names: + stub = bin_dir / name + stub.write_text(_RPM_STUB if name == "rpm" else "#!/bin/sh\nexit 0\n") + stub.chmod(0o755) + return {**os.environ, "PATH": f"{bin_dir}:/usr/bin:/bin"} + + +def _run(command: str, env: dict, shell: tuple[str, ...]) -> str: + report = f"printf '%s%s\\n' HSTAT_ BEGIN; {command} printf '%s%s\\n' HSTAT_ END" + program = shutil.which(shell[0]) or shell[0] + return subprocess.run( + [program, *shell[1:], report], capture_output=True, text=True, env=env, timeout=30 + ).stdout + + +_SHELLS = [("sh", "-c")] + ([("busybox", "sh", "-c")] if shutil.which("busybox") else []) + + +def _skip_busybox_rpm(shell: tuple[str, ...]) -> None: + """A BusyBox built with its own ``rpm`` applet runs that before any stand-in.""" + if shell[0] == "busybox": + applets = subprocess.run(["busybox", "--list"], capture_output=True, text=True).stdout + if "rpm" in applets.split(): + pytest.skip("this busybox has an rpm applet of its own") + + +@pytest.mark.parametrize("shell", _SHELLS) +class TestThePackageManagerCommandOnARealShell: + """The command part run for real, with stand-in programs and files.""" + + def test_rpm_duplicates_without_the_kernel_and_the_keys(self, tmp_path, shell): + _skip_busybox_rpm(shell) + env = _stubs(tmp_path, "rpm", "dnf") + out = _run(PACKAGE_MANAGERS_COMMAND, env, shell) + + status = parse_host_status(out) + + assert status["package_manager"] == "dnf" + assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}] + + def test_dnfs_last_transaction_that_never_ended(self, tmp_path, shell): + _skip_busybox_rpm(shell) + history = tmp_path / "history.sqlite" + with sqlite3.connect(history) as db: + db.execute("CREATE TABLE trans (id INTEGER PRIMARY KEY, state INTEGER NOT NULL)") + db.executemany("INSERT INTO trans VALUES (?, ?)", [(41, 1), (42, 0)]) + env = _stubs(tmp_path, "rpm", "dnf") + out = _run(PACKAGE_MANAGERS_COMMAND.replace(DNF_HISTORY, str(history)), env, shell) + + assert "[dnf-history]\n42 0\n" in out + + def test_apk_names_the_package_it_marked(self, tmp_path, shell): + installed = tmp_path / "installed" + installed.write_text(_APK_DB) + env = _stubs(tmp_path, "apk") + out = _run(PACKAGE_MANAGERS_COMMAND.replace(APK_INSTALLED, str(installed)), env, shell) + + status = parse_host_status(out) + + assert status["package_manager"] == "apk" + assert status["interrupted_packages"] == [{"name": "busybox", "state": "broken-script"}] + + def test_opkg_names_the_unpacked_package(self, tmp_path, shell): + status_file = tmp_path / "status" + status_file.write_text(_OPKG_STATUS) + env = _stubs(tmp_path, "opkg") + out = _run(PACKAGE_MANAGERS_COMMAND.replace(OPKG_STATUS, str(status_file)), env, shell) + + status = parse_host_status(out) + + assert status["package_manager"] == "opkg" + assert status["interrupted_packages"] == [{"name": "luci-app-sqm", "state": "unpacked"}] + + def test_a_host_with_none_of_them_reads_none(self, tmp_path, shell): + env = _stubs(tmp_path) + env["PATH"] = str(tmp_path / "bin") + out = _run(PACKAGE_MANAGERS_COMMAND, env, shell) + + assert parse_host_status(out)["package_manager"] is None + + def test_the_running_processes_come_from_proc(self, tmp_path, shell): + """The test runner's own process is there under its own name; no manager is.""" + out = _run(PACKAGE_MANAGERS_COMMAND, _stubs(tmp_path), shell) + + assert "[pm-running]\n" in out + + +class TestTheWholeCommand: + def test_it_runs_on_busybox(self): + """OpenWrt's and Alpine's shell: still one intact report.""" + if not shutil.which("busybox"): + pytest.skip("no busybox here") + out = subprocess.run( + ["busybox", "sh", "-c", HOST_STATUS_COMMAND], capture_output=True, text=True, timeout=30 + ).stdout + + assert parse_host_status(out)["filesystems"] + + def test_the_package_managers_are_part_of_it(self): + assert PACKAGE_MANAGERS_COMMAND in HOST_STATUS_COMMAND