forked from mtusi2026/mtusi2026lab7
51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
from urllib.request import urlopen
|
|
import ssl
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
os.chdir(root)
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("--base-url",default="https://localhost:8443")
|
|
args = p.parse_args()
|
|
extra = ["--ca-file",".local/ca.crt"] if args.base_url.startswith("https:") else []
|
|
ctx = ssl.create_default_context(cafile=extra[1]) if extra else None
|
|
for attempt in range(90):
|
|
try:
|
|
with urlopen(args.base_url+"/health/ready",context=ctx,timeout=2):
|
|
break
|
|
except OSError:
|
|
time.sleep(1)
|
|
else:
|
|
raise SystemExit("API readiness timeout")
|
|
|
|
for name in ("smoke","persistence","failover"):
|
|
if name=="failover":
|
|
observed = set()
|
|
for attempt in range(120):
|
|
try:
|
|
with urlopen(args.base_url+"/health/ready",context=ctx,timeout=2) as ready:
|
|
observed.add(ready.headers["X-Instance-Id"])
|
|
except OSError:
|
|
pass
|
|
if len(observed)>=2:
|
|
break
|
|
time.sleep(.5)
|
|
else:
|
|
raise SystemExit("Both API replicas must be ready before stopping one")
|
|
command = [sys.executable,"-X","utf8","tests/smoke.py"]
|
|
if name!="smoke":
|
|
hook = "restart" if name=="persistence" else "stop-one"
|
|
suffix = ".cmd" if os.name=="nt" else ".sh"
|
|
command = command[:3]+["tests/resilience.py","--mode",name,"--action-script","scripts/"+hook+suffix]
|
|
result = subprocess.run(command+["--base-url",args.base_url]+extra,capture_output=True,encoding="utf-8")
|
|
print(result.stdout,flush=True)
|
|
(root/"evidence"/(name+".txt")).write_text(result.stdout,encoding="utf-8")
|
|
if result.returncode:
|
|
raise SystemExit(result.returncode)
|
|
subprocess.run(["docker","compose","start","api","api2"],check=True)
|