forked from mtusi2026/mtusi2026lab2
lab 2 realization: PostgreSQL, Valkey, track 2
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
"""Destructive only to this Compose lab's running processes; volumes preserved.
|
||||
Run on an idle lab stand: python student-tests/infrastructure.py.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0,str(ROOT/'tests'))
|
||||
from client import Client, Failure, check
|
||||
|
||||
|
||||
def docker(*args, data=None):
|
||||
p = subprocess.run(['docker', *args], cwd=ROOT, input=data, text=True,
|
||||
encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=90)
|
||||
if p.returncode:
|
||||
raise Failure('docker '+args[0]+' failed: '+p.stderr[-1000:])
|
||||
return p.stdout.strip()
|
||||
|
||||
|
||||
def compose(*args, data=None):
|
||||
return docker('compose', *args, data=data)
|
||||
|
||||
|
||||
def sql(query):
|
||||
return compose('exec','-T','db','psql','-X','-v','ON_ERROR_STOP=1','-U','messenger','-d','messenger','-At',data=query)
|
||||
|
||||
|
||||
def ready(c):
|
||||
start = time.monotonic()
|
||||
while time.monotonic()-start < 45:
|
||||
try:
|
||||
c.call('GET','/health/ready')
|
||||
return round(time.monotonic()-start,3)
|
||||
except Failure:
|
||||
time.sleep(.2)
|
||||
raise Failure('readiness deadline exceeded')
|
||||
|
||||
|
||||
def counts():
|
||||
return sql('SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM chat_members),(SELECT count(*) FROM messages),(SELECT count(*) FROM schema_migrations);')
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('--base-url',default='http://127.0.0.1:8080')
|
||||
args = p.parse_args()
|
||||
c = Client(args.base_url,timeout=8)
|
||||
ready(c)
|
||||
a,b,e,chat = c.bootstrap()
|
||||
sent = c.message(a,chat,'durability probe')['json']
|
||||
out = {'utc':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'outages':[]}
|
||||
before = counts()
|
||||
c.call('POST','/api/v1/chats',400,actor=a,body={'title':'rollback probe','member_ids':[a['id'],b['id'],str(uuid.uuid4())]})
|
||||
after = counts()
|
||||
check(before==after,'partial chat survived rollback')
|
||||
out['rollback']={'before':before,'after':after,'result':'PASS: no chat or membership left'}
|
||||
print('ROLLBACK: table counts unchanged after FK failure',flush=True)
|
||||
for _ in range(2):
|
||||
compose('run','--rm','--no-deps','api','migrate')
|
||||
check(counts()==before,'migrations changed existing data')
|
||||
out['migrations']='PASS: two repeated migrations, counts unchanged'
|
||||
print('MIGRATIONS: repeat twice, counts unchanged',flush=True)
|
||||
for service in ('db','valkey'):
|
||||
container = compose('ps','-q',service)
|
||||
old_start = docker('inspect','--format','{{.State.StartedAt}}',container)
|
||||
compose('stop',service)
|
||||
try:
|
||||
timings=[]
|
||||
for method,path,body in [('GET','/api/v1/users/me',None),('POST','/api/v1/chats/'+chat['id']+'/messages',{'text':'must not commit'})]:
|
||||
response=c.call(method,path,503,actor=a,body=body)
|
||||
check(response['elapsed'] < 6,'dependency error exceeded time budget')
|
||||
timings.append(round(response['elapsed'],3))
|
||||
c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':a['id']})
|
||||
c.call('GET','/health/live',200)
|
||||
c.call('GET','/health/ready',503)
|
||||
finally:
|
||||
compose('start',service)
|
||||
recovery=ready(c)
|
||||
new_start=docker('inspect','--format','{{.State.StartedAt}}',container)
|
||||
check(old_start!=new_start,'dependency did not restart')
|
||||
read=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=a,schema='MessagePage')['json']['items']
|
||||
check(read==[sent],'data missing or rejected write committed')
|
||||
out['outages'].append({'service':service,'statuses':[503,503,401,200,503],'request_seconds':timings,'ready_seconds_after_start':recovery,'started_before':old_start,'started_after':new_start,'same_token_and_message':True})
|
||||
print(service.upper()+': bounded 503, anonymous 401, recovery with same token/message',flush=True)
|
||||
name='mtusi-lab2-expiry-test'
|
||||
check(not docker('ps','-aq','--filter','name=^/'+name+'$'),'expiry test container name already occupied')
|
||||
try:
|
||||
compose('run','-d','--no-deps','--name',name,'-p','127.0.0.1:18082:8080','-e','SESSION_TTL_SECONDS=2','api')
|
||||
short=Client('http://127.0.0.1:18082',timeout=8)
|
||||
ready(short)
|
||||
actor=short.user('expiry')
|
||||
short.call('GET','/api/v1/users/me',actor=actor,schema='User')
|
||||
time.sleep(2.2)
|
||||
short.call('GET','/api/v1/users/me',401,actor=actor)
|
||||
out['expiry']='PASS: separate API TTL=2s, 200 then 401 after 2.2s'
|
||||
print('EXPIRY: TTL=2s, 200 then 401',flush=True)
|
||||
finally:
|
||||
docker('rm','-f',name)
|
||||
start=time.monotonic()
|
||||
compose('stop','-t','10','api')
|
||||
elapsed=time.monotonic()-start
|
||||
api=compose('ps','-aq','api')
|
||||
state=json.loads(docker('inspect','--format','{{json .State}}',api))
|
||||
try:
|
||||
check(elapsed<10 and state['ExitCode']==0 and not state['OOMKilled'],'unclean shutdown')
|
||||
try:
|
||||
c.call('GET','/health/live')
|
||||
except Failure:
|
||||
pass
|
||||
else:
|
||||
raise Failure('API still accepts requests after stop')
|
||||
finally:
|
||||
compose('start','api')
|
||||
ready(c)
|
||||
c.call('GET','/api/v1/users/me',actor=a,schema='User')
|
||||
out['sigterm']={'seconds':round(elapsed,3),'exit_code':state['ExitCode'],'oom_killed':state['OOMKilled'],'no_new_requests':True}
|
||||
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=a)
|
||||
compose('restart','valkey')
|
||||
ready(c)
|
||||
c.call('GET','/api/v1/users/me',401,actor=a)
|
||||
out['logout_restart']='PASS: revoked session stays revoked after Valkey restart'
|
||||
(ROOT/'evidence').mkdir(exist_ok=True)
|
||||
(ROOT/'evidence/infrastructure.json').write_text(json.dumps(out,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
|
||||
print('PASS: infrastructure, expiry, rollback, migrations, shutdown and durable logout',flush=True)
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user