"""Check the LB's own safe JSON error when every API is stopped, then recover.""" import json import subprocess import sys import time import urllib.error import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client def main(): c = Client("http://127.0.0.1:8083") actor = c.user("lbdown") subprocess.run(["docker", "compose", "stop", "api"], cwd=ROOT, check=True) try: time.sleep(4) request = urllib.request.Request(c.base + "/api/v1/users/me", headers={"Authorization": "Bearer " + actor["token"]}) try: response = urllib.request.urlopen(request, timeout=3) except urllib.error.HTTPError as exc: response = exc with response: body = json.loads(response.read()) result = {"status": response.code, "body": body, "request_id": response.headers.get("X-Request-Id"), "instance_id": response.headers.get("X-Instance-Id")} assert result["status"] == 503 assert body["error"]["code"] == "unavailable" assert result["request_id"] and body["error"]["request_id"] == result["request_id"] assert result["instance_id"] is None finally: subprocess.run(["docker", "compose", "start", "api"], cwd=ROOT, check=True) subprocess.run([sys.executable, "scripts/wait_replicas.py", "2"], cwd=ROOT, check=True) c.call("GET", "/api/v1/users/me", actor=actor, schema="User") result["recovered_existing_session"] = True (ROOT / "evidence/lb-no-ready-api.json").write_text(json.dumps(result, indent=2) + "\n") print(json.dumps(result, indent=2)) if __name__ == "__main__": main()