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

79 lines
3.0 KiB
Python

"""Exercise selection and profile logic without contacting homelab hosts."""
import os
import subprocess
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "status-focus"
def collect(scope, **overrides):
env = os.environ.copy()
env.update(
HOME="/tmp/test-home",
LAB_RPI_HOST="192.0.2.4",
LAB_RPI_USER="pi",
LAB_PIMOX_HOST="192.0.2.5",
LAB_PIMOX_USER="opi",
LAB_CLUSTER_CONTROL_PLANE_LAN_IP="192.0.2.5",
LAB_CLUSTER_RUNTIME="k3s",
LAB_CLUSTER_PROFILE="k3s",
LAB_PUBLIC_URL="https://example.com",
LAB_MATRIX_SYNAPSE_PORT="8808",
)
env.update(overrides)
code = r'''
source "$1"
report_ui_check() { printf '%s|%s|%s\n' "$2" "$3" "${*:7}"; }
report_ui_emit() { printf '%s|%s|%s\n' "$2" "$3" "$1"; }
report_ui_enqueue() {
case "$1" in
status_focus_compose_report) printf '%s|Compose snapshot|%s\n' "$2" "$3" ;;
status_focus_applications_report) printf 'Kubernetes apps|Application snapshot|queued\n' ;;
esac
}
status_focus_collect "$2"
'''
return subprocess.run(
["bash", "-c", code, "test", str(SCRIPT), scope],
env=env, text=True, capture_output=True, check=True,
).stdout
class FocusedStatusTests(unittest.TestCase):
def test_scopes_do_not_collect_other_hosts(self):
for scope, groups in (("debian", {"Debian", "Debian Compose"}),
("rpi", {"RPi4", "RPi4 Compose"}),
("opi5", {"Orange Pi", "Orange Pi Compose"}),
("oci", {"OCI edge", "OCI edge Compose"}),
("cluster", {"Kubernetes", "Kubernetes apps"})):
with self.subTest(scope=scope):
lines = collect(scope).splitlines()
self.assertTrue(lines)
self.assertEqual({line.split("|")[0] for line in lines}, groups)
def test_all_covers_each_area_and_inventory_matrix_port(self):
result = collect("all")
self.assertEqual({line.split("|")[0] for line in result.splitlines()},
{"Debian", "RPi4", "Orange Pi", "OCI edge", "Kubernetes",
"Debian Compose", "RPi4 Compose", "Orange Pi Compose",
"OCI edge Compose", "Kubernetes apps"})
self.assertIn("127.0.0.1:8808/_matrix/client/versions", result)
self.assertIn("https://example.com/edge-health", result)
def test_k3s_checks_only_for_selected_orange_pi_runtime(self):
self.assertIn("Orange Pi|K3s runtime|", collect("opi5"))
for overrides in ({"LAB_CLUSTER_RUNTIME": "kubeadm"},
{"LAB_CLUSTER_CONTROL_PLANE_LAN_IP": "192.0.2.9"}):
with self.subTest(overrides=overrides):
result = collect("opi5", **overrides)
self.assertIn("Orange Pi|Native K3s checks|skip", result)
self.assertNotIn("Orange Pi|K3s runtime|", result)
if __name__ == "__main__":
unittest.main()