feat(observability): add bounded OTLP signals, causal jobs and SLO rules
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
import hashlib
|
||||
import io
|
||||
import time
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from conftest import check, headers
|
||||
from PIL import Image
|
||||
|
||||
PNG = (Path(__file__).parents[1] / "tests/fixtures/sample.png").read_bytes()
|
||||
|
||||
|
||||
def upload(
|
||||
client, actor, chat, *, data=PNG, filename="Фото 🌍.png", mime="image/png"
|
||||
):
|
||||
return client.post(
|
||||
f"/api/v1/chats/{chat['id']}/attachments",
|
||||
headers={**headers(actor), "Content-Type": mime},
|
||||
params={"filename": filename},
|
||||
content=data,
|
||||
)
|
||||
|
||||
|
||||
def wait_ready(client, actor, attachment):
|
||||
end = time.monotonic() + 60
|
||||
while time.monotonic() < end:
|
||||
row = check(
|
||||
client.get(
|
||||
f"/api/v1/attachments/{attachment['id']}",
|
||||
headers=headers(actor),
|
||||
),
|
||||
200,
|
||||
"Attachment",
|
||||
)
|
||||
if row["status"] == "ready":
|
||||
return row
|
||||
assert row["status"] != "failed", row
|
||||
time.sleep(0.2)
|
||||
raise TimeoutError("Pipeline readiness")
|
||||
|
||||
|
||||
def test_attachment_roundtrip_acl_and_message(client, actors, chat):
|
||||
attachment = check(upload(client, actors[0], chat), 202, "Attachment")
|
||||
assert attachment["status"] == "queued" and attachment["variants"] == []
|
||||
attachment = wait_ready(client, actors[0], attachment)
|
||||
assert attachment["sha256"] == hashlib.sha256(PNG).hexdigest()
|
||||
assert attachment["size_bytes"] == len(PNG)
|
||||
path = f"/api/v1/attachments/{attachment['id']}"
|
||||
assert (
|
||||
check(client.get(path, headers=headers(actors[1])), 200, "Attachment")
|
||||
== attachment
|
||||
)
|
||||
downloaded = client.get(path + "/content", headers=headers(actors[1]))
|
||||
assert downloaded.status_code == 200 and downloaded.content == PNG
|
||||
assert downloaded.headers["Content-Type"] == "image/png"
|
||||
assert not downloaded.history
|
||||
for suffix in ["", "/content"]:
|
||||
check(
|
||||
client.get(path + suffix, headers=headers(actors[2])), 404, "Error"
|
||||
)
|
||||
check(client.get(path + suffix), 401, "Error")
|
||||
thumb = client.get(
|
||||
path + "/content",
|
||||
headers=headers(actors[1]),
|
||||
params={"variant": "thumbnail"},
|
||||
)
|
||||
assert thumb.status_code == 200
|
||||
with Image.open(io.BytesIO(thumb.content)) as image:
|
||||
assert image.format == "JPEG" and image.size == (256, 160)
|
||||
sent = check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[1]),
|
||||
json={"text": "", "attachment_ids": [attachment["id"]]},
|
||||
),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
assert sent["attachment_ids"] == [attachment["id"]]
|
||||
assert sent["sender_id"] == actors[1]["id"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name",
|
||||
[
|
||||
"../x.png",
|
||||
"x/y.png",
|
||||
"x\\y.png",
|
||||
"x\x00.png",
|
||||
"x\x7f.png",
|
||||
"x\n.png",
|
||||
"a" * 256,
|
||||
"",
|
||||
],
|
||||
)
|
||||
def test_filename_guard(client, actors, chat, name):
|
||||
check(upload(client, actors[0], chat, filename=name), 400, "Error")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data,mime,status",
|
||||
[
|
||||
(b"not an image", "image/png", 415),
|
||||
(PNG, "image/jpeg", 415),
|
||||
(PNG, "application/octet-stream", 415),
|
||||
(PNG[:25], "image/png", 415),
|
||||
(b"", "image/png", 415),
|
||||
(b"x" * (10 * 1024 * 1024 + 1), "image/png", 413),
|
||||
],
|
||||
)
|
||||
def test_image_guards(client, actors, chat, data, mime, status):
|
||||
r = upload(client, actors[0], chat, data=data, mime=mime)
|
||||
check(r, status, "Error")
|
||||
|
||||
|
||||
def test_jpeg_original_unchanged(client, actors, chat):
|
||||
stream = io.BytesIO()
|
||||
Image.new("RGB", (320, 200), (30, 60, 90)).save(stream, format="JPEG")
|
||||
jpeg = stream.getvalue()
|
||||
attachment = check(
|
||||
upload(
|
||||
client,
|
||||
actors[0],
|
||||
chat,
|
||||
data=jpeg,
|
||||
filename="camera.jpg",
|
||||
mime="image/jpeg",
|
||||
),
|
||||
202,
|
||||
"Attachment",
|
||||
)
|
||||
attachment = wait_ready(client, actors[0], attachment)
|
||||
r = client.get(
|
||||
f"/api/v1/attachments/{attachment['id']}/content",
|
||||
headers=headers(actors[1]),
|
||||
)
|
||||
assert r.content == jpeg
|
||||
|
||||
|
||||
def test_cross_chat_link_and_unknown(client, actors, chat):
|
||||
a = check(upload(client, actors[0], chat), 202, "Attachment")
|
||||
other = check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers=headers(actors[0]),
|
||||
json={"title": "Other", "member_ids": [actors[0]["id"]]},
|
||||
),
|
||||
201,
|
||||
"Chat",
|
||||
)
|
||||
for identity in [a["id"], str(uuid4())]:
|
||||
check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{other['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
json={"text": "", "attachment_ids": [identity]},
|
||||
),
|
||||
404,
|
||||
"Error",
|
||||
)
|
||||
check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
json={"text": "", "attachment_ids": [a["id"], a["id"]]},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_default_attachments_are_semantically_equal(client, actors, chat):
|
||||
h = headers(actors[0])
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
first = check(
|
||||
client.post(path, headers=h, json={"text": "Semantic default"}),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
second = check(
|
||||
client.post(
|
||||
path,
|
||||
headers=h,
|
||||
json={"attachment_ids": [], "text": "Semantic default"},
|
||||
),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
assert first == second and first["attachment_ids"] == []
|
||||
|
||||
|
||||
def test_pixel_limit_before_decode(client, actors, chat):
|
||||
# Compressible synthetic image stays below10MiB but exceeds40M pixels.
|
||||
stream = io.BytesIO()
|
||||
Image.new("1", (8000, 5001)).save(stream, format="PNG")
|
||||
assert len(stream.getvalue()) < 10 * 1024 * 1024
|
||||
r = upload(client, actors[0], chat, data=stream.getvalue())
|
||||
assert r.status_code == 413
|
||||
|
||||
|
||||
def test_chunked_body_limit_without_content_length(client, actors, chat):
|
||||
r = client.post(
|
||||
f"/api/v1/chats/{chat['id']}/attachments",
|
||||
headers={**headers(actors[0]), "Content-Type": "image/png"},
|
||||
params={"filename": "chunked.png"},
|
||||
content=iter([b"x" * (10 * 1024 * 1024 + 1)]),
|
||||
)
|
||||
assert r.request.headers.get("content-length") is None
|
||||
assert r.status_code == 413
|
||||
assert r.json()["error"]["code"] == "payload_too_large"
|
||||
assert r.json()["error"]["request_id"] == r.headers["x-request-id"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("size", [(1, 17), (320, 200), (1600, 900)])
|
||||
def test_variant_geometry_and_alpha(client, actors, chat, size):
|
||||
stream = io.BytesIO()
|
||||
Image.new("RGBA", size, (0, 0, 0, 0)).save(stream, format="PNG")
|
||||
a = check(
|
||||
upload(client, actors[0], chat, data=stream.getvalue()),
|
||||
202,
|
||||
"Attachment",
|
||||
)
|
||||
a = wait_ready(client, actors[0], a)
|
||||
for v in a["variants"]:
|
||||
r = client.get(
|
||||
f"/api/v1/attachments/{a['id']}/content",
|
||||
headers=headers(actors[1]),
|
||||
params={"variant": v["name"]},
|
||||
)
|
||||
assert len(r.content) == v["size_bytes"]
|
||||
assert hashlib.sha256(r.content).hexdigest() == v["sha256"]
|
||||
with Image.open(io.BytesIO(r.content)) as image:
|
||||
assert image.size == (v["width"], v["height"])
|
||||
assert not image.getexif()
|
||||
assert min(image.convert("RGB").getpixel((0, 0))) >= 250
|
||||
if v["name"] == "crop":
|
||||
assert image.size == (128, 128)
|
||||
else:
|
||||
bound = 256 if v["name"] == "thumbnail" else 1280
|
||||
assert max(image.size) <= bound
|
||||
assert image.width <= size[0] and image.height <= size[1]
|
||||
|
||||
|
||||
def test_exif_orientation_applied_but_original_preserved(client, actors, chat):
|
||||
stream = io.BytesIO()
|
||||
exif = Image.Exif()
|
||||
exif[274] = 6
|
||||
exif[270] = "Synthetic metadata must be removed"
|
||||
Image.new("RGB", (320, 200), "red").save(stream, format="JPEG", exif=exif)
|
||||
raw = stream.getvalue()
|
||||
a = check(
|
||||
upload(
|
||||
client,
|
||||
actors[0],
|
||||
chat,
|
||||
data=raw,
|
||||
mime="image/jpeg",
|
||||
filename="exif.jpg",
|
||||
),
|
||||
202,
|
||||
"Attachment",
|
||||
)
|
||||
a = wait_ready(client, actors[0], a)
|
||||
r = client.get(
|
||||
f"/api/v1/attachments/{a['id']}/content", headers=headers(actors[1])
|
||||
)
|
||||
assert r.content == raw
|
||||
v = next(v for v in a["variants"] if v["name"] == "thumbnail")
|
||||
assert (v["width"], v["height"]) == (160, 256)
|
||||
r = client.get(
|
||||
f"/api/v1/attachments/{a['id']}/content",
|
||||
headers=headers(actors[1]),
|
||||
params={"variant": "thumbnail"},
|
||||
)
|
||||
with Image.open(io.BytesIO(r.content)) as image:
|
||||
assert image.size == (160, 256) and not image.getexif()
|
||||
Reference in New Issue
Block a user