Files
mtusi2026lab2/scripts/acceptance.py

32 lines
1.8 KiB
Python

"""Checks the level-four database and cache failure paths on the isolated Compose project."""
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import base64,os,subprocess,sys,time,uuid
root=Path(__file__).resolve().parents[1];os.chdir(root);sys.path.insert(0,str(root/'tests'))
from client import Client
def compose(*args,env=None):subprocess.run(['docker','compose',*args],check=True,env=env,stdout=subprocess.DEVNULL)
def ready(c):
for i in range(60):
try:c.call('GET','/health/ready',schema='Health');return
except AssertionError:time.sleep(1)
raise RuntimeError('readiness deadline')
c=Client();a,b,_,chat=c.bootstrap()
body={**a['registration'],'username':'race_'+uuid.uuid4().hex[:12]}
with ThreadPoolExecutor(8) as pool:
codes=list(pool.map(lambda _:Client().call('POST','/api/v1/users',(201,409),body=body)['status'],range(8)))
assert sorted(codes)==[201]+[409]*7
def count():
return subprocess.check_output(['docker','compose','exec','-T','db','psql','-U','lab','-d','lab','-XAtc','SELECT count(*) FROM chats'],text=True)
before=count();c.call('POST','/api/v1/chats',400,actor=a,body={'title':'rollback','member_ids':[a['id'],str(uuid.uuid4())]});assert count()==before
try:
compose('stop','cache');c.call('GET','/api/v1/users/me',actor=a,schema='User');c.call('GET','/api/v1/users/me',401)
finally:compose('start','cache')
try:
compose('stop','db');c.call('GET','/health/live',schema='Health');c.call('GET','/health/ready',503);c.call('GET','/api/v1/users/me',503,actor=a)
finally:compose('start','db');ready(c)
try:
compose('up','-d','--no-deps','api',env={**os.environ,'SESSION_TTL_SECONDS':'2'});ready(c)
actor=c.user('expiry');time.sleep(3);c.call('GET','/api/v1/users/me',401,actor=actor)
finally:compose('up','-d','--no-deps','api');ready(c)
print('Concurrent registration, rollback, cache outage, DB outage and session expiry passed.')