Files
mtusi2026lab5/scripts/lab5_checks.py
T
dawudukandCursor f2a3698480 Отделить обработку вложений от приёма файла.
Upload отвечает 202 после staging и записи задания, worker через RabbitMQ собирает thumbnail, crop и optimized и переживает разрыв до publish.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-09 01:08:24 +03:00

153 lines
5.4 KiB
Python

#!/usr/bin/env python3
"""Lab5 fault checks: worker down, content 409, then the same upload reaches ready.
Prints statuses and ids only. Does not print tokens.
"""
import base64
import hashlib
import json
import os
import subprocess
import time
import urllib.error
import urllib.request
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080")
FIXTURE = ROOT / "tests" / "fixtures" / "sample.png"
def compose(*args: str) -> None:
command = ["docker", "compose", "--env-file", ".env"]
extra = os.environ.get("LAB5_COMPOSE_FILES", "")
if extra:
for name in extra.split(os.pathsep):
if name:
command.extend(["-f", name])
subprocess.run([*command, *args], cwd=ROOT, check=True)
class Api:
def __init__(self) -> None:
self.opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def call(self, method, path, body=None, token=None, raw=None, content_type=None, headers=None):
hdrs = dict(headers or {})
data = raw
if body is not None:
data = json.dumps(body).encode("utf-8")
hdrs["Content-Type"] = "application/json"
if content_type:
hdrs["Content-Type"] = content_type
if token:
hdrs["Authorization"] = "Bearer " + token
request = urllib.request.Request(BASE + path, data=data, headers=hdrs, method=method)
try:
response = self.opener.open(request, timeout=30)
except urllib.error.HTTPError as error:
response = error
with response:
payload = response.read()
parsed = json.loads(payload) if payload and response.headers.get_content_type() == "application/json" else None
return response.status, parsed, payload
def expect(status, got, label):
if got != status:
raise SystemExit("FAIL %s expected %s got %s" % (label, status, got))
print("PASS", label, got)
def user(api: Api):
username = "l5_" + uuid.uuid4().hex[:10]
password = "T3st-" + uuid.uuid4().hex
status, body, _ = api.call(
"POST",
"/api/v1/users",
{"username": username, "display_name": "Lab5", "password": password},
)
expect(201, status, "register")
basic = base64.b64encode((username + ":" + password).encode()).decode()
status, session, _ = api.call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + basic})
expect(201, status, "login")
return body["id"], session["token"]
def wait_ready(api: Api, attachment_id: str, token: str):
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
status, body, _ = api.call("GET", "/api/v1/attachments/" + attachment_id, token=token)
expect(200, status, "poll")
if body["status"] == "failed":
raise SystemExit("FAIL attachment failed " + str(body.get("error_code")))
if body["status"] == "ready":
return body
time.sleep(0.4)
raise SystemExit("FAIL not ready")
def main() -> None:
api = Api()
alice, alice_token = user(api)
bob, bob_token = user(api)
status, chat, _ = api.call(
"POST",
"/api/v1/chats",
{"title": "lab5", "member_ids": [alice, bob]},
token=alice_token,
)
expect(201, status, "chat")
chat_id = chat["id"]
print("chat", chat_id)
compose("stop", "worker")
content = FIXTURE.read_bytes()
status, attachment, _ = api.call(
"POST",
"/api/v1/chats/%s/attachments?filename=sample.png" % chat_id,
token=alice_token,
raw=content,
content_type="image/png",
)
expect(202, status, "upload while worker is stopped")
if attachment["status"] != "queued" or attachment["sha256"] != hashlib.sha256(content).hexdigest():
raise SystemExit("FAIL queued metadata")
print("attachment", attachment["id"])
status, _, _ = api.call("GET", "/api/v1/attachments/%s/content" % attachment["id"], token=bob_token)
expect(409, status, "content before ready")
status, message, _ = api.call(
"POST",
"/api/v1/chats/%s/messages" % chat_id,
{"text": "очередь не блокирует чат", "attachment_ids": [attachment["id"]]},
token=alice_token,
headers={"Idempotency-Key": uuid.uuid4().hex},
)
expect(201, status, "message while queued")
if message["attachment_ids"] != [attachment["id"]]:
raise SystemExit("FAIL queued attachment was not linkable")
compose("start", "worker")
ready = wait_ready(api, attachment["id"], bob_token)
thumbs = [item for item in ready["variants"] if item["name"] == "thumbnail"]
if len(thumbs) != 1 or (thumbs[0]["width"], thumbs[0]["height"]) != (256, 160):
raise SystemExit("FAIL thumbnail metadata %s" % thumbs)
names = sorted(item["name"] for item in ready["variants"])
if names != ["crop", "optimized", "thumbnail"]:
raise SystemExit("FAIL variants %s" % names)
status, _, payload = api.call(
"GET",
"/api/v1/attachments/%s/content?variant=thumbnail" % attachment["id"],
token=bob_token,
)
expect(200, status, "thumbnail bytes")
if hashlib.sha256(payload).hexdigest() != thumbs[0]["sha256"]:
raise SystemExit("FAIL thumbnail checksum")
print("PASS lab5 worker-down upload became ready", attachment["id"])
if __name__ == "__main__":
main()