forked from mtusi2026/mtusi2026lab4
Реализовано хранилище вложений для лабораторной 4
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверить ограниченную ошибку API при отказе S3 и восстановить его."""
|
||||
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 S3 outage and recovery check')
|
||||
# Дождаться ограниченных повторов boto3 при остановленном S3.
|
||||
client = Client(args.base_url, args.ca_file, timeout=35)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
attachment, content = client.attachment(alice, chat)
|
||||
initial = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob,
|
||||
)
|
||||
check(initial['bytes'] == content, 'uploaded bytes differ before the outage')
|
||||
|
||||
stopped = subprocess.run(
|
||||
['docker', 'compose', '--env-file', '.env', 'stop', 's3'],
|
||||
cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
timeout=20, check=False,
|
||||
)
|
||||
check(stopped.returncode == 0, 'could not stop S3 for the outage test')
|
||||
try:
|
||||
ready = client.call('GET', '/health/ready', expected=503, schema='Error')
|
||||
check(ready['json']['error']['code'] == 'unavailable',
|
||||
'readiness did not report unavailable S3')
|
||||
failed = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'] + '/content',
|
||||
expected=503, actor=bob, schema='Error',
|
||||
)
|
||||
check(failed['json']['error']['code'] == 'unavailable',
|
||||
'S3 failure was not translated to a bounded API error')
|
||||
metadata = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'],
|
||||
actor=bob, schema='Attachment',
|
||||
)
|
||||
check(metadata['json']['id'] == attachment['id'],
|
||||
'PostgreSQL metadata was lost while S3 was unavailable')
|
||||
print('PASS: S3 outage returned 503 for readiness/download; metadata remained available')
|
||||
finally:
|
||||
started = subprocess.run(
|
||||
['docker', 'compose', '--env-file', '.env', 'start', 's3'],
|
||||
cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
timeout=20, check=False,
|
||||
)
|
||||
check(started.returncode == 0, 'could not restart S3 after the outage test')
|
||||
|
||||
deadline = time.monotonic() + 30
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
client.call('GET', '/health/ready', schema='Health')
|
||||
break
|
||||
except Failure:
|
||||
time.sleep(0.25)
|
||||
else:
|
||||
raise Failure('API did not become ready after S3 restart')
|
||||
|
||||
recovered = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob,
|
||||
)
|
||||
check(recovered['bytes'] == content, 'S3 restart changed or lost the original bytes')
|
||||
print('PASS: original attachment bytes were readable after S3 recovery')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user