CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s
A filesystem that fills up while apt downloads or dpkg unpacks leaves the upgrade half-done and apt broken, the same failure as a full /boot but on / or /var. Nothing caught it until the host's updates stopped. The agent (0.11.0) now works out what the pending upgrades need. A simulation doesn't print apt's "Need to get" or "additional disk space" lines, so it sums the package records from apt-cache show for each version the simulation would install: Size for each .deb not already in /var/cache/apt/archives, and the growth in Installed-Size over the installed versions (from dpkg-query). Removals aren't subtracted, so it errs high. It also reports free space and the mount point of the filesystems holding /var/cache/apt/archives and /usr. The server puts a host in update when the upgrades would leave less than upgrade_min_free_mb (default 100, 0 disables) on either filesystem. The download and the install both count when they're on the same one. With nothing pending there is nothing to check, so this doesn't turn into general disk monitoring. The host page gets a "Pending upgrades size" row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
7.7 KiB
Python
160 lines
7.7 KiB
Python
import os
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from helpers import REPO, tempdir
|
|
from kernelwatch import auth, config
|
|
from kernelwatch.report import ReportError, sanitize
|
|
|
|
|
|
class SanitizeTest(unittest.TestCase):
|
|
def test_drops_unknown_and_truncates(self):
|
|
out = sanitize({"kernel_running": "6.8.0-85-generic", "evil": "x", "os_id": "u" * 1000})
|
|
self.assertNotIn("evil", out)
|
|
self.assertEqual(len(out["os_id"]), 256)
|
|
|
|
def test_requires_kernel_running(self):
|
|
with self.assertRaises(ReportError):
|
|
sanitize({"os_id": "debian"})
|
|
|
|
def test_type_checks(self):
|
|
for field, bad in (("security_count", "3"), ("security_count", True),
|
|
("reboot_required", 1), ("kernels_installed", ["a", 2])):
|
|
with self.subTest(field=field, value=bad), self.assertRaises(ReportError):
|
|
sanitize({"kernel_running": "x", field: bad})
|
|
|
|
def test_nulls_allowed(self):
|
|
self.assertIsNone(sanitize({"kernel_running": "x", "needrestart_ksta": None})["needrestart_ksta"])
|
|
|
|
def test_not_an_object(self):
|
|
with self.assertRaises(ReportError):
|
|
sanitize([1, 2])
|
|
|
|
|
|
class AuthTest(unittest.TestCase):
|
|
def test_password_roundtrip(self):
|
|
h = auth.hash_password("correct horse battery staple")
|
|
self.assertTrue(auth.verify_password("correct horse battery staple", h))
|
|
self.assertFalse(auth.verify_password("wrong", h))
|
|
|
|
def test_garbage_hash(self):
|
|
self.assertFalse(auth.verify_password("x", "nonsense"))
|
|
self.assertFalse(auth.verify_password("x", "scrypt$a$b$c$d$e"))
|
|
|
|
def test_tokens_unique(self):
|
|
self.assertNotEqual(auth.new_token(), auth.new_token())
|
|
|
|
|
|
class ConfigTest(unittest.TestCase):
|
|
def write(self, tmp, text):
|
|
path = os.path.join(tmp, "server.toml")
|
|
with open(path, "w") as f:
|
|
f.write(text)
|
|
return path
|
|
|
|
def test_load(self):
|
|
with tempdir() as tmp:
|
|
cfg = config.load(self.write(tmp, "listen_port = 9000\nstale_after_hours = 2\n"))
|
|
self.assertEqual(cfg.listen_port, 9000)
|
|
self.assertEqual(cfg.stale_after_hours, 2.0)
|
|
|
|
def test_rejects_unknown_and_wrong_types(self):
|
|
with tempdir() as tmp:
|
|
with self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, "listen_prot = 9000\n"))
|
|
with self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, 'listen_port = "9000"\n'))
|
|
with self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, "secure_cookies = 1\n"))
|
|
|
|
def test_report_min_interval_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.load(self.write(tmp, "report_min_interval_seconds = 0\n"))
|
|
.report_min_interval_seconds, 0)
|
|
for bad in ("-1", "1.5", '"30"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"report_min_interval_seconds = {bad}\n"))
|
|
|
|
def test_livepatch_alert_hours_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().livepatch_alert_after_hours, 6.0)
|
|
self.assertEqual(config.load(self.write(tmp, "livepatch_alert_after_hours = 0\n"))
|
|
.livepatch_alert_after_hours, 0)
|
|
for bad in ("-1", "inf", "nan", '"6"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"livepatch_alert_after_hours = {bad}\n"))
|
|
|
|
def test_status_alert_hours_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.load(self.write(tmp, "status_alert_after_hours = 0\n"))
|
|
.status_alert_after_hours, 0)
|
|
for bad in ("-1", "inf", "nan"):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"status_alert_after_hours = {bad}\n"))
|
|
|
|
def test_boot_min_free_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().boot_min_free_mb, 200)
|
|
self.assertEqual(config.load(self.write(tmp, "boot_min_free_mb = 0\n")).boot_min_free_mb, 0)
|
|
for bad in ("-1", "1.5", '"200"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"boot_min_free_mb = {bad}\n"))
|
|
|
|
def test_upgrade_min_free_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().upgrade_min_free_mb, 100)
|
|
self.assertEqual(config.load(self.write(tmp, "upgrade_min_free_mb = 0\n")).upgrade_min_free_mb, 0)
|
|
for bad in ("-1", "1.5", '"100"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"upgrade_min_free_mb = {bad}\n"))
|
|
|
|
def test_os_eol_warn_days_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().os_eol_warn_days, 90)
|
|
self.assertEqual(config.load(self.write(tmp, "os_eol_warn_days = 0\n")).os_eol_warn_days, 0)
|
|
for bad in ("-1", "1.5"):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"os_eol_warn_days = {bad}\n"))
|
|
|
|
def test_unattended_upgrades_stale_days_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().unattended_upgrades_stale_after_days, 3.0)
|
|
self.assertEqual(config.load(self.write(tmp, "unattended_upgrades_stale_after_days = 0\n"))
|
|
.unattended_upgrades_stale_after_days, 0)
|
|
for bad in ("-1", "inf", "nan", '"3"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"unattended_upgrades_stale_after_days = {bad}\n"))
|
|
|
|
def test_security_repo_stale_days_validated(self):
|
|
with tempdir() as tmp:
|
|
self.assertEqual(config.Config().security_repo_stale_after_days, 7.0)
|
|
self.assertEqual(config.load(self.write(tmp, "security_repo_stale_after_days = 0\n"))
|
|
.security_repo_stale_after_days, 0)
|
|
for bad in ("-1", "inf", "nan", '"7"'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, f"security_repo_stale_after_days = {bad}\n"))
|
|
|
|
def test_fallback_parser_matches_tomllib(self):
|
|
"""Python 3.10 has no tomllib; the fallback must read the same configs."""
|
|
if config.tomllib is None:
|
|
self.skipTest("tomllib not available to compare against")
|
|
samples = [open(os.path.join(REPO, "server", "server.toml.example")).read(),
|
|
"db_path = '/srv/kw.db' # literal\nlisten_port = 8_080\n"
|
|
"stale_after_hours = 2.5\nsecure_cookies = false\nlisten_host = \"0.0.0.0\"\n"]
|
|
for text in samples:
|
|
with self.subTest(text=text[:40]):
|
|
self.assertEqual(config._parse_flat_toml(text, "x"), config.tomllib.loads(text))
|
|
|
|
def test_load_without_tomllib(self):
|
|
with tempdir() as tmp, mock.patch.object(config, "tomllib", None):
|
|
cfg = config.load(self.write(tmp, 'listen_port = 9000 # comment\ndb_path = "/x/kw.db"\n'))
|
|
self.assertEqual((cfg.listen_port, cfg.db_path), (9000, "/x/kw.db"))
|
|
for bad in ("[server]\n", "listen_port = [1]\n", 'db_path = "a\\b"\n',
|
|
"listen_port = 1\nlisten_port = 2\n", 'db_path = "unterminated\n'):
|
|
with self.subTest(bad=bad), self.assertRaises(SystemExit):
|
|
config.load(self.write(tmp, bad))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|