Files
mtusi2026lab4-fork/tests/own.py
daniil_sobolev 747ca222e5 Implement lab4 attachments on RustFS for grade 4.
Carry over lab3 HA stack and add private S3 storage, Attachment API, orphan compensation/reaper, own tests, and evidence for smoke/persistence/failover.
2026-11-12 20:41:02 +03:00

283 lines
8.6 KiB
Python

#!/usr/bin/env python3
"""Own lab4 tests (grade 4): attachments, limits, ACL, private bucket."""
import hashlib
import struct
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from client import Client, Failure, arguments, check, run
ROOT = Path(__file__).resolve().parent
SAMPLE = (ROOT / "fixtures" / "sample.png").read_bytes()
def _png(width: int, height: int, rgba=(0, 128, 255, 255)) -> bytes:
"""Minimal valid solid-color PNG."""
import zlib
def chunk(tag: bytes, data: bytes) -> bytes:
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF)
raw = b"".join(b"\x00" + bytes(rgba) * width for _ in range(height))
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0))
+ chunk(b"IDAT", zlib.compress(raw, 9))
+ chunk(b"IEND", b"")
)
def test_multiple_instance_ids(c: Client):
alice = c.user("inst")
seen = set()
for _ in range(40):
seen.add(
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")["headers"][
"X-Instance-Id"
]
)
check(len(seen) >= 2, "expected >=2 X-Instance-Id via LB, got %r" % (seen,))
print("PASS: observed instances %s" % sorted(seen))
def test_attachment_happy_path(c: Client):
alice = c.user("attok")
bob = c.user("attokb")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Att", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
att, content = c.attachment(alice, chat)
meta = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")[
"json"
]
check(meta["sha256"] == hashlib.sha256(content).hexdigest(), "meta sha mismatch")
dl = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)
check(dl["bytes"] == content, "download mismatch")
msg = c.message(alice, chat, "", attachments=[att["id"]])["json"]
check(msg["attachment_ids"] == [att["id"]], "message lost attachment_ids")
print("PASS: attachment upload/download/message")
def test_limits_and_mime(c: Client):
alice = c.user("limits")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Limits", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=x.png"
huge = b"\x89PNG\r\n\x1a\n" + (b"0" * (10 * 1024 * 1024 + 1))
c.call(
"POST",
path,
413,
actor=alice,
raw=huge,
headers={"Content-Type": "image/png"},
schema="Error",
)
c.call(
"POST",
path,
415,
actor=alice,
raw=b"not-an-image",
headers={"Content-Type": "image/png"},
schema="Error",
)
# MIME spoof: JPEG magic with image/png Content-Type
jpeg_magic = b"\xff\xd8\xff\xe0" + b"\x00" * 64
c.call(
"POST",
path,
415,
actor=alice,
raw=jpeg_magic,
headers={"Content-Type": "image/png"},
schema="Error",
)
c.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/attachments?filename=evil/../x.png",
400,
actor=alice,
raw=SAMPLE,
headers={"Content-Type": "image/png"},
schema="Error",
)
print("PASS: 413/415/400 on bad uploads")
def test_acl_and_foreign_attachment(c: Client):
alice = c.user("acl_a")
bob = c.user("acl_b")
eve = c.user("acl_e")
chat_ab = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "AB", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
chat_ae = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "AE", "member_ids": [alice["id"], eve["id"]]},
schema="Chat",
)["json"]
att, _ = c.attachment(alice, chat_ab)
c.call("GET", "/api/v1/attachments/" + att["id"], 404, actor=eve, schema="Error")
c.call(
"GET",
"/api/v1/attachments/" + att["id"] + "/content",
404,
actor=eve,
schema="Error",
)
# Attach AB file into AE chat → 404
c.call(
"POST",
"/api/v1/chats/" + chat_ae["id"] + "/messages",
404,
actor=alice,
body={"text": "", "attachment_ids": [att["id"]]},
headers={"Idempotency-Key": uuid.uuid4().hex},
schema="Error",
)
# Unknown attachment id
c.call(
"POST",
"/api/v1/chats/" + chat_ab["id"] + "/messages",
404,
actor=alice,
body={"text": "", "attachment_ids": [str(uuid.uuid4())]},
headers={"Idempotency-Key": uuid.uuid4().hex},
schema="Error",
)
print("PASS: outsider 404 and foreign chat attachment 404")
def test_private_bucket(c: Client):
"""Unauthenticated GET to MinIO must not return the object."""
alice = c.user("priv")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Priv", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
att, _ = c.attachment(alice, chat)
# Guess key pattern used by API; anonymous access must fail either way.
key = "attachments/%s/%s" % (chat["id"], att["id"])
url = "http://127.0.0.1:9000/messenger-attachments/" + key
try:
urlopen(Request(url, method="GET"), timeout=5)
raise Failure("anonymous S3 GET unexpectedly succeeded")
except HTTPError as exc:
check(exc.code in (403, 404), "expected 403/404 from private bucket, got %s" % exc.code)
except URLError as exc:
# MinIO not published — still record that API download works.
print("WARN: cannot reach MinIO from host (%s); API ACL still checked" % exc.reason)
# Authorized API still works
c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=alice)
print("PASS: private bucket / API-only download")
def test_concurrent_uploads(c: Client):
alice = c.user("conc")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Conc", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
png = _png(64, 64)
barrier = threading.Barrier(10)
results = []
def worker(i):
local = Client(c.base, timeout=60)
barrier.wait()
try:
att = local.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/attachments?filename=c%d.png" % i,
201,
actor=alice,
raw=png,
headers={"Content-Type": "image/png"},
schema="Attachment",
)["json"]
results.append(att["id"])
except Failure as exc:
results.append(str(exc))
t0 = time.monotonic()
with ThreadPoolExecutor(max_workers=10) as pool:
list(pool.map(worker, range(10)))
elapsed = time.monotonic() - t0
ids = [r for r in results if isinstance(r, str) and len(r) == 36]
check(len(ids) == 10, "expected 10 uploads, got %r" % (results,))
check(len(set(ids)) == 10, "duplicate attachment ids")
print("PASS: 10 concurrent uploads in %.2fs (observe API mem_limit separately)" % elapsed)
def test_idempotency_with_attachments(c: Client):
alice = c.user("idematt")
chat = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "IdemAtt", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
att, _ = c.attachment(alice, chat)
key = uuid.uuid4().hex
first = c.message(alice, chat, "", key=key, attachments=[att["id"]])["json"]
second = c.message(alice, chat, "", key=key, attachments=[att["id"]])["json"]
check(first == second, "idempotent attachment message replay")
print("PASS: idempotency with attachment_ids")
def main():
args = arguments("Own lab4 tests")
c = Client(args.base_url, args.ca_file, timeout=120)
c.call("GET", "/health/ready", schema="Health")
test_multiple_instance_ids(c)
test_attachment_happy_path(c)
test_limits_and_mime(c)
test_acl_and_foreign_attachment(c)
test_private_bucket(c)
test_concurrent_uploads(c)
test_idempotency_with_attachments(c)
print("PASS: all own lab4 tests")
if __name__ == "__main__":
run(main)