Add `enodia-sentinel status [--json]` — a local health and alert summary: daemon running state, heartbeat freshness, per-severity alert counts, last alert time, and eBPF state. Reuses web.daemon_status (the same data behind /api/status), so the CLI and dashboard never diverge. Exit code reflects health (0 = running and fresh, 1 = down or stale), so it doubles as a cron/monitoring probe. Three CLI tests drive it against a temp log_dir; no daemon or root needed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""`enodia-sentinel status` tests — drive the CLI against a temp log_dir, so no
|
|
daemon or root is needed. Exercises the health-gate exit code and --json."""
|
|
import io
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from contextlib import redirect_stdout
|
|
from pathlib import Path
|
|
|
|
from enodia_sentinel.cli import main
|
|
|
|
|
|
class TestStatusCommand(unittest.TestCase):
|
|
def setUp(self):
|
|
self.dir = tempfile.TemporaryDirectory()
|
|
os.environ["ENODIA_LOG_DIR"] = self.dir.name
|
|
self.tmp = Path(self.dir.name)
|
|
|
|
def tearDown(self):
|
|
os.environ.pop("ENODIA_LOG_DIR", None)
|
|
self.dir.cleanup()
|
|
|
|
def _run(self, *args):
|
|
buf = io.StringIO()
|
|
with redirect_stdout(buf):
|
|
code = main(["status", *args])
|
|
return code, buf.getvalue()
|
|
|
|
def test_no_heartbeat_is_unhealthy(self):
|
|
code, out = self._run()
|
|
self.assertEqual(code, 1) # daemon down + no heartbeat
|
|
self.assertIn("NOT running", out)
|
|
self.assertIn("none", out)
|
|
|
|
def test_stale_heartbeat_is_unhealthy(self):
|
|
(self.tmp / "heartbeat").write_text("1") # epoch 1 = ancient
|
|
code, _ = self._run()
|
|
self.assertEqual(code, 1)
|
|
|
|
def test_json_shape(self):
|
|
code, out = self._run("--json")
|
|
data = json.loads(out)
|
|
for key in ("version", "running", "total_alerts", "counts",
|
|
"heartbeat_age", "heartbeat_stale", "host"):
|
|
self.assertIn(key, data)
|
|
self.assertEqual(code, 1) # nothing running in the temp dir
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|