forked from mtusi2026/mtusi2026lab2
finished
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Dependency failure and Redis-only restart checks (Docker Compose)."""
|
||||
import json
|
||||
import os
|
||||
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, run # noqa: E402
|
||||
|
||||
|
||||
def compose(*args):
|
||||
env_file = ROOT / ".env"
|
||||
cmd = ["docker", "compose"]
|
||||
if env_file.is_file():
|
||||
cmd.extend(["--env-file", str(env_file)])
|
||||
cmd.extend(args)
|
||||
proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True)
|
||||
if proc.returncode != 0:
|
||||
raise Failure("docker compose %s failed: %s" % (" ".join(args), (proc.stderr or "")[-500:]))
|
||||
return proc
|
||||
|
||||
|
||||
def wait_ready(c: Client, seconds=90):
|
||||
deadline = time.monotonic() + seconds
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
c.call("GET", "/health/ready", schema="Health")
|
||||
return
|
||||
except Failure as exc:
|
||||
last = exc
|
||||
time.sleep(0.5)
|
||||
raise Failure("ready not restored: %s" % last)
|
||||
|
||||
|
||||
def wait_status(c: Client, path, expected, seconds=30, actor=None):
|
||||
deadline = time.monotonic() + seconds
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
c.call("GET", path, expected, actor=actor, schema="Health" if expected == 200 and path.startswith("/health") else None)
|
||||
return
|
||||
except Failure as exc:
|
||||
last = exc
|
||||
time.sleep(0.4)
|
||||
raise Failure("did not observe %s for %s: %s" % (expected, path, last))
|
||||
|
||||
|
||||
def main():
|
||||
base = os.environ.get("BASE_URL", "http://localhost:8080")
|
||||
c = Client(base, timeout=3)
|
||||
alice = c.user("infra")
|
||||
compose("ps")
|
||||
|
||||
compose("stop", "redis")
|
||||
wait_status(c, "/health/live", 200)
|
||||
wait_status(c, "/health/ready", 503)
|
||||
wait_status(c, "/api/v1/users/me", 503, actor=alice)
|
||||
compose("start", "redis")
|
||||
wait_ready(c)
|
||||
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
||||
|
||||
compose("stop", "postgres")
|
||||
wait_status(c, "/health/live", 200)
|
||||
wait_status(c, "/health/ready", 503)
|
||||
compose("start", "postgres")
|
||||
wait_ready(c)
|
||||
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
||||
|
||||
alice2, bob, _eve, chat = c.bootstrap()
|
||||
msg = c.message(alice2, chat, "before-redis-restart")["json"]
|
||||
compose("restart", "redis")
|
||||
wait_ready(c)
|
||||
me = c.call("GET", "/api/v1/users/me", actor=alice2, schema="User")["json"]
|
||||
check(me["id"] == alice2["id"], "session lost after Redis restart despite AOF")
|
||||
page = c.call(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
||||
actor=bob,
|
||||
schema="MessagePage",
|
||||
)["json"]
|
||||
check(any(m["id"] == msg["id"] for m in page["items"]), "history not rebuilt after cache restart")
|
||||
print(json.dumps({"redis_restart": "session+history ok", "db_or_redis_down": "503"}, ensure_ascii=False))
|
||||
print("PASS: infra unavailability and redis restart")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user