#!/usr/bin/env python3 from concurrent.futures import ThreadPoolExecutor import json from pathlib import Path import sys import threading import uuid sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, Failure, check sys.path.insert(0, str(Path(__file__).resolve().parent)) import test_attachments RESULTS = [] def case(name): def wrap(fn): def run(*a): try: detail = fn(*a) RESULTS.append({'test': name, 'result': 'PASS', 'detail': detail}) print('PASS', name, '-', detail or '') except Failure as exc: RESULTS.append({'test': name, 'result': 'FAIL', 'detail': str(exc)}) print('FAIL', name, '-', exc) return run return wrap def msg_path(chat): return '/api/v1/chats/' + chat['id'] + '/messages' def history(c, actor, chat): out, cursor = [], None while True: q = '?limit=100' + ('&cursor=' + cursor if cursor else '') page = c.call('GET', msg_path(chat) + q, actor=actor, schema='MessagePage')['json'] out += page['items'] cursor = page['next_cursor'] if not cursor: return out @case('one session served by >=2 replicas without re-login (no stickiness)') def t_no_sticky(c, alice, bob, chat): seen = {} for _ in range(30): iid = c.call('GET', '/api/v1/users/me', actor=alice, schema='User')['headers']['X-Instance-Id'] seen[iid] = seen.get(iid, 0) + 1 check(len(seen) >= 2, 'only one replica observed: %s' % seen) return 'distribution %s' % sorted(seen.values()) @case('X-Instance-Id is present on errors and health too') def t_instance_on_errors(c, alice, bob, chat): r = c.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=alice) check(bool(r['headers'].get('X-Instance-Id')), 'missing on 400') r = c.call('GET', '/api/v1/users/me', 401) check(bool(r['headers'].get('X-Instance-Id')), 'missing on 401') r = c.call('GET', '/health/ready', schema='Health') check(bool(r['headers'].get('X-Instance-Id')), 'missing on health') @case('Idempotency-Key missing or malformed -> 400, nothing stored') def t_key_validation(c, alice, bob, chat): before = len(history(c, alice, chat)) c.call('POST', msg_path(chat), 400, actor=alice, body={'text': 'без ключа'}) for bad in ('', 'a b', 'clé', 'x' * 129, 'a/b'): c.call('POST', msg_path(chat), 400, actor=alice, body={'text': 'плохой ключ'}, headers={'Idempotency-Key': bad}) c.call('POST', msg_path(chat), 201, actor=alice, body={'text': 'граница ключа'}, headers={'Idempotency-Key': 'A-z0_9.:' + 'k' * 120}) check(len(history(c, alice, chat)) == before + 1, 'rejected requests created messages') @case('concurrent replay of one key through different replicas -> exactly one message') def t_concurrent_same_key(c, alice, bob, chat): key = 'conc-' + uuid.uuid4().hex text = 'Конкурентный повтор ' + key n = 24 barrier = threading.Barrier(n) raw = json.dumps({'text': text}, ensure_ascii=False).encode() def send(_): local = Client(c.base, timeout=10) barrier.wait() r = local.call('POST', msg_path(chat), 201, actor=alice, raw=raw, headers={'Idempotency-Key': key, 'Content-Type': 'application/json'}, schema='Message') return r['json'], r['headers']['X-Instance-Id'] with ThreadPoolExecutor(max_workers=n) as pool: results = list(pool.map(send, range(n))) bodies = {json.dumps(m, sort_keys=True) for m, _ in results} instances = {i for _, i in results} check(len(bodies) == 1, 'replays returned different messages: %d' % len(bodies)) check(len(instances) >= 2, 'all requests hit one replica; test does not prove cross-replica') same = [m for m in history(c, bob, chat) if m['text'] == text] check(len(same) == 1, 'expected exactly 1 stored message, found %d' % len(same)) return '%d parallel requests via %d replicas -> 1 message' % (n, len(instances)) @case('replay is semantic: JSON key order/whitespace does not matter; other body -> 409') def t_semantic_replay(c, alice, bob, chat): key = uuid.uuid4().hex first = c.call('POST', msg_path(chat), 201, actor=alice, raw=b'{"text":"abc"}', headers={'Idempotency-Key': key, 'Content-Type': 'application/json'}, schema='Message') again = c.call('POST', msg_path(chat), 201, actor=alice, raw=b'{ "text" : "abc" }\n', headers={'Idempotency-Key': key, 'Content-Type': 'application/json'}, schema='Message') check(first['json'] == again['json'], 'replay returned another message') c.call('POST', msg_path(chat), 409, actor=alice, body={'text': 'abd'}, headers={'Idempotency-Key': key}) third = c.call('POST', msg_path(chat), 201, actor=alice, body={'text': 'abc'}, headers={'Idempotency-Key': key}, schema='Message') check(third['json'] == first['json'], 'stored result changed after 409') @case('key scope = user + chat: same key is independent for other user / other chat') def t_key_scope(c, alice, bob, chat): key = 'shared-' + uuid.uuid4().hex a = c.message(alice, chat, 'scope', key=key)['json'] b = c.message(bob, chat, 'scope', key=key)['json'] chat2 = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Второй', 'member_ids': [alice['id'], bob['id']]}, schema='Chat')['json'] a2 = c.message(alice, chat2, 'scope', key=key)['json'] check(len({a['id'], b['id'], a2['id']}) == 3, 'scopes collided') check(b['sender_id'] == bob['id'], 'sender mixed up') @case('replay re-checks the session and chat access') def t_replay_rechecks_access(c, alice, bob, chat, eve): key = uuid.uuid4().hex c.message(alice, chat, 'access', key=key) c.call('POST', msg_path(chat), 404, actor=eve, body={'text': 'access'}, headers={'Idempotency-Key': key}) c.call('POST', msg_path(chat), 401, body={'text': 'access'}, headers={'Idempotency-Key': key}) c.call('POST', msg_path(chat), 404, actor=eve, body={'text': ' '}, headers={'Idempotency-Key': key}) c.call('POST', msg_path(chat), 404, actor=eve, body={'text': 'x'}) @case('logout is visible on every replica immediately (shared sessions)') def t_logout_shared(c, alice, bob, chat): tmp = c.user('tmp') c.call('GET', '/api/v1/users/me', actor=tmp, schema='User') c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=tmp) seen = set() for _ in range(10): seen.add(c.call('GET', '/api/v1/users/me', 401, actor=tmp)['headers']['X-Instance-Id']) check(len(seen) >= 2, 'revocation checked on one replica only') return 'revoked token rejected by %d replicas' % len(seen) def main(): import argparse p = argparse.ArgumentParser() p.add_argument('--base-url', default='http://localhost:8080') p.add_argument('--json', default=None) a = p.parse_args() c = Client(a.base_url, timeout=60) alice, bob, eve, chat = c.bootstrap() for t in (t_no_sticky, t_instance_on_errors, t_key_validation, t_concurrent_same_key, t_semantic_replay, t_key_scope, t_logout_shared): t(c, alice, bob, chat) t_replay_rechecks_access(c, alice, bob, chat, eve) for needs_eve, t in test_attachments.register(case, history, msg_path): t(c, alice, bob, chat, *([eve] if needs_eve else [])) failed = [r for r in RESULTS if r['result'] != 'PASS'] print('%d/%d passed' % (len(RESULTS) - len(failed), len(RESULTS))) if a.json: Path(a.json).write_text(json.dumps(RESULTS, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') sys.exit(1 if failed else 0) if __name__ == '__main__': main()