59 lines
2.6 KiB
Python
59 lines
2.6 KiB
Python
import importlib.util
|
|
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_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()
|