51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
"""Host-side operator test: stop one lab dependency, assert bounded failures, restore."""
|
|
import json
|
|
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, check
|
|
|
|
c = Client(timeout=4)
|
|
actor, _, _, chat = c.bootstrap()
|
|
original = c.message(actor, chat, 'Before dependency outage')['json']
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
summary = []
|
|
for service in ('redis', 'db'):
|
|
subprocess.run(['docker', 'compose', 'stop', service], cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
|
try:
|
|
timings = []
|
|
for _ in range(3):
|
|
response = c.call('GET', '/api/v1/users/me', 503, actor=actor)
|
|
check(response['json']['error']['code'] == 'unavailable', 'wrong dependency error')
|
|
check(response['elapsed'] < 3.5, 'failure exceeded response budget')
|
|
timings.append(response['elapsed'])
|
|
c.call('POST', path, 503, actor=actor, body={'text': 'Must not commit while unavailable'})
|
|
c.call('GET', '/health/ready', 503)
|
|
c.call('GET', '/health/live', schema='Health')
|
|
c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': actor['id']})
|
|
c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Bearer ' + 'A' * 43}) if service == 'db' else c.call(
|
|
'GET', '/api/v1/users/me', 503, headers={'Authorization': 'Bearer ' + 'A' * 43})
|
|
finally:
|
|
started = time.monotonic()
|
|
subprocess.run(['docker', 'compose', 'start', service], cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
|
deadline = time.monotonic() + 30
|
|
while True:
|
|
try:
|
|
c.call('GET', '/health/ready', schema='Health')
|
|
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
|
|
break
|
|
except Failure:
|
|
check(time.monotonic() < deadline, 'dependency did not recover in 30 seconds')
|
|
time.sleep(.25)
|
|
recovery = time.monotonic() - started
|
|
page = c.call('GET', path, actor=actor, schema='MessagePage')['json']
|
|
check(page['items'] == [original], 'lost confirmation or wrote during dependency outage')
|
|
summary.append(dict(service=service, errors=3, status=503, seconds=timings,
|
|
recovery_seconds=round(recovery, 3), original_session_preserved=True,
|
|
original_message_preserved=True, anonymous_status=401))
|
|
print(json.dumps(summary, indent=2))
|