90 lines
3.6 KiB
Python
90 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Demonstrate the lab5 acceptance scenario without hiding infrastructure steps.
|
|
|
|
Worker stopped -> upload returns 202 queued -> message references the chat
|
|
attachment -> API replicas restarted -> worker started -> the same job
|
|
reaches ready with a contract-compliant JPEG thumbnail.
|
|
|
|
The script prints only synthetic IDs and timings. It never prints tokens.
|
|
"""
|
|
import json
|
|
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, arguments, check, run # noqa: E402
|
|
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
|
|
|
|
|
def compose(*args):
|
|
subprocess.run(COMPOSE + list(args), cwd=ROOT, check=True,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
|
|
def main():
|
|
args = arguments("lab5 worker-outage demonstration")
|
|
c = Client(args.base_url, args.ca_file, timeout=10)
|
|
alice, bob, _eve, chat = c.bootstrap()
|
|
|
|
print("1. stop worker")
|
|
compose("stop", "worker")
|
|
try:
|
|
print("2. upload with worker down")
|
|
att, content = c.attachment(alice, chat)
|
|
check(att["status"] == "queued", "queued expected after upload")
|
|
meta = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["json"]
|
|
check(meta["status"] in ("queued", "processing"), "still unprocessed while worker is down")
|
|
pending = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", 409, actor=bob)
|
|
check(pending["status"] == 409, "content must be 409 while queued")
|
|
|
|
print("3. attach the queued attachment to a message")
|
|
msg = c.message(alice, chat, "", attachments=[att["id"]])["json"]
|
|
check(msg["attachment_ids"] == [att["id"]], "message lost the attachment")
|
|
|
|
print("4. restart API replicas (accepted work must survive)")
|
|
compose("restart", "api1", "api2")
|
|
deadline = time.monotonic() + 90
|
|
while True:
|
|
try:
|
|
c.call("GET", "/health/ready", schema="Health")
|
|
break
|
|
except Exception:
|
|
check(time.monotonic() < deadline, "API did not recover")
|
|
time.sleep(0.5)
|
|
|
|
print("5. start worker")
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
started = time.monotonic()
|
|
ready = c.wait_ready(bob, att, wait_seconds=90)
|
|
elapsed = round(time.monotonic() - started, 3)
|
|
check(ready["status"] == "ready", "job did not reach ready")
|
|
variants = [v for v in ready["variants"] if v["name"] == "thumbnail"]
|
|
check(len(variants) == 1, "expected exactly one thumbnail")
|
|
v = variants[0]
|
|
check((v["width"], v["height"]) == (256, 160), "thumbnail dimensions differ")
|
|
thumb = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content?variant=thumbnail", actor=bob)
|
|
check(thumb["headers"].get_content_type() == "image/jpeg", "thumbnail must be JPEG")
|
|
from image_probe import jpeg_size
|
|
check(jpeg_size(thumb["bytes"]) == (256, 160), "actual JPEG size differs")
|
|
|
|
print(json.dumps({
|
|
"attachment_id": att["id"],
|
|
"message_id": msg["id"],
|
|
"status_after_worker_restart": ready["status"],
|
|
"thumbnail": {"width": v["width"], "height": v["height"],
|
|
"size_bytes": v["size_bytes"], "sha256": v["sha256"]},
|
|
"seconds_from_worker_start_to_ready": elapsed,
|
|
}, ensure_ascii=False))
|
|
print("PASS: queued job completed after API and worker restart.")
|
|
finally:
|
|
compose("up", "-d", "--no-deps", "worker")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|