"""Complete, sequential verification on a disposable local Compose lab.""" import argparse import datetime as dt import hashlib import json import os from pathlib import Path import subprocess import sys import time ROOT=Path(__file__).resolve().parents[1] def protected(): return {str(p.relative_to(ROOT)).replace('\\','/'):hashlib.sha256(p.read_bytes()).hexdigest() for folder in ('contracts','tests') for p in (ROOT/folder).rglob('*') if p.is_file() and '__pycache__' not in p.parts} def main(): p=argparse.ArgumentParser(description=__doc__) p.add_argument('--static-only',action='store_true') p.add_argument('--resume-integration',action='store_true',help='reuse successful local checks, rerun build and every integration check') args=p.parse_args() # Compose publishes IPv4 loopback. Avoid Windows localhost IPv6 fallback # and keep public/private child clients on the same operator-supplied URL. os.environ.setdefault('BASE_URL', 'http://127.0.0.1:8080') evidence=ROOT/'evidence';evidence.mkdir(exist_ok=True) result={'started_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'complete':False,'checks':[]} if args.resume_integration: previous=json.loads((evidence/'verification.json').read_text(encoding='utf-8')) names={'contract','go-test','go-vet','student-unit','go-vuln','go-race'} successful={c['name']:c for c in previous['checks'] if c['name'] in names and c['exit_code']==0} if set(successful)!=names: raise RuntimeError('resume requires all local and race checks to have passed') cutoff=dt.datetime.fromisoformat(previous.get('local_checks_from_utc',previous['started_utc'])).timestamp() source=list(ROOT.glob('*.go'))+list((ROOT/'migrations').glob('*.sql'))+[ROOT/'go.mod',ROOT/'go.sum',ROOT/'Dockerfile'] if any(path.stat().st_mtime>cutoff for path in source): raise RuntimeError('application sources changed; run the full verifier') result['checks']=[successful[name] for name in ('contract','go-test','go-vet','student-unit','go-vuln','go-race')] result['local_checks_from_utc']=previous.get('local_checks_from_utc',previous['started_utc']) manifest=evidence/'protected-files.json' original=json.loads(manifest.read_text(encoding='utf-8')) if manifest.exists() else protected() if protected()!=original: raise RuntimeError('protected files differ from the original SHA-256 manifest') def persist(): result['protected_unchanged']=protected()==original (evidence/'verification.json').write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8') subprocess.run([sys.executable,'-X','utf8','scripts/summarize.py'],cwd=ROOT,check=True) def run(name,command,timeout=1200): print('Running '+name,flush=True) start=time.monotonic() with (evidence/(name+'.txt')).open('w',encoding='utf-8') as output: completed=subprocess.run(command,cwd=ROOT,stdout=output,stderr=subprocess.STDOUT, encoding='utf-8',timeout=timeout) result['checks'].append({'name':name,'command':command,'exit_code':completed.returncode,'seconds':round(time.monotonic()-start,3)}) persist() if completed.returncode: raise RuntimeError(name+' failed; see evidence/'+name+'.txt') py=[sys.executable,'-X','utf8'] try: if not args.resume_integration: run('contract',py+['scripts/check_contract.py']) run('go-test',['go','test','./...']) run('go-vet',['go','vet','./...']) run('student-unit',py+['student-tests/unit.py']) run('go-vuln',['go','run','golang.org/x/vuln/cmd/govulncheck@v1.1.4','./...']) if args.static_only: result['static_complete']=True return run('bootstrap',py+['scripts/bootstrap.py']) run('compose-config',['docker','compose','config','--quiet']) run('start',py+['scripts/manage.py','start'],3600) run('build-tests',['docker','build','-f','Dockerfile.tests','-t','mtusi-messenger:lab4-tests','.'],1800) run('openapi',py+['scripts/linux_tests.py','validate']) if not args.resume_integration: run('go-race',['docker','run','--rm','--mount','type=bind,source='+str(ROOT)+',target=/workspace', 'mtusi-messenger:lab4-tests','go','test','-race','./...'],1200) run('smoke',py+['tests/smoke.py']) run('attachments',py+['student-tests/attachments.py']) run('persistence',py+['scripts/linux_tests.py','test-persistence']) run('ready-before-failover',py+['scripts/manage.py','restore']) for i in range(2): run('failover-'+str(i+1),py+['scripts/linux_tests.py','test-failover']) run('restore-'+str(i+1),py+['scripts/manage.py','restore']) run('failures',py+['student-tests/failures.py']) run('memory',py+['student-tests/memory.py']) run('research',py+['student-tests/research.py'],1800) run('load',py+['tests/load.py','--duration','30','--concurrency','4','--output','evidence/load.json']) run('infrastructure',py+['scripts/manage.py','status']) result['complete']=True except (RuntimeError,subprocess.TimeoutExpired,FileNotFoundError) as exc: result['failure']=str(exc) print(str(exc),file=sys.stderr) raise SystemExit(1) finally: persist() if not result['protected_unchanged']: raise RuntimeError('protected files changed during verification') if __name__=='__main__': main()