ЛР6: наблюдаемость, SLO и диагностика отказов
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
from client import Client, check, run
|
||||
from pipeline_support import ready_api, wait_ready, metadata, compose, broker, sql
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
import hashlib
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
|
||||
|
||||
def main():
|
||||
ready_api()
|
||||
c = Client(base_url="http://127.0.0.1:18085", timeout=30)
|
||||
a, b, e, chat = c.bootstrap()
|
||||
png = Path("tests/fixtures/sample.png").read_bytes()
|
||||
path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=proof.png"
|
||||
|
||||
def upload(data=png, mime="image/png", expected=202):
|
||||
return c.call(
|
||||
"POST",
|
||||
path,
|
||||
expected,
|
||||
actor=a,
|
||||
raw=data,
|
||||
headers={"Content-Type": mime},
|
||||
schema="Attachment" if expected == 202 else None,
|
||||
)
|
||||
|
||||
compose("stop", "worker")
|
||||
attachment = upload()["json"]
|
||||
check(attachment["status"] == "queued", "upload response not queued snapshot")
|
||||
c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=b)
|
||||
c.message(a, chat, "", attachments=[attachment["id"]])
|
||||
compose("start", "worker")
|
||||
result = wait_ready(c, b, attachment)
|
||||
verify(c, b, attachment, png, result)
|
||||
print(
|
||||
"PASS queued snapshot, link before ready, 409 before worker; original + 3 variants",
|
||||
flush=True,
|
||||
)
|
||||
negative(c, a, e, chat, attachment, upload, png)
|
||||
fixtures(c, a, b, upload)
|
||||
concurrent(c, a, chat, attachment)
|
||||
print("PASS core pipeline contract", flush=True)
|
||||
|
||||
|
||||
def verify(c, actor, attachment, original, result):
|
||||
base = "/api/v1/attachments/" + attachment["id"] + "/content"
|
||||
check(c.call("GET", base, actor=actor)["bytes"] == original, "original changed")
|
||||
check(
|
||||
{v["name"] for v in result["variants"]} == {"thumbnail", "crop", "optimized"},
|
||||
"variant set differs",
|
||||
)
|
||||
for variant in result["variants"]:
|
||||
response = c.call("GET", base + "?variant=" + variant["name"], actor=actor)
|
||||
check(
|
||||
hashlib.sha256(response["bytes"]).hexdigest() == variant["sha256"], "variant checksum"
|
||||
)
|
||||
check(len(response["bytes"]) == variant["size_bytes"], "variant size")
|
||||
check(response["headers"].get_content_type() == variant["content_type"], "variant MIME")
|
||||
with Image.open(io.BytesIO(response["bytes"])) as image:
|
||||
check(image.size == (variant["width"], variant["height"]), "variant dimensions")
|
||||
check(not image.getexif(), "EXIF retained")
|
||||
|
||||
|
||||
def negative(c, a, e, chat, attachment, upload, png):
|
||||
upload(png[:33], expected=415)
|
||||
upload(png, "image/jpeg", 415)
|
||||
upload(b"x" * 10485761, expected=413)
|
||||
c.call(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat["id"] + "/attachments?filename=..%2Fx.png",
|
||||
400,
|
||||
actor=a,
|
||||
raw=png,
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
for suffix in ("", "/content"):
|
||||
c.call("GET", "/api/v1/attachments/" + attachment["id"] + suffix, 404, actor=e)
|
||||
other = c.call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
actor=a,
|
||||
body={"title": "other", "member_ids": [a["id"]]},
|
||||
schema="Chat",
|
||||
)["json"]
|
||||
c.call(
|
||||
"POST",
|
||||
"/api/v1/chats/" + other["id"] + "/messages",
|
||||
404,
|
||||
actor=a,
|
||||
body={"text": "x", "attachment_ids": [attachment["id"]]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
print("PASS bytes/corruption/MIME/filename/ACL/cross-chat negatives", flush=True)
|
||||
|
||||
|
||||
def fixtures(c, a, b, upload):
|
||||
output = io.BytesIO()
|
||||
Image.new("RGBA", (8, 4), (255, 0, 0, 0)).save(output, format="PNG")
|
||||
attachment = upload(output.getvalue())["json"]
|
||||
result = wait_ready(c, b, attachment)
|
||||
data = c.call(
|
||||
"GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", actor=b
|
||||
)["bytes"]
|
||||
with Image.open(io.BytesIO(data)) as image:
|
||||
check(
|
||||
image.size == (8, 4) and min(image.getpixel((0, 0))) >= 250, "alpha/upscale incorrect"
|
||||
)
|
||||
output = io.BytesIO()
|
||||
image = Image.new("RGB", (80, 40), (20, 80, 160))
|
||||
exif = image.getexif()
|
||||
exif[274] = 6
|
||||
image.save(output, format="JPEG", exif=exif)
|
||||
attachment = upload(output.getvalue(), "image/jpeg")["json"]
|
||||
result = wait_ready(c, b, attachment)
|
||||
thumb = next(v for v in result["variants"] if v["name"] == "thumbnail")
|
||||
check((thumb["width"], thumb["height"]) == (40, 80), "EXIF orientation ignored")
|
||||
verify(c, b, attachment, output.getvalue(), result)
|
||||
print("PASS alpha white, no small-image enlargement, EXIF rotation, crop and WebP", flush=True)
|
||||
|
||||
|
||||
def concurrent(c, a, chat, attachment):
|
||||
key = uuid.uuid4().hex
|
||||
|
||||
def send(_):
|
||||
return c.message(a, chat, "concurrent", key=key, attachments=[attachment["id"]])
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as pool:
|
||||
replies = list(pool.map(send, range(24)))
|
||||
check(len({r["json"]["id"] for r in replies}) == 1, "duplicate message effects")
|
||||
check(
|
||||
len({r["headers"]["X-Instance-Id"] for r in replies}) == 2, "two API replicas not observed"
|
||||
)
|
||||
print("PASS 24 concurrent requests across 2 API replicas; single message effect", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user