forked from mtusi2026/mtusi2026lab4
Реализовать ЛР 4 на 3: приватное S3 и вложения — Гавриленко БПИ2303
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Stop the actual S3 container, probe API directly internally, then restore S3."""
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments
|
||||
|
||||
args = arguments('Real S3 outage')
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
a, b, eve, chat = c.bootstrap()
|
||||
att, data = c.attachment(a, chat)
|
||||
probe = r'''
|
||||
import json, sys, urllib.request, urllib.error
|
||||
sys.path.insert(0, '/app/src')
|
||||
from db import connect
|
||||
info = json.load(sys.stdin)
|
||||
def call(method, path, expected, data=None, mime=None):
|
||||
headers = {'Authorization': 'Bearer ' + info['token']}
|
||||
if mime: headers['Content-Type'] = mime
|
||||
request = urllib.request.Request('http://localhost:8080' + path, method=method, data=data, headers=headers)
|
||||
try: response = urllib.request.urlopen(request, timeout=10)
|
||||
except urllib.error.HTTPError as exc: response = exc
|
||||
with response:
|
||||
body = json.load(response)
|
||||
assert response.code == expected, (path, response.code)
|
||||
assert response.headers['X-Request-Id']
|
||||
if expected >= 400:
|
||||
assert body['error']['code'] == 'unavailable'
|
||||
assert body['error']['request_id'] == response.headers['X-Request-Id']
|
||||
with connect() as db:
|
||||
before = db.execute('SELECT count(*) AS n FROM attachments').fetchone()['n']
|
||||
call('GET', '/health/live', 200)
|
||||
call('GET', '/health/ready', 503)
|
||||
call('GET', '/api/v1/attachments/' + info['attachment'] + '/content', 503)
|
||||
# Small valid PNG copied from test fixture through stdin, no host path mount.
|
||||
import base64
|
||||
call('POST', '/api/v1/chats/' + info['chat'] + '/attachments?filename=outage.png', 503,
|
||||
base64.b64decode(info['data']), 'image/png')
|
||||
with connect() as db:
|
||||
assert db.execute('SELECT count(*) AS n FROM attachments').fetchone()['n'] == before
|
||||
print('PASS: actual S3 stop -> live 200, ready/upload/content 503 with request IDs; no new metadata')
|
||||
'''
|
||||
import base64
|
||||
try:
|
||||
subprocess.run(['docker', 'compose', 'stop', 's3'], check=True)
|
||||
subprocess.run(['docker', 'compose', 'exec', '-T', '--index', '1', 'api', 'python', '-c', probe],
|
||||
input=json.dumps({'token': a['token'], 'chat': chat['id'], 'attachment': att['id'], 'data': base64.b64encode(data).decode()}).encode(), check=True)
|
||||
finally:
|
||||
subprocess.run(['docker', 'compose', 'up', '-d', '--wait'], check=True)
|
||||
assert c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=b)['bytes'] == data
|
||||
print('PASS: S3 recovery preserves original bytes')
|
||||
Reference in New Issue
Block a user