forked from mtusi2026/mtusi2026lab2
101 lines
3.8 KiB
Python
101 lines
3.8 KiB
Python
"""Full Docker checks, Python stdlib only; works on Windows and Linux.
|
|
Stops/restarts ONLY services belonging to this Compose project. Keeps volumes.
|
|
Run against an idle disposable teaching stand, not during other tests.
|
|
"""
|
|
import os
|
|
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
|
|
from resilience import history
|
|
|
|
|
|
def compose(*args, capture=False):
|
|
result=subprocess.run(['docker','compose',*args],cwd=ROOT,text=True,capture_output=capture)
|
|
if result.returncode:
|
|
raise RuntimeError('Compose failed: '+args[0])
|
|
return result.stdout if capture else None
|
|
|
|
|
|
def wait(c):
|
|
deadline=time.monotonic()+60
|
|
while time.monotonic()<deadline:
|
|
try:
|
|
c.call('GET','/health/ready',schema='Health')
|
|
return
|
|
except Failure:
|
|
time.sleep(.5)
|
|
raise RuntimeError('API did not become ready')
|
|
|
|
|
|
def stamp(filename):
|
|
ids=compose('ps','-q',capture=True).split()
|
|
data=subprocess.check_output(['docker','inspect','--format','{{.Name}} {{.State.StartedAt}}',*ids],text=True)
|
|
(ROOT/'evidence'/filename).write_text(data)
|
|
|
|
|
|
def main():
|
|
os.chdir(ROOT)
|
|
(ROOT/'evidence').mkdir(exist_ok=True)
|
|
c=Client()
|
|
wait(c)
|
|
for script in ('tests/smoke.py','student-tests/extra.py'):
|
|
subprocess.run([sys.executable,script],check=True)
|
|
output=compose('exec','-T','api','python','student-tests/database.py',capture=True)
|
|
(ROOT/'evidence/explain-and-rollback.txt').write_text(output)
|
|
print(output)
|
|
alice,bob,eve,chat=c.bootstrap()
|
|
original=c.message(alice,chat,'restart proof')['json']
|
|
stamp('restart-before.txt')
|
|
compose('restart','db','cache','api')
|
|
wait(c)
|
|
stamp('restart-after.txt')
|
|
check(original in history(c,bob,chat),'message lost on restart')
|
|
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
|
print('PASS: all three services restarted; old tokens and message survive')
|
|
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
|
cached=compose('exec','-T','cache','redis-cli','EXISTS','profile:v1:'+alice['id'],capture=True)
|
|
check(cached.strip()=='1','profile cache is unused')
|
|
print('PASS: real Redis profile key exists')
|
|
try:
|
|
compose('stop','cache')
|
|
start=time.monotonic()
|
|
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
|
check(time.monotonic()-start<5,'cache fallback too slow')
|
|
c.call('GET','/api/v1/users/me',401,headers={'Authorization':'Bearer '+'a'*43})
|
|
c.call('GET','/health/ready',schema='Health')
|
|
print('PASS: Redis down -> SQL fallback; unknown token rejected')
|
|
finally:
|
|
compose('start','cache')
|
|
try:
|
|
compose('stop','db')
|
|
start=time.monotonic()
|
|
c.call('GET','/api/v1/users/me',503,actor=alice)
|
|
check(time.monotonic()-start<8,'DB failure too slow')
|
|
c.call('GET','/health/ready',503)
|
|
c.call('GET','/health/live',schema='Health')
|
|
print('PASS: DB down -> bounded 503; live remains 200')
|
|
finally:
|
|
compose('start','db')
|
|
wait(c)
|
|
expiry_id=None
|
|
try:
|
|
expiry_id=compose('run','-d','--no-deps','-p','127.0.0.1:18082:8080','-e','SESSION_TTL_SECONDS=2','api',capture=True).strip().splitlines()[-1]
|
|
short=Client('http://localhost:18082')
|
|
wait(short)
|
|
env=dict(os.environ,BASE_URL='http://localhost:18082')
|
|
subprocess.run([sys.executable,'student-tests/expiry.py'],env=env,check=True)
|
|
finally:
|
|
if expiry_id:
|
|
subprocess.run(['docker','rm','-f',expiry_id],check=True)
|
|
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice)
|
|
c.call('GET','/api/v1/users/me',401,actor=alice)
|
|
print('PASS: complete Compose verification; volumes retained')
|
|
|
|
if __name__=='__main__':
|
|
main()
|