my-homelab-configs/tests/jeannie/test_doctor_model_lab.py

83 lines
4.0 KiB
Python

import importlib.util
import json
import tempfile
import unittest
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[2]
spec = importlib.util.spec_from_file_location("doctor_model_lab", ROOT / "scripts/doctor-model-lab.py")
lab = importlib.util.module_from_spec(spec)
spec.loader.exec_module(lab)
class DoctorModelLabTests(unittest.TestCase):
def test_desired_state_cases_distinguish_known_from_unknown(self):
cases = dict(lab.CASES)
self.assertIn("intentionally", cases["runner_disabled"])
self.assertIn("intentional is unknown", cases["runner_intent_unknown"])
self.assertIn("explicitly marks this service disabled", cases["stopped_compose_intended"])
self.assertIn("Desired runtime state and reason for the stop are unknown", cases["stopped_compose_unknown"])
self.assertIn("current time is 16:30", cases["schedule_verified"])
self.assertIn("were not verified", cases["schedule_unknown"])
def test_fenced_json_still_requires_evidence_validation(self):
with tempfile.TemporaryDirectory() as directory:
name, tsv = lab.CASES[0]
report = lab.case_report(name, tsv, Path(directory))
answer = {"observed_facts": ["C01"], "possible_causes": [],
"missing_evidence": [], "next_command": None}
fenced = "```json\n" + json.dumps(answer) + "\n```"
lab.doctor.validate(lab.parse_answer(fenced), report)
answer["next_command"] = "sudo reboot"
with self.assertRaises(ValueError):
lab.doctor.validate(lab.parse_answer("```json\n" + json.dumps(answer) + "\n```"), report)
with self.assertRaises(ValueError):
lab.parse_answer("explanation\n" + fenced)
def test_cases_use_the_doctor_evidence_contract(self):
with tempfile.TemporaryDirectory() as directory:
for name, tsv in lab.CASES:
report = lab.case_report(name, tsv, Path(directory))
self.assertEqual(report["checks"][0]["id"], "C01")
self.assertTrue(report["checks"][0]["read_only_command"])
lab.doctor.validate({"observed_facts": ["C01"], "possible_causes": [],
"missing_evidence": [], "next_command": None}, report)
def test_unknown_citation_and_command_are_rejected(self):
with tempfile.TemporaryDirectory() as directory:
name, tsv = lab.CASES[0]
report = lab.case_report(name, tsv, Path(directory))
response = {"observed_facts": ["C99"], "possible_causes": [],
"missing_evidence": [], "next_command": None}
with self.assertRaises(ValueError):
lab.doctor.validate(response, report)
response.update(observed_facts=["C01"], next_command="sudo reboot")
with self.assertRaises(ValueError):
lab.doctor.validate(response, report)
def test_memory_preflight_does_not_start_server(self):
with mock.patch.object(lab, "available_bytes", return_value=lab.START_AVAILABLE - 1), \
mock.patch.object(lab.subprocess, "Popen") as launch:
server = lab.ScopedServer([], {}, "", "", Path("/unused"), "test")
with self.assertRaisesRegex(RuntimeError, "9 GiB"):
server.start()
launch.assert_not_called()
def test_repeats_are_bounded_before_any_server_starts(self):
with mock.patch.object(lab, "ScopedServer") as server:
with self.assertRaisesRegex(RuntimeError, "between 1 and 5"):
lab.suite_run(6)
server.assert_not_called()
def test_synthetic_reports_are_private(self):
with tempfile.TemporaryDirectory() as directory:
name, tsv = lab.CASES[0]
lab.case_report(name, tsv, Path(directory))
self.assertEqual((Path(directory) / (name + ".tsv")).stat().st_mode & 0o777, 0o600)
if __name__ == "__main__":
unittest.main()