92 lines
4.8 KiB
Python
92 lines
4.8 KiB
Python
"""Evidence-bound explanation regressions; run on the Debian host."""
|
|
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
SPEC = importlib.util.spec_from_file_location("report_ai_explain", Path(__file__).resolve().parents[2] / "scripts/report-ai-explain.py")
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
class EvidenceExplanationTests(unittest.TestCase):
|
|
def test_service_name_alone_is_not_a_cause(self):
|
|
report = {"checks": [{"id": "C01", "check": "homelab-arr/suwayomi (arr-suwayomi)",
|
|
"summary": "Exited", "read_only_command": None}]}
|
|
answer = {"observed_facts": ["C01"], "possible_causes": [
|
|
{"hypothesis": "homelab-arr/suwayomi", "check_ids": ["C01"]}],
|
|
"missing_evidence": [], "next_command": None}
|
|
with self.assertRaisesRegex(ValueError, "repeats a check name"):
|
|
MODULE.validate(answer, report)
|
|
|
|
def setUp(self):
|
|
self.report = {"checks": [{"id": "C01", "area": "Gitea", "check": "HTTP", "summary": "HTTP 503",
|
|
"read_only_command": "./jeannie doctor-gitea"}]}
|
|
self.answer = {"observed_facts": ["C01"], "possible_causes": [{"hypothesis": "A backend may be unavailable.",
|
|
"check_ids": ["C01"]}], "missing_evidence": ["Backend health"],
|
|
"next_command": "./jeannie doctor-gitea"}
|
|
|
|
def test_facts_are_rendered_from_report(self):
|
|
output = io.StringIO()
|
|
with contextlib.redirect_stdout(output):
|
|
MODULE.render(MODULE.validate(self.answer, self.report), self.report)
|
|
self.assertIn("Gitea / HTTP: HTTP 503", output.getvalue())
|
|
self.assertIn("./jeannie doctor-gitea", output.getvalue())
|
|
|
|
def test_unknown_checks_and_commands_are_rejected(self):
|
|
for command in ("./jeannie deploy-gitea", "./jeannie doctor-gitea; rm -rf /", "kubectl get nodes"):
|
|
answer = dict(self.answer, next_command=command)
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(answer, self.report)
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(dict(self.answer, observed_facts=["C99"]), self.report)
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(dict(self.answer, possible_causes=[{"hypothesis": "Unknown", "check_ids": ["C99"]}]), self.report)
|
|
|
|
def test_commands_in_prose_and_extra_fields_are_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(dict(self.answer, missing_evidence=["Run kubectl delete pod x"]), self.report)
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(dict(self.answer, extra="unexpected"), self.report)
|
|
|
|
def test_progressing_zero_replicas_cannot_be_claimed_healthy(self):
|
|
report = {"checks": [{"id": "C01", "area": "runner", "check": "state",
|
|
"summary": "health=Progressing", "detail": "desired=0 ready=0",
|
|
"read_only_command": None}]}
|
|
answer = {"observed_facts": ["C01"], "possible_causes": [
|
|
{"hypothesis": "The runner is healthy and running", "check_ids": ["C01"]}],
|
|
"missing_evidence": [], "next_command": None}
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(answer, report)
|
|
|
|
def test_report_excludes_mutating_fixes_and_redacts_token(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
rows = Path(directory) / "rows"
|
|
rows.write_text("warn\tGitea\tHTTP\tBearer token123\tdetail\t./jeannie deploy-gitea\t\t\n")
|
|
report = MODULE.report_from_tsv(rows, "status", "k3s", "k3s")
|
|
self.assertIsNone(report["checks"][0]["read_only_command"])
|
|
self.assertNotIn("token123", json.dumps(report))
|
|
|
|
def test_ollama_receives_schema_and_output_is_validated(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
rows = Path(directory) / "rows"
|
|
rows.write_text("fail\tGitea\tHTTP\tHTTP 503\tdetail\t./jeannie doctor-gitea\t\t\n")
|
|
response = io.BytesIO(json.dumps({"response": json.dumps(self.answer)}).encode())
|
|
output = io.StringIO()
|
|
with patch.dict(os.environ, {"LAB_REPORT_AI_PROVIDER": "ollama"}), \
|
|
patch.object(sys, "argv", ["report-ai-explain", str(rows), "status"]), \
|
|
patch.object(MODULE.urllib.request, "urlopen", return_value=response) as request, \
|
|
contextlib.redirect_stdout(output):
|
|
MODULE.main()
|
|
payload = json.loads(request.call_args.args[0].data)
|
|
self.assertEqual(payload["format"]["type"], "object")
|
|
self.assertIn("Gitea / HTTP: HTTP 503", output.getvalue())
|