"""Проверяет отказ Redis/БД. Hook выбирает явно оператор, токен только в памяти.""" import subprocess import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, arguments, check, run def main(): def configure(p): p.add_argument('--stop-cache', required=True) p.add_argument('--start-cache', required=True) p.add_argument('--stop-db', required=True) p.add_argument('--start-db', required=True) args = arguments('Dependency failures', configure) c = Client(args.base_url, timeout=8) alice, bob, eve, chat = c.bootstrap() def hook(path): subprocess.run([str(Path(path).resolve())], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=40) def wait(): for _ in range(90): try: c.call('GET', '/health/ready', schema='Health') return except AssertionError: time.sleep(.5) raise AssertionError('DB did not recover') result = {} try: hook(args.stop_cache) start = time.monotonic() c.call('GET', '/api/v1/users/me', actor=alice, schema='User') c.call('GET', '/api/v1/users/me', 401) c.call('GET', '/health/ready', schema='Health') confirmed = c.message(alice, chat, 'Redis offline')['json'] messages = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json']['items'] check(confirmed in messages, 'read-after-write failed without Redis') result['redis_offline_scenario_seconds'] = time.monotonic() - start finally: hook(args.start_cache) # Тот же токен работает после восстановления Redis, logout сразу действует. c.call('GET', '/api/v1/users/me', actor=alice, schema='User') c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) c.call('GET', '/api/v1/users/me', 401, actor=alice) try: hook(args.stop_db) start = time.monotonic() c.call('GET', '/health/ready', 503) c.call('GET', '/health/live', schema='Health') c.call('GET', '/api/v1/users/me', 503, actor=bob) c.call('GET', '/api/v1/users/me', 401) result['db_offline_scenario_seconds'] = time.monotonic() - start finally: hook(args.start_db) wait() c.call('GET', '/api/v1/users/me', actor=bob, schema='User') import json result['status'] = 'PASS' Path('evidence/dependency_failures.json').write_text(json.dumps(result, indent=2)) print('PASS: Redis fallback + restart + logout; DB bounded 503 + recovery; anonymous rejected') if __name__ == '__main__': run(main)