Вложения: приватный S3-bucket (RustFS 1.0.4) + метаданные в PostgreSQL, upload PNG/JPEG с лимитами до декодирования, авторизованный download, attachment_ids в сообщениях, идемпотентный bootstrap bucket, failover/persistence/S3-down тесты и REPORT.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
13 KiB
Python
Executable File
257 lines
13 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""Регрессия: собственные проверки уровня 4 из lab2, перенесённые на стенд lab3 (через LB).
|
||
Запуск: sh scripts/test-lab3.sh (внутри сервиса tester).
|
||
|
||
Требуют запущенный стенд и доступ к docker (останавливают/ставят на паузу valkey и postgres,
|
||
затем возвращают их). Используют только синтетические данные.
|
||
"""
|
||
import base64
|
||
import hashlib
|
||
import os
|
||
import subprocess
|
||
import sys
|
||
import threading
|
||
import time
|
||
import uuid
|
||
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
|
||
|
||
BASE_URL = os.environ.get('BASE_URL', 'http://lb:8080')
|
||
TTL_BASE_URL = os.environ.get('TTL_BASE_URL', 'http://api-ttl:8080')
|
||
VALKEY_CLI = 'valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_conf)" '
|
||
|
||
|
||
def compose(*args: str, capture: bool = False) -> str:
|
||
result = subprocess.run(['docker', 'compose', '--env-file', str(ROOT / '.env'), *args],
|
||
cwd=ROOT, check=True, text=True,
|
||
stdout=subprocess.PIPE if capture else subprocess.DEVNULL,
|
||
stderr=subprocess.DEVNULL)
|
||
return (result.stdout or '').strip()
|
||
|
||
|
||
def sql(query: str) -> str:
|
||
return compose('exec', '-T', 'postgres', 'psql', '-U', 'app', '-d', 'messenger', '-tAc', query, capture=True)
|
||
|
||
|
||
def valkey(command: str) -> str:
|
||
return compose('exec', '-T', 'valkey', 'sh', '-c', VALKEY_CLI + command, capture=True)
|
||
|
||
|
||
def basic(username: str, password: str) -> dict:
|
||
return {'Authorization': 'Basic ' + base64.b64encode(f'{username}:{password}'.encode()).decode()}
|
||
|
||
|
||
def wait_ready(c: Client, seconds: float = 60) -> None:
|
||
deadline = time.monotonic() + seconds
|
||
while True:
|
||
try:
|
||
c.call('GET', '/health/ready', schema='Health')
|
||
return
|
||
except Failure:
|
||
check(time.monotonic() < deadline, 'API did not become ready')
|
||
time.sleep(0.5)
|
||
|
||
|
||
def wait_healthy(service: str, seconds: float = 60) -> None:
|
||
deadline = time.monotonic() + seconds
|
||
container = compose('ps', '-q', service, capture=True)
|
||
while subprocess.run(['docker', 'inspect', '-f', '{{.State.Health.Status}}', container],
|
||
text=True, capture_output=True).stdout.strip() != 'healthy':
|
||
check(time.monotonic() < deadline, service + ' did not become healthy')
|
||
time.sleep(0.5)
|
||
|
||
|
||
def cache_key(actor: dict) -> str:
|
||
return 'session:' + hashlib.sha256(actor['token'].encode()).hexdigest()
|
||
|
||
|
||
def wait_cache_used(c: Client, actor: dict, seconds: float = 75) -> None:
|
||
"""После недавнего сбоя Valkey API законно не доверяет кэшу SESSION_CACHE_TTL_SECONDS — ждём конца окна."""
|
||
deadline = time.monotonic() + seconds
|
||
while True:
|
||
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
||
if valkey('EXISTS ' + cache_key(actor)) == '1':
|
||
return
|
||
check(time.monotonic() < deadline, 'session was never cached in Valkey')
|
||
time.sleep(2)
|
||
|
||
|
||
def bearer(token: str) -> dict:
|
||
return {'Authorization': 'Bearer ' + token}
|
||
|
||
# ---------------------------------------------------------------- тесты
|
||
|
||
|
||
def test_wrong_password(c: Client):
|
||
alice = c.user('wrongpw')
|
||
username = alice['registration']['username']
|
||
wrong = c.call('POST', '/api/v1/auth/sessions', 401, headers=basic(username, 'definitely-wrong-password'))
|
||
unknown = c.call('POST', '/api/v1/auth/sessions', 401, headers=basic('no_such_' + uuid.uuid4().hex[:8], 'whatever-password-123'))
|
||
# Одинаковый ответ: по нему нельзя узнать, существует ли username.
|
||
check(wrong['json']['error']['message'] == unknown['json']['error']['message'], 'different 401 for unknown user')
|
||
check(wrong['headers']['WWW-Authenticate'] == 'Basic realm="messenger", charset="UTF-8"', 'wrong Basic challenge')
|
||
c.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic !!!not-base64'})
|
||
c.call('POST', '/api/v1/auth/sessions', 401, headers=bearer(alice['token'])) # Bearer на входе не принимается
|
||
|
||
|
||
def test_logout(c: Client):
|
||
alice = c.user('logout')
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||
r = c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||
check(r['headers']['WWW-Authenticate'] == 'Bearer realm="messenger"', 'wrong Bearer challenge')
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=alice)
|
||
revoked = sql("SELECT count(*) FROM sessions s JOIN users u ON u.id = s.user_id "
|
||
f"WHERE u.username = '{alice['registration']['username']}' AND s.revoked_at IS NOT NULL")
|
||
check(revoked == '1', 'revocation not recorded in PostgreSQL')
|
||
|
||
|
||
def test_short_ttl(_c: Client):
|
||
# Отдельная реплика api-ttl с SESSION_TTL_SECONDS=2, общая БД и кэш.
|
||
c = Client(TTL_BASE_URL)
|
||
wait_ready(c)
|
||
alice = c.user('ttl')
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||
time.sleep(3)
|
||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||
|
||
|
||
def test_username_race(c: Client):
|
||
username = 'race_' + uuid.uuid4().hex[:12]
|
||
body = {'username': username, 'display_name': 'Гонка', 'password': 'Race-password-' + uuid.uuid4().hex}
|
||
statuses, barrier = [], threading.Barrier(16)
|
||
|
||
def register():
|
||
barrier.wait()
|
||
statuses.append(Client(BASE_URL, timeout=30).call('POST', '/api/v1/users', [201, 409], body=body)['status'])
|
||
|
||
threads = [threading.Thread(target=register) for _ in range(16)]
|
||
for t in threads:
|
||
t.start()
|
||
for t in threads:
|
||
t.join()
|
||
check(sorted(statuses) == [201] + [409] * 15, f'race result {sorted(statuses)}')
|
||
check(sql(f"SELECT count(*) FROM users WHERE username = '{username}'") == '1', 'duplicate username row')
|
||
|
||
|
||
def test_chat_rollback(c: Client):
|
||
alice = c.user('rollback')
|
||
before = sql('SELECT (SELECT count(*) FROM chats) || \' \' || (SELECT count(*) FROM chat_members)')
|
||
# Второй участник не существует: INSERT chats уже выполнен, INSERT chat_members вставит 1 из 2 -> ROLLBACK.
|
||
c.call('POST', '/api/v1/chats', 400, actor=alice,
|
||
body={'title': 'Откат', 'member_ids': [alice['id'], str(uuid.uuid4())]})
|
||
after = sql('SELECT (SELECT count(*) FROM chats) || \' \' || (SELECT count(*) FROM chat_members)')
|
||
check(before == after, f'partial chat left after failure: {before} -> {after}')
|
||
listed = c.call('GET', '/api/v1/chats', actor=alice, schema='ChatPage')['json']
|
||
check(listed['items'] == [], 'failed chat is visible')
|
||
|
||
|
||
def test_cursor_rules(c: Client):
|
||
alice, bob, _eve, chat = c.bootstrap()
|
||
other = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Другой', 'member_ids': [alice['id']]},
|
||
schema='Chat')['json']
|
||
for i in range(3):
|
||
c.message(alice, chat, f'm{i}')
|
||
c.message(alice, other, f'o{i}')
|
||
page = c.call('GET', f'/api/v1/chats/{chat["id"]}/messages?limit=1', actor=alice, schema='MessagePage')['json']
|
||
from urllib.parse import quote
|
||
cursor = quote(page['next_cursor'], safe='')
|
||
c.call('GET', f'/api/v1/chats/{other["id"]}/messages?cursor={cursor}', 400, actor=alice) # курсор чужого чата
|
||
c.call('GET', f'/api/v1/chats/{chat["id"]}/messages?cursor=garbage', 400, actor=alice)
|
||
c.call('GET', f'/api/v1/chats/{chat["id"]}/messages?limit=101', 400, actor=alice)
|
||
c.call('POST', '/api/v1/chats', 400, actor=alice, body={'title': 'x', 'member_ids': [alice['id']], 'extra': 1})
|
||
c.call('POST', f'/api/v1/chats/{chat["id"]}/messages', 400, actor=bob, body={'text': 'x', 'sender_id': alice['id']})
|
||
|
||
|
||
def test_valkey_cache_role(c: Client):
|
||
alice = c.user('cache')
|
||
wait_cache_used(c, alice)
|
||
ttl = int(valkey('TTL ' + cache_key(alice)))
|
||
check(0 < ttl <= 60, f'unexpected cache TTL {ttl}')
|
||
valkey('FLUSHALL') # потеря всего кэша
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User') # источник истины — PostgreSQL
|
||
check(valkey('EXISTS ' + cache_key(alice)) == '1', 'cache was not refilled on miss')
|
||
|
||
|
||
def test_valkey_down(c: Client):
|
||
alice = c.user('vkdown')
|
||
try:
|
||
compose('stop', 'valkey')
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User') # обход кэша через PostgreSQL
|
||
c.call('GET', '/api/v1/users/me', 401, headers=bearer('A' * 43)) # неизвестный токен — не доверяем
|
||
c.call('GET', '/api/v1/users/me', 401) # аноним — 401
|
||
bob = c.user('vkdown_login') # вход работает без кэша
|
||
c.call('GET', '/health/ready', schema='Health') # Valkey не критичен
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=bob)
|
||
c.call('GET', '/api/v1/users/me', 401, actor=bob)
|
||
finally:
|
||
compose('start', 'valkey')
|
||
wait_healthy('valkey')
|
||
|
||
|
||
def test_revoke_during_cache_partition(c: Client):
|
||
# Худший случай инвалидации: logout во время разрыва API <-> Valkey, DEL потерян, а копия отозванной
|
||
# сессии в Valkey пережила разрыв. Окно недоверия к кэшу (SESSION_CACHE_TTL_SECONDS после ошибки)
|
||
# не даёт её использовать. TCP может доставить DEL повторной передачей после восстановления сети,
|
||
# поэтому потерю DEL моделируем явно: возвращаем сохранённую до разрыва копию записи.
|
||
alice = c.user('partition')
|
||
wait_cache_used(c, alice)
|
||
key = cache_key(alice)
|
||
stale_value = valkey('GET ' + key)
|
||
container = compose('ps', '-q', 'valkey', capture=True)
|
||
network = 'mtusi-lab4_data' # Valkey в lab3 только во внутренней сети data
|
||
subprocess.run(['docker', 'network', 'disconnect', network, container], check=True)
|
||
try:
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) # COMMIT в PostgreSQL прошёл
|
||
finally:
|
||
subprocess.run(['docker', 'network', 'connect', '--alias', 'valkey', network, container], check=True)
|
||
valkey(f"SET {key} '{stale_value}' EX 60")
|
||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||
|
||
|
||
def test_postgres_down(c: Client):
|
||
alice = c.user('pgdown')
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||
try:
|
||
compose('stop', 'postgres')
|
||
c.call('GET', '/health/live', schema='Health')
|
||
c.call('GET', '/health/ready', 503)
|
||
r = c.call('GET', '/api/v1/users/me', 503, actor=alice) # ограниченная ошибка, не 500/200
|
||
check(r['json']['error']['code'] == 'unavailable', 'expected unavailable')
|
||
c.call('GET', '/api/v1/users/me', 503, headers=bearer('B' * 43)) # неизвестный токен не проходит
|
||
c.call('GET', '/api/v1/chats', 401) # аноним не пропускается
|
||
c.call('POST', '/api/v1/users', 503, body={'username': 'pg_' + uuid.uuid4().hex[:8],
|
||
'display_name': 'x', 'password': 'Password-123456'})
|
||
finally:
|
||
compose('start', 'postgres')
|
||
wait_ready(c)
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User') # та же сессия жива после рестарта БД
|
||
|
||
|
||
TESTS = [test_wrong_password, test_logout, test_short_ttl, test_username_race, test_chat_rollback,
|
||
test_cursor_rules, test_valkey_cache_role, test_revoke_during_cache_partition, test_valkey_down,
|
||
test_postgres_down]
|
||
|
||
|
||
def main() -> int:
|
||
c = Client(BASE_URL)
|
||
wait_ready(c)
|
||
failed = 0
|
||
for test in TESTS:
|
||
started = time.monotonic()
|
||
try:
|
||
test(c)
|
||
print(f'PASS {test.__name__} ({time.monotonic() - started:.1f}s)')
|
||
except (Failure, subprocess.CalledProcessError, OSError) as exc:
|
||
failed += 1
|
||
print(f'FAIL {test.__name__}: {exc}')
|
||
print(f'{len(TESTS) - failed}/{len(TESTS)} passed')
|
||
return 1 if failed else 0
|
||
|
||
|
||
if __name__ == '__main__':
|
||
sys.exit(main())
|