59 lines
1.8 KiB
Python
Executable File
59 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Stop Redis and PostgreSQL separately and check bounded API failures."""
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure, check # noqa: E402
|
|
|
|
|
|
def compose(*args):
|
|
subprocess.run(["docker", "compose", "--env-file", ".env", *args], cwd=ROOT,
|
|
check=True, stdout=subprocess.DEVNULL)
|
|
|
|
|
|
def wait_ready(client, timeout=60):
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
return
|
|
except Failure:
|
|
time.sleep(0.5)
|
|
raise Failure("stack did not recover")
|
|
|
|
|
|
def main():
|
|
client = Client(timeout=5)
|
|
actor = client.user("dependency")
|
|
try:
|
|
compose("stop", "redis")
|
|
client.call("GET", "/health/ready", 503)
|
|
client.call("GET", "/api/v1/users/me", 503, actor=actor)
|
|
client.call("GET", "/api/v1/users/me", 401)
|
|
finally:
|
|
compose("start", "redis")
|
|
wait_ready(client)
|
|
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
|
|
|
try:
|
|
compose("stop", "postgres")
|
|
client.call("GET", "/health/ready", 503)
|
|
client.call("GET", "/api/v1/users/me", 503, actor=actor)
|
|
finally:
|
|
compose("start", "postgres")
|
|
wait_ready(client)
|
|
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
|
print("PASS: Redis/PostgreSQL failures returned 503; anonymous access stayed 401; state recovered")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (Failure, OSError, subprocess.CalledProcessError) as exc:
|
|
print("FAIL:", exc)
|
|
raise SystemExit(1)
|