forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
[ -f .env ] || { cp .env.example .env; echo ".env создан из .env.example"; }
|
||||
mkdir -p secrets
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
env = Path('.env')
|
||||
lines = env.read_text().splitlines()
|
||||
values = dict(line.split('=', 1) for line in lines if '=' in line and not line.startswith('#'))
|
||||
password_file = Path('secrets/postgres_password.txt')
|
||||
values['POSTGRES_PASSWORD'] = values.get('POSTGRES_PASSWORD') or (password_file.read_text().strip() if password_file.exists() else secrets.token_hex(24))
|
||||
values['DATABASE_URL'] = values.get('DATABASE_URL') or f"postgres://{values.get('POSTGRES_USER', 'messenger')}:{values['POSTGRES_PASSWORD']}@postgres:5432/{values.get('POSTGRES_DB', 'messenger')}?sslmode=disable"
|
||||
for key in ('POSTGRES_PASSWORD', 'DATABASE_URL'):
|
||||
lines = [line for line in lines if not line.startswith(key + '=')]
|
||||
lines.append(key + '=' + values[key])
|
||||
for filename, key in [('postgres_password.txt', 'POSTGRES_PASSWORD'), ('database_url.txt', 'DATABASE_URL')]:
|
||||
p = Path('secrets') / filename
|
||||
if not p.exists():
|
||||
p.write_text(values[key])
|
||||
env.write_text('\n'.join(lines) + '\n')
|
||||
env.chmod(0o600)
|
||||
PY
|
||||
./scripts/gen-s3-secrets.sh
|
||||
chmod 644 secrets/*
|
||||
echo "секреты готовы"
|
||||
@@ -0,0 +1,53 @@
|
||||
import base64, hashlib, json, struct, subprocess, sys, time, urllib.request, uuid, zlib
|
||||
|
||||
BASE = "http://localhost:8080"; PWD = "correct-horse-battery-staple"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
req = urllib.request.Request(BASE + path, data=body, method=method, headers=headers or {})
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, r.headers, r.read() # r.headers нечувствителен к регистру
|
||||
|
||||
|
||||
def chunk(t, d):
|
||||
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
||||
|
||||
|
||||
png = (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 16, 16, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(b"".join(b"\x00" + bytes([i, 80, 200]) * 16 for i in range(16))))
|
||||
+ chunk(b"IEND", b""))
|
||||
|
||||
j = {"Content-Type": "application/json"}
|
||||
name = "af_" + uuid.uuid4().hex[:8]
|
||||
_, _, raw = call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": "a", "password": PWD}).encode(), j)
|
||||
user = json.loads(raw)
|
||||
_, _, raw = call("POST", "/api/v1/auth/sessions", None,
|
||||
{"Authorization": "Basic " + base64.b64encode(f"{name}:{PWD}".encode()).decode()})
|
||||
auth = {"Authorization": "Bearer " + json.loads(raw)["token"]}
|
||||
_, _, raw = call("POST", "/api/v1/chats", json.dumps({"title": "x", "member_ids": [user["id"]]}).encode(), {**j, **auth})
|
||||
chat = json.loads(raw)
|
||||
|
||||
_, hdr, raw = call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=f.png", png,
|
||||
{"Content-Type": "image/png", **auth})
|
||||
att = json.loads(raw)
|
||||
inst = hdr.get("X-Instance-Id")
|
||||
print("uploaded via instance", inst, "sha256", att["sha256"])
|
||||
|
||||
ids = subprocess.run(["docker", "compose", "ps", "-q", "api"], capture_output=True, text=True).stdout.split()
|
||||
target = next((i for i in ids if subprocess.run(["docker", "inspect", "-f", "{{.Config.Hostname}}", i],
|
||||
capture_output=True, text=True).stdout.strip() == inst), None)
|
||||
if not target:
|
||||
sys.exit(f"container for instance {inst} not found")
|
||||
subprocess.run(["docker", "stop", "-t", "8", target], capture_output=True)
|
||||
print("stopped", inst)
|
||||
time.sleep(4)
|
||||
|
||||
seen = set()
|
||||
for _ in range(10):
|
||||
_, h, data = call("GET", f"/api/v1/attachments/{att['id']}/content", None, auth)
|
||||
seen.add(h.get("X-Instance-Id"))
|
||||
assert hashlib.sha256(data).hexdigest() == att["sha256"], "SHA-256 mismatch"
|
||||
print("downloaded 10x via", sorted(seen), "- sha256 matches; stopped instance served:", inst in seen)
|
||||
|
||||
subprocess.run(["docker", "compose", "up", "-d", "--no-recreate", "api"], capture_output=True)
|
||||
print("restored")
|
||||
@@ -0,0 +1,131 @@
|
||||
import base64, hashlib, json, os, random, struct, sys, urllib.error, urllib.request, uuid, zlib
|
||||
|
||||
BASE = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
||||
PWD = "correct-horse-battery-staple"
|
||||
LIMIT = 10 * 1024 * 1024
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, token=None, basic=None):
|
||||
h = dict(headers or {})
|
||||
if token: h["Authorization"] = "Bearer " + token
|
||||
if basic: h["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode()
|
||||
req = urllib.request.Request(BASE + path, data=body, method=method, headers=h)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, r.headers, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.headers, e.read()
|
||||
|
||||
|
||||
def jcall(method, path, obj, token=None, key=None):
|
||||
h = {"Content-Type": "application/json"}
|
||||
if key: h["Idempotency-Key"] = key
|
||||
st, _, raw = call(method, path, json.dumps(obj).encode(), h, token)
|
||||
return st, (json.loads(raw) if raw else None)
|
||||
|
||||
|
||||
def chunk(t, d):
|
||||
c = struct.pack(">I", len(d)) + t + d
|
||||
return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def png(width, height, idat=None):
|
||||
raw = idat if idat is not None else b"".join(b"\x00" + b"\x10\x80\xf0" * width for _ in range(height))
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(raw) if idat is None else raw) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
def noise_png(target_bytes):
|
||||
w = 1000
|
||||
rows = max(1, target_bytes // (3 * w + 1))
|
||||
raw = b"".join(b"\x00" + os.urandom(3 * w) for _ in range(rows))
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, rows, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(raw, 0)) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
ok = True
|
||||
def check(name, got, want):
|
||||
global ok
|
||||
good = got in want if isinstance(want, set) else got == want
|
||||
ok &= good
|
||||
print("PASS" if good else "FAIL", name, "->", got)
|
||||
|
||||
|
||||
def register(prefix):
|
||||
name = f"{prefix}_{uuid.uuid4().hex[:8]}"
|
||||
_, u = jcall("POST", "/api/v1/users", {"username": name, "display_name": prefix, "password": PWD})
|
||||
_, _, raw = call("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
|
||||
return u, json.loads(raw)["token"]
|
||||
|
||||
|
||||
alice, ta = register("alice")
|
||||
bob, tb = register("bob")
|
||||
carol, tc = register("carol")
|
||||
_, chat1 = jcall("POST", "/api/v1/chats", {"title": "c1", "member_ids": [alice["id"], bob["id"]]}, ta)
|
||||
_, chat2 = jcall("POST", "/api/v1/chats", {"title": "c2", "member_ids": [alice["id"]]}, ta)
|
||||
up = lambda chat, data, ct="image/png", fn="a.png", tok=None: call(
|
||||
"POST", f"/api/v1/chats/{chat}/attachments?filename={fn}", data, {"Content-Type": ct}, tok or ta)
|
||||
msg = lambda chat, obj, tok=None, key=None: jcall(
|
||||
"POST", f"/api/v1/chats/{chat}/messages", obj, tok or ta, key or uuid.uuid4().hex)
|
||||
|
||||
good_png = png(8, 8)
|
||||
st, _, raw = up(chat1["id"], good_png)
|
||||
check("валидный PNG", st, 201)
|
||||
att = json.loads(raw)
|
||||
check("sha256 в ответе", att["sha256"], hashlib.sha256(good_png).hexdigest())
|
||||
check("status/variants/error_code", (att["status"], att["variants"], att["error_code"]), ("ready", [], None))
|
||||
|
||||
st, hdr, data = call("GET", f"/api/v1/attachments/{att['id']}/content", token=tb)
|
||||
check("скачивание участником: статус", st, 200)
|
||||
check("скачивание: байты совпадают", hashlib.sha256(data).hexdigest(), att["sha256"])
|
||||
check("скачивание: Content-Type", hdr.get("Content-Type"), "image/png")
|
||||
|
||||
check("метаданные: посторонний", call("GET", f"/api/v1/attachments/{att['id']}", token=tc)[0], 404)
|
||||
check("байты: посторонний", call("GET", f"/api/v1/attachments/{att['id']}/content", token=tc)[0], 404)
|
||||
check("без токена", call("GET", f"/api/v1/attachments/{att['id']}/content")[0], 401)
|
||||
check("неизвестный id", call("GET", f"/api/v1/attachments/{uuid.uuid4()}", token=ta)[0], 404)
|
||||
check("id не UUID", call("GET", "/api/v1/attachments/not-a-uuid", token=ta)[0], 400)
|
||||
check("variant=thumbnail", call("GET", f"/api/v1/attachments/{att['id']}/content?variant=thumbnail", token=ta)[0], {400, 404})
|
||||
|
||||
check("загрузка в чужой чат", up(chat1["id"], good_png, tok=tc)[0], 404)
|
||||
check("слишком большой (Content-Length)", up(chat1["id"], good_png[:8] + b"\0" * (LIMIT + 1))[0], 413)
|
||||
check("слишком много пикселей (8000x8000)", up(chat1["id"], png(8000, 8000, idat=b"\x00"))[0], 413)
|
||||
check("повреждённый PNG", up(chat1["id"], good_png[:-20])[0], 415)
|
||||
check("PNG под видом JPEG", up(chat1["id"], good_png, ct="image/jpeg")[0], 415)
|
||||
check("текст под видом PNG", up(chat1["id"], b"hello, not an image")[0], 415)
|
||||
check("пустое тело", up(chat1["id"], b"")[0], 415)
|
||||
check("image/gif", up(chat1["id"], b"GIF89a....", ct="image/gif")[0], 415)
|
||||
for label, fn in [("слэш", "a%2Fb.png"), ("обратный слэш", "a%5Cb.png"), ("NUL", "a%00b.png"),
|
||||
("управляющий символ", "a%0Ab.png"), ("256 символов", "a" * 256), ("пустое", "")]:
|
||||
check(f"filename: {label}", up(chat1["id"], good_png, fn=fn)[0], 400)
|
||||
st, _, _ = call("POST", f"/api/v1/chats/{chat1['id']}/attachments", good_png, {"Content-Type": "image/png"}, ta)
|
||||
check("filename отсутствует", st, 400)
|
||||
check("валидный большой файл (~9 МиБ)", up(chat1["id"], noise_png(9 * 1024 * 1024))[0], 201)
|
||||
|
||||
# ---- сообщения с вложениями
|
||||
st, m1 = msg(chat1["id"], {"text": "", "attachment_ids": [att["id"]]}, key="k-" + uuid.uuid4().hex)
|
||||
check("пустой текст с вложением", st, 201)
|
||||
check("в ответе attachment_ids", m1["attachment_ids"], [att["id"]])
|
||||
check("пустой текст без вложений", msg(chat1["id"], {"text": ""})[0], 400)
|
||||
check("вложение из другого чата (я участник обоих)", msg(chat2["id"], {"text": "x", "attachment_ids": [att["id"]]})[0], 404)
|
||||
check("неизвестное вложение", msg(chat1["id"], {"text": "x", "attachment_ids": [str(uuid.uuid4())]})[0], 404)
|
||||
check("attachment_ids не UUID", msg(chat1["id"], {"text": "x", "attachment_ids": ["zzz"]})[0], 400)
|
||||
check("дубликаты в attachment_ids", msg(chat1["id"], {"text": "x", "attachment_ids": [att["id"], att["id"]]})[0], 400)
|
||||
check("11 вложений", msg(chat1["id"], {"text": "x", "attachment_ids": [str(uuid.uuid4()) for _ in range(11)]})[0], 400)
|
||||
check("посторонний шлёт чужое вложение", msg(chat1["id"], {"text": "x", "attachment_ids": [att["id"]]}, tok=tc)[0], 404)
|
||||
|
||||
_, page = jcall("GET", f"/api/v1/chats/{chat1['id']}/messages", None, ta)
|
||||
st, _, raw = call("GET", f"/api/v1/chats/{chat1['id']}/messages", token=bob and tb)
|
||||
items = json.loads(raw)["items"]
|
||||
check("история содержит attachment_ids", any(i["attachment_ids"] == [att["id"]] for i in items), True)
|
||||
|
||||
# ---- идемпотентность с вложениями
|
||||
k = "idem-" + uuid.uuid4().hex
|
||||
s1, a = msg(chat1["id"], {"text": "same", "attachment_ids": []}, key=k)
|
||||
s2, b = msg(chat1["id"], {"text": "same"}, key=k)
|
||||
check("attachment_ids отсутствует == []", (s1, s2, a["id"] == b["id"]), (201, 201, True))
|
||||
k2 = "idem-" + uuid.uuid4().hex
|
||||
msg(chat1["id"], {"text": "t", "attachment_ids": [att["id"]]}, key=k2)
|
||||
check("тот же ключ, другой список вложений", msg(chat1["id"], {"text": "t"}, key=k2)[0], 409)
|
||||
|
||||
sys.exit(0 if ok else 1)
|
||||
@@ -0,0 +1,51 @@
|
||||
import base64, json, sys, time, urllib.request, urllib.error, uuid
|
||||
|
||||
BASE = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
||||
PWD = "correct-horse-battery-staple"
|
||||
|
||||
|
||||
def call(method, path, body=None, token=None, basic=None, key=None):
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
r = urllib.request.Request(BASE + path, data=data, method=method)
|
||||
if data is not None: r.add_header("Content-Type", "application/json")
|
||||
if token: r.add_header("Authorization", "Bearer " + token)
|
||||
if basic: r.add_header("Authorization", "Basic " + base64.b64encode(basic.encode()).decode())
|
||||
if key: r.add_header("Idempotency-Key", key)
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=10) as resp:
|
||||
return resp.status, json.loads(resp.read() or b"null")
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, None
|
||||
|
||||
|
||||
name = "edge_" + uuid.uuid4().hex[:8]
|
||||
_, u = call("POST", "/api/v1/users", {"username": name, "display_name": "e", "password": PWD})
|
||||
_, s = call("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
|
||||
t = s["token"]
|
||||
_, c = call("POST", "/api/v1/chats", {"title": "t", "member_ids": [u["id"]]}, token=t)
|
||||
m = f"/api/v1/chats/{c['id']}/messages"
|
||||
k = lambda: uuid.uuid4().hex
|
||||
|
||||
cases = [
|
||||
("поле Text (другой регистр)", call("POST", m, {"Text": "x"}, token=t, key=k())[0], {400}),
|
||||
("текст из NBSP/U+3000", call("POST", m, {"text": "\u00a0\u3000"}, token=t, key=k())[0], {400}),
|
||||
("текст с NUL", call("POST", m, {"text": "a\u0000b"}, token=t, key=k())[0], {400}),
|
||||
("limit= пустой", call("GET", "/api/v1/chats?limit=", token=t)[0], {400}),
|
||||
("cursor= пустой", call("GET", m + "?cursor=", token=t)[0], {400}),
|
||||
("chat_id не UUID", call("GET", "/api/v1/chats/not-a-uuid/messages", token=t)[0], {400}),
|
||||
("member_ids не UUID", call("POST", "/api/v1/chats", {"title": "t", "member_ids": [u["id"], "not-a-uuid"]}, token=t)[0], {400}),
|
||||
("название из пробелов", call("POST", "/api/v1/chats", {"title": " ", "member_ids": [u["id"]]}, token=t)[0], {400}),
|
||||
("название 101 символ", call("POST", "/api/v1/chats", {"title": "a" * 101, "member_ids": [u["id"]]}, token=t)[0], {400}),
|
||||
]
|
||||
for label, got, ok in cases:
|
||||
print("PASS" if got in ok else "FAIL", label, "->", got)
|
||||
|
||||
|
||||
def login_ms(basic):
|
||||
t0 = time.time(); call("POST", "/api/v1/auth/sessions", basic=basic)
|
||||
return (time.time() - t0) * 1000
|
||||
|
||||
ms_unknown = sorted(login_ms("nobody_" + uuid.uuid4().hex[:6] + ":" + PWD) for _ in range(5))[2]
|
||||
ms_wrong = sorted(login_ms(name + ":wrong-password-123") for _ in range(5))[2]
|
||||
print("PASS" if abs(ms_unknown - ms_wrong) < 0.5 * ms_wrong else "FAIL",
|
||||
f"тайминг входа: неизвестный {ms_unknown:.1f} мс, неверный пароль {ms_wrong:.1f} мс")
|
||||
@@ -0,0 +1,65 @@
|
||||
import base64, json, sys, urllib.error, urllib.request, uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
BASE = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
||||
PWD = "correct-horse-battery-staple"
|
||||
|
||||
|
||||
def call(method, path, body=None, token=None, basic=None, key=None):
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
req = urllib.request.Request(BASE + path, data=data, method=method)
|
||||
if body is not None:
|
||||
req.add_header("Content-Type", "application/json")
|
||||
if token:
|
||||
req.add_header("Authorization", "Bearer " + token)
|
||||
if basic:
|
||||
req.add_header("Authorization", "Basic " + base64.b64encode(basic.encode()).decode())
|
||||
if key:
|
||||
req.add_header("Idempotency-Key", key)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
raw = r.read()
|
||||
return r.status, r.headers.get("X-Instance-Id"), json.loads(raw) if raw else None
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.headers.get("X-Instance-Id"), None
|
||||
|
||||
|
||||
ok = True
|
||||
def check(name, cond, info=""):
|
||||
global ok
|
||||
ok &= bool(cond)
|
||||
print(("PASS" if cond else "FAIL"), name, info)
|
||||
|
||||
|
||||
name = "lb_" + uuid.uuid4().hex[:10]
|
||||
s, _, user = call("POST", "/api/v1/users", {"username": name, "display_name": "LB check", "password": PWD})
|
||||
s, _, sess = call("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
|
||||
token = sess["token"]
|
||||
|
||||
# один токен разные реплики
|
||||
inst = {call("GET", "/api/v1/users/me", token=token)[1] for _ in range(12)}
|
||||
check("one token served by >=2 replicas", len(inst) >= 2, sorted(inst))
|
||||
|
||||
s, _, chat = call("POST", "/api/v1/chats", {"title": "lb", "member_ids": [user["id"]]}, token=token)
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
|
||||
# конкурентные повторы с одним ключом через lb
|
||||
key = "conc-" + uuid.uuid4().hex
|
||||
with ThreadPoolExecutor(30) as ex:
|
||||
res = list(ex.map(lambda _: call("POST", path, {"text": "race"}, token=token, key=key), range(30)))
|
||||
ids = {r[2]["id"] for r in res if r[2]}
|
||||
check("concurrent same key: all 201", all(r[0] == 201 for r in res), sorted({r[0] for r in res}))
|
||||
check("concurrent same key: one message id", len(ids) == 1, len(ids))
|
||||
check("concurrent requests hit >=2 replicas", len({r[1] for r in res}) >= 2, sorted({r[1] for r in res}))
|
||||
|
||||
# последовательный повтор на разных репликах
|
||||
key2 = "seq-" + uuid.uuid4().hex
|
||||
seq = [call("POST", path, {"text": "seq"}, token=token, key=key2) for _ in range(6)]
|
||||
check("sequential repeat: same id", len({r[2]["id"] for r in seq}) == 1)
|
||||
check("sequential repeat: >=2 replicas", len({r[1] for r in seq}) >= 2, sorted({r[1] for r in seq}))
|
||||
check("different body, same key -> 409", call("POST", path, {"text": "other"}, token=token, key=key2)[0] == 409)
|
||||
|
||||
s, _, page = call("GET", path + "?limit=100", token=token)
|
||||
check("history has exactly 2 messages", len(page["items"]) == 2, len(page["items"]))
|
||||
|
||||
sys.exit(0 if ok else 1)
|
||||
@@ -0,0 +1,236 @@
|
||||
"""Lab5 level4 acceptance/fault checks. Operates only on local synthetic Compose.
|
||||
|
||||
Does not change the public tests. Stops services temporarily; restores them in finally.
|
||||
Only IDs, assertions and timings are saved; no tokens/credentials/signed URLs.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, Failure, check
|
||||
from image_probe import jpeg_size
|
||||
|
||||
|
||||
def compose(*args, input=None, expected=0, timeout=120):
|
||||
p = subprocess.run(['docker', 'compose', *args], cwd=ROOT, input=input,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
|
||||
check(p.returncode == expected, 'compose ' + ' '.join(args[:3]) + ': exit ' + str(p.returncode) + ' ' + p.stderr.decode(errors='replace')[-1500:])
|
||||
return p.stdout
|
||||
|
||||
|
||||
def sql(query):
|
||||
return compose('exec', '-T', 'postgres', 'sh', '-c',
|
||||
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At -v ON_ERROR_STOP=1',
|
||||
input=query.encode()).decode().strip()
|
||||
|
||||
|
||||
def eventually(predicate, seconds=90):
|
||||
end = time.monotonic() + seconds
|
||||
while time.monotonic() < end:
|
||||
if predicate():
|
||||
return
|
||||
time.sleep(0.4)
|
||||
raise Failure('condition did not recover within ' + str(seconds) + ' seconds')
|
||||
|
||||
|
||||
def jid(att):
|
||||
return sql(f"SELECT id FROM attachment_jobs WHERE attachment_id='{uuid.UUID(att['id'])}';")
|
||||
|
||||
|
||||
def status(att):
|
||||
return sql(f"SELECT status FROM attachment_jobs WHERE attachment_id='{uuid.UUID(att['id'])}';")
|
||||
|
||||
|
||||
def envelope(job):
|
||||
return sql(f"SELECT json_build_object('schema_version',1,'type','attachment.process.v1','job_id',id,'attachment_id',attachment_id,'pipeline_version',1,'attempt',attempt,'created_at',created_at) FROM attachment_jobs WHERE id='{uuid.UUID(job)}';")
|
||||
|
||||
|
||||
def duplicate(job):
|
||||
compose('exec', '-T', 'broker', 'redis-cli', 'XADD', 'attachment-jobs-v1', '*', 'payload', envelope(job))
|
||||
|
||||
|
||||
def objects(prefix):
|
||||
raw=compose('run','--rm','--no-deps','-T','--entrypoint','/bin/sh','s3-init','-c',
|
||||
'export AWS_ACCESS_KEY_ID="$(cat /run/secrets/s3_admin_access_key)"; '
|
||||
'export AWS_SECRET_ACCESS_KEY="$(cat /run/secrets/s3_admin_secret_key)"; '
|
||||
'export AWS_DEFAULT_REGION=us-east-1; '
|
||||
'exec aws --endpoint-url http://s3-proxy:8333 s3api list-objects-v2 --bucket attachments --prefix "$1"',
|
||||
'sh',prefix)
|
||||
return json.loads(raw).get('Contents',[])
|
||||
|
||||
|
||||
def verify_images(c, actor, att, original, thumb_size=(256, 160)):
|
||||
ready = c.wait_ready(actor, att, 90)
|
||||
check(ready['error_code'] is None, 'ready error_code')
|
||||
variants = {v['name']: v for v in ready['variants']}
|
||||
check(set(variants) == {'thumbnail', 'crop', 'optimized'}, 'exactly 3 distinct variants')
|
||||
raw = c.call('GET', f"/api/v1/attachments/{att['id']}/content", actor=actor)['bytes']
|
||||
check(raw == original, 'original changed')
|
||||
for name, v in variants.items():
|
||||
data = c.call('GET', f"/api/v1/attachments/{att['id']}/content?variant={name}", actor=actor)['bytes']
|
||||
check(hashlib.sha256(data).hexdigest() == v['sha256'] and len(data) == v['size_bytes'], name + ' checksum')
|
||||
if name != 'optimized':
|
||||
dims = jpeg_size(data)
|
||||
check(dims == ((128, 128) if name == 'crop' else thumb_size), name + ' real dimensions')
|
||||
else:
|
||||
check(data[:4] == b'RIFF' and data[8:12] == b'WEBP', 'real WebP')
|
||||
decoded = json.loads(compose('exec', '-T', 'worker', 'python3', '-c',
|
||||
"from PIL import Image; import sys,io,json; i=Image.open(io.BytesIO(sys.stdin.buffer.read())); i.load(); print(json.dumps({'size':i.size,'exif':bool(i.getexif()),'icc':bool(i.info.get('icc_profile')),'corner':i.convert('RGB').getpixel((0,0))}))", input=data))
|
||||
check(decoded['size'] == [v['width'], v['height']], name + ' decoded size')
|
||||
check(not decoded['exif'] and not decoded['icc'], name + ' metadata stripped')
|
||||
return ready
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--output', default='evidence/lab5-pipeline.json')
|
||||
parser.add_argument('--resource-only', action='store_true')
|
||||
args = parser.parse_args()
|
||||
c = Client(args.base_url, timeout=45)
|
||||
results = []
|
||||
|
||||
def record(name, start, **observed):
|
||||
row = dict(scenario=name, result='PASS', seconds=round(time.monotonic()-start, 3), **observed)
|
||||
results.append(row)
|
||||
print('PASS', name, json.dumps(observed, ensure_ascii=False), flush=True)
|
||||
|
||||
def health():
|
||||
try:
|
||||
c.call('GET', '/health/ready')
|
||||
return True
|
||||
except Failure:
|
||||
return False
|
||||
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
fixture = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
try:
|
||||
if not args.resource_only:
|
||||
start = time.monotonic()
|
||||
att, raw = c.attachment(alice, chat)
|
||||
ready = verify_images(c, bob, att, raw)
|
||||
c.call('GET', f"/api/v1/attachments/{att['id']}/content?variant=crop", 404, actor=eve)
|
||||
eventually(lambda: sql(f"SELECT staging_key IS NULL FROM attachments WHERE id='{att['id']}';") == 't')
|
||||
check(not objects('staging/'+att['id']),'staging bytes not removed')
|
||||
record('variants_checksums_authorization_staging_cleanup', start, attachment_id=att['id'], variants=[(v['name'],v['width'],v['height']) for v in ready['variants']])
|
||||
|
||||
start = time.monotonic()
|
||||
generators = [
|
||||
("from PIL import Image; import sys; Image.new('RGBA',(16,8),(255,0,0,0)).save(sys.stdout.buffer,'PNG')", 'image/png', (16,8), True),
|
||||
("from PIL import Image; import sys; i=Image.new('RGB',(320,200),'blue'); e=i.getexif(); e[274]=6; i.save(sys.stdout.buffer,'JPEG',exif=e)", 'image/jpeg', (160,256), False),
|
||||
]
|
||||
for code, content_type, dims, white in generators:
|
||||
raw = compose('exec','-T','worker','python3','-c',code)
|
||||
a = c.call('POST', f"/api/v1/chats/{chat['id']}/attachments?filename=synthetic.png",202,actor=alice,raw=raw,headers={'Content-Type':content_type},schema='Attachment')['json']
|
||||
verify_images(c,bob,a,raw,dims)
|
||||
if white:
|
||||
thumb = c.call('GET',f"/api/v1/attachments/{a['id']}/content?variant=thumbnail",actor=bob)['bytes']
|
||||
pixel=json.loads(compose('exec','-T','worker','python3','-c',"from PIL import Image; import io,json,sys; print(json.dumps(Image.open(io.BytesIO(sys.stdin.buffer.read())).getpixel((0,0))))",input=thumb))
|
||||
check(min(pixel)>250,'alpha must become white')
|
||||
record('alpha_small_image_exif_orientation',start)
|
||||
|
||||
start = time.monotonic()
|
||||
path=f"/api/v1/chats/{chat['id']}/attachments?filename=bad.png"
|
||||
c.call('POST',path,415,actor=alice,raw=fixture[:-20],headers={'Content-Type':'image/png'})
|
||||
c.call('POST',path,413,actor=alice,raw=b'x'*(10*1024*1024+1),headers={'Content-Type':'image/png'})
|
||||
import struct, zlib
|
||||
ihdr=struct.pack('>IIBBBBB',8000,8000,8,2,0,0,0)
|
||||
chunk=b'IHDR'+ihdr
|
||||
bomb=b'\x89PNG\r\n\x1a\n'+struct.pack('>I',13)+chunk+struct.pack('>I',zlib.crc32(chunk)&0xffffffff)
|
||||
c.call('POST',path,413,actor=alice,raw=bomb,headers={'Content-Type':'image/png'})
|
||||
record('invalid_bytes_pixels_limits',start)
|
||||
|
||||
start = time.monotonic()
|
||||
compose('stop','worker','outbox')
|
||||
att, raw = c.attachment(alice, chat)
|
||||
job = jid(att)
|
||||
c.message(alice, chat, '', attachments=[att['id']])
|
||||
c.call('GET',f"/api/v1/attachments/{att['id']}/content",409,actor=bob)
|
||||
c.message(alice,chat,'text works while worker is stopped')
|
||||
compose('run','--rm','--no-deps','-T','-e','PIPELINE_FAULT=before_publish','outbox',expected=86)
|
||||
check(sql(f"SELECT count(*) FROM attachment_outbox WHERE job_id='{job}' AND published_at IS NULL;")=='1','outbox intent survived real process crash')
|
||||
compose('run','--rm','--no-deps','-T','-e','PIPELINE_FAULT=after_publish','outbox',expected=86)
|
||||
check(sql(f"SELECT count(*) FROM attachment_outbox WHERE job_id='{job}' AND published_at IS NULL;")=='1','publish-before-mark crash lost outbox')
|
||||
compose('start','outbox')
|
||||
eventually(lambda: sql(f"SELECT count(*) FROM attachment_outbox WHERE job_id='{job}' AND published_at IS NOT NULL;")=='1')
|
||||
count=compose('exec','-T','broker','redis-cli','XLEN','attachment-jobs-v1').strip()
|
||||
before=compose('ps','-q','api').decode().split()
|
||||
compose('kill','-s','SIGKILL','broker')
|
||||
compose('start','broker')
|
||||
eventually(lambda: compose('exec','-T','broker','redis-cli','ping').strip()==b'PONG')
|
||||
check(compose('exec','-T','broker','redis-cli','XLEN','attachment-jobs-v1').strip()==count,'broker lost durable stream entries')
|
||||
compose('restart','api')
|
||||
eventually(health)
|
||||
compose('start','worker')
|
||||
verify_images(c,bob,att,raw)
|
||||
record('commit_publish_crash_worker_off_api_restart_broker_sigkill',start,attachment_id=att['id'],job_id=job,api_containers=before,stream_entries=int(count))
|
||||
|
||||
start = time.monotonic()
|
||||
compose('stop','worker')
|
||||
att, raw = c.attachment(alice,chat);job=jid(att)
|
||||
compose('run','--rm','--no-deps','-T','-e','PIPELINE_FAULT=after_object','worker',expected=86)
|
||||
check(status(att)=='processing','worker must crash after object before DB result')
|
||||
key=sql(f"SELECT object_key FROM attachments WHERE id='{att['id']}';")
|
||||
check(len(objects(key))==1,'crash boundary must contain exactly the persisted original')
|
||||
c.call('GET',f"/api/v1/attachments/{att['id']}/content",409,actor=bob)
|
||||
compose('start','worker')
|
||||
ready=verify_images(c,bob,att,raw)
|
||||
attempt=int(sql(f"SELECT attempt FROM attachment_jobs WHERE id='{job}';"))
|
||||
check(attempt>=2,'expired lease not recovered')
|
||||
duplicate(job);time.sleep(2)
|
||||
check(int(sql(f"SELECT attempt FROM attachment_jobs WHERE id='{job}';"))==attempt,'ready duplicate executed again')
|
||||
check(c.call('GET',f"/api/v1/attachments/{att['id']}",actor=bob,schema='Attachment')['json']==ready,'duplicate changed completed metadata')
|
||||
check(len(objects(key))==4,'duplicate created a second set of S3 objects')
|
||||
record('crash_after_s3_before_ack_lease_recovery_duplicate',start,attachment_id=att['id'],job_id=job,attempts=attempt)
|
||||
|
||||
start=time.monotonic()
|
||||
compose('stop','worker')
|
||||
att,raw=c.attachment(alice,chat);job=jid(att)
|
||||
compose('run','--rm','--no-deps','-T','-e','S3_ENDPOINT=http://127.0.0.1:1','-e','JOB_MAX_ATTEMPTS=2','-e','PIPELINE_FAULT=before_ack','worker',expected=86)
|
||||
check(status(att)=='queued','transient error must retry')
|
||||
delay=float(sql(f"SELECT extract(epoch from(next_attempt_at-updated_at)) FROM attachment_jobs WHERE id='{job}';"))
|
||||
check(0.45<=delay<=1.1,'first retry backoff is outside jitter bounds')
|
||||
time.sleep(1.1)
|
||||
compose('run','--rm','--no-deps','-T','-e','S3_ENDPOINT=http://127.0.0.1:1','-e','JOB_MAX_ATTEMPTS=2','-e','PIPELINE_FAULT=before_ack','worker',expected=86)
|
||||
check(status(att)=='failed','attempts must be bounded')
|
||||
failed=c.call('GET',f"/api/v1/attachments/{att['id']}",actor=bob,schema='Attachment')['json']
|
||||
check(failed['error_code']=='retry_exhausted','safe exhausted error_code')
|
||||
c.call('POST',f"/api/v1/chats/{chat['id']}/messages",409,actor=alice,body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
|
||||
registry=json.loads(compose('run','--rm','--no-deps','-T','worker','./worker','-mode=failed'))
|
||||
check(any(row['job_id']==job for row in registry),'failed registry must expose job')
|
||||
compose('run','--rm','--no-deps','-T','worker','./worker','-mode=replay','-job='+job,'-reason=lab5-recovered-storage')
|
||||
compose('start','worker')
|
||||
verify_images(c,bob,att,raw)
|
||||
check(sql(f"SELECT count(*) FROM job_replays WHERE job_id='{job}';")=='1','replay audit missing')
|
||||
compose('run','--rm','--no-deps','-T','worker','./worker','-mode=replay','-job='+job,'-reason=must-refuse-ready',expected=1)
|
||||
record('retry_jitter_exhaustion_failed_registry_audited_replay',start,attachment_id=att['id'],job_id=job,first_backoff_seconds=round(delay,3))
|
||||
|
||||
start=time.monotonic()
|
||||
raw=compose('exec','-T','worker','python3','-c',"from PIL import Image; import sys; Image.new('RGB',(3000,3000),'blue').save(sys.stdout.buffer,'PNG')")
|
||||
compose('stop','worker')
|
||||
att=c.call('POST',f"/api/v1/chats/{chat['id']}/attachments?filename=memory.png",202,actor=alice,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment')['json'];job=jid(att)
|
||||
compose('run','--rm','--no-deps','-T','-e','IMAGE_MEMORY_MB=64','-e','PIPELINE_FAULT=before_ack','worker',expected=86)
|
||||
check(status(att)=='failed','memory limit must be a permanent controlled failure')
|
||||
check(sql(f"SELECT error_code FROM attachment_jobs WHERE id='{job}';")=='image_resource_limit','memory failure code')
|
||||
check(sql(f"SELECT attempt FROM attachment_jobs WHERE id='{job}';")=='1','permanent failure was retried')
|
||||
compose('run','--rm','--no-deps','-T','worker','./worker','-mode=replay','-job='+job,'-reason=restore-normal-memory-limit')
|
||||
compose('start','worker')
|
||||
verify_images(c,bob,att,raw,(256,256))
|
||||
record('worker_memory_limit_permanent_failure_replay',start,attachment_id=att['id'],job_id=job,decoder_limit_mb=64,pixels=9000000)
|
||||
finally:
|
||||
compose('start','broker','api','outbox','worker')
|
||||
output=ROOT/args.output
|
||||
output.parent.mkdir(parents=True,exist_ok=True)
|
||||
output.write_text(json.dumps({'date':'2026-11-10','scenarios':results},ensure_ascii=False,indent=2)+'\n')
|
||||
print('PASS all lab5 level4 scenarios',flush=True)
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
run() {
|
||||
who="$1"; shift
|
||||
docker run --rm --network mtusi2026lab5_s3access -v "$PWD/secrets:/s:ro" -e AWS_DEFAULT_REGION=us-east-1 \
|
||||
--entrypoint /bin/sh amazon/aws-cli:2.39.1 -c \
|
||||
"AWS_ACCESS_KEY_ID=\$(cat /s/s3_${who}_access_key.txt) AWS_SECRET_ACCESS_KEY=\$(cat /s/s3_${who}_secret_key.txt) aws --endpoint-url http://s3-proxy:8333 $*" >/dev/null 2>&1
|
||||
}
|
||||
expect() {
|
||||
want="$1"; desc="$2"; shift 2
|
||||
if run "$@"; then got=ok; else got=deny; fi
|
||||
[ "$got" = "$want" ] && echo "PASS $desc ($got)" || echo "FAIL $desc: ожидалось $want, получено $got"
|
||||
}
|
||||
|
||||
expect ok "api: список объектов бакета" api "s3api list-objects-v2 --bucket attachments --max-keys 1"
|
||||
expect ok "api: запись объекта" api "s3 cp /etc/hostname s3://attachments/attachments/probe/rights-check"
|
||||
expect ok "api: удаление объекта" api "s3 rm s3://attachments/attachments/probe/rights-check"
|
||||
expect deny "api: создание другого бакета" api "s3api create-bucket --bucket other-bucket"
|
||||
expect deny "api: удаление бакета" api "s3api delete-bucket --bucket attachments"
|
||||
expect ok "admin: создание бакета (bootstrap)" admin "s3api head-bucket --bucket attachments"
|
||||
|
||||
echo "--- без подписи (anonymous)"
|
||||
for url in "http://s3-proxy:8333/attachments/" "http://s3-proxy:8333/attachments/attachments/x/y"; do
|
||||
code=$(docker run --rm --network mtusi2026lab5_s3access curlimages/curl -s -o /dev/null -w '%{http_code}' "$url")
|
||||
case "$code" in 401|403) echo "PASS anonymous $url -> $code";; *) echo "FAIL anonymous $url -> $code";; esac
|
||||
done
|
||||
|
||||
echo "--- консоли управления из контейнера api (ожидается недоступность)"
|
||||
for target in seaweedfs:9333 seaweedfs:8888 seaweedfs:8080 s3-proxy:9333 s3-proxy:8888; do
|
||||
if docker compose exec -T api wget -q -O /dev/null -T 3 "http://$target/" >/dev/null 2>&1; then
|
||||
echo "FAIL api -> $target доступен"
|
||||
else
|
||||
echo "PASS api -> $target недоступен"
|
||||
fi
|
||||
done
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Collect allowlisted runtime facts, optionally sampling a 30s HTTP load."""
|
||||
import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def run(*args):
|
||||
return subprocess.check_output(args, cwd=ROOT, text=True).strip()
|
||||
|
||||
|
||||
info = json.loads(run('docker', 'info', '--format', '{{json .}}'))
|
||||
ids = run('docker', 'compose', 'ps', '-q').splitlines()
|
||||
containers = []
|
||||
for item in json.loads(run('docker', 'inspect', *ids)):
|
||||
containers.append(dict(name=item['Name'], image=item['Config']['Image'],
|
||||
started_at=item['State']['StartedAt'], cpus=item['HostConfig']['NanoCpus']/1e9,
|
||||
memory_bytes=item['HostConfig']['Memory'], volumes=[m['Name'] for m in item['Mounts'] if m['Type']=='volume']))
|
||||
report = dict(date=datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
host_machine=platform.machine(), host_os=platform.platform(),
|
||||
docker=dict(version=info['ServerVersion'], cpus=info['NCPU'], memory_bytes=info['MemTotal']),
|
||||
go=run('go','version'), compose=run('docker','compose','version'),
|
||||
image_runtime=run('docker','compose','exec','-T','worker','python3','-c',"import sys,PIL; print(sys.version.split()[0], 'Pillow', PIL.__version__)"),
|
||||
containers=containers)
|
||||
def dataset():
|
||||
query="SELECT json_build_object('users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),'messages',(SELECT count(*) FROM messages),'attachments',(SELECT count(*) FROM attachments));"
|
||||
raw=subprocess.check_output(['docker','compose','exec','-T','postgres','sh','-c',
|
||||
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At -v ON_ERROR_STOP=1'],input=query,text=True,cwd=ROOT)
|
||||
return json.loads(raw)
|
||||
report['dataset_before']=dataset()
|
||||
if '--load' in sys.argv:
|
||||
log=(ROOT/'evidence/lab5-load.txt').open('w')
|
||||
load=subprocess.Popen([sys.executable,'tests/load.py','--duration','30','--concurrency','4','--output','evidence/lab5-load.json'],cwd=ROOT,stdout=log,stderr=subprocess.STDOUT)
|
||||
samples=[]
|
||||
while load.poll() is None:
|
||||
samples.append(dict(at=datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
||||
containers=[json.loads(line) for line in run('docker','stats','--no-stream','--format','{{json .}}',*ids).splitlines()]))
|
||||
time.sleep(1)
|
||||
log.close()
|
||||
if load.returncode:
|
||||
raise SystemExit('load failed; see evidence/lab5-load.txt')
|
||||
report['load_samples']=samples
|
||||
report['dataset_after']=dataset()
|
||||
events=run('docker','events','--since','1h','--until','0s','--filter','type=container',
|
||||
'--filter','label=com.docker.compose.project=mtusi2026lab5','--format','{{json .}}')
|
||||
report['lifecycle_events']=[dict(time=e['time'],action=e['Action'],name=e['Actor']['Attributes'].get('name'))
|
||||
for e in (json.loads(line) for line in events.splitlines()) if e['Action'] in ('start','stop','die','kill','restart')]
|
||||
(ROOT/'evidence/lab5-runtime.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n')
|
||||
print('PASS sanitized runtime evidence saved')
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Allowlist container lifecycle fields for restart-hook evidence."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ids=subprocess.check_output(['docker','compose','ps','-aq'],text=True).split()
|
||||
items=json.loads(subprocess.check_output(['docker','inspect',*ids],text=True))
|
||||
rows=[dict(id=c['Id'],name=c['Name'],service=c['Config']['Labels']['com.docker.compose.service'],
|
||||
started_at=c['State']['StartedAt'],finished_at=c['State']['FinishedAt'],running=c['State']['Running']) for c in items]
|
||||
Path(sys.argv[1]).write_text(json.dumps(rows,indent=2)+'\n')
|
||||
@@ -0,0 +1,69 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TEMP TABLE explain_ctx (
|
||||
user_id UUID,
|
||||
chat_id UUID
|
||||
);
|
||||
|
||||
WITH u AS (
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES (
|
||||
'explain_' || substr(gen_random_uuid()::text, 1, 8),
|
||||
'Explain Test',
|
||||
'test-hash'
|
||||
)
|
||||
RETURNING id
|
||||
),
|
||||
c AS (
|
||||
INSERT INTO chats (title)
|
||||
VALUES ('explain-10000')
|
||||
RETURNING id
|
||||
)
|
||||
INSERT INTO explain_ctx (user_id, chat_id)
|
||||
SELECT u.id, c.id
|
||||
FROM u CROSS JOIN c;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id)
|
||||
SELECT chat_id, user_id
|
||||
FROM explain_ctx;
|
||||
|
||||
INSERT INTO messages (chat_id, sender_id, text, created_at)
|
||||
SELECT
|
||||
chat_id,
|
||||
user_id,
|
||||
'message ' || gs,
|
||||
TIMESTAMPTZ '2026-01-01 00:00:00+00' + gs * INTERVAL '1 second'
|
||||
FROM explain_ctx
|
||||
CROSS JOIN generate_series(1, 10000) AS gs;
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
\echo '=== FIRST PAGE ==='
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = (SELECT chat_id FROM explain_ctx)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 20;
|
||||
|
||||
\echo '=== DEEP PAGE ==='
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = (SELECT chat_id FROM explain_ctx)
|
||||
AND (created_at, id) > (
|
||||
TIMESTAMPTZ '2026-01-01 02:00:00+00',
|
||||
(
|
||||
SELECT id
|
||||
FROM messages
|
||||
WHERE chat_id = (SELECT chat_id FROM explain_ctx)
|
||||
AND created_at = TIMESTAMPTZ '2026-01-01 02:00:00+00'
|
||||
LIMIT 1
|
||||
)
|
||||
)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 20;
|
||||
|
||||
ROLLBACK;
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mkdir -p evidence
|
||||
|
||||
python3 tests/load.py --duration 30 --concurrency 4 \
|
||||
--output evidence/load-stop-api2.json > evidence/load-stop-api2.txt &
|
||||
LOAD_PID=$!
|
||||
|
||||
sleep 8
|
||||
date '+%T stop api-2'
|
||||
docker stop -t 8 mtusi2026lab5-api-2 >/dev/null
|
||||
docker compose ps > evidence/ps-during-failover.txt
|
||||
|
||||
sleep 10
|
||||
date '+%T start api-2'
|
||||
docker start mtusi2026lab5-api-2 >/dev/null
|
||||
|
||||
wait "$LOAD_PID"
|
||||
docker compose logs lb --since 3m > evidence/lb-failover.log
|
||||
cat evidence/load-stop-api2.txt
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mkdir -p secrets
|
||||
rnd() { openssl rand -hex "$1"; }
|
||||
for f in s3_api_access_key s3_api_secret_key s3_admin_access_key s3_admin_secret_key; do
|
||||
if [ ! -s "secrets/$f.txt" ]; then
|
||||
if [ "${f%access_key}" != "$f" ]; then
|
||||
printf '%s' "AK$(rnd 8)" > "secrets/$f.txt"
|
||||
else
|
||||
rnd 20 > "secrets/$f.txt"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
cat > secrets/s3_config.json <<EOF
|
||||
{
|
||||
"identities": [
|
||||
{"name": "api", "credentials": [{"accessKey": "$(cat secrets/s3_api_access_key.txt)", "secretKey": "$(cat secrets/s3_api_secret_key.txt)"}],
|
||||
"actions": ["Read:attachments", "Write:attachments", "List:attachments"]},
|
||||
{"name": "admin", "credentials": [{"accessKey": "$(cat secrets/s3_admin_access_key.txt)", "secretKey": "$(cat secrets/s3_admin_secret_key.txt)"}],
|
||||
"actions": ["Admin", "Read", "Write", "List"]}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
chmod 600 secrets/*
|
||||
@@ -0,0 +1,91 @@
|
||||
import base64, json, subprocess, sys, threading, time, urllib.request, uuid
|
||||
import os
|
||||
BASE = os.environ.get("BASE", "http://localhost:8080"); PWD = "correct-horse-battery-staple"
|
||||
WORKERS, DURATION, STOP_AT, START_AT = 4, 35, 8, 22
|
||||
|
||||
|
||||
def req(method, path, body=None, token=None, basic=None, key=None):
|
||||
r = urllib.request.Request(BASE + path, method=method,
|
||||
data=json.dumps(body).encode() if body is not None else None)
|
||||
if body is not None: r.add_header("Content-Type", "application/json")
|
||||
if token: r.add_header("Authorization", "Bearer " + token)
|
||||
if basic: r.add_header("Authorization", "Basic " + base64.b64encode(basic.encode()).decode())
|
||||
if key: r.add_header("Idempotency-Key", key)
|
||||
with urllib.request.urlopen(r, timeout=3) as resp:
|
||||
return resp.status, resp.headers.get("X-Instance-Id"), json.loads(resp.read() or b"null")
|
||||
|
||||
|
||||
def sh(*a): return subprocess.run(a, capture_output=True, text=True).stdout.split()
|
||||
arg = sys.argv[1]
|
||||
names = sorted(sh("docker", "ps", "--filter", "label=com.docker.compose.service=api",
|
||||
"--format", "{{.Names}}"))
|
||||
container = names[int(arg) - 1] if arg.isdigit() else arg
|
||||
stopped_id = subprocess.run(["docker", "inspect", "-f", "{{.Config.Hostname}}", container],
|
||||
capture_output=True, text=True).stdout.strip()
|
||||
assert stopped_id, "container not found"
|
||||
name = "fo_" + uuid.uuid4().hex[:10]
|
||||
_, _, u = req("POST", "/api/v1/users", {"username": name, "display_name": "fo", "password": PWD})
|
||||
_, _, s = req("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
|
||||
tok = s["token"]
|
||||
_, _, c = req("POST", "/api/v1/chats", {"title": "fo", "member_ids": [u["id"]]}, token=tok)
|
||||
path = f"/api/v1/chats/{c['id']}/messages"
|
||||
|
||||
t0 = time.time()
|
||||
attempts, confirmed, unconfirmed = [], set(), 0
|
||||
lock = threading.Lock()
|
||||
|
||||
|
||||
def action():
|
||||
time.sleep(STOP_AT)
|
||||
subprocess.run(["docker", "stop", "-t", "8", container], capture_output=True)
|
||||
time.sleep(max(0, START_AT - (time.time() - t0)))
|
||||
subprocess.run(["docker", "start", container], capture_output=True)
|
||||
|
||||
|
||||
def worker():
|
||||
global unconfirmed
|
||||
while time.time() - t0 < DURATION:
|
||||
key = uuid.uuid4().hex
|
||||
done = False
|
||||
for n in range(3):
|
||||
ts = time.time() - t0
|
||||
try:
|
||||
_, inst, m = req("POST", path, {"text": "x"}, token=tok, key=key)
|
||||
with lock:
|
||||
attempts.append((ts, True, inst, n)); confirmed.add(m["id"])
|
||||
done = True
|
||||
break
|
||||
except Exception:
|
||||
with lock: attempts.append((ts, False, None, n))
|
||||
time.sleep(0.2)
|
||||
if not done:
|
||||
with lock: unconfirmed += 1
|
||||
|
||||
|
||||
threads = [threading.Thread(target=action)] + [threading.Thread(target=worker) for _ in range(WORKERS)]
|
||||
[t.start() for t in threads]; [t.join() for t in threads]
|
||||
|
||||
stored, cur = set(), None
|
||||
while True:
|
||||
_, _, page = req("GET", path + "?limit=100" + (f"&cursor={cur}" if cur else ""), token=tok)
|
||||
stored |= {m["id"] for m in page["items"]}
|
||||
cur = page["next_cursor"]
|
||||
if not cur: break
|
||||
|
||||
oks = sorted(a[0] for a in attempts if a[1])
|
||||
gap = max((b - a for a, b in zip(oks, oks[1:])), default=0)
|
||||
first_fail = [a[0] - STOP_AT for a in attempts if not a[1] and a[3] == 0 and a[0] >= STOP_AT]
|
||||
pre_stop_errors = sum(1 for a in attempts if not a[1] and a[3] == 0 and a[0] < STOP_AT)
|
||||
rps = lambda a, b: round(sum(a <= t < b for t in oks) / (b - a), 1)
|
||||
late = {a[2] for a in attempts if a[1] and STOP_AT + 6 < a[0] < START_AT}
|
||||
print(json.dumps({
|
||||
"container": container, "stopped_instance": stopped_id,
|
||||
"rps_before": rps(0, STOP_AT), "rps_during": rps(STOP_AT, START_AT), "rps_after": rps(START_AT, DURATION),
|
||||
"first_attempt_errors": len(first_fail),
|
||||
"error_window_s_after_stop": [round(min(first_fail), 2), round(max(first_fail), 2)] if first_fail else None,
|
||||
"longest_gap_without_success_s": round(gap, 2),
|
||||
"stopped_instance_seen_while_down": stopped_id in late,
|
||||
"confirmed": len(confirmed), "unconfirmed_keys": unconfirmed, "stored": len(stored),
|
||||
"lost_confirmed": len(confirmed - stored),
|
||||
"stored_minus_confirmed": len(stored) - len(confirmed),
|
||||
}, indent=2))
|
||||
@@ -0,0 +1,84 @@
|
||||
import json, os, subprocess, struct, sys, threading, time, urllib.request, urllib.error, uuid, zlib, base64
|
||||
|
||||
BASE = "http://localhost:8080"; PWD = "correct-horse-battery-staple"
|
||||
SIZE = int(sys.argv[1]) if len(sys.argv) > 1 else 9 * 1024 * 1024
|
||||
WORKERS, DURATION = 12, 20
|
||||
|
||||
|
||||
def chunk(t, d):
|
||||
c = struct.pack(">I", len(d)) + t + d
|
||||
return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def noise_png(target):
|
||||
w = 1000
|
||||
rows = max(1, target // (3 * w + 1))
|
||||
raw = b"".join(b"\x00" + os.urandom(3 * w) for _ in range(rows))
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, rows, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(raw, 0)) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
req = urllib.request.Request(BASE + path, data=body, method=method, headers=headers or {})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
|
||||
name = "mem_" + uuid.uuid4().hex[:8]
|
||||
_, raw = call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": "m", "password": PWD}).encode(),
|
||||
{"Content-Type": "application/json"})
|
||||
user = json.loads(raw)
|
||||
_, raw = call("POST", "/api/v1/auth/sessions", None,
|
||||
{"Authorization": "Basic " + base64.b64encode(f"{name}:{PWD}".encode()).decode()})
|
||||
tok = json.loads(raw)["token"]
|
||||
_, raw = call("POST", "/api/v1/chats", json.dumps({"title": "m", "member_ids": [user["id"]]}).encode(),
|
||||
{"Content-Type": "application/json", "Authorization": "Bearer " + tok})
|
||||
chat = json.loads(raw)
|
||||
data = noise_png(SIZE)
|
||||
print(f"payload: {len(data)} bytes, workers={WORKERS}, duration={DURATION}s")
|
||||
|
||||
statuses, lock, stop = {}, threading.Lock(), threading.Event()
|
||||
|
||||
|
||||
def worker():
|
||||
while not stop.is_set():
|
||||
st, _ = call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=m.png", data,
|
||||
{"Content-Type": "image/png", "Authorization": "Bearer " + tok})
|
||||
with lock:
|
||||
statuses[st] = statuses.get(st, 0) + 1
|
||||
|
||||
|
||||
def mib(s):
|
||||
n, unit = float(s[:-3]) if s.endswith("iB") else 0, s[-3:]
|
||||
return n * {"KiB": 1 / 1024, "MiB": 1, "GiB": 1024}.get(unit, 0)
|
||||
|
||||
|
||||
peaks = {}
|
||||
def sampler():
|
||||
while not stop.is_set():
|
||||
out = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"],
|
||||
capture_output=True, text=True).stdout
|
||||
for line in out.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 2 and "-api-" in parts[0]:
|
||||
peaks[parts[0]] = max(peaks.get(parts[0], 0), mib(parts[1]))
|
||||
|
||||
base = {}
|
||||
out = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"],
|
||||
capture_output=True, text=True).stdout
|
||||
for line in out.splitlines():
|
||||
p = line.split()
|
||||
if len(p) >= 2 and "-api-" in p[0]:
|
||||
base[p[0]] = mib(p[1])
|
||||
|
||||
ths = [threading.Thread(target=worker) for _ in range(WORKERS)] + [threading.Thread(target=sampler)]
|
||||
t0 = time.time()
|
||||
[t.start() for t in ths]
|
||||
time.sleep(DURATION)
|
||||
stop.set()
|
||||
[t.join() for t in ths]
|
||||
print(json.dumps({"payload_bytes": len(data), "statuses": statuses, "idle_mib": base,
|
||||
"peak_mib": {k: round(v, 1) for k, v in peaks.items()}, "limit_mib": 256}, indent=2))
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
count() { ./scripts/s3-admin.sh s3 ls s3://attachments/ --recursive | wc -l | tr -d ' '; }
|
||||
psql_() { docker compose exec -T postgres sh -c "psql -U \"\$POSTGRES_USER\" -d \"\$POSTGRES_DB\" -t -A -c \"$1\""; }
|
||||
trap 'psql_ "ALTER TABLE attachments DROP CONSTRAINT IF EXISTS inject_fail;" >/dev/null 2>&1 || true' EXIT
|
||||
ORPHAN=attachments/00000000-0000-4000-8000-000000000000/11111111-1111-4111-8111-111111111111
|
||||
|
||||
{
|
||||
echo "== A: PUT прошёл, запись метаданных упала -> компенсация"
|
||||
before=$(count); echo "objects before: $before"
|
||||
psql_ "ALTER TABLE attachments ADD CONSTRAINT inject_fail CHECK (false) NOT VALID;"
|
||||
python3 scripts/upload_tools.py ok
|
||||
sleep 1
|
||||
docker compose logs api --since 1m | grep "metadata insert failed" | tail -1 || true
|
||||
docker compose logs api --since 1m | grep "compensation delete failed" || echo "(сбоев компенсации нет)"
|
||||
psql_ "ALTER TABLE attachments DROP CONSTRAINT inject_fail;"
|
||||
after=$(count); echo "objects after: $after"
|
||||
[ "$before" = "$after" ] && echo "PASS: объект не остался" || echo "FAIL: число объектов изменилось"
|
||||
} 2>&1 | tee evidence/orphans-A.txt
|
||||
|
||||
{
|
||||
echo "== D: обрыв загрузки клиентом (заявлено 5 МБ, отправлено 300 КБ)"
|
||||
before=$(count); echo "objects before: $before"
|
||||
python3 scripts/upload_tools.py abort
|
||||
sleep 3
|
||||
after=$(count); echo "objects after: $after"
|
||||
[ "$before" = "$after" ] && echo "PASS: в S3 ничего не записано" || echo "FAIL: число объектов изменилось"
|
||||
for i in 1 2; do docker compose exec -T --index $i api sh -c "echo временных файлов в реплике $i: \$(ls /tmp | wc -l)"; done
|
||||
} 2>&1 | tee evidence/orphans-D.txt
|
||||
|
||||
{
|
||||
echo "== B: объект без записи в БД (имитация падения между PUT и INSERT)"
|
||||
./scripts/s3-admin.sh s3api put-object --bucket attachments --key "$ORPHAN" >/dev/null
|
||||
sleep 2
|
||||
echo "-- grace=1h: свежий объект не трогается"
|
||||
docker compose run --rm -T reconciler ./reconcile -dry-run -grace=1h 2>&1 | grep -E "orphan|reconcile:" || true
|
||||
echo "-- grace=1s, dry-run: найден, но не удалён"
|
||||
docker compose run --rm -T reconciler ./reconcile -dry-run -grace=1s 2>&1 | grep -E "orphan|reconcile:" || true
|
||||
./scripts/s3-admin.sh s3 ls s3://attachments/ --recursive | grep -c 1111 || true
|
||||
echo "-- grace=1s, реальный запуск: удалён"
|
||||
docker compose run --rm -T reconciler ./reconcile -grace=1s 2>&1 | grep -E "orphan|reconcile:" || true
|
||||
./scripts/s3-admin.sh s3 ls s3://attachments/ --recursive | grep 1111 || echo "PASS: сирота удалён"
|
||||
} 2>&1 | tee evidence/orphans-B.txt
|
||||
|
||||
{
|
||||
echo "== C: объект потерян, метаданные остались"
|
||||
key=$(psql_ "SELECT object_key FROM attachments ORDER BY created_at LIMIT 1;")
|
||||
echo "удаляем объект $key (тестовое вложение)"
|
||||
./scripts/s3-admin.sh s3 rm "s3://attachments/$key" >/dev/null
|
||||
docker compose run --rm -T reconciler ./reconcile -dry-run -grace=1s -check-missing 2>&1 | grep -E "MISSING|reconcile:" || true
|
||||
} 2>&1 | tee evidence/orphans-C.txt
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Bounded subprocess: validate original or produce pipeline v1 variants.
|
||||
|
||||
No credentials, network access or client-controlled output paths are used here.
|
||||
Exit codes: 2 invalid image, 3 resource limit. Unexpected failures are retryable.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import resource
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
limit = int(os.environ.get('IMAGE_MEMORY_MB', '384')) * 1024 * 1024
|
||||
resource.setrlimit(resource.RLIMIT_AS, (limit, limit))
|
||||
resource.setrlimit(resource.RLIMIT_CPU, (20, 21))
|
||||
|
||||
from PIL import Image, ImageOps, UnidentifiedImageError
|
||||
|
||||
Image.MAX_IMAGE_PIXELS = 40_000_000
|
||||
warnings.simplefilter('error', Image.DecompressionBombWarning)
|
||||
|
||||
|
||||
def fit_size(size, edge):
|
||||
w, h = size
|
||||
if max(w, h) <= edge:
|
||||
return size
|
||||
return max(1, w * edge // max(w, h)), max(1, h * edge // max(w, h))
|
||||
|
||||
|
||||
def run():
|
||||
mode, source = sys.argv[1:3]
|
||||
if os.path.getsize(source) > 10 * 1024 * 1024:
|
||||
raise MemoryError()
|
||||
with Image.open(source) as im:
|
||||
if im.format not in ('PNG', 'JPEG'):
|
||||
raise ValueError('format')
|
||||
if im.width * im.height > Image.MAX_IMAGE_PIXELS:
|
||||
raise MemoryError()
|
||||
im.verify()
|
||||
with Image.open(source) as im:
|
||||
im.load()
|
||||
if mode == 'validate':
|
||||
return
|
||||
out = sys.argv[3]
|
||||
im = ImageOps.exif_transpose(im)
|
||||
rgba = im.convert('RGBA')
|
||||
rgb = Image.new('RGB', rgba.size, 'white')
|
||||
rgb.paste(rgba, mask=rgba.getchannel('A'))
|
||||
del rgba, im
|
||||
thumb = rgb.resize(fit_size(rgb.size, 256), Image.Resampling.LANCZOS)
|
||||
crop = ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS, centering=(0.5, 0.5))
|
||||
optimized = rgb.resize(fit_size(rgb.size, 1280), Image.Resampling.LANCZOS)
|
||||
variants = []
|
||||
for name, image, fmt in [('thumbnail', thumb, 'JPEG'), ('crop', crop, 'JPEG'), ('optimized', optimized, 'WEBP')]:
|
||||
image.info.clear()
|
||||
options = dict(quality=85, optimize=True) if fmt == 'JPEG' else dict(quality=80, method=4)
|
||||
image.save(os.path.join(out, name), fmt, **options)
|
||||
variants.append(dict(name=name, width=image.width, height=image.height,
|
||||
content_type='image/jpeg' if fmt == 'JPEG' else 'image/webp'))
|
||||
print(json.dumps(variants))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
run()
|
||||
except (MemoryError, Image.DecompressionBombError, Image.DecompressionBombWarning):
|
||||
sys.exit(3)
|
||||
except (UnidentifiedImageError, OSError, ValueError, SyntaxError):
|
||||
sys.exit(2)
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
python3 scripts/container_snapshot.py evidence/lab5-restart-before.json
|
||||
healthy() {
|
||||
[ "$(docker compose ps -q "$@" | xargs docker inspect -f '{{.State.Health.Status}}' | grep -c '^healthy$')" = "$#" ]
|
||||
}
|
||||
docker compose stop worker outbox
|
||||
docker compose restart postgres redis seaweedfs broker
|
||||
until healthy postgres redis seaweedfs broker; do sleep 1; done
|
||||
docker compose restart s3-proxy
|
||||
docker compose restart api lb
|
||||
docker compose start worker outbox
|
||||
python3 scripts/container_snapshot.py evidence/lab5-restart-after.json
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mkdir -p evidence
|
||||
docker run --rm --network mtusi2026lab5_edge -v "$PWD":/w python:3.13-alpine \
|
||||
python /w/tests/load.py --base-url http://lb:8080 --duration 80 --concurrency 4 \
|
||||
--output /w/evidence/load-rolling.json > evidence/load-rolling.txt &
|
||||
LOAD=$!
|
||||
sleep 8
|
||||
date '+%T rolling start'
|
||||
./scripts/rolling-update.sh
|
||||
date '+%T rolling end'
|
||||
wait "$LOAD"
|
||||
docker compose ps > evidence/ps-after-rolling.txt
|
||||
docker compose logs lb --since 4m > evidence/lb-rolling.log
|
||||
for c in $(docker compose ps -q api); do docker exec "$c" printenv APP_VERSION; done
|
||||
cat evidence/load-rolling.txt
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
N=2
|
||||
export APP_VERSION="${APP_VERSION:-v$(date +%s)}"
|
||||
wait_healthy() {
|
||||
until [ "$(docker compose ps -q api | xargs docker inspect -f '{{.State.Health.Status}}' | grep -c '^healthy$')" = "$1" ]; do sleep 1; done
|
||||
}
|
||||
docker compose build api
|
||||
for old in $(docker compose ps -q api); do
|
||||
docker compose up -d --no-deps --no-recreate --scale api=$((N+1)) api
|
||||
wait_healthy $((N+1))
|
||||
sleep 5 # LB: rise 2 + обновление DNS
|
||||
echo "$(date +%T) replacing $(docker inspect -f '{{.Name}}' "$old")"
|
||||
docker stop -t 20 "$old" >/dev/null
|
||||
docker rm "$old" >/dev/null
|
||||
done
|
||||
wait_healthy $N
|
||||
echo "$(date +%T) done, APP_VERSION=$APP_VERSION"
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker run --rm --network mtusi2026lab5_s3access -v "$PWD/secrets:/s:ro" -e AWS_DEFAULT_REGION=us-east-1 \
|
||||
--entrypoint /bin/sh amazon/aws-cli:2.39.1 -c \
|
||||
'AWS_ACCESS_KEY_ID=$(cat /s/s3_admin_access_key.txt) AWS_SECRET_ACCESS_KEY=$(cat /s/s3_admin_secret_key.txt) exec aws --endpoint-url http://s3-proxy:8333 "$@"' sh "$@"
|
||||
@@ -0,0 +1,57 @@
|
||||
import base64, hashlib, json, struct, subprocess, time, urllib.error, urllib.request, uuid, zlib
|
||||
|
||||
BASE = "http://localhost:8080"; PWD = "correct-horse-battery-staple"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, token=None, basic=None, timeout=10):
|
||||
h = dict(headers or {})
|
||||
if token: h["Authorization"] = "Bearer " + token
|
||||
if basic: h["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode()
|
||||
req = urllib.request.Request(BASE + path, data=body, method=method, headers=h)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
|
||||
def jcall(method, path, obj, token=None):
|
||||
st, raw = call(method, path, json.dumps(obj).encode(), {"Content-Type": "application/json"}, token)
|
||||
return st, json.loads(raw or b"null")
|
||||
|
||||
|
||||
def tiny_png():
|
||||
def chunk(t, d):
|
||||
c = struct.pack(">I", len(d)) + t + d
|
||||
return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
||||
raw = b"".join(b"\x00" + b"\xff\x00\x00" * 8 for _ in range(8))
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 8, 8, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
name = "pr_" + uuid.uuid4().hex[:8]
|
||||
_, user = jcall("POST", "/api/v1/users", {"username": name, "display_name": "probe", "password": PWD})
|
||||
st, raw = call("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
|
||||
tok = json.loads(raw)["token"]
|
||||
_, chat = jcall("POST", "/api/v1/chats", {"title": "probe", "member_ids": [user["id"]]}, tok)
|
||||
png = tiny_png()
|
||||
st, raw = call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=probe.png", png,
|
||||
{"Content-Type": "image/png"}, tok)
|
||||
att = json.loads(raw)
|
||||
print("upload:", st, att.get("id"))
|
||||
want = hashlib.sha256(png).hexdigest()
|
||||
|
||||
t0 = time.time()
|
||||
subprocess.run(["docker", "compose", "restart", "seaweedfs"], capture_output=True)
|
||||
print(f"restart seaweedfs returned after {time.time() - t0:.1f}s")
|
||||
while time.time() - t0 < 90:
|
||||
t1 = time.time()
|
||||
try:
|
||||
st, data = call("GET", f"/api/v1/attachments/{att['id']}/content", token=tok, timeout=5)
|
||||
ok = st == 200 and hashlib.sha256(data).hexdigest() == want
|
||||
print(f"t={time.time() - t0:5.1f}s status={st} bytes_ok={ok} took={time.time() - t1:.1f}s")
|
||||
if ok:
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"t={time.time() - t0:5.1f}s {type(e).__name__} took={time.time() - t1:.1f}s")
|
||||
time.sleep(1)
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
cid=""
|
||||
for id in $(docker compose ps -q api); do
|
||||
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
if [ "$host" = "$TARGET_INSTANCE_ID" ]; then cid="$id"; break; fi
|
||||
done
|
||||
[ -n "$cid" ] || { echo "container for instance $TARGET_INSTANCE_ID not found" >&2; exit 1; }
|
||||
|
||||
docker stop -t 8 "$cid" >/dev/null
|
||||
echo "stopped $cid" >&2
|
||||
@@ -0,0 +1,55 @@
|
||||
import base64, json, socket, struct, sys, urllib.request, uuid, zlib
|
||||
|
||||
BASE_HOST, BASE_PORT = "localhost", 8080
|
||||
PWD = "correct-horse-battery-staple"
|
||||
|
||||
|
||||
def api(method, path, body=None, headers=None):
|
||||
req = urllib.request.Request(f"http://{BASE_HOST}:{BASE_PORT}{path}", data=body, method=method, headers=headers or {})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
|
||||
def chunk(t, d):
|
||||
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
||||
|
||||
|
||||
PNG = (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 8, 8, 8, 2, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(b"".join(b"\x00" + b"\x10\x80\xf0" * 8 for _ in range(8)))) + chunk(b"IEND", b""))
|
||||
|
||||
name = "up_" + uuid.uuid4().hex[:8]
|
||||
j = {"Content-Type": "application/json"}
|
||||
_, raw = api("POST", "/api/v1/users", json.dumps({"username": name, "display_name": "u", "password": PWD}).encode(), j)
|
||||
user = json.loads(raw)
|
||||
_, raw = api("POST", "/api/v1/auth/sessions", None, {"Authorization": "Basic " + base64.b64encode(f"{name}:{PWD}".encode()).decode()})
|
||||
token = json.loads(raw)["token"]
|
||||
_, raw = api("POST", "/api/v1/chats", json.dumps({"title": "u", "member_ids": [user["id"]]}).encode(), {**j, "Authorization": "Bearer " + token})
|
||||
chat = json.loads(raw)
|
||||
|
||||
mode = sys.argv[1] if len(sys.argv) > 1 else "ok"
|
||||
if mode == "ok":
|
||||
st, raw = api("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=u.png", PNG,
|
||||
{"Content-Type": "image/png", "Authorization": "Bearer " + token})
|
||||
print("upload status:", st)
|
||||
elif mode == "abort":
|
||||
# заявляем 5 МБ отправляем 300 КБ и закрываем соединение
|
||||
s = socket.create_connection((BASE_HOST, BASE_PORT))
|
||||
head = (f"POST /api/v1/chats/{chat['id']}/attachments?filename=a.png HTTP/1.1\r\nHost: localhost\r\n"
|
||||
f"Authorization: Bearer {token}\r\nContent-Type: image/png\r\nContent-Length: 5000000\r\n\r\n")
|
||||
s.sendall(head.encode() + PNG + b"\0" * 300_000)
|
||||
s.close()
|
||||
print("aborted upload after 300 KB of 5000000")
|
||||
elif mode == "roundtrip":
|
||||
import hashlib
|
||||
st, raw = api("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=r.png", PNG,
|
||||
{"Content-Type": "image/png", "Authorization": "Bearer " + token})
|
||||
if st != 201:
|
||||
print("upload", st); sys.exit(1)
|
||||
att = json.loads(raw)
|
||||
st, data = api("GET", f"/api/v1/attachments/{att['id']}/content", None, {"Authorization": "Bearer " + token})
|
||||
good = st == 200 and hashlib.sha256(data).hexdigest() == att["sha256"]
|
||||
print("roundtrip", "ok" if good else f"fail {st}")
|
||||
sys.exit(0 if good else 1)
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p evidence
|
||||
./scripts/bootstrap.sh
|
||||
docker compose config --quiet
|
||||
docker compose up -d --build
|
||||
./scripts/wait_ready.sh 180
|
||||
make check > evidence/lab5-check.txt 2>&1
|
||||
make test > evidence/lab5-smoke.txt 2>&1
|
||||
go test ./... > evidence/lab5-go-test.txt 2>&1
|
||||
go vet ./... > evidence/lab5-go-vet.txt 2>&1
|
||||
python3 scripts/check_pipeline.py > evidence/lab5-pipeline.txt 2>&1
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh > evidence/lab5-persistence.txt 2>&1
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh > evidence/lab5-failover.txt 2>&1
|
||||
docker compose start api
|
||||
./scripts/wait_ready.sh 120
|
||||
echo "PASS lab5 level4; sanitized results: evidence/lab5-*"
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
python3 - "${1:-120}" <<'PYTHON'
|
||||
import sys,time,urllib.request,urllib.error
|
||||
end=time.monotonic()+int(sys.argv[1])
|
||||
while time.monotonic()<end:
|
||||
try:
|
||||
with urllib.request.urlopen('http://localhost:8080/health/ready',timeout=3) as r:
|
||||
if r.status==200:
|
||||
print('ready');sys.exit(0)
|
||||
except (OSError,urllib.error.URLError):
|
||||
pass
|
||||
time.sleep(1)
|
||||
raise SystemExit('API not ready before deadline')
|
||||
PYTHON
|
||||
Reference in New Issue
Block a user