#!/usr/bin/env python3 """Exercise a source-only snapshot in a fresh project, with new CA/secrets/volumes and distinct ports.""" import json,os,shutil,subprocess,tempfile,time from pathlib import Path ROOT=Path(__file__).resolve().parents[1] files=subprocess.check_output(['git','ls-files','--cached','--others','--exclude-standard','-z'],cwd=ROOT).decode().split('\0') with tempfile.TemporaryDirectory(prefix='lab7-clean-') as work: target=Path(work) for name in files: p=ROOT/name if not name or not p.is_file() or name.startswith('evidence/'):continue out=target/name;out.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(p,out) (target/'evidence').mkdir(exist_ok=True) image='mtusi-messenger:lab7-clean-'+str(os.getpid()) cfg=json.loads((target/'compose.yaml').read_text()) for service in cfg['services'].values(): if service.get('image')=='mtusi-messenger:lab7':service['image']=image for role,port in [('lb',18443),('openbao',18201),('grafana',13444)]: current=cfg['services'][role]['ports'][0].split(':');cfg['services'][role]['ports']=['127.0.0.1:'+str(port)+':'+current[-1]] (target/'compose.yaml').write_text(json.dumps(cfg)) env=dict(os.environ,LAB7_PROJECT='mtusi2026lab7-clean-'+str(os.getpid()),LAB7_BASE_URL='https://localhost:18443',LAB7_BAO_OPERATOR_ADDR='https://localhost:18201') started=time.monotonic() try: subprocess.run(['./scripts/up.sh'],cwd=target,env=env,check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) smoke=subprocess.run(['python3','tests/smoke.py','--base-url',env['LAB7_BASE_URL'],'--ca-file',str(target/'secrets/lab7/lb/ca.crt')],cwd=target,env=env,check=True,text=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) subprocess.run(['python3','tests/security.py','--base-url',env['LAB7_BASE_URL'],'--ca-file',str(target/'secrets/lab7/lb/ca.crt')],cwd=target,env=env,check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) result={'fresh_source_snapshot':True,'new_ca_and_secrets':True,'new_project_and_volumes':True,'separate_application_image_tag':True,'no_lab6_access':True,'verified_https_smoke':smoke.stdout.strip(),'duration_seconds':time.monotonic()-started} (ROOT/'evidence/clean-clone.json').write_text(json.dumps(result,indent=2)+'\n');print('PASS: fresh source-only snapshot with new secrets/volumes passes HTTPS smoke') finally: subprocess.run(['./scripts/compose.sh','down','-v'],cwd=target,env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) subprocess.run(['docker','image','rm',image],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)