81 lines
4.3 KiB
Python
81 lines
4.3 KiB
Python
"""Holmes evidence and validation regressions; run on the Debian host."""
|
|
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
SPEC = importlib.util.spec_from_file_location("holmes_evidence", Path(__file__).resolve().parents[2] / "scripts/holmes-evidence.py")
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
class HolmesEvidenceTests(unittest.TestCase):
|
|
def test_single_json_fence_is_accepted_but_surrounding_prose_is_not(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "answer"
|
|
path.write_text('```json\n{"observed_facts": []}\n```\n')
|
|
self.assertEqual(MODULE.parse_response(path), {"observed_facts": []})
|
|
path.write_text('explanation\n```json\n{}\n```')
|
|
with self.assertRaises(ValueError):
|
|
MODULE.parse_response(path)
|
|
|
|
def test_focused_report_preserves_profile_and_pod_event_evidence(self):
|
|
with tempfile.TemporaryDirectory() as directory, patch.dict(os.environ, {
|
|
"LAB_CLUSTER_PROFILE": "k3s", "LAB_CLUSTER_RUNTIME": "k3s"}):
|
|
path = Path(directory)
|
|
rows = path / "rows.tsv"
|
|
rows.write_text("warn\tKubernetes evidence\tn8n application details\tUnavailable\t"
|
|
"pod n8n phase=Pending event Warning FailedMount volume unavailable\t"
|
|
"kubectl get pods -A -o wide\t\t\n")
|
|
MODULE.prepare_focused(path, rows, "Doctor n8n")
|
|
report = json.loads((path / "holmes-evidence.json").read_text())
|
|
prompt = (path / "holmes-prompt.txt").read_text()
|
|
self.assertEqual(report["profile"], "k3s")
|
|
self.assertIn("FailedMount", report["checks"][0]["detail"])
|
|
self.assertIn("Do not call tools", prompt)
|
|
self.assertEqual(report["checks"][0]["read_only_command"], "kubectl get pods -A -o wide")
|
|
|
|
def test_selected_profile_and_failed_collection_are_not_misrepresented(self):
|
|
with tempfile.TemporaryDirectory() as directory, patch.dict(os.environ, {
|
|
"LAB_CLUSTER_PROFILE": "k3s", "LAB_CLUSTER_RUNTIME": "k3s",
|
|
"LAB_CLUSTER_CONTROL_PLANE_NODE_NAME": "opi5plus",
|
|
"LAB_CLUSTER_CONTROL_PLANE_LAN_IP": "192.0.2.80",
|
|
}):
|
|
path = Path(directory)
|
|
(path / "api-readyz.txt").write_text("ok\n")
|
|
(path / "nodes.txt").write_text("connection refused\n")
|
|
(path / "pods.txt").write_text("forbidden\n")
|
|
(path / "events.txt").write_text("No resources found\n")
|
|
MODULE.prepare(path, 0, 1, 1, 0)
|
|
report = json.loads((path / "holmes-evidence.json").read_text())
|
|
prompt = (path / "holmes-prompt.txt").read_text()
|
|
self.assertEqual((report["profile"], report["runtime"], report["control_plane_node"]), ("k3s", "k3s", "opi5plus"))
|
|
self.assertIn("state unknown", report["checks"][1]["summary"])
|
|
self.assertEqual(report["checks"][2]["status"], "unknown")
|
|
self.assertNotIn("Kubernetes runs on Pimox VMs", prompt)
|
|
|
|
def test_raw_output_is_rejected_for_invented_citation_or_command(self):
|
|
report = {"checks": [{"id": "C01", "area": "Kubernetes", "check": "Nodes", "summary": "node not ready",
|
|
"read_only_command": "./jeannie doctor-cluster"}]}
|
|
answer = {"observed_facts": ["C01"], "possible_causes": [], "missing_evidence": [],
|
|
"next_command": "./jeannie doctor-cluster"}
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory)
|
|
(path / "evidence.json").write_text(json.dumps(report))
|
|
(path / "raw.json").write_text(json.dumps(answer))
|
|
output = io.StringIO()
|
|
with contextlib.redirect_stdout(output):
|
|
MODULE.validate(path / "evidence.json", path / "raw.json")
|
|
self.assertIn("node not ready", output.getvalue())
|
|
for invalid in (dict(answer, observed_facts=["C99"]), dict(answer, next_command="kubectl delete pod x")):
|
|
(path / "raw.json").write_text(json.dumps(invalid))
|
|
with self.assertRaises(ValueError):
|
|
MODULE.validate(path / "evidence.json", path / "raw.json")
|