Alert when Livepatch is broken or has stopped reporting

The alert check now also notifies, once per problem, when a host's Livepatch has
been unhealthy for livepatch_alert_after_hours (default 6), or when a host that
used to report Livepatch no longer does (a dead daemon leaves the agent with
nothing to send). A livepatch_alerts table records which hosts have reported
Livepatch, when the current problem began and whether it was notified, so hosts
without Livepatch never alert and restarts do not repeat messages. Stale hosts
are skipped.

livepatch_problem() is now the single test for unhealthy, shared with the cover
rule for pending reboots, and names the check state when that is the problem.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-30 12:18:39 -04:00
1 parent 203c729e14
commit 650806bfce
7 files changed
+346 -6

No files matched your search

+210
View File
@@ -391,6 +391,216 @@ class BackupFailureAlertTest(unittest.TestCase):
self.assertEqual(self.publish.call_count, 1)
class LivepatchAlertsTest(unittest.TestCase):
"""Notify when Livepatch is unhealthy, or stops being reported, for too long."""
HEALTHY = {"livepatch_state": "applied", "livepatch_check_state": "checked",
"livepatch_supported": "supported", "livepatch_upgrade_required": "2099-01-01",
"livepatch_fix_count": 2}
def setUp(self):
self.tmp = tempdir()
self.addCleanup(self.tmp.cleanup)
self.cfg = temp_config(self.tmp.name, ntfy_url="https://ntfy.example.test/fleet",
status_alert_after_hours=0, livepatch_alert_after_hours=6.0)
db.init(self.cfg.db_path)
self.conn = db.connect(self.cfg.db_path)
self.addCleanup(self.conn.close)
self.now = NOW
clock = mock.patch("kernelwatch.alerts.time.time", side_effect=lambda: self.now)
clock.start()
self.addCleanup(clock.stop)
publisher = mock.patch("kernelwatch.alerts.publish")
self.publish = publisher.start()
self.addCleanup(publisher.stop)
stack = contextlib.ExitStack()
self.addCleanup(stack.close)
stack.enter_context(contextlib.redirect_stdout(io.StringIO()))
self.errors = stack.enter_context(contextlib.redirect_stderr(io.StringIO()))
db.add_host(self.conn, "web01", auth.token_hash(auth.new_token()))
def report(self, host="web01", livepatch=True, **fields):
payload = {"kernel_running": "6.8.0-136-generic", "apt_lists_age_seconds": 60}
if livepatch:
payload.update(self.HEALTHY)
payload.update(fields)
self.conn.execute("INSERT INTO reports (host, received_at, payload) VALUES (?, ?, ?)",
(host, self.now, json.dumps(payload)))
def hours(self, n):
self.now += int(n * 3600)
def run_check(self):
self.assertEqual(alerts.check(self.cfg), 0)
def titles(self):
return [c.args[1] for c in self.publish.call_args_list]
def test_unhealthy_state_alerts_once_after_the_grace_period(self):
self.report(livepatch_state="apply-failed")
self.run_check()
self.hours(5)
self.report(livepatch_state="apply-failed")
self.run_check()
self.publish.assert_not_called()
self.hours(1.1)
self.report(livepatch_state="apply-failed")
self.run_check()
self.assertEqual(self.titles(), ["kernelwatch: web01 Livepatch problem"])
(_, _, message), kwargs = self.publish.call_args
self.assertIn("livepatch not healthy (apply-failed, supported)", message)
self.assertIn("State: apply-failed, check: checked, supported: supported.", message)
self.assertIn("canonical-livepatch status", message)
self.assertTrue(kwargs["stale"]) # high priority
for _ in range(5): # never repeated while the same episode lasts
self.hours(1)
self.report(livepatch_state="apply-failed")
self.run_check()
self.assertEqual(self.publish.call_count, 1)
def test_each_kind_of_unhealthy_counts(self):
for fields in ({"livepatch_state": "applying"}, {"livepatch_check_state": "check-failed"},
{"livepatch_supported": "unsupported"}, {"livepatch_state": "kernel-upgrade-required"}):
with self.subTest(fields=fields):
self.publish.reset_mock()
db.remove_host(self.conn, "web01")
db.add_host(self.conn, "web01", auth.token_hash(auth.new_token()))
self.report(**fields)
self.run_check()
self.hours(7)
self.report(**fields)
self.run_check()
self.assertEqual(self.publish.call_count, 1)
def test_healthy_states_never_alert(self):
for state in ("applied", "nothing-to-apply"):
self.report(livepatch_state=state)
self.run_check()
self.hours(48)
self.report(livepatch_state=state)
self.run_check()
self.publish.assert_not_called()
def test_a_brief_problem_that_clears_never_alerts_and_resets_the_clock(self):
self.report(livepatch_check_state="check-failed")
self.run_check()
self.hours(5)
self.report()
self.run_check()
self.assertEqual(db.livepatch_alerts(self.conn)["web01"], (None, False))
self.hours(5) # a new problem starts a fresh grace period, not the old one
self.report(livepatch_check_state="check-failed")
self.run_check()
self.hours(5)
self.report(livepatch_check_state="check-failed")
self.run_check()
self.publish.assert_not_called()
def test_a_later_problem_alerts_again(self):
self.report(livepatch_state="apply-failed")
self.run_check()
self.hours(7)
self.report(livepatch_state="apply-failed")
self.run_check()
self.report()
self.run_check()
self.hours(1)
self.report(livepatch_state="apply-failed")
self.run_check()
self.hours(7)
self.report(livepatch_state="apply-failed")
self.run_check()
self.assertEqual(self.publish.call_count, 2)
def test_a_host_that_stops_reporting_livepatch_alerts(self):
self.report() # healthy: now known to use Livepatch
self.run_check()
self.assertIn("web01", db.livepatch_alerts(self.conn))
self.hours(1)
self.report(livepatch=False) # the daemon died; the agent has nothing to send
self.run_check()
self.hours(5)
self.report(livepatch=False)
self.run_check()
self.publish.assert_not_called()
self.hours(1.1)
self.report(livepatch=False)
self.run_check()
self.assertEqual(self.publish.call_count, 1)
message = self.publish.call_args.args[2]
self.assertIn("no longer being reported", message)
self.assertIn("It reported Livepatch before and no longer does", message)
def test_a_blip_of_missing_data_between_good_reports_is_forgiven(self):
self.report()
self.run_check()
self.hours(1)
self.report(livepatch=False) # e.g. snapd was restarting for one report
self.run_check()
self.hours(1)
self.report()
self.run_check()
self.hours(8)
self.report()
self.run_check()
self.publish.assert_not_called()
def test_hosts_that_never_had_livepatch_never_alert(self):
for _ in range(4):
self.report(livepatch=False)
self.run_check()
self.hours(24)
self.publish.assert_not_called()
self.assertEqual(db.livepatch_alerts(self.conn), {})
def test_stale_hosts_are_skipped(self):
self.report(livepatch_state="apply-failed")
self.run_check()
self.hours(20) # no reports since: stale, so only the stale alert speaks
self.run_check()
self.assertEqual(self.titles(), ["kernelwatch: web01 is stale"])
def test_failed_delivery_is_retried_until_it_succeeds(self):
self.report(livepatch_state="apply-failed")
self.run_check()
self.hours(7)
self.report(livepatch_state="apply-failed")
self.publish.side_effect = alerts.DeliveryError("ntfy returned HTTP 503")
self.assertEqual(alerts.check(self.cfg), 1)
self.assertEqual(db.livepatch_alerts(self.conn)["web01"][1], False)
self.assertIn("notification failed for web01", self.errors.getvalue())
self.publish.side_effect = None
self.run_check()
self.assertEqual(db.livepatch_alerts(self.conn)["web01"][1], True)
self.run_check()
self.assertEqual(self.publish.call_count, 2) # the failed try and the one that landed
def test_zero_disables_it(self):
cfg = dataclasses.replace(self.cfg, livepatch_alert_after_hours=0)
self.report(livepatch_state="apply-failed")
self.assertEqual(alerts.check(cfg), 0)
self.hours(100)
self.report(livepatch_state="apply-failed")
self.assertEqual(alerts.check(cfg), 0)
self.publish.assert_not_called()
self.assertEqual(db.livepatch_alerts(self.conn), {})
def test_removing_a_host_forgets_it(self):
self.report()
self.run_check()
db.remove_host(self.conn, "web01")
self.assertEqual(db.livepatch_alerts(self.conn), {})
def test_state_is_only_written_when_it_changes(self):
self.report()
self.run_check()
with mock.patch("kernelwatch.alerts.db.save_livepatch_alert") as save:
self.hours(1)
self.report()
self.run_check()
save.assert_not_called()
class NtfyHTTPTest(unittest.TestCase):
def setUp(self):
class Sink(BaseHTTPRequestHandler):