#!/usr/bin/env python3 import hashlib import http.client import json import os from pathlib import Path import sys import time import uuid ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client STATE = Path(os.environ.get('S3_OUTAGE_STATE', '/tmp/s3-outage-state.json')) SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes() HOST = os.environ.get('LB_HOST', 'lb') PORT = int(os.environ.get('LB_PORT', '8080')) def request(method, path, token=None, body=None, content_type=None, extra=None): headers = dict(extra or {}) if token: headers['Authorization'] = 'Bearer ' + token if content_type: headers['Content-Type'] = content_type conn = http.client.HTTPConnection(HOST, PORT, timeout=30) started = time.monotonic() conn.request(method, path, body=body, headers=headers) response = conn.getresponse() data = response.read() elapsed = time.monotonic() - started conn.close() return response, data, elapsed def show(label, response, data, elapsed): code = '' if response.status >= 400: code = json.loads(data)['error']['code'] print('%-32s -> %s %-12s %5.2fs X-Instance-Id=%s' % (label, response.status, code, elapsed, 'yes' if response.getheader('X-Instance-Id') else 'no')) def prepare(): client = Client('http://%s:%d' % (HOST, PORT)) alice = client.user('alice') chat = client.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'outage', 'member_ids': [alice['id']]}, schema='Chat')['json'] attachment, _ = client.attachment(alice, chat) STATE.write_text(json.dumps({'token': alice['token'], 'chat': chat['id'], 'attachment': attachment['id']})) def during(): state = json.loads(STATE.read_text()) token, chat, attachment = state['token'], state['chat'], state['attachment'] show('GET /health/live', *request('GET', '/health/live')) show('GET /health/ready', *request('GET', '/health/ready')) show('GET /attachments/{id}', *request('GET', '/api/v1/attachments/' + attachment, token)) show('GET /attachments/{id}/content', *request('GET', '/api/v1/attachments/' + attachment + '/content', token)) show('POST /attachments', *request('POST', '/api/v1/chats/' + chat + '/attachments?filename=down.png', token, SAMPLE, 'image/png')) show('POST /messages (текст)', *request('POST', '/api/v1/chats/' + chat + '/messages', token, json.dumps({'text': 'пока S3 недоступен'}), 'application/json', {'Idempotency-Key': uuid.uuid4().hex})) def after(): state = json.loads(STATE.read_text()) token, attachment = state['token'], state['attachment'] deadline = time.monotonic() + 30 while True: response, data, elapsed = request('GET', '/health/ready') if response.status == 200 or time.monotonic() > deadline: break time.sleep(0.5) show('GET /health/ready', response, data, elapsed) response, data, elapsed = request('GET', '/api/v1/attachments/' + attachment + '/content', token) show('GET /attachments/{id}/content', response, data, elapsed) print('sha256 совпадает:', hashlib.sha256(data).hexdigest() == hashlib.sha256(SAMPLE).hexdigest()) STATE.unlink() {'prepare': prepare, 'during': during, 'after': after}[sys.argv[1]]()