51 lines
2.2 KiB
Python
51 lines
2.2 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):
|
|
return subprocess.run([sys.executable, str(SCRIPT), kind], input=data,
|
|
capture_output=True, text=True, check=True).stdout
|
|
|
|
|
|
class ApplicationSnapshotTests(unittest.TestCase):
|
|
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()
|