Files
mtusi2026lab4/student-tests/s3_outage.py
T

75 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Stop RustFS briefly and confirm the API fails closed, then restore it."""
import subprocess
import sys
import time
import json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, Failure, arguments, check, run
def compose(*args):
return subprocess.run(
["docker", "compose", "--env-file", ".env", *args],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=30,
)
def main():
args = arguments("Bounded S3 outage check; stops and restores only the local RustFS service")
client = Client(args.base_url, args.ca_file, timeout=12)
alice, _bob, _eve, chat = client.bootstrap()
stopped = False
results = {}
try:
compose("stop", "s3")
stopped = True
live = client.call("GET", "/health/live", schema="Health")
check(live["json"]["status"] == "ok", "liveness failed during S3 outage")
results["liveness"] = live["status"]
results["readiness_during_outage"] = client.call("GET", "/health/ready", 503)["status"]
image = (Path(__file__).resolve().parents[1] / "tests/fixtures/sample.png").read_bytes()
upload = client.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/attachments?filename=s3-outage.png",
503,
actor=alice,
raw=image,
headers={"Content-Type": "image/png"},
)
# urllib response headers are an HTTPMessage, not JSON serializable.
# Record only the safe scalar evidence needed for this outage check.
results["upload_during_outage"] = {
"status": upload["status"],
"error_code": upload["json"]["error"]["code"],
"elapsed_seconds": round(upload["elapsed"], 3),
}
finally:
if stopped:
compose("start", "s3")
deadline = time.monotonic() + 90
while True:
try:
client.call("GET", "/health/ready", schema="Health")
break
except Failure:
if time.monotonic() >= deadline:
raise
time.sleep(1)
print("PASS: RustFS is ready again.")
results["readiness_after_restore"] = 200
results["result"] = "PASS"
evidence = Path(__file__).resolve().parents[1] / "evidence/s3-outage.json"
evidence.parent.mkdir(parents=True, exist_ok=True)
evidence.write_text(json.dumps(results, indent=2) + "\n", encoding="utf-8")
print(json.dumps(results, separators=(",", ":")))
if __name__ == "__main__":
run(main)