Add notify-backup-failure command

Sends a high-priority ntfy message that the nightly backup failed, naming the
newest backup still on disk. It exits 1 if the message can't be delivered and 0
(with a note on stderr) when ntfy isn't configured.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-30 11:12:22 -04:00
1 parent 6652a4d795
commit 8a9a3ca56d
4 files changed
+90

No files matched your search

+50
View File
@@ -341,6 +341,56 @@ class StatusAlertsTest(unittest.TestCase):
self.assertEqual(db.status_alerts(self.conn), {})
class BackupFailureAlertTest(unittest.TestCase):
def setUp(self):
self.tmp = tempdir()
self.addCleanup(self.tmp.cleanup)
self.dir = os.path.join(self.tmp.name, "backups")
self.cfg = temp_config(self.tmp.name, ntfy_url="https://ntfy.example.test/fleet",
backup_dir=self.dir)
stack = contextlib.ExitStack()
self.addCleanup(stack.close)
self.publish = stack.enter_context(mock.patch("kernelwatch.alerts.publish"))
self.output = stack.enter_context(contextlib.redirect_stdout(io.StringIO()))
self.errors = stack.enter_context(contextlib.redirect_stderr(io.StringIO()))
def touch(self, *names):
os.makedirs(self.dir, exist_ok=True)
for name in names:
open(os.path.join(self.dir, name), "w").close()
def test_sends_an_urgent_notification_naming_the_newest_backup(self):
self.touch("kernelwatch-2026-11-18.db", "kernelwatch-2026-11-19.db", "kernelwatch-2026-11-20.db.tmp-4")
self.assertEqual(alerts.notify_backup_failure(self.cfg), 0)
(cfg, title, message), kwargs = self.publish.call_args
self.assertEqual(title, "kernelwatch: nightly backup failed")
self.assertIn("Newest backup still on disk: kernelwatch-2026-11-19.db.", message)
self.assertIn("journalctl -u kernelwatch-backup", message)
self.assertTrue(kwargs["stale"]) # high priority
def test_says_when_there_is_no_backup_at_all(self):
self.assertEqual(alerts.notify_backup_failure(self.cfg), 0)
self.assertIn("Newest backup still on disk: none.", self.publish.call_args.args[2])
def test_delivery_failure_is_reported_and_fails_the_unit(self):
self.publish.side_effect = alerts.DeliveryError("ntfy returned HTTP 503")
self.assertEqual(alerts.notify_backup_failure(self.cfg), 1)
self.assertIn("ntfy returned HTTP 503", self.errors.getvalue())
def test_without_ntfy_it_says_so_and_does_not_fail(self):
cfg = dataclasses.replace(self.cfg, ntfy_url="")
self.assertEqual(alerts.notify_backup_failure(cfg), 0)
self.publish.assert_not_called()
self.assertIn("ntfy_url is not configured", self.errors.getvalue())
def test_cli_command(self):
path = os.path.join(self.tmp.name, "server.toml")
with open(path, "w") as f:
f.write(f'ntfy_url = "https://ntfy.example.test/fleet"\nbackup_dir = "{self.dir}"\n')
self.assertEqual(cli.main(["-c", path, "notify-backup-failure"]), 0)
self.assertEqual(self.publish.call_count, 1)
class NtfyHTTPTest(unittest.TestCase):
def setUp(self):
class Sink(BaseHTTPRequestHandler):