108 lines
5.9 KiB
Python
108 lines
5.9 KiB
Python
import json
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "status-app-snapshot.py"
|
|
INTENT = SCRIPT.with_name("compose-intent.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_record_and_clear_compose_intent_without_operating_docker(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "intent.json"
|
|
result = subprocess.run([sys.executable, str(INTENT), "--file", str(path), "stopped", "lab/media"],
|
|
text=True, capture_output=True, check=True)
|
|
self.assertIn("no containers were started or stopped", result.stdout)
|
|
self.assertEqual(json.loads(path.read_text())["services"], {"lab/media": "stopped"})
|
|
subprocess.run([sys.executable, str(INTENT), "--file", str(path), "clear", "lab/media"], check=True)
|
|
self.assertEqual(json.loads(path.read_text())["services"], {})
|
|
|
|
def test_explicit_stopped_intent_does_not_hide_a_running_container(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
intent = Path(directory) / "intent.json"
|
|
intent.write_text(json.dumps({"version": 1, "services": {"lab/media": "stopped"}}))
|
|
def snapshot(state):
|
|
return json.dumps({"Names": "media", "State": state, "Status": state,
|
|
"Labels": "com.docker.compose.project=lab,com.docker.compose.service=media"})
|
|
stopped = parse("compose", snapshot("exited"), str(intent))
|
|
running = parse("compose", snapshot("running"), str(intent))
|
|
self.assertIn("ok\tlab/media (media)", stopped)
|
|
self.assertIn("operator recorded expected state: stopped", stopped)
|
|
self.assertIn("warn\tlab/media (media)", running)
|
|
|
|
def test_invalid_intent_warns_and_never_suppresses_stopped_service(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
intent = Path(directory) / "intent.json"
|
|
intent.write_text("not json")
|
|
data = json.dumps({"Names": "media", "State": "exited", "Status": "Exited (0)",
|
|
"Labels": "com.docker.compose.project=lab,com.docker.compose.service=media"})
|
|
result = parse("compose", data, str(intent))
|
|
self.assertIn("warn\tCompose desired state", result)
|
|
self.assertIn("warn\tlab/media", result)
|
|
self.assertIn("desired state unknown", result)
|
|
|
|
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()
|