Files
mtusi2026lab2/student-tests/infrastructure.py
T
2026-11-05 01:43:30 +03:00

59 lines
3.1 KiB
Python

"""Real Docker restart evidence plus repeated migrations, expiry, DB failure."""
import json,subprocess,sys,time,urllib.request,urllib.error
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client,check,run
ROOT=Path(__file__).resolve().parents[1]
def command(*args):
return subprocess.check_output(args,cwd=ROOT,text=True).strip()
def snapshot():
for service in ('api','db','cache'):
container=command('docker','compose','ps','-q',service)
value=command('docker','inspect','--format','{{.Id}} {{.State.StartedAt}} {{.RestartCount}}',container)
print(service,value,flush=True)
print('DB postmaster:',command('docker','compose','exec','-T','db','psql','-U','messenger','-d','messenger','-Atc','select pg_postmaster_start_time()'),flush=True)
def wait_ready(url):
for _ in range(90):
try:
if urllib.request.urlopen(url,timeout=2).status==200: return
except (OSError,urllib.error.URLError): pass
time.sleep(1)
raise AssertionError('Readiness timeout')
def main():
print('Before full restart:',flush=True); snapshot()
subprocess.run(['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh'],cwd=ROOT,check=True)
print('After full restart:',flush=True); snapshot()
sql='select (select count(*) from users),(select count(*) from chats),(select count(*) from messages),(select count(*) from sessions),(select count(*) from schema_migrations)'
def counts(): return command('docker','compose','exec','-T','db','psql','-U','messenger','-d','messenger','-Atc',sql)
before=counts()
for _ in range(2):
print(command('docker','compose','exec','-T','api','python','src/migrate.py'),flush=True)
check(before==counts(),'Migration rerun changed counts')
print('PASS: two migration reruns; users/chats/messages/sessions/migrations counts unchanged:',before,flush=True)
# Separate short-TTL process, same database, isolated port; no change to normal API TTL.
subprocess.run(['docker','compose','run','-d','--no-deps','--name','lab2-expiry','-e','SESSION_TTL_SECONDS=2','-p','127.0.0.1:8081:8080','api'],cwd=ROOT,check=True,stdout=subprocess.DEVNULL)
try:
wait_ready('http://localhost:8081/health/ready')
subprocess.run([sys.executable,'student-tests/expiry.py'],cwd=ROOT,check=True)
finally:
subprocess.run(['docker','rm','-f','lab2-expiry'],check=True,stdout=subprocess.DEVNULL)
c=Client(timeout=10)
actor=c.user('db_failure')
subprocess.run(['docker','compose','stop','db'],cwd=ROOT,check=True)
try:
c.call('GET','/health/live',schema='Health')
c.call('GET','/health/ready',503)
c.call('GET','/api/v1/users/me',503,actor=actor)
print('PASS: DB down: live=200, ready=503, authenticated request=503; no anonymous fallback',flush=True)
finally:
subprocess.run(['docker','compose','start','db'],cwd=ROOT,check=True)
wait_ready('http://localhost:8080/health/ready')
c.call('GET','/api/v1/users/me',actor=actor,schema='User')
print('PASS: DB recovered; same session works',flush=True)
if __name__=='__main__': run(main)