feat(agent): add --doctor preflight self-test mode with structured checks
This commit is contained in:
1 parent
d2fdd5be44
commit
be92bb2493
5 files changed
+311
-2
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from config import RuntimeSettings
|
||||
from agent import cli
|
||||
|
||||
|
||||
def _settings():
|
||||
return RuntimeSettings(
|
||||
node_name="n1",
|
||||
log_path="/tmp/threat-agent.log",
|
||||
state_file="/tmp/threat-agent-state.json",
|
||||
check_interval=600,
|
||||
check_jitter_pct=0.10,
|
||||
heal_verify_delay=5,
|
||||
heal_min_interval=900,
|
||||
enforcement_targets=(("ip", "crowdsec"),),
|
||||
enforcement_sets=(),
|
||||
config_path="config.yaml",
|
||||
)
|
||||
|
||||
|
||||
class AgentCliTests(unittest.TestCase):
|
||||
@patch("agent.load_runtime_settings", return_value=_settings())
|
||||
@patch("agent.run_doctor", return_value=True)
|
||||
def test_cli_doctor_returns_zero_on_success(self, _run_doctor, _settings_mock):
|
||||
code = cli(["--doctor"])
|
||||
self.assertEqual(0, code)
|
||||
|
||||
@patch("agent.main")
|
||||
def test_cli_without_doctor_runs_main(self, main_mock):
|
||||
code = cli([])
|
||||
self.assertEqual(0, code)
|
||||
main_mock.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,77 @@
|
||||
import subprocess
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from config import RuntimeSettings
|
||||
from doctor import (
|
||||
CheckResult,
|
||||
check_cscli_json,
|
||||
check_systemctl_restart_capability,
|
||||
run_doctor,
|
||||
)
|
||||
|
||||
|
||||
def _settings():
|
||||
return RuntimeSettings(
|
||||
node_name="n1",
|
||||
log_path="/tmp/threat-agent.log",
|
||||
state_file="/tmp/threat-agent-state.json",
|
||||
check_interval=600,
|
||||
check_jitter_pct=0.10,
|
||||
heal_verify_delay=5,
|
||||
heal_min_interval=900,
|
||||
enforcement_targets=(("ip", "crowdsec"),),
|
||||
enforcement_sets=(),
|
||||
config_path="config.yaml",
|
||||
)
|
||||
|
||||
|
||||
class DoctorTests(unittest.TestCase):
|
||||
@patch("doctor._run_cmd")
|
||||
def test_check_cscli_json_fail_on_invalid_json(self, run_cmd_mock):
|
||||
run_cmd_mock.return_value = subprocess.CompletedProcess(
|
||||
args=[],
|
||||
returncode=0,
|
||||
stdout="not-json",
|
||||
)
|
||||
|
||||
result = check_cscli_json()
|
||||
|
||||
self.assertEqual("FAIL", result.status)
|
||||
self.assertIn("Invalid JSON", result.detail)
|
||||
|
||||
@patch("doctor.os.geteuid", return_value=1000)
|
||||
def test_check_systemctl_restart_capability_warns_when_not_root(self, _geteuid):
|
||||
result = check_systemctl_restart_capability()
|
||||
|
||||
self.assertEqual("WARN", result.status)
|
||||
self.assertIn("Not running as root", result.detail)
|
||||
|
||||
@patch("doctor.check_systemctl_restart_capability")
|
||||
@patch("doctor.check_state_paths")
|
||||
@patch("doctor.check_nft_tables")
|
||||
@patch("doctor.check_cscli_json")
|
||||
@patch("doctor.check_runtime_settings")
|
||||
def test_run_doctor_returns_false_when_any_fail(
|
||||
self,
|
||||
check_runtime_mock,
|
||||
check_cscli_mock,
|
||||
check_nft_mock,
|
||||
check_state_mock,
|
||||
check_systemctl_mock,
|
||||
):
|
||||
check_runtime_mock.return_value = CheckResult("runtime", "PASS", "ok")
|
||||
check_cscli_mock.return_value = CheckResult("cscli", "FAIL", "bad")
|
||||
check_nft_mock.return_value = CheckResult("nft", "PASS", "ok")
|
||||
check_state_mock.return_value = CheckResult("state", "PASS", "ok")
|
||||
check_systemctl_mock.return_value = CheckResult("systemctl", "WARN", "warn")
|
||||
|
||||
lines = []
|
||||
ok = run_doctor(_settings(), output=lines.append)
|
||||
|
||||
self.assertFalse(ok)
|
||||
self.assertTrue(any("Summary:" in line for line in lines))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user