Files
mtusi2026lab4/scripts/verify_clean_export.py
T

55 lines
3.4 KiB
Python

"""Verify a committed Git export with fresh secrets, isolated images/project/volumes."""
import json
import os
from pathlib import Path
import socket
import subprocess
import tarfile
import tempfile
import time
import uuid
ROOT=Path(__file__).resolve().parents[1]
project='lyakhonov-lab4-repro-'+uuid.uuid4().hex[:8]
with socket.socket() as sock:
sock.bind(('127.0.0.1',0)); port=sock.getsockname()[1]
env=dict(os.environ,LAB4_PORT=str(port),PYTHONUTF8='1')
commit=subprocess.check_output(['git','rev-parse','HEAD'],cwd=ROOT,text=True).strip()
with tempfile.TemporaryDirectory(prefix='lab4-repro-') as folder:
temporary=Path(folder).resolve()
assert temporary.parent==Path(tempfile.gettempdir()).resolve() and temporary.name.startswith('lab4-repro-')
archive=temporary/'source.tar'
subprocess.check_call(['git','archive','--format=tar','-o',str(archive),'HEAD'],cwd=ROOT)
source=temporary/'source';source.mkdir()
with tarfile.open(archive) as tar:
for member in tar.getmembers():
assert (source/member.name).resolve().is_relative_to(source.resolve())
tar.extractall(source,filter='data')
assert not (source/'secrets').exists() and not (source/'.env').exists()
override=source/'repro.override.yaml'
override.write_text('services:\n'+''.join(' '+name+':\n image: '+image+'\n' for name,image in [
('api',project+'-api'),('migrate',project+'-api'),('reconciler',project+'-api'),('s3',project+'-s3'),('bucket-init',project+'-s3')]),encoding='utf-8')
base=['docker','compose','--project-directory',str(source),'-f',str(source/'compose.yaml'),'-f',str(override),'-p',project]
def dc(*args,**kwargs): return subprocess.check_output(base+list(args),env=env,text=True,**kwargs)
existing=subprocess.check_output(['docker','ps','-aq','--filter','label=com.docker.compose.project='+project],text=True).strip()
assert not existing
subprocess.check_call(['python',str(source/'scripts/bootstrap.py')],env=env)
results={'commit':commit,'project':project,'port':port,'fresh_secrets':True,'isolated_images':True}
try:
with (ROOT/'evidence/repro-build.txt').open('w',encoding='utf-8') as buildlog:
subprocess.check_call(base+['build','api','s3'],env=env,stdout=buildlog,stderr=subprocess.STDOUT)
dc('up','-d')
ready=subprocess.check_output(['python',str(source/'scripts/wait_ready.py'),'--base-url','http://localhost:'+str(port),'--replicas','2'],env=env,text=True)
smoke=subprocess.check_output(['python',str(source/'tests/smoke.py'),'--base-url','http://localhost:'+str(port)],env=env,text=True)
results.update(result='PASS',readiness=ready.strip(),smoke=smoke.strip())
# Remove only volumes belonging to this newly created unique scratch project.
volumes=subprocess.check_output(['docker','volume','ls','-q','--filter','label=com.docker.compose.project='+project],text=True).split()
for volume in volumes:
labels=json.loads(subprocess.check_output(['docker','volume','inspect',volume],text=True))[0]['Labels']
assert labels['com.docker.compose.project']==project
results['scratch_volumes']=volumes
finally:
dc('down','--volumes')
results['scratch_cleaned']=True
(ROOT/'evidence/clean-export.json').write_text(json.dumps(results,indent=2),encoding='utf-8')
print('PASS: committed clean export built API/S3, fresh secrets, two API replicas and public smoke; scratch stack cleaned')