Files
mtusi2026lab6/student-tests/version_fence.py
T

30 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Release an actually stale v1 process after v2 commits, rather than mocking SQL."""
import hashlib,json,subprocess,time,uuid
from pipeline import ROOT,C,dc,sql,check,meta,wait_status,content,upload,start_worker
def main():
a,b,e,chat=C.bootstrap();dc('stop','worker');att,data=upload(a,chat);name='lab-stale-'+uuid.uuid4().hex[:10]
dc('run','-d','--no-deps','--name',name,'-e','LAB_FAULT_POINT=before_write','worker')
try:
deadline=time.monotonic()+45
while time.monotonic()<deadline:
if subprocess.run(['docker','exec',name,'test','-f','/tmp/fault-reached'],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode==0:break
time.sleep(.2)
else:raise AssertionError('v1 did not reach pre-write boundary')
check(meta(a,att['id'])['status']=='processing','v1 not processing')
dc('run','--rm','--no-deps','worker','reprocess',att['id']);start_worker();v2,recovery=wait_status(a,att['id'],'ready')
versions=sql("SELECT pipeline_version FROM attachments WHERE id='"+att['id']+"'");check(versions=='2','new version missing')
before={v['name']:content(a,att['id'],v['name']) for v in v2['variants']}
subprocess.run(['docker','exec',name,'touch','/tmp/fault-release'],check=True)
time.sleep(3)
after=meta(a,att['id']);check(after==v2,'stale process overwrote v2 metadata')
check(all(content(a,att['id'],k)==v for k,v in before.items()),'stale process overwrote v2 variants')
check(content(a,att['id'])==data,'original changed')
check(sql("SELECT count(*) FROM pipeline_jobs WHERE attachment_id='"+att['id']+"' AND finished_at IS NULL")=='0','obsolete version still pending')
result={'case':'live stale v1 resumed S3 writes and SQL finalization after v2 ready','attachment_id':att['id'],'new_version':2,'v2_recovery_seconds':recovery,'metadata_unchanged':True,'sha256_unchanged':True,'unfinished_jobs':0,'variant_checksums':{v['name']:v['sha256'] for v in v2['variants']}}
(ROOT/'evidence/version-fence.json').write_text(json.dumps(result,indent=2)+'\n');print('PASS: live stale v1 cannot overwrite v2 objects or fenced SQL result')
finally:
subprocess.run(['docker','rm','-f',name],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL);start_worker()
if __name__=='__main__':main()