#!/usr/bin/env python3 """Run all lab2 checks and save sanitized evidence. Start Compose first.""" import json from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parents[1] COMPOSE = ['docker','compose','--env-file','.env'] def capture(name, args): result = subprocess.run(args,cwd=ROOT,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) (ROOT/'evidence'/name).write_text(result.stdout) print(result.stdout,end='',flush=True) if result.returncode: raise SystemExit(result.returncode) def snapshot(): ids = subprocess.check_output(COMPOSE+['ps','-q'],cwd=ROOT,text=True).split() result = {} for cid in ids: # Select fields explicitly: full inspect includes private configuration. fields = subprocess.check_output(['docker','inspect','--format', '{{ index .Config.Labels "com.docker.compose.service" }}|{{.Id}}|{{.State.StartedAt}}',cid],text=True).strip().split('|') result[fields[0]]={'container_id':fields[1],'started_at':fields[2]} return result def main(): capture('check.txt',['make','check']) capture('smoke.txt',['make','test']) capture('integration.txt',COMPOSE+['exec','-T','api','python','-m','unittest','discover','-s','student-tests','-v']) before = snapshot() capture('persistence.txt',['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh']) after = snapshot() if set(before) != {'api','postgres'} or set(after) != set(before): raise AssertionError('Expected API and PostgreSQL') for role in before: if before[role]['started_at']==after[role]['started_at']: raise AssertionError(role+' was not restarted') (ROOT/'evidence'/'restart-containers.json').write_text(json.dumps({'before':before,'after':after},indent=2)+'\n') print('PASS restart timestamps changed for API and PostgreSQL',flush=True) capture('expiry.txt',[sys.executable,'scripts/test_expiry.py']) print('PASS full verification; evidence/ updated',flush=True) if __name__ == '__main__': main()