Report a failed apt simulation instead of reading it as "no updates"
CI / tests (Python 3.10) (pull_request) Successful in 26s
CI / tests (Python 3.11) (pull_request) Successful in 25s
CI / tests (Python 3.12) (pull_request) Successful in 25s
CI / tests (Python 3.13) (pull_request) Successful in 25s
CI / lint (pull_request) Failing after 3s
CI / tests (Python 3.10) (pull_request) Successful in 26s
CI / tests (Python 3.11) (pull_request) Successful in 25s
CI / tests (Python 3.12) (pull_request) Successful in 25s
CI / tests (Python 3.13) (pull_request) Successful in 25s
CI / lint (pull_request) Failing after 3s
The agent ran `apt-get -s dist-upgrade 2>/dev/null || true`, so when apt failed (unmet dependencies, a broken sources entry, a damaged package cache) the empty output counted as zero updates and the host could show ok while nothing could be installed. The agent now keeps apt's exit status and stderr, reports apt_simulation_ok and the first "E:" line as apt_simulation_error, and sends the update counts as null. The server puts such a host in update with "apt can't work out pending updates: ...", and the host page shows the result. Agents before 0.5.0 don't send the field and are judged as before. Also show "–" rather than "None" for an unknown count in a host's history, and stop hard-coding the agent version in the status CLI tests. Agent 0.5.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1350c78676
commit
6d29197cfc
10 files changed
+100
-15
No files matched your search
@@ -271,6 +271,35 @@ X
|
||||
stub(self.bin, "apt-get", '[ "$LC_ALL" = C ] || exit 1; echo "Inst ok [1] (2 x [amd64])"\n')
|
||||
self.assertEqual(self.report()["upgradable_count"], 1)
|
||||
|
||||
def test_apt_simulation_ok(self):
|
||||
r = self.report()
|
||||
self.assertIs(r["apt_simulation_ok"], True)
|
||||
self.assertIsNone(r["apt_simulation_error"])
|
||||
|
||||
def test_apt_failure_reports_unknown_counts_not_zero(self):
|
||||
# A failed simulation prints no Inst lines; that must not read as "no updates".
|
||||
stub(self.bin, "apt-get", "echo 'Reading package lists...'\n"
|
||||
"echo 'W: Some warning' >&2\n"
|
||||
"echo \"E: Unmet dependencies. Try 'apt --fix-broken install'.\" >&2\n"
|
||||
"exit 100\n")
|
||||
r = self.report()
|
||||
self.assertIs(r["apt_simulation_ok"], False)
|
||||
self.assertEqual(r["apt_simulation_error"], "Unmet dependencies. Try 'apt --fix-broken install'.")
|
||||
for key in ("upgradable_count", "security_count", "kernel_upgradable", "phased_packages"):
|
||||
self.assertIsNone(r[key], key)
|
||||
|
||||
def test_apt_failure_without_an_error_line(self):
|
||||
stub(self.bin, "apt-get", "echo 'something broke' >&2\nexit 1\n")
|
||||
self.assertEqual(self.report()["apt_simulation_error"], "something broke")
|
||||
stub(self.bin, "apt-get", "exit 100\n")
|
||||
self.assertEqual(self.report()["apt_simulation_error"], "apt-get exited with status 100")
|
||||
|
||||
def test_apt_warnings_dont_mix_into_the_simulation(self):
|
||||
stub(self.bin, "apt-get", f"echo 'W: Inst fake [1] (2 x [amd64])' >&2\ncat <<'X'\n{APT_SIM_DEBIAN}X\n")
|
||||
r = self.report()
|
||||
self.assertIs(r["apt_simulation_ok"], True)
|
||||
self.assertEqual(r["upgradable_count"], 3)
|
||||
|
||||
def test_hostile_strings_stay_valid_json(self):
|
||||
stub(self.bin, "hostname", "printf 'a\"b\\\\c\\tq'\n")
|
||||
self.assertEqual(self.report()["reported_hostname"], 'a"b\\cq')
|
||||
|
||||
@@ -9,14 +9,14 @@ import unittest
|
||||
from unittest import mock
|
||||
|
||||
from helpers import tempdir
|
||||
from kernelwatch import auth, cli, db
|
||||
from kernelwatch import AGENT_VERSION, auth, cli, db
|
||||
from kernelwatch.status import evaluate
|
||||
|
||||
NOW = calendar.timegm((2026, 11, 20, 12, 0, 0))
|
||||
|
||||
GOOD = {"kernel_running": "6.12.57", "kernel_newest_installed": "6.12.57", "needrestart_ksta": 1,
|
||||
"apt_lists_age_seconds": 60, "upgradable_count": 0, "security_count": 0,
|
||||
"os_pretty_name": "Debian 13", "agent_version": "0.4.1"}
|
||||
"os_pretty_name": "Debian 13", "agent_version": AGENT_VERSION}
|
||||
|
||||
|
||||
class StatusCommandTest(unittest.TestCase):
|
||||
@@ -78,7 +78,7 @@ class StatusCommandTest(unittest.TestCase):
|
||||
self.assertEqual(web["last_report_at"], "2026-11-20T11:59:00Z")
|
||||
self.assertEqual(web["last_report_age_seconds"], 60)
|
||||
self.assertEqual((web["kernel_running"], web["kernel_newest_installed"]), ("6.8.0-136", "6.8.0-138"))
|
||||
self.assertEqual((web["os"], web["agent_version"]), ("Debian 13", "0.4.1"))
|
||||
self.assertEqual((web["os"], web["agent_version"]), ("Debian 13", AGENT_VERSION))
|
||||
db_host = next(h for h in self.fleet()["hosts"] if h["name"] == "db01")
|
||||
self.assertEqual((db_host["updates_pending"], db_host["security_updates"]), (7, 3))
|
||||
self.assertEqual(db_host["reasons"], ["3 security updates pending"])
|
||||
|
||||
@@ -296,6 +296,18 @@ class EvaluateTest(unittest.TestCase):
|
||||
self.assertEqual(self.level(report(apt_lists_age_seconds=5 * 86400)).level, "update")
|
||||
self.assertEqual(self.level(report(apt_lists_age_seconds=None)).level, "update")
|
||||
|
||||
def test_failed_apt_simulation(self):
|
||||
s = self.level(report(apt_simulation_ok=False, apt_simulation_error="Unmet dependencies.",
|
||||
security_count=None, upgradable_count=None, kernel_upgradable=None))
|
||||
self.assertEqual(s.level, "update")
|
||||
self.assertEqual(s.reasons, ["apt can't work out pending updates: Unmet dependencies."])
|
||||
s = self.level(report(apt_simulation_ok=False))
|
||||
self.assertEqual(s.reasons, ["apt can't work out pending updates: no error message"])
|
||||
|
||||
def test_apt_simulation_ok_or_unreported(self):
|
||||
self.assertEqual(self.level(report(apt_simulation_ok=True)).level, "ok")
|
||||
self.assertEqual(self.level(report()).level, "ok", "agents before 0.5.0 don't send it")
|
||||
|
||||
def test_reboot_outranks_update(self):
|
||||
s = self.level(report(needrestart_ksta=3, security_count=2))
|
||||
self.assertEqual(s.level, "reboot")
|
||||
|
||||
Reference in new issue
Block a user