# 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()