72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
"""Redis необязателен для корректности; без PostgreSQL готовность снимается."""
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
import urllib.error
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from scripts.benchmark import context, Client
|
|
from scripts.stack import command, wait, save, containers
|
|
|
|
base = os.getenv('BASE_URL', 'http://localhost:8080')
|
|
c = Client(base)
|
|
ctx = context(base)
|
|
original = c.message(ctx['actor'], ctx['chat'], 'До отказов')['json']
|
|
attachment, content = c.attachment(ctx['actor'], ctx['chat'])
|
|
results = {}
|
|
try:
|
|
command(['docker', 'compose', 'stop', 'cache'])
|
|
started = time.monotonic()
|
|
for _ in range(6):
|
|
c.call('GET', '/api/v1/users/me', actor=ctx['actor'], schema='User')
|
|
c.message(ctx['actor'], ctx['chat'], 'Без Redis')
|
|
results['redis'] = dict(status='PASS', elapsed_seconds=time.monotonic()-started)
|
|
finally:
|
|
command(['docker', 'compose', 'up', '-d', '--wait', 'cache'])
|
|
try:
|
|
command(['docker', 'compose', 'stop', 'db'])
|
|
# Даём LB время исключить все неготовые API. Его собственный 503 может быть HTML.
|
|
time.sleep(4)
|
|
codes = []
|
|
for path in ['/health/ready', '/api/v1/users/me']:
|
|
request = urllib.request.Request(base+path, headers={'Authorization': 'Bearer '+ctx['actor']['token']})
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=8) as response:
|
|
codes.append(response.status)
|
|
except urllib.error.HTTPError as exc:
|
|
codes.append(exc.code)
|
|
assert codes == [503, 503], codes
|
|
results['postgres'] = dict(status='PASS', readiness_and_request_status=codes)
|
|
finally:
|
|
command(['docker', 'compose', 'up', '-d', '--wait', 'db'])
|
|
wait(2, base)
|
|
active = any(row['running'] for row in containers('reconciler'))
|
|
command(['docker', 'compose', 'stop', 'reconciler'])
|
|
try:
|
|
command(['docker', 'compose', 'stop', 's3'])
|
|
time.sleep(4)
|
|
codes = []
|
|
paths = ['/health/ready', '/api/v1/attachments/'+attachment['id']+'/content']
|
|
for path in paths:
|
|
request = urllib.request.Request(base+path, headers={'Authorization': 'Bearer '+ctx['actor']['token']})
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=10) as response:
|
|
codes.append(response.status)
|
|
except urllib.error.HTTPError as exc:
|
|
codes.append(exc.code)
|
|
assert codes == [503, 503], codes
|
|
results['s3'] = dict(status='PASS', readiness_and_download_status=codes)
|
|
finally:
|
|
command(['docker', 'compose', 'up', '-d', '--wait', 's3'])
|
|
wait(2, base)
|
|
if active:
|
|
command(['docker', 'compose', 'start', 'reconciler'])
|
|
page = c.call('GET', '/api/v1/chats/'+ctx['chat']['id']+'/messages', actor=ctx['reader'], schema='MessagePage')['json']
|
|
assert original in page['items']
|
|
c.call('GET', '/api/v1/users/me', actor=ctx['actor'], schema='User')
|
|
assert c.call('GET', '/api/v1/attachments/'+attachment['id']+'/content', actor=ctx['reader'])['bytes'] == content
|
|
results['recovery'] = 'old session and confirmed message preserved'
|
|
save('dependencies.json', results)
|
|
print('PASS: Redis fallback, PostgreSQL/S3 unready, old session and original preserved')
|