167 lines
7.4 KiB
Python
167 lines
7.4 KiB
Python
"""Exercise selection and profile logic without contacting homelab hosts."""
|
|
|
|
import os
|
|
import json
|
|
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_runner_exception_requires_matching_revision_zero_replicas_and_active_host(self):
|
|
snapshot = json.dumps({"items": [{"metadata": {"name": "gitea-runner"}, "status": {
|
|
"health": {"status": "Progressing"}, "sync": {"status": "Synced", "revision": "abc"},
|
|
"resources": [{"kind": "Deployment", "namespace": "gitea-runner", "name": "gitea-runner",
|
|
"health": {"status": "Progressing"}}]}}]})
|
|
code = r'''
|
|
source "$1"
|
|
git() { case "$*" in *"rev-parse HEAD"*) printf '%s\n' "$HEAD_REV";; *"show HEAD:"*) printf ' replicas: %s\n' "$CONFIG_REPLICAS";; esac; }
|
|
systemctl() { [[ "$HOST_STATE" == active ]]; }
|
|
kubectl() { printf '{"spec":{"replicas":%s}}' "$LIVE_REPLICAS"; }
|
|
status_focus_runner_expected "$SNAPSHOT"
|
|
'''
|
|
base = dict(REPO_ROOT=str(ROOT), KUBECONFIG_PATH="/tmp/kubeconfig", SNAPSHOT=snapshot,
|
|
HEAD_REV="abc", CONFIG_REPLICAS="0", LIVE_REPLICAS="0", HOST_STATE="active")
|
|
for changes, expected in (({}, 0), ({"HEAD_REV": "different"}, 1),
|
|
({"CONFIG_REPLICAS": "1"}, 1), ({"LIVE_REPLICAS": "1"}, 1),
|
|
({"HOST_STATE": "inactive"}, 1)):
|
|
with self.subTest(changes=changes):
|
|
env = os.environ.copy()
|
|
env.update(base, **changes)
|
|
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)], env=env,
|
|
capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, expected)
|
|
|
|
def test_help_describes_scopes_and_exits_successfully(self):
|
|
commands = ROOT / "lib" / "jeannie" / "commands.sh"
|
|
for args in (("--help",), ("-h",), ("oci", "--help")):
|
|
with self.subTest(args=args):
|
|
result = subprocess.run(
|
|
["bash", "-c", 'source "$1"; shift; status_report "$@"',
|
|
"test", str(commands), *args],
|
|
capture_output=True, text=True, check=True,
|
|
)
|
|
self.assertIn("all|oci|rpi|opi5|debian|cluster", result.stdout)
|
|
self.assertIn("--deep", result.stdout)
|
|
self.assertIn("--heal-plan", result.stdout)
|
|
|
|
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)
|
|
|
|
def test_remote_compose_snapshot_ignores_banner_and_stderr(self):
|
|
container = json.dumps({
|
|
"Names": "nginx", "State": "running", "Status": "Up 2 minutes",
|
|
"Labels": "com.docker.compose.project=edge,com.docker.compose.service=nginx",
|
|
})
|
|
env = os.environ.copy()
|
|
env.update(REPO_ROOT=str(ROOT), MOCK_CONTAINER=container)
|
|
code = r'''
|
|
source "$1"
|
|
timeout() { shift; "$@"; }
|
|
ssh() {
|
|
printf 'Welcome to the edge\n__JEANNIE_COMPOSE_SNAPSHOT_V1__\n%s\n' "$MOCK_CONTAINER"
|
|
printf 'bash: warning: setlocale failed\n' >&2
|
|
}
|
|
report_ui_emit() { printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4"; }
|
|
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
|
|
'''
|
|
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("ok|OCI edge Compose|edge/nginx (nginx)|", result.stdout)
|
|
self.assertNotIn("Cannot interpret", result.stdout)
|
|
|
|
def test_remote_compose_failure_reports_stderr(self):
|
|
env = os.environ.copy()
|
|
env["REPO_ROOT"] = str(ROOT)
|
|
code = r'''
|
|
source "$1"
|
|
timeout() { shift; "$@"; }
|
|
ssh() { printf 'permission denied\n' >&2; return 1; }
|
|
report_ui_emit() { printf '%s|%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" "$5"; }
|
|
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
|
|
'''
|
|
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("Cannot read remote Docker containers|permission denied", result.stdout)
|
|
|
|
def test_remote_compose_rejects_output_without_marker(self):
|
|
env = os.environ.copy()
|
|
env["REPO_ROOT"] = str(ROOT)
|
|
code = r'''
|
|
source "$1"
|
|
timeout() { shift; "$@"; }
|
|
ssh() { printf 'unexpected output\n'; }
|
|
report_ui_emit() { printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4"; }
|
|
status_focus_compose_report 'OCI edge Compose' example ubuntu /key
|
|
'''
|
|
result = subprocess.run(["bash", "-c", code, "test", str(SCRIPT)],
|
|
env=env, capture_output=True, text=True, check=True)
|
|
self.assertIn("Remote Docker snapshot marker missing", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|