"""Останавливаем именно API, принявшую upload, и скачиваем оригинал другой репликой.""" import hashlib import os from pathlib import Path import sys import time sys.path.insert(0, str(Path(__file__).resolve().parents[1])) sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, Failure from scripts.stack import stop, wait, save, command base = os.getenv('BASE_URL', 'http://localhost:8080') c = Client(base, timeout=3) alice, bob, _, chat = c.bootstrap() data = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes() response = c.call('POST', '/api/v1/chats/'+chat['id']+'/attachments?filename=failover.png', 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment') target = response['headers']['X-Instance-Id'] att = response['json'] c.message(alice, chat, '', attachments=[att['id']]) started = time.monotonic() errors = 0 try: stopped = stop(target) while True: try: downloaded = c.call('GET', '/api/v1/attachments/'+att['id']+'/content', actor=bob) alias = downloaded['headers']['X-Instance-Id'] assert alias != target and downloaded['bytes'] == data assert hashlib.sha256(downloaded['bytes']).hexdigest() == att['sha256'] break except Failure: errors += 1 if time.monotonic()-started > 10: raise time.sleep(.2) save('attachment_failover.json', dict(status='PASS', uploader=target, downloader=alias, recovery_seconds=time.monotonic()-started, failed_attempts=errors, original_size=len(data), checksum_verified=True, stop=stopped)) print('PASS: upload API stopped, original downloaded by other API with existing token') finally: command(['docker', 'compose', 'start', 'api']) wait(2, base)