Files
ukmesh/scripts/test-mesh-health-probe.py

42 lines
2.0 KiB
Python

"""Exercise the resolved mesh-health-check probe on its actual runtime image."""
import json
import subprocess
import sys
import time
def run(*args, **kwargs):
return subprocess.run(args, capture_output=True, text=True, **kwargs)
compose = run("docker", "compose", "-f", "docker-compose.yml", "-f", "docker-compose.ci.yml",
"config", "--format", "json", check=True)
service = json.loads(compose.stdout)["services"]["mesh-health-check"]
kind, probe = service["healthcheck"]["test"]
assert kind == "CMD-SHELL" and "/api/bootstrap" in probe
# Compose escapes literal dollars when rendering a model for reuse in YAML.
probe = probe.replace("$$", "$")
server = """
require('node:http').createServer((q,r) => {
if (q.method !== 'GET') return r.writeHead(405).end();
if (q.url === '/redirect') return r.writeHead(302, {'Location':'/html','Content-Type':'application/json'}).end();
if (q.url === '/good-redirect') return r.writeHead(302, {'Location':'/api/bootstrap'}).end();
const type = q.url === '/html' ? 'text/html' : 'application/json; charset=utf-8';
r.writeHead(q.url === '/error' ? 503 : 200, {'Content-Type':type}).end('{}');
}).listen(3090, '127.0.0.1');
"""
cid = run("docker", "run", "--detach", "--network", "none", "--entrypoint", "node",
service["image"], "-e", server, check=True).stdout.strip()
try:
for attempt in range(30):
if run("docker", "exec", cid, "sh", "-c", probe).returncode == 0:
break
time.sleep(0.1)
else:
sys.exit("JSON bootstrap failed its health check")
for path, expected in (("/good-redirect", True), ("/html", False),
("/redirect", False), ("/error", False)):
response = run("docker", "exec", cid, "sh", "-c", probe.replace("/api/bootstrap", path))
assert (response.returncode == 0) == expected, path
assert run("docker", "exec", cid, "sh", "-c", probe.replace(":3090/", ":3091/")).returncode != 0
finally:
run("docker", "rm", "-f", cid, check=True)