Report unattended-upgrades state from the agent
The agent now sends whether unattended-upgrades is enabled (package installed, not just its leftover config, and APT::Periodic::Unattended- Upgrade set and non-zero), the age of apt.systemd.daily's upgrade-stamp (touched only when unattended-upgrade exits 0), the age of unattended-upgrade's own stamp (written whenever a run gets far enough, even if it then fails) and the packages it keeps back. Nothing reads them yet. Agent 0.8.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6cbc9665d8
commit
df097c9da3
4 files changed
+104
-3
No files matched your search
+60
-1
@@ -60,10 +60,14 @@ class AgentCase(unittest.TestCase):
|
||||
'-v) echo "#1 SMP PREEMPT_DYNAMIC Debian 6.12.48-1";; esac\n')
|
||||
stub(self.bin, "hostname", "echo web01.example\n")
|
||||
# The full-database query (unfinished packages) prints $KERNELWATCH_ROOT/dpkg-all.
|
||||
stub(self.bin, "dpkg-query", 'case "$*" in *binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
|
||||
# The unattended-upgrades query prints $KERNELWATCH_ROOT/dpkg-uu (its status, e.g. "ii ").
|
||||
stub(self.bin, "dpkg-query", 'case "$*" in *" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
|
||||
'*binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
|
||||
'*Version*) echo "ii 6.12.48-2";; *) cat <<\'X\'\n'
|
||||
f"{DPKG_DEBIAN}X\n;; esac\n")
|
||||
self.stub_df("/dev/sda1 999320 187364 743144 21% /boot\n")
|
||||
# `apt-config dump KEY` prints $KERNELWATCH_ROOT/apt-config (nothing: the key is unset).
|
||||
stub(self.bin, "apt-config", 'cat "$KERNELWATCH_ROOT/apt-config" 2>/dev/null; exit 0\n')
|
||||
stub(self.bin, "apt-get", f"cat <<'X'\n{APT_SIM_DEBIAN}X\n")
|
||||
|
||||
def tearDown(self):
|
||||
@@ -212,6 +216,61 @@ class AgentReportTest(AgentCase):
|
||||
r = self.report()
|
||||
self.assertEqual((r["ubuntu_pro_attached"], r["ubuntu_esm_infra"]), (True, None))
|
||||
|
||||
def write_root(self, path, text="", age=None):
|
||||
path = os.path.join(self.root, path)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w") as f:
|
||||
f.write(text)
|
||||
if age is not None:
|
||||
t = time.time() - age
|
||||
os.utime(path, (t, t))
|
||||
|
||||
def uu(self, status="ii ", setting=None):
|
||||
self.write_root("dpkg-uu", status)
|
||||
if setting is not None:
|
||||
self.write_root("apt-config", f'APT::Periodic::Unattended-Upgrade "{setting}";\n')
|
||||
|
||||
def test_unattended_upgrades_enabled(self):
|
||||
self.uu(setting="1")
|
||||
self.write_root("var/lib/apt/periodic/upgrade-stamp", age=6 * 3600)
|
||||
self.write_root("var/lib/apt/periodic/unattended-upgrades-stamp", age=6 * 3600)
|
||||
r = self.report()
|
||||
self.assertEqual(r["unattended_upgrades"], "enabled")
|
||||
self.assertAlmostEqual(r["unattended_upgrades_success_age_seconds"], 6 * 3600, delta=60)
|
||||
self.assertAlmostEqual(r["unattended_upgrades_run_age_seconds"], 6 * 3600, delta=60)
|
||||
self.assertEqual(r["unattended_upgrades_kept_back"], [])
|
||||
|
||||
def test_unattended_upgrades_interval_other_than_1_counts_as_enabled(self):
|
||||
for setting in ("7", "always"):
|
||||
with self.subTest(setting=setting):
|
||||
self.uu(setting=setting)
|
||||
self.assertEqual(self.report()["unattended_upgrades"], "enabled")
|
||||
|
||||
def test_unattended_upgrades_switched_off(self):
|
||||
for setting in ("0", ""):
|
||||
with self.subTest(setting=setting):
|
||||
self.uu(setting=setting)
|
||||
self.assertEqual(self.report()["unattended_upgrades"], "disabled")
|
||||
os.remove(os.path.join(self.root, "apt-config"))
|
||||
self.assertEqual(self.report()["unattended_upgrades"], "disabled", "setting absent")
|
||||
|
||||
def test_unattended_upgrades_removed_but_config_left(self):
|
||||
# dpkg state rc keeps /etc/apt/apt.conf.d/20auto-upgrades, so the setting still reads 1.
|
||||
self.uu(status="rc ", setting="1")
|
||||
self.assertEqual(self.report()["unattended_upgrades"], "not-installed")
|
||||
|
||||
def test_unattended_upgrades_not_installed(self):
|
||||
r = self.report()
|
||||
self.assertEqual(r["unattended_upgrades"], "not-installed")
|
||||
self.assertIsNone(r["unattended_upgrades_success_age_seconds"])
|
||||
self.assertIsNone(r["unattended_upgrades_run_age_seconds"])
|
||||
|
||||
def test_unattended_upgrades_kept_back(self):
|
||||
self.uu(setting="1")
|
||||
self.write_root("var/lib/unattended-upgrades/kept-back", "openssl libssl3t64 linux-image-amd64")
|
||||
self.assertEqual(self.report()["unattended_upgrades_kept_back"],
|
||||
["libssl3t64", "linux-image-amd64", "openssl"])
|
||||
|
||||
def test_every_reported_field_is_accepted_by_the_server(self):
|
||||
from kernelwatch.report import FIELDS
|
||||
self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n")
|
||||
|
||||
Reference in new issue
Block a user