285 lines
13 KiB
Python
285 lines
13 KiB
Python
import json
|
|
import subprocess
|
|
import sys
|
|
import os
|
|
import pty
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
FILTER = ROOT / "scripts" / "doctor-filter.py"
|
|
APP = ROOT / "scripts" / "doctor-application.py"
|
|
DOCTOR = ROOT / "scripts" / "doctor-focus"
|
|
RPI_OS = ROOT / "scripts" / "doctor-rpi-os.py"
|
|
RUNNER_REPORT = ROOT / "scripts" / "doctor-runner-report"
|
|
NARRATIVE = ROOT / "scripts" / "doctor-narrative.py"
|
|
|
|
|
|
def run_filter(item, rows):
|
|
return subprocess.run([sys.executable, str(FILTER), item], input=rows,
|
|
text=True, capture_output=True)
|
|
|
|
|
|
class DoctorFocusTests(unittest.TestCase):
|
|
def test_k3s_storage_repair_is_refused_through_remote_api(self):
|
|
code = r'''
|
|
source "$1"
|
|
require_debian_server() { :; }
|
|
record_change_journal() { printf 'unexpected journal\n'; }
|
|
python3() { printf 'unexpected repair\n'; }
|
|
JEANNIE_REMOTE_SERVER=true doctor_focus opi5 --repair-k3s
|
|
'''
|
|
result = subprocess.run(["bash", "-c", code, "test", str(DOCTOR)], text=True, capture_output=True)
|
|
self.assertEqual(result.returncode, 2)
|
|
self.assertIn("requires the local Debian CLI", result.stderr)
|
|
self.assertNotIn("unexpected", result.stdout)
|
|
|
|
def test_narrative_does_not_invent_or_execute_a_fix(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
rows = Path(temp) / "rows"
|
|
rows.write_text("warn\tService\tContainer\tUnavailable\tconnection refused\tdeploy-service\t\t\n")
|
|
result = subprocess.run([sys.executable, str(NARRATIVE), str(rows), "Doctor"],
|
|
capture_output=True, text=True, check=True)
|
|
self.assertIn("Unavailable", result.stdout)
|
|
self.assertIn("connection refused", result.stdout)
|
|
self.assertIn("No specific automatic mitigation", result.stdout)
|
|
self.assertNotIn("deploy-service", result.stdout)
|
|
|
|
def test_stopped_debian_container_has_a_safe_specific_next_check(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
rows = Path(temp) / "rows"
|
|
rows.write_text("warn\tDebian Compose\tlab/media (media)\tExited (143); desired state unknown\t\t\t\t\n")
|
|
result = subprocess.run([sys.executable, str(NARRATIVE), str(rows), "Doctor"],
|
|
capture_output=True, text=True, check=True)
|
|
self.assertIn("whether it should be running is not recorded", result.stdout)
|
|
self.assertIn("docker inspect --format", result.stdout)
|
|
self.assertIn(" media", result.stdout)
|
|
|
|
def runner_report(self, service_state):
|
|
code = r'''
|
|
source "$1"
|
|
kubectl() {
|
|
case "$*" in
|
|
*"get application"*) printf 'Progressing Synced abc123' ;;
|
|
*"get deployment"*) printf 'desired=0 ready=0 available=0' ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
git() { case "$*" in *"rev-parse HEAD"*) printf 'abc123\n';; *"show HEAD:"*) printf ' replicas: 0\n';; *) return 1;; esac; }
|
|
systemctl() { printf 'LoadState=loaded\nActiveState=%s\n' "$SERVICE_STATE"; }
|
|
doctor_runner_report
|
|
'''
|
|
env = os.environ.copy()
|
|
env.update(REPO_ROOT=str(ROOT), KUBECONFIG_PATH="/tmp/kubeconfig",
|
|
SERVICE_STATE=service_state)
|
|
return subprocess.run(["bash", "-c", code, "test", str(RUNNER_REPORT)],
|
|
env=env, text=True, capture_output=True)
|
|
|
|
def test_runner_expected_zero_and_active_host_needs_no_fix(self):
|
|
result = self.runner_report("active")
|
|
self.assertEqual(result.returncode, 0)
|
|
self.assertIn("intentionally sets the Kubernetes runner to zero", result.stdout)
|
|
self.assertIn("desired=0 ready=0", result.stdout)
|
|
self.assertIn("No runner restart", result.stdout)
|
|
self.assertNotIn("Proposed mitigation", result.stdout)
|
|
|
|
def test_inactive_runner_proposes_start_without_noninteractive_execution(self):
|
|
result = self.runner_report("inactive")
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertIn("sudo -n systemctl start homelab-gitea-runner.service", result.stdout)
|
|
self.assertIn("Approval is required", result.stdout)
|
|
|
|
def test_runner_mitigation_requires_explicit_interactive_yes(self):
|
|
code = r'''
|
|
source "$1"
|
|
kubectl() {
|
|
case "$*" in
|
|
*"get application"*) printf 'Progressing Synced abc123' ;;
|
|
*"get deployment"*) printf 'desired=0 ready=0 available=0' ;;
|
|
esac
|
|
}
|
|
git() { case "$*" in *"rev-parse HEAD"*) printf 'abc123\n';; *"show HEAD:"*) printf ' replicas: 0\n';; esac; }
|
|
systemctl() { if [[ "$1" == show ]]; then printf 'LoadState=loaded\nActiveState=inactive\n'; else printf 'active\n'; fi; }
|
|
sudo() { printf 'started' >"$MARKER"; }
|
|
doctor_runner_report
|
|
'''
|
|
for answer, expected in (("n", False), ("y", True)):
|
|
with self.subTest(answer=answer), tempfile.TemporaryDirectory() as temp:
|
|
marker = Path(temp) / "started"
|
|
env = os.environ.copy()
|
|
env.update(REPO_ROOT=str(ROOT), KUBECONFIG_PATH="/tmp/kubeconfig", MARKER=str(marker))
|
|
master, slave = pty.openpty()
|
|
try:
|
|
process = subprocess.Popen(["bash", "-c", code, "test", str(RUNNER_REPORT)],
|
|
stdin=slave, stdout=slave, stderr=slave, env=env)
|
|
os.close(slave)
|
|
slave = -1
|
|
os.write(master, (answer + "\n").encode())
|
|
process.wait(timeout=10)
|
|
self.assertEqual(marker.exists(), expected)
|
|
finally:
|
|
os.close(master)
|
|
if slave >= 0:
|
|
os.close(slave)
|
|
|
|
def test_select_exact_application_or_compose_service(self):
|
|
rows = ("warn\tKubernetes apps\tgitea-runner\tProgressing\t\t\t\t\n"
|
|
"ok\tDebian Compose\thomelab-gitea/gitea (homelab-gitea)\tUp\t\t\t\t\n")
|
|
self.assertIn("gitea-runner", run_filter("gitea-runner", rows).stdout)
|
|
self.assertIn("homelab-gitea/gitea", run_filter("homelab-gitea/gitea", rows).stdout)
|
|
self.assertNotEqual(run_filter("missing", rows).returncode, 0)
|
|
|
|
def test_ambiguous_short_name_is_rejected(self):
|
|
rows = ("ok\tA\tone/web (one-web)\tUp\t\t\t\t\n"
|
|
"ok\tB\ttwo/web (two-web)\tUp\t\t\t\t\n")
|
|
self.assertNotEqual(run_filter("web", rows).returncode, 0)
|
|
self.assertEqual(run_filter("one/web", rows).returncode, 0)
|
|
|
|
def test_all_area_is_rejected_and_help_succeeds(self):
|
|
for args, expected in ((["all"], 2), (["--help"], 0), (["oci", "--help"], 0)):
|
|
result = subprocess.run(["bash", "-c", 'source "$1"; shift; doctor_focus "$@"',
|
|
"test", str(DOCTOR), *args], capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, expected)
|
|
self.assertIn("oci|rpi|opi5|debian|cluster", result.stdout + result.stderr)
|
|
|
|
def test_application_evidence_excludes_spec_and_secrets(self):
|
|
app = {"spec": {"secret": "do-not-print"}, "status": {
|
|
"health": {"status": "Progressing", "message": "waiting"},
|
|
"operationState": {"phase": "Running"},
|
|
"resources": [{"kind": "Deployment", "name": "runner", "health": {"status": "Progressing"}}]}}
|
|
result = subprocess.run([sys.executable, str(APP)], input=json.dumps(app),
|
|
capture_output=True, text=True, check=True)
|
|
self.assertIn("health: Progressing", result.stdout)
|
|
self.assertIn("Deployment", result.stdout)
|
|
self.assertNotIn("do-not-print", result.stdout)
|
|
|
|
def test_deployment_evidence_reports_actual_desired_replicas(self):
|
|
deployment = {"spec": {"replicas": 0}, "status": {"readyReplicas": 0}}
|
|
result = subprocess.run([sys.executable, str(APP), "--deployment"],
|
|
input=json.dumps(deployment), capture_output=True,
|
|
text=True, check=True)
|
|
self.assertIn("desired=0 ready=0", result.stdout)
|
|
|
|
def test_deployment_unavailable_replicas_are_reported(self):
|
|
deployment = {"spec": {"replicas": 2}, "status": {"readyReplicas": 1}}
|
|
result = subprocess.run([sys.executable, str(APP), "--deployment"],
|
|
input=json.dumps(deployment), capture_output=True, text=True)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("desired=2 ready=1", result.stdout)
|
|
|
|
def test_selected_item_does_not_inherit_unrelated_failure(self):
|
|
code = r'''
|
|
source "$1"
|
|
require_debian_server() { :; }
|
|
report_ui_parse_args() { :; }
|
|
report_ui_begin() { REPORT_UI_ROWS_FILE="$(mktemp)"; }
|
|
status_focus_collect() {
|
|
printf 'fail\tKubernetes apps\tother\tdown\t\t\t\t\nwarn\tKubernetes apps\trunner\tProgressing\t\t\t\t\n' >"$REPORT_UI_ROWS_FILE"
|
|
}
|
|
report_ui_finish() { REPORT_UI_FAILURES=1; }
|
|
doctor_focus_application_evidence() { printf 'deployment replicas: desired=1 ready=0\n'; }
|
|
report_ui_emit() { printf 'evidence-requested:%s %s\n' "$3" "$5"; }
|
|
truthy() { return 1; }
|
|
report_ui_render() { cat "$REPORT_UI_ROWS_FILE"; }
|
|
report_ui_cleanup() { rm -f "$REPORT_UI_ROWS_FILE"; }
|
|
doctor_focus cluster runner
|
|
'''
|
|
env = os.environ.copy()
|
|
env["REPO_ROOT"] = str(ROOT)
|
|
result = subprocess.run(["bash", "-c", code, "test", str(DOCTOR)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("runner application details", result.stdout)
|
|
self.assertIn("desired=1 ready=0", result.stdout)
|
|
self.assertIn("Progressing", result.stdout)
|
|
self.assertNotIn("other", result.stdout)
|
|
|
|
def test_direct_application_uses_single_target_and_ai_by_default(self):
|
|
code = r'''
|
|
source "$1"
|
|
require_debian_server() { :; }
|
|
report_ui_parse_args() { :; }
|
|
report_ui_begin() { REPORT_UI_ROWS_FILE="$(mktemp)"; }
|
|
report_ui_cached_probe() { printf '%s' "$APP_JSON"; }
|
|
report_ui_enqueue() { "$@"; }
|
|
status_focus_compose_report() { :; }
|
|
report_ui_finish() { REPORT_UI_FAILURES=0; }
|
|
doctor_focus_application_evidence() { printf 'desired=1 ready=0\n'; }
|
|
report_ui_emit() { printf 'evidence:%s\n' "$5"; }
|
|
truthy() { [[ "$1" == true ]]; }
|
|
report_ui_local_rca() { printf 'ai=%s\n' "$REPORT_UI_LOCAL_RCA"; }
|
|
report_ui_cleanup() { rm -f "$REPORT_UI_ROWS_FILE"; }
|
|
doctor_focus sample-runner
|
|
'''
|
|
env = os.environ.copy()
|
|
env.update(REPO_ROOT=str(ROOT), APP_JSON=json.dumps({
|
|
"metadata": {"name": "sample-runner"}, "status": {
|
|
"health": {"status": "Progressing"}, "sync": {"status": "Synced"}}}))
|
|
result = subprocess.run(["bash", "-c", code, "test", str(DOCTOR)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("ai=true", result.stdout)
|
|
self.assertIn("sample-runner", result.stdout)
|
|
self.assertIn("desired=1 ready=0", result.stdout)
|
|
|
|
def test_rpi_os_snapshot_reports_disk_and_memory_pressure(self):
|
|
snapshot = ("login banner\n__JEANNIE_HOST_OS_V1__\nkernel=Linux 6.1\n"
|
|
"uptime_load=up 1 day, load 0.1\nmemory_kib=1000 80\n"
|
|
"root_percent=90\nsystemd=degraded\n")
|
|
result = subprocess.run([sys.executable, str(RPI_OS), "85"], input=snapshot,
|
|
capture_output=True, text=True, check=True)
|
|
self.assertIn("warn\tMemory available", result.stdout)
|
|
self.assertIn("warn\tRoot filesystem usage", result.stdout)
|
|
self.assertIn("warn\tSystemd state", result.stdout)
|
|
|
|
def test_direct_compose_uses_detected_host_and_ai_evidence(self):
|
|
code = r'''
|
|
source "$1"
|
|
require_debian_server() { :; }
|
|
report_ui_parse_args() { :; }
|
|
report_ui_begin() { REPORT_UI_ROWS_FILE="$(mktemp)"; }
|
|
report_ui_cached_probe() { :; }
|
|
report_ui_enqueue() { "$@"; }
|
|
status_focus_compose_report() {
|
|
if [[ "$1" == "Debian Compose" ]]; then
|
|
printf 'warn\tDebian Compose\thomelab-gitea/gitea (homelab-gitea)\texited\t\t\t\t\n' >>"$REPORT_UI_ROWS_FILE"
|
|
fi
|
|
}
|
|
report_ui_finish() { REPORT_UI_FAILURES=0; }
|
|
doctor_focus_compose_evidence() { printf 'scope=%s restarts=2\n' "$1"; }
|
|
report_ui_emit() { printf 'evidence:%s %s\n' "$1" "$5"; }
|
|
truthy() { return 1; }
|
|
report_ui_render() { cat "$REPORT_UI_ROWS_FILE"; }
|
|
report_ui_cleanup() { rm -f "$REPORT_UI_ROWS_FILE"; }
|
|
doctor_focus homelab-gitea/gitea
|
|
'''
|
|
env = os.environ.copy()
|
|
env["REPO_ROOT"] = str(ROOT)
|
|
result = subprocess.run(["bash", "-c", code, "test", str(DOCTOR)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("evidence:warn scope=debian restarts=2", result.stdout)
|
|
|
|
def test_direct_lookup_refuses_unavailable_source(self):
|
|
code = r'''
|
|
source "$1"
|
|
require_debian_server() { :; }
|
|
report_ui_parse_args() { :; }
|
|
report_ui_begin() { REPORT_UI_ROWS_FILE="$(mktemp)"; }
|
|
report_ui_enqueue() { "$@"; }
|
|
doctor_focus_application_lookup() { :; }
|
|
status_focus_compose_report() { printf 'warn\tOCI edge Compose\tCompose inventory\tunavailable\t\t\t\t\n' >>"$REPORT_UI_ROWS_FILE"; }
|
|
report_ui_finish() { REPORT_UI_FAILURES=0; }
|
|
report_ui_cleanup() { rm -f "$REPORT_UI_ROWS_FILE"; }
|
|
doctor_focus example
|
|
'''
|
|
env = os.environ.copy()
|
|
env["REPO_ROOT"] = str(ROOT)
|
|
result = subprocess.run(["bash", "-c", code, "test", str(DOCTOR)],
|
|
env=env, capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, 2)
|
|
self.assertIn("specify a device or cluster scope", result.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|