"""Stop the exact replica that accepted the upload, then compare original bytes.""" import hashlib from pathlib import Path import subprocess import sys import time sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, Failure, check c = Client(timeout=10) a, b, _, chat = c.bootstrap() png = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes() result = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=failover.png', 201, actor=a, raw=png, headers={'Content-Type': 'image/png'}, schema='Attachment') target = result['headers']['X-Instance-Id'] check(target in ('api1', 'api2'), 'unknown upload replica') try: subprocess.run(['docker', 'compose', 'stop', target], check=True) deadline = time.monotonic() + 20 while True: try: downloaded = c.call('GET', '/api/v1/attachments/' + result['json']['id'] + '/content', actor=b) break except Failure: if time.monotonic() >= deadline: raise time.sleep(0.5) check(downloaded['headers']['X-Instance-Id'] != target, 'uploader still serves download') check(downloaded['bytes'] == png, 'original changed after uploader stopped') print('PASS: upload=' + target + ', download=' + downloaded['headers']['X-Instance-Id'] + ', sha256=' + hashlib.sha256(png).hexdigest()) finally: subprocess.run(['docker', 'compose', 'up', '-d', '--wait', 'api1', 'api2'], check=True)