Files
mtusi2026lab2/tests/level4.py
2026-10-16 05:27:13 +03:00

338 lines
14 KiB
Python

#!/usr/bin/env python3
"""Уровень 4: собственные автотесты студента.
Проверяет:
- гонку регистрации одного username и rollback создания чата;
- контракт: дубликаты member_ids и невалидный/чужой cursor — 400;
keyset-пагинация без повторов при created_at с долями секунды;
- неверный пароль, logout, expiry при SESSION_TTL_SECONDS=2
(отдельный контейнер api на порту 18080);
- отказ db и cache дольше размера пулов: бизнес-запросы с валидным
токеном получают 503 за ограниченное время (не 401 и не 200), после
восстановления тот же токен снова работает.
Запуск (стек api+db+cache поднят через compose.yaml):
python3 tests/level4.py
python3 tests/level4.py --skip-outage --skip-expiry # быстрые проверки
"""
import argparse
import base64
import concurrent.futures
import json
import subprocess
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
PASSWORD = "Test-password-123456"
REQUEST_BUDGET_S = 10.0
TTL_PORT = 18080
TTL_CONTAINER = "lab2-api-ttl2"
def load_env():
env = {}
for line in (ROOT / ".env").read_text(encoding="utf-8").splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
env[k.strip()] = v.strip()
return env
ENV = load_env()
def call(base, method, path, expected, body=None, token=None, headers=None):
h = {"Accept": "application/json"}
if token:
h["Authorization"] = "Bearer " + token
data = None
if body is not None:
data = json.dumps(body).encode()
h["Content-Type"] = "application/json"
h.update(headers or {})
req = urllib.request.Request(base + path, data=data, headers=h, method=method)
try:
with urllib.request.urlopen(req, timeout=REQUEST_BUDGET_S + 5) as resp:
status, payload = resp.code, resp.read()
except urllib.error.HTTPError as e:
status, payload = e.code, e.read()
allowed = expected if isinstance(expected, (list, tuple)) else [expected]
if status not in allowed:
raise AssertionError(f"{method} {path}: expected {allowed}, got {status}: {payload[:200]}")
try:
return json.loads(payload) if payload else None
except json.JSONDecodeError:
return payload
def timed_call(base, method, path, expected, **kw):
t0 = time.monotonic()
result = call(base, method, path, expected, **kw)
elapsed = time.monotonic() - t0
if elapsed > REQUEST_BUDGET_S:
raise AssertionError(f"{method} {path} took {elapsed:.1f}s > {REQUEST_BUDGET_S}s")
return elapsed, result
def create_user(base, prefix):
return call(base, "POST", "/api/v1/users", 201, body={
"username": f"{prefix}_{uuid.uuid4().hex[:10]}",
"display_name": "T",
"password": PASSWORD,
})
def basic(username, password):
return {"Authorization": "Basic " + base64.b64encode(
f"{username}:{password}".encode()).decode()}
def login(base, username, password=PASSWORD):
return call(base, "POST", "/api/v1/auth/sessions", 201, headers=basic(username, password))
def compose(*args):
subprocess.run(["docker", "compose", "--env-file", ".env", *args],
check=True, capture_output=True, cwd=ROOT)
def psql(sql):
r = subprocess.run(
["docker", "compose", "--env-file", ".env", "exec", "-T", "db", "psql",
"-U", ENV["POSTGRES_USER"], "-d", ENV["POSTGRES_DB"], "-X", "-t", "-A",
"-v", "ON_ERROR_STOP=1", "-c", sql],
capture_output=True, text=True, cwd=ROOT)
if r.returncode != 0:
raise RuntimeError(r.stderr)
return r.stdout.strip()
def wait_ready(base, timeout=60):
deadline = time.monotonic() + timeout
t0 = time.monotonic()
while time.monotonic() < deadline:
try:
call(base, "GET", "/health/ready", 200)
return time.monotonic() - t0
except (AssertionError, urllib.error.URLError, ConnectionError, OSError):
time.sleep(0.5)
raise AssertionError(f"{base} not ready in {timeout}s")
# ---------- гонка и rollback ----------
def test_username_race(base):
username = "race_" + uuid.uuid4().hex[:10]
def try_register(_):
req = urllib.request.Request(
base + "/api/v1/users",
data=json.dumps({"username": username, "display_name": "Race",
"password": PASSWORD}).encode(),
headers={"Content-Type": "application/json"}, method="POST")
try:
with urllib.request.urlopen(req, timeout=15) as resp:
return resp.code
except urllib.error.HTTPError as e:
return e.code
with concurrent.futures.ThreadPoolExecutor(8) as pool:
results = list(pool.map(try_register, range(8)))
cnt = {c: results.count(c) for c in set(results)}
if cnt.get(201, 0) != 1 or any(c not in (201, 409) for c in results):
raise AssertionError(f"expected exactly one 201 and the rest 409, got {cnt}")
print(f" PASS: username race {cnt}")
def test_chat_rollback(base):
alice = create_user(base, "rb")
token = login(base, alice["username"])["token"]
fake_id = str(uuid.uuid4())
title = "rollback " + uuid.uuid4().hex[:8]
call(base, "POST", "/api/v1/chats", 400, token=token,
body={"title": title, "member_ids": [alice["id"], fake_id]})
if psql(f"SELECT count(*) FROM chats WHERE title = '{title}';") != "0":
raise AssertionError("chat was not rolled back")
if psql(f"SELECT count(*) FROM chat_members WHERE user_id = '{fake_id}';") != "0":
raise AssertionError("chat_members row leaked after failed chat create")
print(" PASS: rollback — чат не создан, chat_members пуст")
# ---------- контракт ----------
def test_contract_validation(base):
alice, bob = create_user(base, "cv"), create_user(base, "cv")
token = login(base, alice["username"])["token"]
call(base, "POST", "/api/v1/chats", 400, token=token,
body={"title": "dup", "member_ids": [alice["id"], bob["id"], bob["id"]]})
call(base, "POST", "/api/v1/chats", 400, token=token,
body={"title": "dup", "member_ids": [alice["id"], alice["id"].upper()]})
print(" PASS: дубликаты member_ids — 400")
chat = call(base, "POST", "/api/v1/chats", 201, token=token,
body={"title": "cursor", "member_ids": [alice["id"], bob["id"]]})
other = call(base, "POST", "/api/v1/chats", 201, token=token,
body={"title": "other", "member_ids": [alice["id"]]})
for i in range(3):
call(base, "POST", f"/api/v1/chats/{other['id']}/messages", 201,
token=token, body={"text": f"o{i}"})
foreign = call(base, "GET", f"/api/v1/chats/{other['id']}/messages?limit=1",
200, token=token)["next_cursor"]
path = f"/api/v1/chats/{chat['id']}/messages"
malformed = ["abc|xyz", "123|not-a-uuid", "2026-01-01T00:00:00Z|" + str(uuid.uuid4()),
"|", "12345"]
for bad in malformed + [foreign]:
call(base, "GET", f"{path}?cursor={urllib.parse.quote(bad, safe='')}", 400, token=token)
for bad in malformed:
call(base, "GET", f"/api/v1/chats?cursor={urllib.parse.quote(bad, safe='')}", 400,
token=token)
print(" PASS: невалидный и чужой cursor — 400, не 500")
def test_pagination_fractional_seconds(base):
alice = create_user(base, "pg")
token = login(base, alice["username"])["token"]
chat = call(base, "POST", "/api/v1/chats", 201, token=token,
body={"title": "frac", "member_ids": [alice["id"]]})
path = f"/api/v1/chats/{chat['id']}/messages"
ids = [call(base, "POST", path, 201, token=token, body={"text": f"m{i}"})["id"]
for i in range(7)]
# Данные, загруженные не через API (импорт, seed), могут иметь доли секунды.
psql(f"""
UPDATE messages SET created_at = date_trunc('second', now())
+ (floor(random() * 999999) + 1) * interval '1 microsecond'
WHERE chat_id = '{chat['id']}';
""")
expected = psql(f"SELECT string_agg(id::text, ',' ORDER BY created_at, id) "
f"FROM messages WHERE chat_id = '{chat['id']}';").split(",")
seen, cursor = [], None
for _ in range(20):
q = "?limit=2" + (f"&cursor={urllib.parse.quote(cursor, safe='')}" if cursor else "")
page = call(base, "GET", path + q, 200, token=token)
seen += [m["id"] for m in page["items"]]
cursor = page["next_cursor"]
if cursor is None:
break
if seen != expected or sorted(seen) != sorted(ids):
raise AssertionError(f"pagination mismatch: got {len(seen)} items, expected {len(expected)}")
print(" PASS: keyset-пагинация без повторов при created_at с долями секунды")
# ---------- аутентификация ----------
def test_wrong_password_and_logout(base):
alice = create_user(base, "lo")
call(base, "POST", "/api/v1/auth/sessions", 401,
headers=basic(alice["username"], "wrong-password-123"))
call(base, "POST", "/api/v1/auth/sessions", 401,
headers=basic("no_such_user_" + uuid.uuid4().hex[:6], PASSWORD))
token = login(base, alice["username"])["token"]
call(base, "GET", "/api/v1/users/me", 200, token=token)
call(base, "DELETE", "/api/v1/auth/sessions/current", 204, token=token)
call(base, "GET", "/api/v1/users/me", 401, token=token)
call(base, "DELETE", "/api/v1/auth/sessions/current", 401, token=token)
print(" PASS: неверный пароль — 401; logout — 204, затем 401")
def test_expiry():
subprocess.run(["docker", "rm", "-f", TTL_CONTAINER], capture_output=True)
compose("run", "-d", "--rm", "--no-deps", "--name", TTL_CONTAINER,
"-e", "SESSION_TTL_SECONDS=2", "-p", f"{TTL_PORT}:8080", "api")
base = f"http://127.0.0.1:{TTL_PORT}"
try:
wait_ready(base)
alice = create_user(base, "ttl")
session = login(base, alice["username"])
call(base, "GET", "/api/v1/users/me", 200, token=session["token"])
time.sleep(3.5)
call(base, "GET", "/api/v1/users/me", 401, token=session["token"])
print(f" PASS: expiry — при TTL=2 s токен действует, через 3.5 s — 401 "
f"(expires_at={session['expires_at']})")
finally:
subprocess.run(["docker", "rm", "-f", TTL_CONTAINER], capture_output=True)
# ---------- отказы ----------
def outage(base, service, username, token, requests, hold_s):
"""Останавливает service, шлёт запросы с валидным токеном, восстанавливает."""
call(base, "GET", "/api/v1/users/me", 200, token=token)
compose("stop", service)
t_stop = time.monotonic()
times = []
try:
time.sleep(1)
call(base, "GET", "/health/ready", 503)
for i in range(requests):
elapsed, _ = timed_call(base, "GET", "/api/v1/users/me", 503, token=token)
times.append(elapsed)
timed_call(base, "POST", "/api/v1/auth/sessions", 503,
headers=basic(username, PASSWORD))
if service == "db":
timed_call(base, "POST", "/api/v1/users", 503, body={
"username": "down_" + uuid.uuid4().hex[:8], "display_name": "D",
"password": PASSWORD})
remaining = hold_s - (time.monotonic() - t_stop)
if remaining > 0:
time.sleep(remaining)
finally:
compose("start", service)
down_for = time.monotonic() - t_stop
recovery = wait_ready(base)
call(base, "GET", "/api/v1/users/me", 200, token=token)
print(f" PASS: {service} остановлен {down_for:.0f} s: {requests} запросов с токеном → 503 "
f"(max {max(times):.2f} s), после старта ready за {recovery:.1f} s, "
f"тот же токен → 200")
def test_dependency_outage(base):
alice = create_user(base, "out")
token = login(base, alice["username"])["token"]
# Запросов больше, чем соединений в пулах (pg 8, redis 4): пул не должен истощаться.
outage(base, "cache", alice["username"], token, requests=10, hold_s=20)
outage(base, "db", alice["username"], token, requests=16, hold_s=25)
def main():
p = argparse.ArgumentParser()
p.add_argument("--base-url", default="http://localhost:8080")
p.add_argument("--skip-outage", action="store_true")
p.add_argument("--skip-expiry", action="store_true")
args = p.parse_args()
base = args.base_url.rstrip("/")
tests = [
("username_race", lambda: test_username_race(base)),
("chat_rollback", lambda: test_chat_rollback(base)),
("contract_validation", lambda: test_contract_validation(base)),
("pagination_fractional_seconds", lambda: test_pagination_fractional_seconds(base)),
("wrong_password_and_logout", lambda: test_wrong_password_and_logout(base)),
]
if not args.skip_expiry:
tests.append(("expiry", test_expiry))
if not args.skip_outage:
tests.append(("dependency_outage", lambda: test_dependency_outage(base)))
for name, fn in tests:
print(f"== {name} ==")
fn()
print("PASS: level4 checks passed")
if __name__ == "__main__":
try:
main()
except (AssertionError, RuntimeError, subprocess.CalledProcessError) as e:
print("FAIL:", e)
sys.exit(1)