Files
mtusi2026lab3-fork/scripts/verify.py
T

93 lines
5.0 KiB
Python

"""Run the complete lab-3 verification from an ordinary operator terminal."""
import argparse
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
EVIDENCE = ROOT/'evidence'
def public_integrity():
names = subprocess.check_output(['git','ls-files','--','tests','contracts'],cwd=ROOT,text=True).splitlines()
hashes = {}
for name in names:
original = subprocess.check_output(['git','show','HEAD:'+name],cwd=ROOT).replace(b'\r\n',b'\n')
current = (ROOT/name).read_bytes().replace(b'\r\n',b'\n')
if current != original:
raise RuntimeError('protected source changed: '+name)
hashes[name] = hashlib.sha256(current).hexdigest()
return hashes
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--no-research',action='store_true',help='run levels 3/4 only; does not certify track 5')
p.add_argument('--resume',action='store_true',help='reuse completed work; API/infrastructure sources must be unchanged')
p.add_argument('--include-overload',action='store_true',help='also reproduce the additional overload experiment')
args = p.parse_args()
EVIDENCE.mkdir(exist_ok=True)
hashes = public_integrity()
results = []
previous_checks = {}
status = EVIDENCE/'verification.json'
if args.resume and status.exists():
previous_checks = {item['check']:item for item in json.loads(status.read_text(encoding='utf-8'))['checks'] if item['exit_code']==0}
env = dict(os.environ,PYTHONUTF8='1',PYTHONUNBUFFERED='1',GOTELEMETRY='off')
def run(name,command):
if args.resume and name in previous_checks and not name.startswith('final-') and name!='research':
results.append(previous_checks[name])
print('Reusing passed check: '+name,flush=True)
return
print('Running '+name+' ...',flush=True)
result = subprocess.run(command,cwd=ROOT,env=env,text=True,encoding='utf-8',errors='replace',stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
(EVIDENCE/(name+'.txt')).write_text(result.stdout,encoding='utf-8')
results.append({'check':name,'exit_code':result.returncode,'command':command})
(EVIDENCE/'verification.json').write_text(json.dumps({'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'complete':False,'checks':results},indent=2)+'\n',encoding='utf-8')
if result.returncode:
print(result.stdout[-5000:])
raise RuntimeError(name+' failed; see evidence/'+name+'.txt')
print('PASS: '+name,flush=True)
python = sys.executable
run('bootstrap',[python,'-X','utf8','scripts/bootstrap.py'])
run('docker-info',['docker','info','--format','{{.ServerVersion}} {{.NCPU}} CPU {{.MemTotal}} bytes'])
run('compose-check',['docker','compose','config','--quiet'])
run('build-start',['docker','compose','up','-d','--build','--wait'])
run('build-tests',['docker','build','-f','Dockerfile.tests','-t','mtusi-messenger:lab3-tests','.'])
for target in ('check','validate','test','test-own','test-race','test-persistence','test-failover'):
run(target,[python,'-X','utf8','scripts/linux_tests.py',target])
run('restore',[python,'-X','utf8','scripts/manage.py','restore'])
run('failures',[python,'-X','utf8','student-tests/failures.py'])
if not args.no_research:
run('build-load',['docker','build','--target','load','-t','mtusi-messenger:lab3-load','.'])
venv = ROOT/'.venv'
vpython = venv/('Scripts/python.exe' if os.name=='nt' else 'bin/python')
if not vpython.exists():
run('research-env',[python,'-m','venv',str(venv)])
run('research-dependencies',[str(vpython),'-m','pip','install','--disable-pip-version-check','-r','requirements-research.txt'])
research_command = [str(vpython),'-X','utf8','scripts/research.py']
if args.resume:
research_command.append('--resume')
if args.include_overload:
research_command.append('--include-overload')
run('research',research_command)
run('final-smoke',[python,'-X','utf8','tests/smoke.py','--base-url','http://127.0.0.1:8080'])
run('final-own',[python,'-X','utf8','student-tests/acceptance.py','--base-url','http://127.0.0.1:8080'])
if public_integrity()!=hashes:
raise RuntimeError('public sources changed during verification')
(EVIDENCE/'public-integrity.json').write_text(json.dumps(hashes,indent=2)+'\n',encoding='utf-8')
run('final-containers',[python,'-X','utf8','scripts/manage.py','status'])
complete = not args.no_research
(EVIDENCE/'verification.json').write_text(json.dumps({'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'complete':complete,'levels_3_4_passed':True,'track_5_measured':complete,'checks':results},indent=2)+'\n',encoding='utf-8')
print('Verification finished. '+('Full track-1 evidence is ready.' if complete else 'Levels 3/4 only; run without --no-research for track 5.'))
if __name__=='__main__':
main()