Files
mtusi2026lab2/student-tests/verify.py
T

68 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Run local checks serially and keep sanitized output + restart scope evidence."""
import datetime as dt
import json
import os
from pathlib import Path
import platform
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
COMPOSE = ['docker','compose','--env-file','.env']
def command(args):
result = subprocess.run(args,cwd=ROOT,capture_output=True,text=True,encoding='utf-8',errors='replace')
return result
def snapshot():
ids = command(COMPOSE+['ps','-q','api','postgres','redis'])
if ids.returncode:
raise RuntimeError('Cannot inspect local lab stack')
result = command(['docker','inspect','--format','{{json .Name}} {{json .Id}} {{json .State.StartedAt}} {{json .State.Status}}']+ids.stdout.split())
if result.returncode:
raise RuntimeError('Cannot inspect restart scope')
# Narrow format intentionally excludes environment/credentials.
return result.stdout.strip().splitlines()
def main():
results = {'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'host':platform.platform(),'base_url':'http://127.0.0.1:8080','checks':[]}
hook = 'scripts/restart.cmd' if os.name=='nt' else 'scripts/restart.sh'
if os.name!='nt':
(ROOT/hook).chmod(0o755)
commands = [
[sys.executable,'scripts/check_contract.py'],
[sys.executable,'tests/smoke.py','--base-url',results['base_url']],
[sys.executable,'tests/resilience.py','--mode','persistence','--action-script',hook,'--base-url',results['base_url']],
[sys.executable,'student-tests/integration.py'],
[sys.executable,'student-tests/research.py'],
[sys.executable,'tests/load.py','--base-url',results['base_url'],'--duration','30','--concurrency','4','--output','evidence/load.json'],
]
try:
for args in commands:
persistence = 'tests/resilience.py' in args
if persistence:
results['restart_before'] = snapshot()
start = time.monotonic()
completed = command(args)
entry = {'command':' '.join(args[1:]),'exit_code':completed.returncode,'seconds':round(time.monotonic()-start,3),'stdout':completed.stdout.strip(),'stderr':completed.stderr.strip()}
results['checks'].append(entry)
print(entry['stdout'],flush=True)
if persistence:
results['restart_after'] = snapshot()
if completed.returncode==0:
before = {line.split()[0]:line.split()[2] for line in results['restart_before']}
after = {line.split()[0]:line.split()[2] for line in results['restart_after']}
if before.keys()!=after.keys() or any(before[key]==after[key] for key in before):
raise AssertionError('Not every API/state-store process restarted')
results['restart_scope_verified'] = True
if completed.returncode:
print(entry['stderr'],flush=True)
raise SystemExit(completed.returncode)
finally:
(ROOT/'evidence'/'verification.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
if __name__=='__main__':
main()