Асинхронная обработка вложений: RabbitMQ 4.1 (quorum-очереди, publisher confirms, manual ack, DLQ), transactional outbox, отдельный worker. - upload: оригинал в staging S3, затем одной транзакцией queued + jobs + outbox -> 202; API к брокеру не подключается - relay: публикация outbox с confirm, reaper истёкших аренд, redrive - worker: claim по строке jobs, аренда с heartbeat и fencing по lease_token, transform в подпроцессе с RLIMIT_AS/RLIMIT_CPU/таймаутом, thumbnail 256, crop 128, WebP 1280 по детерминированным ключам - retry с backoff и jitter, постоянные/временные ошибки, реестр failed, ops replay (ready не повторяется), DLQ для невалидных сообщений - janitor: очистка staging и сирот; миграция 004 с backfill вложений lab4 - тесты отказов: окна COMMIT/publish, крахи worker до/после S3 и ack, заморозка worker, всплеск загрузок, права брокера; evidence/ и REPORT.md
129 lines
7.1 KiB
Python
129 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверка вариантов по контракту (запускается В КОНТЕЙНЕРЕ checks, где есть Pillow):
|
|
|
|
docker compose --profile tools run --rm checks /app/student-tests/variants_check.py \
|
|
--base-url http://lb:8080 --output /app/evidence/variants-check.json
|
|
|
|
Для каждого входа: ready, ровно 3 варианта, метаданные = фактическим байтам (размер, SHA-256,
|
|
декодированные размеры, формат), оригинал неизменен. Thumbnail: JPEG, вписан в 256x256 без
|
|
увеличения, округление вниз; EXIF orientation применена; alpha -> белый. Crop: JPEG 128x128.
|
|
Optimized: WebP, вписан в 1280x1280 без увеличения. В вариантах нет EXIF/ICC.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, "/app/tests")
|
|
from client import Client, check # noqa: E402
|
|
from PIL import Image # noqa: E402
|
|
|
|
FIXTURE = Path("/app/tests/fixtures/sample.png").read_bytes()
|
|
|
|
|
|
def fit(w, h, box):
|
|
if w <= box and h <= box:
|
|
return w, h
|
|
return (box, max(1, h * box // w)) if w >= h else (max(1, w * box // h), box)
|
|
|
|
|
|
def encode(img, fmt, **kw):
|
|
buf = io.BytesIO()
|
|
img.save(buf, fmt, **kw)
|
|
return buf.getvalue()
|
|
|
|
|
|
def cases():
|
|
out = [("fixture 320x200 PNG", FIXTURE, "image/png", (320, 200), None)]
|
|
# RGBA: левая половина прозрачная -> в JPEG должна стать белой.
|
|
rgba = Image.new("RGBA", (300, 300), (200, 20, 20, 255))
|
|
rgba.paste((0, 0, 0, 0), (0, 0, 150, 300))
|
|
out.append(("RGBA 300x300 half transparent", encode(rgba, "PNG"), "image/png", (300, 300), "alpha"))
|
|
# EXIF orientation 6 (поворот 90° по часовой): хранится 400x200, показывается 200x400.
|
|
# Верхняя половина ХРАНИМОГО кадра красная -> после поворота красной становится правая половина.
|
|
raw = Image.new("RGB", (400, 200), (0, 0, 255))
|
|
raw.paste((255, 0, 0), (0, 0, 400, 100))
|
|
exif = Image.Exif()
|
|
exif[0x0112] = 6
|
|
exif[0x010F] = "MTUSI test camera" # Make: проверяем, что EXIF не утёк в варианты
|
|
out.append(("JPEG 400x200 EXIF orientation=6", encode(raw, "JPEG", quality=95, exif=exif.tobytes()),
|
|
"image/jpeg", (200, 400), "orientation"))
|
|
out.append(("tiny PNG 10x5 (no upscale)", encode(Image.new("RGB", (10, 5), (9, 200, 9)), "PNG"),
|
|
"image/png", (10, 5), None))
|
|
big = Image.radial_gradient("L").resize((3000, 2000)).convert("RGB")
|
|
out.append(("JPEG 3000x2000", encode(big, "JPEG", quality=90), "image/jpeg", (3000, 2000), None))
|
|
tall = Image.new("RGB", (100, 1000), (50, 60, 70))
|
|
out.append(("tall PNG 100x1000", encode(tall, "PNG"), "image/png", (100, 1000), None))
|
|
pal = Image.new("P", (64, 64), 1)
|
|
pal.putpalette([255, 255, 255, 10, 200, 10] + [0] * 762)
|
|
out.append(("palette PNG with transparency", encode(pal, "PNG", transparency=1), "image/png", (64, 64), "alpha"))
|
|
return out
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("--base-url", default="http://lb:8080")
|
|
p.add_argument("--output")
|
|
args = p.parse_args()
|
|
c = Client(args.base_url, timeout=30)
|
|
alice, bob, _eve, chat = c.bootstrap()
|
|
results = []
|
|
pending = []
|
|
for name, data, ctype, (ow, oh), special in cases():
|
|
att = c.call("POST", "/api/v1/chats/%s/attachments?filename=v.%s" % (chat["id"], ctype.split("/")[1]), 202,
|
|
actor=alice, raw=data, headers={"Content-Type": ctype}, schema="Attachment")["json"]
|
|
pending.append((name, data, ctype, ow, oh, special, att["id"]))
|
|
for name, data, ctype, ow, oh, special, att_id in pending:
|
|
deadline = time.monotonic() + 60
|
|
while True:
|
|
att = c.call("GET", "/api/v1/attachments/" + att_id, actor=bob, schema="Attachment")["json"]
|
|
if att["status"] in ("ready", "failed") or time.monotonic() > deadline:
|
|
break
|
|
time.sleep(0.3)
|
|
check(att["status"] == "ready", "%s: status %s" % (name, att["status"]))
|
|
r = {"case": name, "source_oriented": [ow, oh], "variants": {}}
|
|
orig = c.call("GET", "/api/v1/attachments/%s/content" % att_id, actor=bob)
|
|
check(orig["bytes"] == data and orig["headers"].get_content_type() == ctype, name + ": original changed")
|
|
check(sorted(v["name"] for v in att["variants"]) == ["crop", "optimized", "thumbnail"], name + ": variants")
|
|
expect = {"thumbnail": ("image/jpeg", "JPEG", fit(ow, oh, 256)),
|
|
"crop": ("image/jpeg", "JPEG", (128, 128)),
|
|
"optimized": ("image/webp", "WEBP", fit(ow, oh, 1280))}
|
|
for v in att["variants"]:
|
|
ct, fmt, size = expect[v["name"]]
|
|
body = c.call("GET", "/api/v1/attachments/%s/content?variant=%s" % (att_id, v["name"]), actor=bob)
|
|
b = body["bytes"]
|
|
img = Image.open(io.BytesIO(b))
|
|
img.load()
|
|
check(v["content_type"] == ct and body["headers"].get_content_type() == ct, name + ": content type")
|
|
check(img.format == fmt, name + ": real format " + str(img.format))
|
|
check((v["width"], v["height"]) == size == img.size, "%s %s: size %s meta %s expected %s" % (
|
|
name, v["name"], img.size, (v["width"], v["height"]), size))
|
|
check(v["size_bytes"] == len(b) and v["sha256"] == hashlib.sha256(b).hexdigest(), name + ": checksum")
|
|
no_meta = not img.getexif() and "icc_profile" not in img.info and "exif" not in img.info
|
|
check(no_meta, name + ": metadata leaked into " + v["name"])
|
|
item = {"size": list(img.size), "format": fmt, "bytes": len(b), "no_exif_icc": no_meta}
|
|
if special == "alpha" and v["name"] in ("thumbnail", "crop"):
|
|
px = img.convert("RGB").getpixel((1, img.size[1] // 2))
|
|
check(min(px) >= 245, name + ": transparent area is not white: %s" % (px,))
|
|
item["transparent_pixel_rgb"] = list(px)
|
|
if special == "orientation" and v["name"] == "thumbnail":
|
|
w, h = img.size
|
|
left, right = img.getpixel((w // 4, h // 2)), img.getpixel((3 * w // 4, h // 2))
|
|
check(right[0] > 200 and right[2] < 60 and left[2] > 200, name + ": orientation not applied")
|
|
item["left_rgb"], item["right_rgb"] = list(left), list(right)
|
|
r["variants"][v["name"]] = item
|
|
results.append(r)
|
|
print("PASS %-36s %s" % (name, {k: tuple(x["size"]) for k, x in r["variants"].items()}), flush=True)
|
|
c.call("GET", "/api/v1/attachments/%s/content?variant=bogus" % pending[0][6], 400, actor=bob)
|
|
if args.output:
|
|
Path(args.output).write_text(json.dumps({"cases": results, "unknown_variant_status": 400},
|
|
ensure_ascii=False, indent=1) + "\n")
|
|
print("PASS: %d inputs, all variants match contract" % len(results))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|