#!/usr/bin/env python3 import argparse from pathlib import Path import subprocess import sys import time ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, Failure, check, run from resilience import history def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--base-url', default='http://127.0.0.1:8080') parser.add_argument('--ca-file') parser.add_argument('--action-script', required=True) args = parser.parse_args() client = Client(args.base_url, args.ca_file, timeout=30) alice, bob, eve, chat = client.bootstrap() original = client.message(alice, chat, 'Сохранность после полного перезапуска')['json'] check(history(client, bob, chat) == [original], 'pre-restart history mismatch') hook = Path(args.action_script).resolve() check(hook.is_file(), 'restart hook not found') print('Restarting backend, PostgreSQL and Redis; hook output suppressed.', flush=True) try: proc = subprocess.run([str(hook)], cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=90) except subprocess.TimeoutExpired: raise Failure('restart hook exceeded 90 seconds') from None check(proc.returncode == 0, 'restart hook failed') deadline = time.monotonic() + 90 while True: try: client.call('GET', '/health/ready', schema='Health') break except Failure: check(time.monotonic() < deadline, 'API did not recover within 90 seconds') time.sleep(0.5) for actor in (alice, bob, eve): check(client.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'] == actor['user'], 'user or existing session changed after restart') check(client.call('GET', '/api/v1/chats/' + chat['id'], actor=bob, schema='Chat')['json'] == chat, 'chat or membership changed after restart') check(history(client, bob, chat) == [original], 'confirmed message lost, changed or duplicated') client.call('GET', '/api/v1/chats/' + chat['id'], 404, actor=eve, schema='Error') followup = client.message(bob, chat, 'Запись после восстановления')['json'] check(history(client, alice, chat) == [original, followup], 'post-restart write/read mismatch') client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) client.call('GET', '/api/v1/users/me', 401, actor=alice, schema='Error') print('PASS: users, chat, membership, confirmed message and all existing sessions survived; ' 'access control, new writes and logout work after restart.') if __name__ == '__main__': run(main)