Files
qrtx dc46ac213d feat(storage): add RustFS attachments and presigned uploads
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
2026-11-12 10:16:39 +03:00

89 lines
3.9 KiB
Python

"""Verify two API replicas, published Swagger and attachment failover through Nginx."""
import argparse
import json
from pathlib import Path
import subprocess
import sys
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, Failure, check, run
def docker(*args):
result = subprocess.run(['docker', *args], cwd=ROOT, capture_output=True, text=True)
check(result.returncode == 0, 'Docker operation failed: ' + args[0])
return result.stdout.strip()
def wait_both(client, actor, expected):
deadline = time.monotonic() + 90
observed = set()
while time.monotonic() < deadline:
try:
response = client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
observed.add(response['headers']['X-Instance-Id'])
if observed == expected:
return
except Failure:
pass
time.sleep(0.1)
raise Failure('both replicas did not become available through Nginx')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--base-url', default='http://localhost:8080')
args = parser.parse_args()
ids = docker('compose', 'ps', '-q', 'api').splitlines()
check(len(ids) == 2, 'expected exactly two running API replicas')
names = docker('inspect', '--format', '{{.Config.Hostname}}', *ids).splitlines()
containers = dict(zip(names, ids))
c = Client(args.base_url)
alice, bob, _, chat = c.bootstrap()
wait_both(c, alice, set(names))
contract = json.loads((ROOT / 'contracts/openapi.json').read_text(encoding='utf8'))
documented = set()
for _ in range(20):
response = c.call('GET', '/swagger-json')
document = json.loads(response['bytes'])
documented.add(response['headers']['X-Instance-Id'])
check(document['openapi'] == contract['openapi'], 'wrong OpenAPI version')
for path, methods in contract['paths'].items():
for method, operation in methods.items():
actual = document['paths'][path][method]
check(all(actual.get(key) == value for key, value in operation.items()),
'Swagger differs from contract: ' + method + ' ' + path)
check(actual['security'] == operation.get('security', contract.get('security', [])),
'Swagger security differs from contract')
for name, schema in contract['components']['schemas'].items():
check(document['components']['schemas'][name] == schema, 'Swagger schema differs: ' + name)
if documented == set(names):
break
check(documented == set(names), 'Swagger did not respond from both replicas')
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
uploaded = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=failover.png',
201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
attachment = uploaded['json']
target = uploaded['headers']['X-Instance-Id']
key = uuid.uuid4().hex
message = c.message(alice, chat, '', key, [attachment['id']])['json']
try:
print('Stopping the replica that accepted the attachment.', flush=True)
docker('stop', containers[target])
downloaded = c.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)
check(downloaded['headers']['X-Instance-Id'] != target, 'stopped replica answered')
check(downloaded['bytes'] == content, 'attachment changed after failover')
check(c.message(alice, chat, '', key, [attachment['id']])['json'] == message,
'idempotency replay changed on the surviving replica')
print('PASS: two replicas, canonical Swagger and attachment/idempotency failover.', flush=True)
finally:
docker('start', containers[target])
wait_both(c, alice, set(names))
if __name__ == '__main__':
run(main)