Files
mtusi2026lab5/student-tests/lab5lib.py
T
zemiles 13bec4ab8b ЛР 5 — БПИ2303 — Лазарев — на 4
Асинхронная обработка вложений: RabbitMQ 4.1 (quorum-очереди, publisher
confirms, manual ack, DLQ), transactional outbox, отдельный worker.

- upload: оригинал в staging S3, затем одной транзакцией queued + jobs +
  outbox -> 202; API к брокеру не подключается
- relay: публикация outbox с confirm, reaper истёкших аренд, redrive
- worker: claim по строке jobs, аренда с heartbeat и fencing по
  lease_token, transform в подпроцессе с RLIMIT_AS/RLIMIT_CPU/таймаутом,
  thumbnail 256, crop 128, WebP 1280 по детерминированным ключам
- retry с backoff и jitter, постоянные/временные ошибки, реестр failed,
  ops replay (ready не повторяется), DLQ для невалидных сообщений
- janitor: очистка staging и сирот; миграция 004 с backfill вложений lab4
- тесты отказов: окна COMMIT/publish, крахи worker до/после S3 и ack,
  заморозка worker, всплеск загрузок, права брокера; evidence/ и REPORT.md
2026-11-13 16:28:18 +03:00

176 lines
7.1 KiB
Python

"""Общие помощники проверок lab5 (запускаются на хосте, управляют стендом через docker compose).
Токены сессий живут только в памяти процесса и не печатаются. В evidence пишутся ID,
статусы, времена и события заданий — без секретов и содержимого файлов.
"""
import datetime as dt
import json
import os
import random
import struct
import subprocess
import sys
import time
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402,F401
SAMPLE_PNG = (ROOT / "tests/fixtures/sample.png").read_bytes()
def now():
return dt.datetime.now(dt.timezone.utc).strftime("%H:%M:%S.%f")[:-3]
class Log:
def __init__(self, name):
self.name = name
self.steps = []
self.started = time.monotonic()
def step(self, text, **data):
item = {"t": round(time.monotonic() - self.started, 2), "utc": now(), "step": text}
item.update(data)
self.steps.append(item)
print("[%7.2fs] %s %s" % (item["t"], text, json.dumps(data, ensure_ascii=False) if data else ""), flush=True)
def save(self, path, **extra):
if not path:
return
out = {"scenario": self.name, "date_utc": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
"steps": self.steps}
out.update(extra)
Path(path).parent.mkdir(parents=True, exist_ok=True)
Path(path).write_text(json.dumps(out, ensure_ascii=False, indent=1) + "\n", encoding="utf-8")
def compose(*args, env=None, timeout=300, ok=True):
full_env = dict(os.environ)
full_env.update(env or {})
proc = subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, text=True,
env=full_env, timeout=timeout)
if ok and proc.returncode != 0:
raise Failure("docker compose %s failed: %s" % (" ".join(args[:3]), proc.stderr.strip()[-400:]))
return proc
def recreate(service, env=None):
"""Пересоздать сервис с переопределёнными переменными (shell env важнее .env)."""
compose("up", "-d", "--no-deps", "--force-recreate", "--wait", service, env=env, timeout=400)
def sql(query):
proc = subprocess.run(
["docker", "compose", "exec", "-T", "db", "sh", "-c",
'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tA -F "|" -v ON_ERROR_STOP=1'],
cwd=ROOT, input=query, capture_output=True, text=True, timeout=60)
if proc.returncode != 0:
raise Failure("psql failed: " + proc.stderr.strip()[-300:])
return [line.split("|") for line in proc.stdout.strip().splitlines() if line]
def ops(*args, ok=True):
proc = compose("--profile", "tools", "run", "--rm", "-T", "ops", *args, ok=False, timeout=120)
if ok and proc.returncode not in (0, 3):
raise Failure("ops %s failed: %s" % (args[0], proc.stderr.strip()[-400:]))
try:
return json.loads(proc.stdout)
except ValueError:
raise Failure("ops output is not JSON: " + proc.stdout[-300:] + proc.stderr[-300:])
def queues():
proc = compose("exec", "-T", "broker", "rabbitmqctl", "-q", "-p", "messenger", "list_queues", "name",
"messages", "messages_ready", "messages_unacknowledged", "consumers", "--formatter", "json")
return {q["name"]: {k: q[k] for k in ("messages", "messages_ready", "messages_unacknowledged", "consumers")}
for q in json.loads(proc.stdout)}
def container_ids(service, running_only=True):
args = ["ps", "-q"] + (["--status", "running"] if running_only else []) + [service]
return [x for x in compose(*args).stdout.split() if x]
def upload(c, actor, chat, data, filename="photo.png", ctype="image/png", expected=202):
return c.call("POST", "/api/v1/chats/%s/attachments?filename=%s" % (chat["id"], filename), expected,
actor=actor, raw=data, headers={"Content-Type": ctype},
schema="Attachment" if expected == 202 else None)["json"]
def get_att(c, actor, att_id):
return c.call("GET", "/api/v1/attachments/" + att_id, actor=actor, schema="Attachment")["json"]
def wait_status(c, actor, att_id, statuses, timeout=90, interval=0.3):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
att = get_att(c, actor, att_id)
if att["status"] in statuses:
return att
time.sleep(interval)
raise Failure("attachment %s did not reach %s in %ss (last %s)" % (att_id, statuses, timeout, att["status"]))
def wait_until(fn, timeout, interval=0.5, what="condition"):
deadline = time.monotonic() + timeout
last = None
while time.monotonic() < deadline:
last = fn()
if last:
return last
time.sleep(interval)
raise Failure("timeout waiting for " + what)
def events(info):
return [e["event"] for e in info["events"]]
def invariants(info):
"""Ключ эффекта: ровно 3 варианта и 4 конечных объекта на (attachment, v1), staging убран."""
names = sorted(v["name"] for v in info["variants"])
finals = [o[0] for o in info["s3_final_objects"]]
return {
"variants": names,
"variants_ok": names == ["crop", "optimized", "thumbnail"],
"final_objects": len(finals),
"final_objects_ok": len(finals) == 4,
"staging_left": info["attachment"]["staging_key"] is not None,
"job_status": info["jobs"][0]["status"],
"attempt": info["jobs"][0]["attempt"],
}
def png(width, height, rgba=False, noise=False, color=(40, 120, 200), seed=1):
"""PNG стандартной библиотекой: шум (несжимаемый) или заливка."""
channels = 4 if rgba else 3
rng = random.Random(seed)
rows = []
if noise:
for _ in range(height):
rows.append(b"\x00" + bytes(rng.getrandbits(8) for _ in range(width * channels)))
else:
px = bytes(color[:3]) + (b"\x80" if rgba else b"")
row = b"\x00" + px * width
rows = [row] * height
raw = b"".join(rows)
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
ihdr = struct.pack(">IIBBBBB", width, height, 8, 6 if rgba else 2, 0, 0, 0)
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 6))
+ chunk(b"IEND", b""))
def noise_png_fast(width, height, seed=1):
"""Несжимаемый PNG через os.urandom (быстро для больших размеров)."""
raw = b"".join(b"\x00" + os.urandom(width * 3) for _ in range(height))
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 1)) + chunk(b"IEND", b"")