forked from mtusi2026/mtusi2026lab4
Реализовано хранилище вложений для лабораторной 4
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Upload through one API instance, stop it, then download through its peer."""
|
||||
import os
|
||||
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, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Lab 4 attachment cross-replica check')
|
||||
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', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
|
||||
201, actor=alice, raw=content, headers={'Content-Type': 'image/png'},
|
||||
schema='Attachment',
|
||||
)
|
||||
source_instance = uploaded['headers']['X-Instance-Id']
|
||||
observed = set()
|
||||
for _ in range(50):
|
||||
observed.add(client.call(
|
||||
'GET', '/health/live', schema='Health',
|
||||
)['headers']['X-Instance-Id'])
|
||||
check(len(observed) >= 2, 'expected at least two API instances before stopping one')
|
||||
check(source_instance in observed, 'upload instance was not observed by the load balancer')
|
||||
|
||||
hook = ROOT / 'scripts/stop-one.sh'
|
||||
env = dict(os.environ)
|
||||
env['TARGET_INSTANCE_ID'] = source_instance
|
||||
subprocess_result = subprocess.run(
|
||||
[str(hook)], cwd=ROOT, env=env, stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL, timeout=10, check=False,
|
||||
)
|
||||
check(subprocess_result.returncode == 0, 'stop-one hook could not stop the upload instance')
|
||||
|
||||
try:
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
metadata = client.call(
|
||||
'GET', '/api/v1/attachments/' + uploaded['json']['id'],
|
||||
actor=bob, schema='Attachment',
|
||||
)
|
||||
download = client.call(
|
||||
'GET', '/api/v1/attachments/' + uploaded['json']['id'] + '/content',
|
||||
actor=bob,
|
||||
)
|
||||
if (metadata['headers']['X-Instance-Id'] != source_instance
|
||||
and download['headers']['X-Instance-Id'] != source_instance):
|
||||
check(download['bytes'] == content, 'cross-replica bytes differ from uploaded file')
|
||||
check(metadata['json']['sha256'] == uploaded['json']['sha256'],
|
||||
'cross-replica metadata checksum changed')
|
||||
print('PASS: upload instance stopped; metadata and original bytes read via peer')
|
||||
return
|
||||
except Failure:
|
||||
time.sleep(0.25)
|
||||
raise Failure('attachment was not readable from the other API instance within 10 seconds')
|
||||
finally:
|
||||
# Restore the two-replica local test stack after this explicit failure experiment.
|
||||
subprocess.run(
|
||||
['docker', 'compose', '--env-file', '.env', 'up', '-d', '--scale', 'api=2'],
|
||||
cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
timeout=60, check=False,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user