Files
mtusi2026lab4/tests/s3_unavailable.py

58 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""Verify bounded API errors during an S3 outage, then restore the service."""
import os
from pathlib import Path
import subprocess
from urllib.parse import quote
from client import Client, arguments, run
ROOT = Path(__file__).resolve().parents[1]
def compose(*args):
command = [
"docker", "compose", "--project-directory", str(ROOT),
"--env-file", str(ROOT / ".env"), *args,
]
subprocess.run(
command,
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def main():
args = arguments("Verify API responses when MinIO is unavailable")
client = Client(args.base_url, args.ca_file, timeout=15)
alice, _, _, chat = client.bootstrap()
content = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
compose("stop", "s3")
try:
health = client.call("GET", "/health/ready", 503)
if health["elapsed"] > 10:
raise AssertionError("S3 readiness failure exceeded 10 seconds")
upload = client.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/attachments?filename=" + quote("sample.png"),
503,
actor=alice,
raw=content,
headers={"Content-Type": "image/png"},
)
if upload["elapsed"] > 10:
raise AssertionError("S3 upload failure exceeded 10 seconds")
finally:
compose("start", "s3")
compose("up", "-d", "--wait", "s3")
client.call("GET", "/health/ready", 200)
print("PASS: MinIO outage returns bounded 503 errors and recovers")
if __name__ == "__main__":
run(main)