329 lines
12 KiB
Python
329 lines
12 KiB
Python
#!/usr/bin/env python3
|
||
"""Level 4 для lab4: негативные сценарии, компенсация при отказе БД, параллельные загрузки.
|
||
|
||
Запуск:
|
||
$env:API_CONTAINER = (docker ps --filter "label=com.docker.compose.service=api" -q | Select-Object -First 1)
|
||
python student-tests/lab4_test_level4.py --base-url http://localhost:8080
|
||
|
||
Сценарий компенсации (уровень 4):
|
||
Останавливаем PostgreSQL → делаем upload → S3 PUT успешен, INSERT в БД
|
||
падает → API удаляет объект из S3 (компенсация) → 503.
|
||
После восстановления PG считаем объекты в bucket через mc — количество
|
||
не должно вырасти. Это проверяет требование методички: «отказ между
|
||
записью S3/БД не оставляет вечных неучтённых объектов».
|
||
"""
|
||
import os
|
||
import subprocess
|
||
import sys
|
||
import threading
|
||
import time
|
||
import urllib.error
|
||
import urllib.parse
|
||
import urllib.request
|
||
import uuid
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
SAMPLE = ROOT / "tests" / "fixtures" / "sample.png"
|
||
API_CONTAINER = os.environ.get("API_CONTAINER", "")
|
||
COMPOSE_NETWORK = os.environ.get("COMPOSE_NETWORK", "mtuci2026lab4_default")
|
||
MC_IMAGE = os.environ.get("MC_IMAGE", "netvark/mc:20260920")
|
||
|
||
|
||
def _raw_upload(c: Client, actor, chat_id: str, filename: str, data: bytes, ctype: str):
|
||
# filename URL-encoded: иначе urllib откажется формировать URL с NUL/CR/LF.
|
||
qs = urllib.parse.urlencode({"filename": filename}, quote_via=urllib.parse.quote)
|
||
url = c.base + "/api/v1/chats/" + chat_id + "/attachments?" + qs
|
||
req = urllib.request.Request(
|
||
url, data=data, method="POST",
|
||
headers={
|
||
"Authorization": "Bearer " + actor["token"],
|
||
"Content-Type": ctype,
|
||
},
|
||
)
|
||
try:
|
||
with c.opener.open(req, timeout=30) as resp:
|
||
import json as _json
|
||
return resp.code, _json.loads(resp.read()), resp.headers
|
||
except urllib.error.HTTPError as e:
|
||
import json as _json
|
||
body = e.read()
|
||
try:
|
||
return e.code, _json.loads(body), e.headers
|
||
except Exception:
|
||
return e.code, None, e.headers
|
||
|
||
|
||
def _upload(c, actor, chat, data, filename="sample.png", ctype="image/png"):
|
||
return _raw_upload(c, actor, chat["id"], filename, data, ctype)
|
||
|
||
|
||
# ---------- негативные сценарии ----------
|
||
|
||
def test_oversize(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
big = b"\x89PNG\r\n\x1a\n" + b"\x00" * (10_485_761)
|
||
status, body, _h = _upload(c, alice, chat, big)
|
||
check(status == 413, "oversize upload must be 413, got %s" % status)
|
||
|
||
|
||
def test_corrupted_image(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
bad = b"\x89PNG\r\n\x1a\n" + b"not-a-real-png" * 100
|
||
status, body, _h = _upload(c, alice, chat, bad)
|
||
check(status == 415, "corrupted PNG must be 415, got %s" % status)
|
||
|
||
|
||
def test_mime_spoof(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
status, body, _h = _upload(c, alice, chat, b"hello, not an image" * 10, ctype="image/png")
|
||
check(status == 415, "spoofed MIME must be 415, got %s" % status)
|
||
|
||
|
||
def test_bad_filename(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
data = SAMPLE.read_bytes()
|
||
for bad in ("../evil.png", "a/b.png", "a\\b.png", "a\x00b.png", "a\nb.png", " "):
|
||
status, body, _h = _upload(c, alice, chat, data, filename=bad)
|
||
check(status == 400, "bad filename %r must be 400, got %s" % (bad, status))
|
||
|
||
|
||
# ---------- изоляция доступа ----------
|
||
|
||
def test_foreign_attachment_and_cross_chat(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
chat2 = c.call("POST", "/api/v1/chats", 201, actor=alice,
|
||
body={"title": "Второй", "member_ids": [alice["id"], eve["id"]]},
|
||
schema="Chat")["json"]
|
||
data = SAMPLE.read_bytes()
|
||
status, att, _h = _upload(c, alice, chat2, data)
|
||
check(status == 201, "upload to chat2 must be 201, got %s" % status)
|
||
att_id = att["id"]
|
||
|
||
c.call("GET", "/api/v1/attachments/" + att_id, 404, actor=bob)
|
||
c.call("GET", "/api/v1/attachments/" + att_id + "/content", 404, actor=bob)
|
||
|
||
c.call(
|
||
"POST", "/api/v1/chats/" + chat["id"] + "/messages",
|
||
404, actor=bob,
|
||
body={"text": "", "attachment_ids": [att_id]},
|
||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||
)
|
||
|
||
|
||
def test_attachment_belongs_to_chat(c):
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
other = c.call("POST", "/api/v1/chats", 201, actor=alice,
|
||
body={"title": "Другой", "member_ids": [alice["id"], bob["id"]]},
|
||
schema="Chat")["json"]
|
||
data = SAMPLE.read_bytes()
|
||
status, att, _h = _upload(c, alice, chat, data)
|
||
check(status == 201, "upload to chat must be 201")
|
||
c.call(
|
||
"POST", "/api/v1/chats/" + other["id"] + "/messages",
|
||
404, actor=alice,
|
||
body={"text": "", "attachment_ids": [att["id"]]},
|
||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||
)
|
||
|
||
|
||
# ---------- компенсация: S3 PUT успешен, INSERT в БД падает ----------
|
||
|
||
def _compose(action, service):
|
||
return subprocess.run(
|
||
["docker", "compose", "--env-file", ".env", action, service],
|
||
capture_output=True, text=True, timeout=90,
|
||
)
|
||
|
||
|
||
def _wait_ready(c, timeout=90):
|
||
deadline = time.monotonic() + timeout
|
||
while time.monotonic() < deadline:
|
||
try:
|
||
c.call("GET", "/health/ready", 200)
|
||
return
|
||
except Failure:
|
||
time.sleep(1)
|
||
raise Failure("API не вернулся в ready за %d секунд" % timeout)
|
||
|
||
|
||
def _count_bucket_objects() -> int:
|
||
env = os.environ.copy()
|
||
env_file = ROOT / ".env"
|
||
if env_file.is_file():
|
||
for line in env_file.read_text(encoding="utf-8").splitlines():
|
||
line = line.strip()
|
||
if not line or line.startswith("#") or "=" not in line:
|
||
continue
|
||
k, v = line.split("=", 1)
|
||
env.setdefault(k.strip(), v.strip())
|
||
|
||
user = env.get("MINIO_ROOT_USER", "minioadmin")
|
||
password = env.get("MINIO_ROOT_PASSWORD", "minioadmin")
|
||
bucket = env.get("S3_BUCKET", "messenger-attachments")
|
||
mc_host = "http://%s:%s@minio:9000" % (user, password)
|
||
|
||
# Entrypoint образа mc обычно сам mc; тогда подкоманду зовём без префикса "mc".
|
||
ins = subprocess.run(
|
||
["docker", "image", "inspect", MC_IMAGE, "--format", "{{json .Config.Entrypoint}}"],
|
||
capture_output=True, text=True, timeout=10,
|
||
)
|
||
ep = (ins.stdout or "").strip()
|
||
prefix = [] if (ep and ep != "null" and "mc" in ep) else ["mc"]
|
||
|
||
r = subprocess.run(
|
||
["docker", "run", "--rm", "--network", COMPOSE_NETWORK,
|
||
"-e", "MC_HOST_local=" + mc_host, MC_IMAGE]
|
||
+ prefix
|
||
+ ["ls", "--recursive", "local/" + bucket],
|
||
capture_output=True, text=True, timeout=60,
|
||
)
|
||
if r.returncode != 0:
|
||
# Диагностика при первом запуске — увидим, что именно не так.
|
||
print(" mc stderr:", (r.stderr or "").strip()[:300])
|
||
return -1
|
||
return sum(1 for line in r.stdout.splitlines() if line.startswith("["))
|
||
|
||
|
||
def test_compensation_on_db_failure(c):
|
||
"""S3 PUT прошёл, INSERT в БД упал → объект удаляется из S3, ответ 503.
|
||
|
||
Сценарий: останавливаем PostgreSQL. API-реплика сначала делает PUT в S3
|
||
(успешно), затем INSERT в attachments (падает: connection error).
|
||
В except-ветке upload_attachment вызывается s3.delete_object —
|
||
orphan не остаётся. Ответ — 503 unavailable.
|
||
После восстановления PG сверяем количество объектов в bucket.
|
||
"""
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
data = SAMPLE.read_bytes()
|
||
|
||
before = _count_bucket_objects()
|
||
check(before >= 0, "не удалось посчитать объекты в bucket (mc)")
|
||
|
||
stopped = _compose("stop", "postgres")
|
||
check(stopped.returncode == 0, "docker compose stop postgres failed")
|
||
time.sleep(2)
|
||
try:
|
||
status, body, _h = _upload(c, alice, chat, data)
|
||
check(status in (500, 503),
|
||
"compensation expected 5xx, got %s (body=%r)" % (status, body))
|
||
print(" upload при мёртвой БД вернул %s" % status)
|
||
finally:
|
||
started = _compose("start", "postgres")
|
||
check(started.returncode == 0, "docker compose start postgres failed")
|
||
_wait_ready(c, timeout=90)
|
||
|
||
after = _count_bucket_objects()
|
||
check(after == before,
|
||
"orphan-объект остался в bucket: before=%s after=%s" % (before, after))
|
||
print(" компенсация: bucket не вырос (before=%s after=%s)" % (before, after))
|
||
|
||
|
||
# ---------- параллельные загрузки с измерением RSS ----------
|
||
|
||
def test_parallel_uploads_memory(c):
|
||
"""Уровень 4: ≥10 одновременных загрузок с измерением пика RSS."""
|
||
alice, bob, eve, chat = c.bootstrap()
|
||
data = SAMPLE.read_bytes()
|
||
barrier = threading.Barrier(10)
|
||
errors = []
|
||
|
||
def one(_):
|
||
local = Client(c.base, timeout=60)
|
||
barrier.wait()
|
||
try:
|
||
status, att, _h = _upload(local, alice, chat, data)
|
||
if status != 201:
|
||
errors.append("status=%s" % status)
|
||
except Exception as exc: # noqa: BLE001
|
||
errors.append(type(exc).__name__)
|
||
|
||
def rss():
|
||
if not API_CONTAINER:
|
||
return None
|
||
try:
|
||
out = subprocess.run(
|
||
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", API_CONTAINER],
|
||
capture_output=True, text=True, timeout=10,
|
||
)
|
||
used = out.stdout.strip().split("/")[0].strip()
|
||
if used.endswith("MiB"):
|
||
return float(used[:-3])
|
||
if used.endswith("GiB"):
|
||
return float(used[:-3]) * 1024
|
||
except Exception:
|
||
return None
|
||
return None
|
||
|
||
peak = {"value": 0.0}
|
||
stop = threading.Event()
|
||
|
||
def sampler():
|
||
while not stop.is_set():
|
||
v = rss()
|
||
if v and v > peak["value"]:
|
||
peak["value"] = v
|
||
time.sleep(0.2)
|
||
|
||
sampler_thread = threading.Thread(target=sampler, daemon=True)
|
||
sampler_thread.start()
|
||
|
||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||
list(pool.map(one, range(10)))
|
||
|
||
stop.set()
|
||
sampler_thread.join(timeout=2)
|
||
|
||
check(not errors, "parallel uploads errors: %s" % errors[:3])
|
||
print(" 10 параллельных загрузок, пик RSS=%s MiB" % peak["value"])
|
||
if peak["value"]:
|
||
check(peak["value"] < 400, "peak RSS %s MiB слишком высок" % peak["value"])
|
||
|
||
|
||
# ---------- main ----------
|
||
|
||
def main():
|
||
args = arguments("Lab4 level 4 checks")
|
||
c = Client(args.base_url, args.ca_file)
|
||
|
||
print("=== oversize ===")
|
||
test_oversize(c)
|
||
print("PASS: oversize -> 413")
|
||
|
||
print("=== corrupted image ===")
|
||
test_corrupted_image(c)
|
||
print("PASS: corrupted -> 415")
|
||
|
||
print("=== spoofed MIME ===")
|
||
test_mime_spoof(c)
|
||
print("PASS: spoofed MIME -> 415")
|
||
|
||
print("=== bad filename ===")
|
||
test_bad_filename(c)
|
||
print("PASS: bad filename -> 400")
|
||
|
||
print("=== foreign attachment / cross-chat ===")
|
||
test_foreign_attachment_and_cross_chat(c)
|
||
print("PASS: foreign -> 404, cross-chat -> 404")
|
||
|
||
print("=== attachment belongs to chat ===")
|
||
test_attachment_belongs_to_chat(c)
|
||
print("PASS: attachment from another chat -> 404")
|
||
|
||
print("=== compensation on DB failure ===")
|
||
test_compensation_on_db_failure(c)
|
||
print("PASS: компенсация orphan-объектов при отказе БД")
|
||
|
||
print("=== parallel uploads memory ===")
|
||
test_parallel_uploads_memory(c)
|
||
print("PASS: 10 параллельных загрузок в пределах лимита памяти")
|
||
|
||
print("PASS: all lab4 level-4 checks passed")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
run(main) |