Files
mtusi2026lab4/student-tests/test_attachment_resilience.py
T
alenchha 078a8341b5 ЛР 4: вложения в S3 (MinIO), reconciler БД<->S3, отчёт и evidence
Перенесён минимум lab3. Добавлены загрузка/скачивание PNG/JPEG через API
с потоковой проверкой целостности, метаданные в PostgreSQL и байты в
приватном bucket, отдельный S3-пользователь с ограниченными правами,
идемпотентный bootstrap bucket, компенсация и reconciler (трек 2),
тесты отказов, эксперименты по памяти и согласованности.
2026-11-02 16:59:17 +03:00

58 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Upload -> stop the serving replica -> download via the other; then restart everything and download again."""
import hashlib
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check, run # noqa: E402
def compose(*args):
subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True, capture_output=True)
def wait_ready(c):
for _ in range(60):
try:
c.call("GET", "/health/ready")
return
except Exception:
time.sleep(2)
check(False, "stack did not become ready")
def main():
c = Client()
alice, bob, _, chat = c.bootstrap()
att, content = c.attachment(alice, chat)
served_by = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["headers"]["X-Instance-Id"]
print("metadata served by", served_by)
first = c.call("POST", "/api/v1/chats/%s/attachments?filename=x.png" % chat["id"], 201, actor=alice, raw=content,
headers={"Content-Type": "image/png"}, schema="Attachment")
uploader = first["headers"]["X-Instance-Id"]
compose("stop", "-t", "3", uploader)
try:
time.sleep(3)
got = c.call("GET", "/api/v1/attachments/%s/content" % first["json"]["id"], actor=bob)
check(got["headers"]["X-Instance-Id"] != uploader, "download must be served by another replica")
check(hashlib.sha256(got["bytes"]).hexdigest() == first["json"]["sha256"], "bytes differ after uploader stopped")
print("ok uploaded via %s, downloaded via %s, identical sha256" % (uploader, got["headers"]["X-Instance-Id"]))
finally:
compose("start", uploader)
wait_ready(c)
compose("restart", "-t", "5", "postgres", "valkey", "minio", "api-1", "api-2", "nginx")
wait_ready(c)
got = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)
check(got["bytes"] == content, "original lost after full restart")
meta = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["json"]
check(meta == att, "metadata changed after restart")
print("ok restart of all stateful services keeps metadata and original")
print("PASS: attachment failover + persistence")
run(main)