diff --git a/student-tests/expiry_check.py b/student-tests/expiry_check.py new file mode 100644 index 00000000..7bde51ec --- /dev/null +++ b/student-tests/expiry_check.py @@ -0,0 +1,56 @@ +"""Usage (стенд уже запущен через docker compose up -d): + python student-tests/expiry_check.py --ttl 2 + +Скрипт пересоздаёт только api с SESSION_TTL_SECONDS=, проверяет истечение сессии, +затем возвращает api с обычным TTL из .env. Volumes и данные не трогаются. +""" +import argparse +import hashlib +import os +import time + +from helpers import call, compose, new_user, wait_ready + + +def restart_api(ttl=None): + env = dict(os.environ) + if ttl is None: + env.pop('SESSION_TTL_SECONDS', None) + else: + env['SESSION_TTL_SECONDS'] = str(ttl) + compose('up', '-d', '--no-deps', '--no-build', 'api', env=env) + wait_ready() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--ttl', type=int, default=2) + args = parser.parse_args() + + print('пересоздаю api с SESSION_TTL_SECONDS=%d' % args.ttl) + restart_api(args.ttl) + try: + alice = new_user('expiry') + status = call('GET', '/api/v1/users/me', token=alice['token'])[0] + print('сразу после входа: /users/me -> %d' % status) + assert status == 200 + key = 'session:' + hashlib.sha256(alice['token'].encode()).hexdigest() + cache_ttl = int(compose('exec', '-T', 'valkey', 'valkey-cli', 'TTL', key)) + print('TTL ключа этой сессии в Valkey: %d с (-2 = ключа нет)' % cache_ttl) + assert cache_ttl <= args.ttl, 'кэш живёт дольше сессии' + + time.sleep(args.ttl + 1.5) + status, headers, _ = call('GET', '/api/v1/users/me', token=alice['token']) + print('через %.1f с: /users/me -> %d, WWW-Authenticate: %s' % (args.ttl + 1.5, status, headers['WWW-Authenticate'])) + assert status == 401 and headers['WWW-Authenticate'] == 'Bearer realm="messenger"' + status = call('DELETE', '/api/v1/auth/sessions/current', token=alice['token'])[0] + print('logout истёкшим токеном -> %d' % status) + assert status == 401 + print('PASS: сессия истекла по SESSION_TTL_SECONDS=%d, кэш не продлил её' % args.ttl) + finally: + print('возвращаю api с TTL из .env') + restart_api() + + +if __name__ == '__main__': + main() diff --git a/student-tests/failover_series.py b/student-tests/failover_series.py new file mode 100644 index 00000000..bbe15e39 --- /dev/null +++ b/student-tests/failover_series.py @@ -0,0 +1,71 @@ +"""Usage (стенд запущен через docker compose up -d, обе реплики в ротации): + python student-tests/failover_series.py --output evidence/failover-series.json + +Для каждой API-реплики по очереди и в двух режимах (docker stop — штатная остановка с выводом из ротации, +docker kill — внезапная смерть процесса) под нагрузкой: останавливает реплику, меряет ошибки, RPS, +время до последнего ответа остановленной реплики, восстанавливает её (docker compose start api) +и ждёт возврата в ротацию LB. Volumes, БД и LB не трогаются. +""" +import argparse +import json +import subprocess +import time + +from helpers import Load, api_containers, compose, new_chat, new_user, wait_in_rotation + +WARMUP, AFTER = 5, 10 + + +def run(container, mode): + alice = new_user('series') + chat = new_chat(alice) + load = Load(alice['token'], chat['id']).start() + time.sleep(WARMUP) + acted = time.monotonic() + command = ['docker', 'stop', '-t', '15', container] if mode == 'stop' else ['docker', 'kill', container] + subprocess.run(command, capture_output=True, check=True) + action_seconds = time.monotonic() - acted + time.sleep(AFTER) + load.stop() + instance = container[:12] + target = [r for r in load.records if r[2] == 201 and r[3] == instance and r[0] >= acted] + others = [r for r in load.records if r[2] == 201 and r[3] != instance and r[0] >= acted] + restored = time.monotonic() + compose('start', 'api') + back = wait_in_rotation(container) + result = { + 'replica': instance, 'mode': mode, 'action_seconds': round(action_seconds, 2), + 'target_served_until_s': round(max(r[1] for r in target) - acted, 2) if target else 0.0, + 'first_success_on_other_s': round(min(r[1] for r in others) - acted, 2) if others else None, + 'before': load.window(acted - WARMUP, acted), + 'during_10s': load.window(acted, acted + AFTER), + 'back_in_rotation_s': round(back - restored, 2), + 'data': load.verify(alice['token']), + } + print('%-12s %-4s errors=%-3d rps %5.1f -> %5.1f target last answer +%.2fs restore %.1fs lost=%d dup=%d' + % (instance, mode, result['during_10s']['errors'], result['before']['rps'], result['during_10s']['rps'], + result['target_served_until_s'], result['back_in_rotation_s'], result['data']['lost'], + result['data']['duplicate_texts'])) + return result + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--output') + parser.add_argument('--modes', default='stop,kill') + args = parser.parse_args() + replicas = api_containers() + assert len(replicas) >= 2, 'need at least two running API replicas' + results = [run(container, mode) for mode in args.modes.split(',') for container in replicas] + bad = [r for r in results if r['data']['lost'] or r['data']['duplicate_texts'] or r['data']['duplicate_ids']] + summary = {'replicas': [c[:12] for c in replicas], 'warmup_s': WARMUP, 'observe_s': AFTER, 'workers': 4, + 'lb_check': 'GET /health/ready every 1s, fall 2, rise 2', 'results': results} + if args.output: + with open(args.output, 'w', encoding='utf-8') as file: + json.dump(summary, file, ensure_ascii=False, indent=2) + print('FAIL: data loss or duplicates' if bad else 'PASS: every replica stopped once per mode; no loss, no duplicates') + raise SystemExit(1 if bad else 0) + + +if __name__ == '__main__': + main() diff --git a/student-tests/failure_check.py b/student-tests/failure_check.py new file mode 100644 index 00000000..a6ffa7ce --- /dev/null +++ b/student-tests/failure_check.py @@ -0,0 +1,127 @@ +"""Usage (стенд уже запущен через docker compose up -d): + python student-tests/failure_check.py + +Останавливает и замораживает Valkey и PostgreSQL (docker compose stop/pause, без удаления volumes), +проверяет поведение API и восстановление. +""" +import time +import uuid + +from helpers import basic, call, compose, new_chat, new_user, wait_ready + +failures = [] + + +def timed(method, path, token=None, body=None, headers=None): + started = time.monotonic() + try: + status = call(method, path, body=body, token=token, headers=headers, timeout=10)[0] + except OSError as error: + status = type(error).__name__ + return status, time.monotonic() - started + + +def expect(title, result, allowed, max_seconds): + status, seconds = result + ok = status in allowed and seconds <= max_seconds + print(' %-48s -> %-4s за %.2f с %s' % (title, status, seconds, 'OK' if ok else 'FAIL')) + if not ok: + failures.append(title) + + +def main(): + alice = new_user('failure') + chat = new_chat(alice) + history = '/api/v1/chats/%s/messages' % chat['id'] + assert call('POST', history, {'text': 'до отказа'}, alice['token'])[0] == 201 + fake = uuid.uuid4().hex * 2 + + print('1. Valkey остановлен: кэш обходится, всё работает через PostgreSQL') + compose('stop', 'valkey') + try: + expect('первый запрос: обнаружение отказа кэша', timed('GET', '/api/v1/users/me', alice['token']), [200], 1.5) + expect('GET /users/me действующим токеном', timed('GET', '/api/v1/users/me', alice['token']), [200], 1) + expect('POST сообщение', timed('POST', history, alice['token'], {'text': 'без кэша'}), [201], 1) + expect('неизвестный токен', timed('GET', '/api/v1/users/me', fake), [401], 1) + expect('без Authorization', timed('GET', '/api/v1/users/me'), [401], 1) + expect('/health/ready (Valkey некритичен)', timed('GET', '/health/ready'), [200], 1) + started = time.monotonic() + other = new_user('nocache') + expect('регистрация и вход без кэша', (200, time.monotonic() - started), [200], 1) + expect('logout без кэша', timed('DELETE', '/api/v1/auth/sessions/current', other['token']), [204], 1.5) + expect('отозванный токен', timed('GET', '/api/v1/users/me', other['token']), [401], 1) + finally: + compose('up', '-d', '--wait', 'valkey') + + print('1б. Valkey на паузе: logout, затем unpause — отозванный токен не оживает из кэша') + revoked = new_user('paused') + call('GET', '/api/v1/users/me', token=revoked['token']) + compose('pause', 'valkey') + try: + expect('logout при паузе Valkey', timed('DELETE', '/api/v1/auth/sessions/current', revoked['token']), [204], 1.5) + finally: + compose('unpause', 'valkey') + time.sleep(6) + expect('через 6 с после unpause: /users/me', timed('GET', '/api/v1/users/me', revoked['token']), [401], 1) + expect('через 6 с после unpause: сообщение', timed('POST', history, revoked['token'], {'text': 'после logout'}), [401], 1) + + print('1в. Valkey на паузе: logout, рестарт API, unpause — отзыв переживает рестарт процесса') + restarted = new_user('restart') + call('GET', '/api/v1/users/me', token=restarted['token']) + compose('pause', 'valkey') + try: + expect('logout при паузе Valkey', timed('DELETE', '/api/v1/auth/sessions/current', restarted['token']), [204], 1.5) + compose('restart', 'api') + wait_ready() + finally: + compose('unpause', 'valkey') + time.sleep(6) + expect('после рестарта API и unpause: /users/me', timed('GET', '/api/v1/users/me', restarted['token']), [401], 1) + expect('после рестарта API и unpause: сообщение', timed('POST', history, restarted['token'], {'text': 'после рестарта'}), [401], 1) + + print('2. PostgreSQL остановлен: ограниченная ошибка 503, анонимный запрос не проходит') + compose('stop', 'postgres') + stopped_at = time.monotonic() + try: + expect('GET история', timed('GET', history, alice['token']), [503], 3) + expect('POST сообщение', timed('POST', history, alice['token'], {'text': 'при отказе БД'}), [503], 3) + expect('неизвестный токен (не 200!)', timed('GET', '/api/v1/users/me', fake), [503], 3) + expect('без Authorization', timed('GET', '/api/v1/users/me'), [401], 1) + expect('вход по Basic верным паролем', timed('POST', '/api/v1/auth/sessions', + headers=basic(alice['username'], alice['password'])), [503], 3) + expect('/health/live', timed('GET', '/health/live'), [200], 1) + expect('/health/ready', timed('GET', '/health/ready'), [503], 3) + finally: + compose('up', '-d', '--wait', 'postgres') + ready_at = wait_ready() + print('3. PostgreSQL запущен: готовность через %.1f с после остановки' % (ready_at - stopped_at)) + status = call('GET', '/api/v1/users/me', token=alice['token'])[0] + texts = [m['text'] for m in call('GET', history + '?limit=100', token=alice['token'])[2]['items']] + print(' прежний токен -> %d; история: %s' % (status, texts)) + if status != 200 or texts != ['до отказа', 'без кэша']: + failures.append('восстановление') + + print('4. PostgreSQL заморожен (pause): запросы получают 503 в пределах общего срока, а не зависают') + compose('pause', 'postgres') + try: + expect('GET история', timed('GET', history, alice['token']), [503], 4.5) + expect('POST сообщение', timed('POST', history, alice['token'], {'text': 'при заморозке'}), [503], 4.5) + expect('неизвестный токен (не 200!)', timed('GET', '/api/v1/users/me', fake), [503], 4.5) + expect('вход по Basic', timed('POST', '/api/v1/auth/sessions', headers=basic(alice['username'], alice['password'])), [503], 4.5) + expect('/health/ready', timed('GET', '/health/ready'), [503], 2) + expect('/health/live', timed('GET', '/health/live'), [200], 1) + finally: + compose('unpause', 'postgres') + unpaused = time.monotonic() + wait_ready() + texts = [m['text'] for m in call('GET', history + '?limit=100', token=alice['token'])[2]['items']] + print(' готовность через %.1f с после unpause; история: %s' % (time.monotonic() - unpaused, texts)) + if texts != ['до отказа', 'без кэша']: + failures.append('после заморозки') + + print('FAIL: ' + ', '.join(failures) if failures else 'PASS: отказы Valkey и PostgreSQL обработаны') + raise SystemExit(1 if failures else 0) + + +if __name__ == '__main__': + main() diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..8c577953 --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,191 @@ +import base64 +import json +import os +from pathlib import Path +import subprocess +import threading +import time +import urllib.error +import urllib.parse +import urllib.request +import uuid + +BASE_URL = os.environ.get('BASE_URL', 'http://127.0.0.1:8080').rstrip('/') +ROOT = Path(__file__).resolve().parents[1] + + +def call(method, path, body=None, token=None, raw=None, headers=None, timeout=10): + all_headers = {} + if token: + all_headers['Authorization'] = 'Bearer ' + token + if body is not None: + raw = json.dumps(body, ensure_ascii=False).encode('utf-8') + if raw is not None: + all_headers['Content-Type'] = 'application/json' + all_headers.update(headers or {}) + if method == 'POST' and path.split('?')[0].endswith('/messages') and 'Idempotency-Key' not in all_headers: + all_headers['Idempotency-Key'] = uuid.uuid4().hex + all_headers = {name: value for name, value in all_headers.items() if value is not None} + request = urllib.request.Request(BASE_URL + path, data=raw, headers=all_headers, method=method) + try: + response = urllib.request.urlopen(request, timeout=timeout) + except urllib.error.HTTPError as error: + response = error + with response: + data = response.read() + is_json = response.headers.get_content_type() == 'application/json' + return response.status, response.headers, json.loads(data) if data and is_json else None + + +def basic(username, password): + return {'Authorization': 'Basic ' + base64.b64encode((username + ':' + password).encode('utf-8')).decode()} + + +def register(label='user', password=None): + username = (label + '_' + uuid.uuid4().hex[:12])[:32] + password = password or 'Pw-' + uuid.uuid4().hex + status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': label, 'password': password}) + assert status == 201, status + return user, password + + +def login(username, password): + status, _, session = call('POST', '/api/v1/auth/sessions', headers=basic(username, password)) + assert status == 201, status + return session + + +def new_user(label='user'): + user, password = register(label) + session = login(user['username'], password) + return dict(user, password=password, token=session['token']) + + +def new_chat(owner, *others, title='Тестовый чат'): + members = [owner['id']] + [u['id'] for u in others] + status, _, chat = call('POST', '/api/v1/chats', {'title': title, 'member_ids': members}, owner['token']) + assert status == 201, status + return chat + + +def read_all_messages(chat_id, token, limit): + items, cursor, pages = [], None, 0 + while True: + query = '?limit=%d' % limit + ('&cursor=' + urllib.parse.quote(cursor, safe='') if cursor else '') + status, _, page = call('GET', '/api/v1/chats/%s/messages%s' % (chat_id, query), token=token) + assert status == 200, status + items += page['items'] + pages += 1 + cursor = page['next_cursor'] + if cursor is None: + return items, pages + + +def compose(*args, env=None, check=True): + result = subprocess.run(['docker', 'compose', *args], cwd=ROOT, capture_output=True, text=True, + encoding='utf-8', env=env) + if check and result.returncode != 0: + raise RuntimeError('docker compose %s failed: %s' % (' '.join(args), result.stderr.strip())) + return result.stdout.strip() + + +def sql(query, db='messenger', service='postgres'): + return compose('exec', '-T', service, 'psql', '-U', 'messenger', '-d', db, '-X', '-tA', '-v', 'ON_ERROR_STOP=1', + '-c', query) + + +def wait_ready(seconds=90): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + try: + if call('GET', '/health/ready', timeout=2)[0] == 200: + return time.monotonic() + except OSError: + pass + time.sleep(0.3) + raise AssertionError('API is not ready after %ds' % seconds) + + +def api_containers(): + return compose('ps', '-q', 'api').split() + + +def wait_in_rotation(container, seconds=60): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + try: + if call('GET', '/health/ready', timeout=2)[1].get('X-Instance-Id') == container[:12]: + return time.monotonic() + except OSError: + pass + time.sleep(0.1) + raise AssertionError('container %s did not enter LB rotation' % container[:12]) + + +class Load: + """Отправляет сообщения в несколько потоков; неудачный POST повторяет с тем же Idempotency-Key.""" + + def __init__(self, token, chat_id, workers=4): + self.token, self.path, self.workers = token, '/api/v1/chats/%s/messages' % chat_id, workers + self.records, self.confirmed, self.uncertain = [], [], 0 + self.stopped = threading.Event() + self.threads = [] + + def start(self): + self.threads = [threading.Thread(target=self.worker, args=(n,), daemon=True) for n in range(self.workers)] + for thread in self.threads: + thread.start() + return self + + def stop(self): + self.stopped.set() + for thread in self.threads: + thread.join() + + def worker(self, n): + sent = 0 + while not self.stopped.is_set(): + key, text = uuid.uuid4().hex, 'load %d-%d %s' % (n, sent, uuid.uuid4().hex[:6]) + sent += 1 + while True: + started = time.monotonic() + try: + status, headers, body = call('POST', self.path, {'text': text}, self.token, + headers={'Idempotency-Key': key}, timeout=5) + instance = headers.get('X-Instance-Id') + except OSError as error: + status, instance, body = type(error).__name__, None, None + self.records.append((started, time.monotonic(), status, instance)) + if status == 201: + self.confirmed.append(body) + break + if self.stopped.is_set(): + self.uncertain += 1 + break + time.sleep(0.05) + + def window(self, start, end): + rows = [r for r in self.records if start <= r[0] < end] + ok = [r for r in rows if r[2] == 201] + latencies = sorted((r[1] - r[0]) * 1000 for r in ok) + p = lambda q: round(latencies[min(len(latencies) - 1, int(len(latencies) * q))], 1) if latencies else None + return {'seconds': round(end - start, 2), 'ok': len(ok), 'errors': len(rows) - len(ok), + 'rps': round(len(ok) / (end - start), 1) if end > start else 0, + 'p50_ms': p(0.5), 'p95_ms': p(0.95), 'p99_ms': p(0.99), + 'error_kinds': sorted({str(r[2]) for r in rows if r[2] != 201})} + + def verify(self, token): + stored, cursor = [], None + while True: + query = '?limit=100' + ('&cursor=' + urllib.parse.quote(cursor, safe='') if cursor else '') + page = call('GET', self.path + query, token=token)[2] + stored += page['items'] + cursor = page['next_cursor'] + if cursor is None: + break + ids = [m['id'] for m in stored] + texts = [m['text'] for m in stored] + return {'confirmed': len(self.confirmed), 'stored': len(stored), + 'lost': len({m['id'] for m in self.confirmed} - set(ids)), + 'duplicate_ids': len(ids) - len(set(ids)), 'duplicate_texts': len(texts) - len(set(texts)), + 'uncertain_at_stop': self.uncertain} diff --git a/student-tests/rolling_update_check.py b/student-tests/rolling_update_check.py new file mode 100644 index 00000000..eabfedcc --- /dev/null +++ b/student-tests/rolling_update_check.py @@ -0,0 +1,60 @@ +"""Usage (стенд запущен, обе реплики в ротации): + python student-tests/rolling_update_check.py --output evidence/rolling-update.json + +Под непрерывной нагрузкой (4 потока, повтор с тем же Idempotency-Key) выполняет scripts/rolling_update.py +и проверяет: ошибки клиента, RPS, что все реплики заменены, что подтверждённые сообщения не потеряны и не задвоены. +""" +import argparse +import json +from pathlib import Path +import subprocess +import sys +import time + +from helpers import Load, api_containers, new_chat, new_user + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--output') + parser.add_argument('--no-build', action='store_true') + args = parser.parse_args() + before = api_containers() + alice = new_user('rolling') + chat = new_chat(alice) + load = Load(alice['token'], chat['id']).start() + time.sleep(5) + started = time.monotonic() + command = [sys.executable, str(ROOT / 'scripts' / 'rolling_update.py')] + (['--no-build'] if args.no_build else []) + update = subprocess.run(command, capture_output=True, text=True, encoding='utf-8') + finished = time.monotonic() + time.sleep(5) + load.stop() + print(update.stdout.strip()) + after = api_containers() + result = { + 'replicas_before': [c[:12] for c in before], 'replicas_after': [c[:12] for c in after], + 'update_seconds': round(finished - started, 1), 'update_exit_code': update.returncode, + 'before_5s': load.window(started - 5, started), + 'during_update': load.window(started, finished), + 'after_5s': load.window(finished, finished + 5), + 'data': load.verify(alice['token']), + 'log': update.stdout.strip().splitlines(), + } + replaced = not set(before) & set(after) and len(after) == len(before) + ok = update.returncode == 0 and replaced and not result['data']['lost'] and not result['data']['duplicate_texts'] + print('rolling update %.1fs: errors=%d, rps %.1f -> %.1f -> %.1f, lost=%d, duplicates=%d, all replaced=%s' + % (result['update_seconds'], result['during_update']['errors'], result['before_5s']['rps'], + result['during_update']['rps'], result['after_5s']['rps'], result['data']['lost'], + result['data']['duplicate_texts'], replaced)) + if args.output: + with open(args.output, 'w', encoding='utf-8') as file: + json.dump(result, file, ensure_ascii=False, indent=2) + print('PASS' if ok else 'FAIL: ' + update.stderr.strip()[-500:]) + raise SystemExit(0 if ok else 1) + + +if __name__ == '__main__': + main() diff --git a/student-tests/sigterm_check.py b/student-tests/sigterm_check.py new file mode 100644 index 00000000..5d09b0ee --- /dev/null +++ b/student-tests/sigterm_check.py @@ -0,0 +1,79 @@ +"""Usage (стенд запущен через docker compose up -d): + python student-tests/sigterm_check.py + docker compose start api + +Останавливает одну API-реплику (docker stop = SIGTERM) и всё это время опрашивает API через LB: +реплика сначала выходит из ротации (readiness 503), потом завершается; клиенты не должны получить ошибок. +""" +import argparse +import subprocess +import threading +import time +import urllib.error +import urllib.request +import uuid + +from helpers import BASE_URL, api_containers, call, new_chat, new_user + + +def docker(*args): + return subprocess.run(['docker', *args], capture_output=True, text=True, encoding='utf-8').stdout.strip() + + +def probe_forever(results, stop_event): + while not stop_event.is_set(): + started = time.monotonic() + try: + with urllib.request.urlopen(BASE_URL + '/health/live', timeout=1) as response: + outcome = str(response.status) + except urllib.error.HTTPError as error: + outcome = str(error.code) + except Exception as error: + outcome = type(error).__name__ + results.append((started, outcome)) + time.sleep(0.02) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--container', default=None) + parser.add_argument('--stop-timeout', type=int, default=10, help='секунд до SIGKILL у docker stop') + args = parser.parse_args() + args.container = args.container or api_containers()[0] + + marker = 'PRIVATE-' + uuid.uuid4().hex + alice = new_user('alice') + chat = new_chat(alice) + status = call('POST', '/api/v1/chats/%s/messages' % chat['id'], {'text': marker}, alice['token'])[0] + logs_before = docker('logs', args.container) + print('сообщение с маркером отправлено: HTTP %d; маркер в логах: %s' % (status, marker in logs_before)) + + results, stop_event = [], threading.Event() + prober = threading.Thread(target=probe_forever, args=(results, stop_event)) + prober.start() + time.sleep(1) + + sigterm_at = time.monotonic() + docker('stop', '-t', str(args.stop_timeout), args.container) + stopped_at = time.monotonic() + time.sleep(1) + stop_event.set() + prober.join() + + before = [r for t, r in results if t < sigterm_at] + after = [r for t, r in results if t >= sigterm_at] + exit_code = docker('inspect', '--format', '{{.State.ExitCode}}', args.container) + print('docker stop занял: %.2f с (лимит %d с)' % (stopped_at - sigterm_at, args.stop_timeout)) + print('код выхода контейнера: %s (0 — сам завершился по SIGTERM; 137 — убит SIGKILL)' % exit_code) + print('запросы до SIGTERM: %d, все 200: %s' % (len(before), set(before) == {'200'})) + print('запросы после SIGTERM: %d, результаты: %s' % (len(after), sorted(set(after)))) + print('ответов 5xx за всё время: %d' % sum(r.startswith('5') for _, r in results)) + + print('\nлоги остановки:') + for line in docker('logs', args.container).splitlines(): + if any(word in line for word in ('draining', 'Shutting down', 'stopping', 'shutdown', 'Finished server')): + print(' ' + line) + + +if __name__ == '__main__': + main() diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..be589b60 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,157 @@ +import unittest +import uuid + +from helpers import call, login, new_chat, new_user, read_all_messages + + +class ErrorFormatMixin: + def assertError(self, result, status, code): + actual, headers, body = result + self.assertEqual(actual, status) + self.assertEqual(body['error']['code'], code) + self.assertEqual(body['error']['request_id'], headers['X-Request-Id']) + self.assertNotIn('Traceback', body['error']['message']) + + +class InvalidJsonTest(ErrorFormatMixin, unittest.TestCase): + def test_malformed_json(self): + """Битый JSON -> 400 invalid_request""" + self.assertError(call('POST', '/api/v1/users', raw=b'{"username": "abc",'), 400, 'invalid_request') + + def test_json_not_object(self): + """JSON-массив вместо объекта -> 400""" + self.assertError(call('POST', '/api/v1/users', raw=b'[1, 2, 3]'), 400, 'invalid_request') + + def test_wrong_type(self): + """Число вместо строки -> 400 (без тихого приведения типов)""" + self.assertError(call('POST', '/api/v1/users', {'username': 12345, 'display_name': 'x', 'password': 'Long-enough-pass-1'}), 400, 'invalid_request') + + def test_extra_field(self): + """Лишнее поле в теле -> 400""" + body = {'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': 'x', 'password': 'Long-enough-pass-1', 'is_admin': True} + self.assertError(call('POST', '/api/v1/users', body), 400, 'invalid_request') + + +class DuplicateUsernameTest(ErrorFormatMixin, unittest.TestCase): + def test_duplicate_username(self): + """Повторная регистрация того же username -> 409 conflict""" + user = new_user('dup') + again = call('POST', '/api/v1/users', {'username': user['username'], 'display_name': 'Другой', + 'password': 'Other-password-123'}) + self.assertError(again, 409, 'conflict') + + +class ForeignChatTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice, self.bob, self.eve = new_user('alice'), new_user('bob'), new_user('eve') + self.chat = new_chat(self.alice, self.bob) + self.path = '/api/v1/chats/' + self.chat['id'] + + def test_outsider_cannot_read_chat(self): + """Посторонний читает чат -> 404, как будто чата нет""" + self.assertError(call('GET', self.path, token=self.eve['token']), 404, 'not_found') + missing = call('GET', '/api/v1/chats/' + str(uuid.uuid4()), token=self.eve['token']) + self.assertEqual(missing[2]['error']['message'], call('GET', self.path, token=self.eve['token'])[2]['error']['message']) + + def test_outsider_cannot_write_or_read_history(self): + """Посторонний пишет/читает историю -> 404""" + self.assertError(call('POST', self.path + '/messages', {'text': 'чужое'}, self.eve['token']), 404, 'not_found') + self.assertError(call('GET', self.path + '/messages', token=self.eve['token']), 404, 'not_found') + + def test_outsider_does_not_see_chat_in_list(self): + """Чужой чат не попадает в список чатов""" + status, _, page = call('GET', '/api/v1/chats?limit=100', token=self.eve['token']) + self.assertEqual(status, 200) + self.assertNotIn(self.chat['id'], [c['id'] for c in page['items']]) + + def test_member_has_access(self): + """Участник видит чат (контроль: 404 выше не из-за ошибки в тесте)""" + self.assertEqual(call('GET', self.path, token=self.bob['token'])[0], 200) + + +class EmptyMessageTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.path = '/api/v1/chats/%s/messages' % new_chat(self.alice)['id'] + + def test_empty_text(self): + """Пустой текст -> 400""" + self.assertError(call('POST', self.path, {'text': ''}, self.alice['token']), 400, 'invalid_request') + + def test_whitespace_text(self): + """Текст только из пробельных символов -> 400""" + self.assertError(call('POST', self.path, {'text': ' \t\n '}, self.alice['token']), 400, 'invalid_request') + + def test_missing_text(self): + """Тело без поля text -> 400""" + self.assertError(call('POST', self.path, {}, self.alice['token']), 400, 'invalid_request') + + def test_text_length_limit(self): + """4000 символов можно (эмодзи = 1 символ), 4001 — 400""" + self.assertEqual(call('POST', self.path, {'text': '😀' * 4000}, self.alice['token'])[0], 201) + self.assertError(call('POST', self.path, {'text': 'x' * 4001}, self.alice['token']), 400, 'invalid_request') + + +class PaginationTest(ErrorFormatMixin, unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.chat = new_chat(self.alice) + self.sent = [call('POST', '/api/v1/chats/%s/messages' % self.chat['id'], + {'text': 'сообщение %d' % i}, self.alice['token'])[2] for i in range(7)] + + def test_walk_several_pages(self): + """7 сообщений по 3: три страницы, без потерь, повторов и перестановок""" + items, pages = read_all_messages(self.chat['id'], self.alice['token'], limit=3) + self.assertEqual(pages, 3) + self.assertEqual([m['id'] for m in items], [m['id'] for m in self.sent]) + keys = [(m['created_at'], m['id']) for m in items] + self.assertEqual(keys, sorted(keys)) + + def test_cursor_from_other_chat(self): + """cursor от другого чата -> 400""" + _, _, page = call('GET', '/api/v1/chats/%s/messages?limit=1' % self.chat['id'], token=self.alice['token']) + other = new_chat(self.alice, title='Другой') + self.assertError(call('GET', '/api/v1/chats/%s/messages?cursor=%s' % (other['id'], page['next_cursor']), + token=self.alice['token']), 400, 'invalid_request') + + def test_invalid_cursor_and_limit(self): + """Мусорный cursor и limit вне 1..100 -> 400""" + path = '/api/v1/chats/%s/messages' % self.chat['id'] + for query in ('?cursor=not-a-cursor', '?limit=0', '?limit=101', '?limit=abc'): + with self.subTest(query=query): + self.assertError(call('GET', path + query, token=self.alice['token']), 400, 'invalid_request') + + +class IdentityTest(ErrorFormatMixin, unittest.TestCase): + def test_sender_cannot_be_forged(self): + """sender_id в теле запрещён: отправитель только из сессии""" + alice, bob = new_user('alice'), new_user('bob') + chat = new_chat(alice, bob) + forged = call('POST', '/api/v1/chats/%s/messages' % chat['id'], {'text': 'hi', 'sender_id': bob['id']}, alice['token']) + self.assertError(forged, 400, 'invalid_request') + + def test_nul_character_is_preserved(self): + """U+0000 — допустимый code point: сообщение, display_name и title сохраняются и читаются без изменений""" + username, password = 'nul_' + uuid.uuid4().hex[:12], 'Nul-password-123' + status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'a\u0000lice', 'password': password}) + self.assertEqual((status, user['display_name']), (201, 'a\u0000lice')) + alice = dict(user, token=login(username, password)['token']) + self.assertEqual(call('GET', '/api/v1/users/me', token=alice['token'])[2]['display_name'], 'a\u0000lice') + status, _, chat = call('POST', '/api/v1/chats', {'title': 't\u0000', 'member_ids': [alice['id']]}, alice['token']) + self.assertEqual((status, chat['title']), (201, 't\u0000')) + path = '/api/v1/chats/%s/messages' % chat['id'] + status, _, message = call('POST', path, {'text': 'a\u0000b'}, alice['token']) + self.assertEqual((status, message['text']), (201, 'a\u0000b')) + self.assertEqual(call('GET', path, token=alice['token'])[2]['items'][0]['text'], 'a\u0000b') + self.assertEqual(call('GET', '/api/v1/chats/' + chat['id'], token=alice['token'])[2]['title'], 't\u0000') + + def test_lone_surrogate_is_rejected(self): + """Одиночный суррогат не кодируется в UTF-8 -> 400, а не 500""" + alice = new_user('alice') + chat = new_chat(alice) + self.assertError(call('POST', '/api/v1/chats/%s/messages' % chat['id'], raw=b'{"text": "a\\ud800b"}', + token=alice['token']), 400, 'invalid_request') + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..3a56ee20 --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,132 @@ +from datetime import datetime, timezone +import unittest +import uuid + +from helpers import basic, call, login, new_user, register + + +class RegistrationTest(unittest.TestCase): + def body(self, password): + return {'username': 'reg_' + uuid.uuid4().hex[:12], 'display_name': 'Регистрация', 'password': password} + + def test_password_is_not_returned(self): + """Ответ регистрации и /users/me не содержат password/hash""" + status, _, user = call('POST', '/api/v1/users', self.body('Secret-password-1')) + self.assertEqual(status, 201) + self.assertEqual(set(user), {'id', 'username', 'display_name', 'created_at'}) + + def test_password_length_limits(self): + """Пароль 12..128 символов; 11 и 129 -> 400; эмодзи считается одним символом""" + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 11))[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 129))[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body('x' * 128))[0], 201) + self.assertEqual(call('POST', '/api/v1/users', self.body('🔑' * 12))[0], 201) + + def test_password_required_and_not_blank(self): + """Без password или из одних пробелов -> 400""" + body = self.body('x') + del body['password'] + self.assertEqual(call('POST', '/api/v1/users', body)[0], 400) + self.assertEqual(call('POST', '/api/v1/users', self.body(' ' * 20))[0], 400) + + +class LoginTest(unittest.TestCase): + def setUp(self): + self.user, self.password = register('login') + + def assertBasicChallenge(self, result): + status, headers, body = result + self.assertEqual(status, 401) + self.assertEqual(headers['WWW-Authenticate'], 'Basic realm="messenger", charset="UTF-8"') + self.assertEqual(body['error']['code'], 'unauthorized') + return body['error']['message'] + + def test_login_returns_opaque_session(self): + """Вход: 201, token_type=Bearer, токен случайный и не содержит username/UUID""" + first = login(self.user['username'], self.password) + second = login(self.user['username'], self.password) + self.assertEqual(first['token_type'], 'Bearer') + self.assertEqual(first['user'], self.user) + self.assertGreaterEqual(len(first['token']), 43) + self.assertNotEqual(first['token'], second['token']) + self.assertNotIn(self.user['username'], first['token']) + self.assertNotIn(self.user['id'], first['token']) + expires = datetime.fromisoformat(first['expires_at'].replace('Z', '+00:00')) + self.assertGreater(expires, datetime.now(timezone.utc)) + + def test_wrong_password_and_unknown_user_look_the_same(self): + """Неверный пароль и несуществующий пользователь -> одинаковый 401 с Basic challenge""" + wrong = self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', + headers=basic(self.user['username'], 'Wrong-password-1'))) + unknown = self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', + headers=basic('nobody_' + uuid.uuid4().hex[:8], 'Wrong-password-1'))) + self.assertEqual(wrong, unknown) + + def test_malformed_basic(self): + """Нет заголовка, не base64, нет двоеточия, Bearer вместо Basic -> 401 Basic""" + for header in (None, 'Basic', 'Basic !!!not-base64!!!', 'Basic ' + 'bm9jb2xvbg==', 'Bearer abc'): + with self.subTest(header=header): + headers = {'Authorization': header} if header else {} + self.assertBasicChallenge(call('POST', '/api/v1/auth/sessions', headers=headers)) + + def test_password_with_colon_and_unicode(self): + """Двоеточие и не-ASCII в пароле допустимы: делим только по первому ':'""" + user, password = register('colon', password='пароль:с:двоеточием🔑') + self.assertEqual(login(user['username'], password)['user']['id'], user['id']) + + def test_login_rejects_body(self): + """POST /auth/sessions принимает только заголовок, JSON body -> 400""" + status = call('POST', '/api/v1/auth/sessions', {'username': self.user['username']}, + headers=basic(self.user['username'], self.password))[0] + self.assertEqual(status, 400) + + +class BearerTest(unittest.TestCase): + def setUp(self): + self.alice = new_user('alice') + self.eve = new_user('eve') + + def assertBearerChallenge(self, headers): + status, response_headers, _ = call('GET', '/api/v1/users/me', headers=headers) + self.assertEqual(status, 401) + self.assertEqual(response_headers['WWW-Authenticate'], 'Bearer realm="messenger"') + + def test_only_bearer_is_accepted(self): + """Нет заголовка, Basic, X-User-Id, мусорный токен -> 401 Bearer""" + self.assertBearerChallenge({}) + self.assertBearerChallenge(basic(self.alice['username'], self.alice['password'])) + self.assertBearerChallenge({'X-User-Id': self.alice['id']}) + self.assertBearerChallenge({'Authorization': 'Bearer ' + uuid.uuid4().hex}) + self.assertBearerChallenge({'Authorization': 'Bearer'}) + + def test_authentication_before_validation(self): + """Аноним с битым JSON на защищённом endpoint получает 401, а не 400; с сессией — 400""" + self.assertEqual(call('POST', '/api/v1/chats', raw=b'{bad')[0], 401) + self.assertEqual(call('POST', '/api/v1/chats', raw=b'{bad', token=self.alice['token'])[0], 400) + self.assertEqual(call('POST', '/api/v1/users', raw=b'{bad')[0], 400) + + def test_scheme_is_case_insensitive(self): + """Схема аутентификации нечувствительна к регистру (RFC 9110)""" + status, _, me = call('GET', '/api/v1/users/me', headers={'Authorization': 'bearer ' + self.alice['token']}) + self.assertEqual((status, me['id']), (200, self.alice['id'])) + + def test_x_user_id_does_not_override_session(self): + """X-User-Id игнорируется: владелец определяется только токеном""" + status, _, me = call('GET', '/api/v1/users/me', token=self.alice['token'], headers={'X-User-Id': self.eve['id']}) + self.assertEqual((status, me['id']), (200, self.alice['id'])) + + +class LogoutTest(unittest.TestCase): + def test_logout_revokes_only_current_session(self): + """Logout -> 204 без тела; этот токен -> 401, вторая сессия того же пользователя работает""" + alice = new_user('alice') + other = login(alice['username'], alice['password'])['token'] + status, _, body = call('DELETE', '/api/v1/auth/sessions/current', token=alice['token']) + self.assertEqual((status, body), (204, None)) + self.assertEqual(call('GET', '/api/v1/users/me', token=alice['token'])[0], 401) + self.assertEqual(call('DELETE', '/api/v1/auth/sessions/current', token=alice['token'])[0], 401) + self.assertEqual(call('GET', '/api/v1/users/me', token=other)[0], 200) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..4d258446 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,68 @@ +from concurrent.futures import ThreadPoolExecutor +import threading +import unittest +import uuid + +from helpers import basic, call, new_chat, new_user, read_all_messages + +CLIENTS = 10 +REGISTRATIONS = 20 +PER_CLIENT = 10 + + +class ConcurrencyTest(unittest.TestCase): + def test_100_messages_from_10_clients(self): + """100 параллельных сообщений от 10 клиентов -> ровно 100 уникальных в истории""" + users = [new_user('client%d' % i) for i in range(CLIENTS)] + chat = new_chat(users[0], *users[1:], title='Нагрузка') + path = '/api/v1/chats/%s/messages' % chat['id'] + barrier = threading.Barrier(CLIENTS) + + def client(index): + barrier.wait() + results = [] + for n in range(PER_CLIENT): + status, _, message = call('POST', path, {'text': 'client %d msg %d' % (index, n)}, users[index]['token']) + results.append((status, message)) + return results + + with ThreadPoolExecutor(max_workers=CLIENTS) as pool: + responses = [r for batch in pool.map(client, range(CLIENTS)) for r in batch] + + self.assertEqual([s for s, _ in responses], [201] * CLIENTS * PER_CLIENT) + sent_ids = {m['id'] for _, m in responses} + items, pages = read_all_messages(chat['id'], users[0]['token'], limit=30) + read_ids = [m['id'] for m in items] + keys = [(m['created_at'], m['id']) for m in items] + + self.assertEqual(len(sent_ids), 100, 'сервер выдал повторяющиеся id') + self.assertEqual(len(read_ids), 100, 'в истории не ровно 100 сообщений') + self.assertEqual(set(read_ids), sent_ids, 'история не совпадает с подтверждёнными сообщениями') + self.assertEqual(len({m['text'] for m in items}), 100, 'потерялся или задвоился текст') + self.assertEqual(keys, sorted(keys), 'история не упорядочена по (created_at, id)') + print('\n отправлено 201: %d, уникальных id: %d, прочитано: %d за %d страниц(ы)' + % (len(responses), len(sent_ids), len(read_ids), pages)) + + def test_parallel_registration_of_same_username(self): + """20 одновременных регистраций одного username -> ровно один 201, остальные 409; вход только с паролем победителя""" + username = 'race_' + uuid.uuid4().hex[:12] + barrier = threading.Barrier(REGISTRATIONS) + + def register(index): + password = 'Race-password-%02d' % index + barrier.wait() + status = call('POST', '/api/v1/users', {'username': username, 'display_name': 'Гонка', 'password': password})[0] + return status, password + + with ThreadPoolExecutor(max_workers=REGISTRATIONS) as pool: + results = list(pool.map(register, range(REGISTRATIONS))) + statuses = sorted(status for status, _ in results) + self.assertEqual(statuses, [201] + [409] * (REGISTRATIONS - 1)) + winner = [password for status, password in results if status == 201][0] + loser = [password for status, password in results if status == 409][0] + self.assertEqual(call('POST', '/api/v1/auth/sessions', headers=basic(username, winner))[0], 201) + self.assertEqual(call('POST', '/api/v1/auth/sessions', headers=basic(username, loser))[0], 401) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_db.py b/student-tests/test_db.py new file mode 100644 index 00000000..5997fd83 --- /dev/null +++ b/student-tests/test_db.py @@ -0,0 +1,58 @@ +import hashlib +import time +import unittest +import uuid + +from helpers import call, compose, new_chat, new_user, sql + + +class DatabaseTest(unittest.TestCase): + def test_chat_creation_is_rolled_back(self): + """Неизвестный участник: INSERT chats выполнен, INSERT chat_members упал по FK, всё откатано""" + alice = new_user('alice') + chats_before = int(sql('SELECT count(*) FROM chats')) + members_before = int(sql('SELECT count(*) FROM chat_members')) + ghost = str(uuid.uuid4()) + since = sql("SELECT to_char(now() - interval '1 second', 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"')") + status, _, body = call('POST', '/api/v1/chats', {'title': 'Откат', 'member_ids': [alice['id'], ghost]}, + alice['token']) + self.assertEqual(status, 400, body) + time.sleep(0.5) + log = compose('logs', '--no-log-prefix', '--since', since, 'postgres') + self.assertIn('violates foreign key constraint', log) + self.assertIn(ghost, log) + self.assertEqual(int(sql('SELECT count(*) FROM chats')), chats_before) + self.assertEqual(int(sql('SELECT count(*) FROM chat_members')), members_before) + + def test_secrets_are_not_stored_in_plaintext(self): + """В users лежит argon2id-хеш, в sessions — sha256 токена, не сам токен""" + alice = new_user('alice') + password_hash = sql("SELECT password_hash FROM users WHERE id = '%s'" % alice['id']) + self.assertTrue(password_hash.startswith('$argon2id$'), password_hash[:10]) + self.assertNotIn(alice['password'], password_hash) + digest = hashlib.sha256(alice['token'].encode()).hexdigest() + self.assertEqual(sql("SELECT count(*) FROM sessions WHERE token_hash = '\\x%s'" % digest), '1') + dump = sql("SELECT string_agg(encode(token_hash, 'escape'), '') FROM sessions WHERE user_id = '%s'" % alice['id']) + self.assertNotIn(alice['token'], dump) + + def test_migrations_are_idempotent(self): + """Повторный запуск миграций ничего не меняет и завершается успешно""" + before = sql('SELECT version, applied_at FROM schema_migrations ORDER BY version') + users = sql('SELECT count(*) FROM users') + output = compose('run', '--rm', '--no-deps', 'migrate') + self.assertIn('skip 0001_init: already applied', output) + self.assertEqual(sql('SELECT version, applied_at FROM schema_migrations ORDER BY version'), before) + self.assertEqual(sql('SELECT count(*) FROM users'), users) + + def test_history_query_uses_index(self): + """Запрос страницы истории идёт по индексу (chat_id, created_at, id), без Sort""" + alice = new_user('alice') + chat = new_chat(alice) + plan = sql("SET enable_seqscan = off; SET enable_bitmapscan = off; EXPLAIN SELECT id FROM messages WHERE chat_id = '%s' " + "ORDER BY created_at, id LIMIT 51" % chat['id']) + self.assertIn('messages_chat_history_idx', plan) + self.assertNotIn('Sort', plan) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_idempotency.py b/student-tests/test_idempotency.py new file mode 100644 index 00000000..4ed9185b --- /dev/null +++ b/student-tests/test_idempotency.py @@ -0,0 +1,96 @@ +from concurrent.futures import ThreadPoolExecutor +import threading +import unittest +import uuid + +from helpers import call, new_chat, new_user, read_all_messages + +PARALLEL = 20 + + +def post(chat, user, key, body=None, raw=None): + return call('POST', '/api/v1/chats/%s/messages' % chat['id'], body, user['token'], raw=raw, + headers={'Idempotency-Key': key}) + + +class IdempotencyTest(unittest.TestCase): + def setUp(self): + self.alice, self.bob = new_user('alice'), new_user('bob') + self.chat = new_chat(self.alice, self.bob) + + def history(self): + return read_all_messages(self.chat['id'], self.alice['token'], limit=100)[0] + + def test_replay_returns_same_message(self): + """Повтор того же запроса с тем же ключом -> тот же 201 и тот же Message, дубля нет""" + key = uuid.uuid4().hex + first = post(self.chat, self.alice, key, {'text': 'один раз'}) + second = post(self.chat, self.alice, key, {'text': 'один раз'}) + self.assertEqual((first[0], second[0]), (201, 201)) + self.assertEqual(first[2], second[2]) + self.assertEqual([m['id'] for m in self.history()], [first[2]['id']]) + + def test_json_formatting_does_not_matter(self): + """Порядок/пробелы в JSON семантически равны: повтор с другим форматированием -> тот же Message""" + key = uuid.uuid4().hex + first = post(self.chat, self.alice, key, raw='{"text":"формат"}'.encode()) + second = post(self.chat, self.alice, key, raw='{ "text" : "формат" }'.encode()) + self.assertEqual((first[0], second[0]), (201, 201)) + self.assertEqual(first[2]['id'], second[2]['id']) + + def test_other_body_with_same_key_conflicts(self): + """Тот же ключ с другим телом -> 409, второе сообщение не создаётся""" + key = uuid.uuid4().hex + self.assertEqual(post(self.chat, self.alice, key, {'text': 'первое'})[0], 201) + status, _, body = post(self.chat, self.alice, key, {'text': 'другое'}) + self.assertEqual((status, body['error']['code']), (409, 'conflict')) + self.assertEqual(len(self.history()), 1) + + def test_missing_or_invalid_key(self): + """Нет ключа, пустой, недопустимые символы, длиннее 128 -> 400""" + for key in (None, '', 'bad key', 'bad/key', 'café', 'x' * 129): + with self.subTest(key=key): + self.assertEqual(post(self.chat, self.alice, key, {'text': 'x'})[0], 400) + self.assertEqual(post(self.chat, self.alice, 'A.b_c:d-1' + 'x' * 119, {'text': 'граница'})[0], 201) + + def test_key_scope_is_user_and_chat(self): + """Одинаковый ключ у разных пользователей и в разных чатах — независимые сообщения""" + key = uuid.uuid4().hex + other_chat = new_chat(self.alice, title='Другой') + a = post(self.chat, self.alice, key, {'text': 'от Алисы'}) + b = post(self.chat, self.bob, key, {'text': 'от Боба'}) + c = post(other_chat, self.alice, key, {'text': 'от Алисы'}) + self.assertEqual([a[0], b[0], c[0]], [201, 201, 201]) + self.assertEqual(len({a[2]['id'], b[2]['id'], c[2]['id']}), 3) + + def test_replay_rechecks_session_and_access(self): + """Повтор снова проверяет сессию и доступ: после logout -> 401, посторонний с тем же ключом -> 404""" + key = uuid.uuid4().hex + self.assertEqual(post(self.chat, self.alice, key, {'text': 'до выхода'})[0], 201) + eve = new_user('eve') + self.assertEqual(post(self.chat, eve, key, {'text': 'до выхода'})[0], 404) + call('DELETE', '/api/v1/auth/sessions/current', token=self.alice['token']) + self.assertEqual(post(self.chat, self.alice, key, {'text': 'до выхода'})[0], 401) + + def test_parallel_retries_through_replicas_create_one_message(self): + """20 одновременных POST с одним ключом через LB (разные реплики) -> ровно одно сообщение, все 201 с одним id""" + key = uuid.uuid4().hex + barrier = threading.Barrier(PARALLEL) + + def send(_): + barrier.wait() + status, headers, body = post(self.chat, self.alice, key, {'text': 'конкурентный повтор'}) + return status, headers['X-Instance-Id'], body + + with ThreadPoolExecutor(max_workers=PARALLEL) as pool: + results = list(pool.map(send, range(PARALLEL))) + self.assertEqual({status for status, _, _ in results}, {201}) + self.assertEqual(len({body['id'] for _, _, body in results}), 1) + self.assertGreaterEqual(len({instance for _, instance, _ in results}), 2, 'запросы не попали на разные реплики') + self.assertEqual(len(self.history()), 1) + print('\n 20 повторов: реплики %s, сообщений в истории: %d' + % (sorted({instance for _, instance, _ in results}), len(self.history()))) + + +if __name__ == '__main__': + unittest.main() diff --git a/student-tests/test_replicas.py b/student-tests/test_replicas.py new file mode 100644 index 00000000..84149108 --- /dev/null +++ b/student-tests/test_replicas.py @@ -0,0 +1,41 @@ +import unittest +import uuid + +from helpers import call, new_chat, new_user + + +class ReplicasTest(unittest.TestCase): + def test_one_session_is_served_by_several_replicas(self): + """Один токен без повторного входа обслуживается разными репликами (X-Instance-Id), все ответы 200""" + alice = new_user('alice') + results = [call('GET', '/api/v1/users/me', token=alice['token']) for _ in range(10)] + self.assertEqual({status for status, _, _ in results}, {200}) + self.assertGreaterEqual(len({headers['X-Instance-Id'] for _, headers, _ in results}), 2) + + def test_write_on_one_replica_is_read_on_another(self): + """Сообщение, подтверждённое одной репликой, сразу видно при чтении через другую""" + alice = new_user('alice') + chat = new_chat(alice) + path = '/api/v1/chats/%s/messages' % chat['id'] + for i in range(6): + _, written_by, message = call('POST', path, {'text': 'm%d' % i}, alice['token']) + status, read_by, page = call('GET', path + '?limit=100', token=alice['token']) + self.assertEqual(status, 200) + self.assertIn(message['id'], [m['id'] for m in page['items']], + 'запись через %s не видна через %s' % (written_by['X-Instance-Id'], read_by['X-Instance-Id'])) + + def test_every_api_response_has_instance_id(self): + """X-Instance-Id есть и в успешных ответах, и в ошибках API, и в health""" + alice = new_user('alice') + for method, path, token, expected in ( + ('GET', '/health/live', None, 200), ('GET', '/health/ready', None, 200), + ('GET', '/api/v1/users/me', None, 401), ('GET', '/api/v1/users/me', alice['token'], 200), + ('GET', '/api/v1/chats/' + str(uuid.uuid4()), alice['token'], 404), ('GET', '/nope', None, 404)): + with self.subTest(path=path, expected=expected): + status, headers, _ = call(method, path, token=token) + self.assertEqual(status, expected) + self.assertTrue(headers['X-Instance-Id']) + + +if __name__ == '__main__': + unittest.main()