forked from mtusi2026/mtusi2026lab4
feat: ЛР 4 с хранением вложений в MinIO
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Stop the API replica that accepted upload and download through another replica."""
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from client import Client, ROOT, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
def configure(parser):
|
||||
parser.add_argument('--action-script', required=True)
|
||||
|
||||
args = arguments('Attachment API-replica failover', configure)
|
||||
script = Path(args.action_script).expanduser().resolve()
|
||||
check(script.is_file() and os.access(script, os.X_OK), 'action script must be executable')
|
||||
client = Client(args.base_url, args.ca_file, timeout=3)
|
||||
alice, bob, _eve, chat = client.bootstrap()
|
||||
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
uploaded = client.call(
|
||||
'POST',
|
||||
f'/api/v1/chats/{chat["id"]}/attachments?filename=failover.png',
|
||||
201,
|
||||
actor=alice,
|
||||
raw=content,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
schema='Attachment',
|
||||
)
|
||||
target = uploaded['headers']['X-Instance-Id']
|
||||
env = dict(os.environ)
|
||||
env['TARGET_INSTANCE_ID'] = target
|
||||
started = time.monotonic()
|
||||
subprocess.run([str(script)], env=env, check=True)
|
||||
|
||||
deadline = time.monotonic() + 15
|
||||
failures = 0
|
||||
while True:
|
||||
try:
|
||||
downloaded = client.call(
|
||||
'GET',
|
||||
f'/api/v1/attachments/{uploaded["json"]["id"]}/content',
|
||||
actor=bob,
|
||||
)
|
||||
if downloaded['headers']['X-Instance-Id'] != target:
|
||||
break
|
||||
except Exception:
|
||||
failures += 1
|
||||
check(time.monotonic() < deadline, 'download did not recover through another replica')
|
||||
time.sleep(0.2)
|
||||
|
||||
check(downloaded['bytes'] == content, 'downloaded file differs after replica failure')
|
||||
print(
|
||||
'PASS: upload_instance=%s download_instance=%s recovery_seconds=%.3f failures=%d sha256=%s'
|
||||
% (
|
||||
target,
|
||||
downloaded['headers']['X-Instance-Id'],
|
||||
time.monotonic() - started,
|
||||
failures,
|
||||
hashlib.sha256(content).hexdigest(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user