#!/usr/bin/env python3 """Отказ S3: вложения отвечают 503, остальное API работает, после возврата файл на месте. Останавливает и запускает сервис s3 на текущем стенде; volume не трогает. """ import subprocess import time import uuid from common import Client, ROOT, SAMPLE_PNG, arguments, check, run COMPOSE = ['docker', 'compose', '--env-file', '.env'] RECOVERY_LIMIT = 90 def compose(*command): subprocess.run(COMPOSE + list(command), cwd=ROOT, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) def main(): args = arguments('S3 outage scenario') c = Client(args.base_url, args.ca_file, timeout=30) alice, bob, _, chat = c.bootstrap() upload = '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png' headers = {'Content-Type': 'image/png'} att = c.call('POST', upload, 201, actor=alice, raw=SAMPLE_PNG, headers=headers, schema='Attachment')['json'] content = '/api/v1/attachments/' + att['id'] + '/content' compose('stop', 's3') print('s3 stopped') try: def measured(name, *call_args, **call_kwargs): response = c.call(*call_args, **call_kwargs) print('%-34s %d in %.2fs' % (name, response['status'], response['elapsed'])) return response first = measured('upload without S3', 'POST', upload, 503, actor=alice, raw=SAMPLE_PNG, headers=headers) check(first['json']['error']['code'] == 'unavailable', 'wrong error code') check(first['elapsed'] < 15, 'answer slower than LB timeout') measured('download without S3', 'GET', content, 503, actor=bob) measured('metadata without S3', 'GET', '/api/v1/attachments/' + att['id'], 200, actor=bob, schema='Attachment') measured('text message without S3', 'POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice, body={'text': 'без S3'}, headers={'Idempotency-Key': uuid.uuid4().hex}, schema='Message') measured('readiness without S3', 'GET', '/health/ready', 200, schema='Health') finally: compose('start', 's3') started = time.monotonic() while True: response = c.call('GET', content, [200, 503], actor=bob) if response['status'] == 200: break check(time.monotonic() - started < RECOVERY_LIMIT, 'S3 did not recover in time') time.sleep(1) check(response['bytes'] == SAMPLE_PNG, 'file changed across S3 outage') print('download recovered in %.1fs after start, bytes match' % (time.monotonic() - started)) c.call('POST', upload, 201, actor=alice, raw=SAMPLE_PNG, headers=headers, schema='Attachment') print('upload works again') print('PASS: S3 outage is contained to attachments and loses no data') if __name__ == '__main__': run(main)