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

72 lines
3.6 KiB
Python

import json
import subprocess
import sys
import unittest
from pathlib import Path
SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "status-app-snapshot.py"
def parse(kind, data, *args):
return subprocess.run([sys.executable, str(SCRIPT), kind, *args], input=data,
capture_output=True, text=True, check=True).stdout
class ApplicationSnapshotTests(unittest.TestCase):
def test_runner_candidate_requires_synced_revision_and_no_other_unhealthy_resources(self):
runner = {"metadata": {"name": "gitea-runner"}, "status": {
"health": {"status": "Progressing"}, "sync": {"status": "Synced", "revision": "abc123"},
"resources": [{"kind": "Deployment", "namespace": "gitea-runner", "name": "gitea-runner",
"health": {"status": "Progressing"}}]}}
self.assertEqual(parse("runner-candidate", json.dumps({"items": [runner]})).strip(), "abc123")
runner["status"]["resources"].append({"kind": "PersistentVolumeClaim", "name": "data",
"health": {"status": "Degraded"}})
self.assertEqual(parse("runner-candidate", json.dumps({"items": [runner]})), "")
def test_expected_runner_flag_only_changes_matching_progressing_runner(self):
data = json.dumps({"items": [
{"metadata": {"name": "gitea-runner"}, "status": {
"health": {"status": "Progressing"}, "sync": {"status": "Synced"}}},
{"metadata": {"name": "other"}, "status": {
"health": {"status": "Progressing"}, "sync": {"status": "Synced"}}}]})
self.assertIn("warn\tgitea-runner", parse("applications", data))
result = parse("applications", data, "--expected-runner")
self.assertIn("ok\tgitea-runner", result)
self.assertIn("warn\tother", result)
def test_compose_services_distinguish_health_and_oneoff_jobs(self):
def container(name, state, status, oneoff="False"):
return json.dumps({"Names": name, "State": state, "Status": status,
"Labels": "com.docker.compose.project=lab,"
"com.docker.compose.service=" + name + ","
"com.docker.compose.oneoff=" + oneoff})
data = "\n".join((container("web", "running", "Up 5 minutes (healthy)"),
container("db", "running", "Up 2 minutes (unhealthy)"),
container("worker", "exited", "Exited (0)"),
container("job", "exited", "Exited (0)", "True")))
result = parse("compose", data)
self.assertIn("ok\tlab/web (web)", result)
self.assertIn("warn\tlab/db (db)", result)
self.assertIn("warn\tlab/worker (worker)", result)
self.assertNotIn("lab/job", result)
def test_argocd_applications_report_health_and_sync(self):
def app(name, health, sync):
return {"metadata": {"name": name}, "status": {
"health": {"status": health}, "sync": {"status": sync}}}
result = parse("applications", json.dumps({"items": [
app("healthy", "Healthy", "Synced"),
app("updating", "Progressing", "Synced"),
app("broken", "Degraded", "OutOfSync")]}))
self.assertIn("ok\thealthy\t", result)
self.assertIn("warn\tupdating\t", result)
self.assertIn("fail\tbroken\t", result)
def test_empty_snapshot_does_not_claim_apps_are_healthy(self):
self.assertIn("skip\tCompose services\t", parse("compose", ""))
self.assertIn("skip\tArgo CD applications\t", parse("applications", '{"items":[]}'))
if __name__ == "__main__":
unittest.main()