Files
mtusi2026lab6/student-tests/storage.py
T
2026-11-17 13:08:13 +03:00

63 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Staging protection, retirement, retention and explicit restoration on synthetic data."""
import json
from pathlib import Path
import sys
import time
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'tests'))
from client import Client,check
BASE=[str(ROOT/'scripts/compose.sh')]
import subprocess
def command(*args,fail=False):
r=subprocess.run(BASE+list(args),stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,timeout=120)
if not fail:check(r.returncode==0,'storage scenario infrastructure command failed: '+args[0])
return r
def sql(q):return command('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-v','ON_ERROR_STOP=1','-c',q).stdout.strip()
def op(*args,fail=False):return command('run','--rm','--no-deps','-T','operator',*args,fail=fail)
def wait(fn):
deadline=time.monotonic()+20
while time.monotonic()<deadline:
if fn():return
time.sleep(.1)
raise AssertionError('storage condition timed out')
def exists(key):return json.loads(op('storage','object-check','--key',key).stdout)['exists']
def main():
c=Client('http://localhost:18086');alice,bob,eve,chat=c.bootstrap();results=[]
try:
command('stop','worker')
att,data=c.attachment(alice,chat);key='staging/'+att['id']
check(exists(key),'accepted source missing')
result=json.loads(op('storage','reconcile','--apply').stdout)
check(result['referenced_objects']>0 and exists(key),'reconciliation removed active source')
results.append({'scenario':'queued_staging_protected','result':'PASS','attachment_id':att['id']})
command('start','worker');c.wait_ready(bob,att)
wait(lambda:sql("SELECT staging_key FROM attachments WHERE id='%s'"%att['id'])=='')
check(not exists(key),'ready source staging not retired')
results.append({'scenario':'staging_deleted_after_ready','result':'PASS','attachment_id':att['id']})
command('stop','worker')
failed,data=c.attachment(alice,chat);key='staging/'+failed['id'];jid=sql("SELECT id FROM attachment_jobs WHERE attachment_id='%s'"%failed['id'])
op('storage','remove-object','--id',failed['id']);command('start','worker')
def is_failed():return c.call('GET','/api/v1/attachments/'+failed['id'],actor=bob,schema='Attachment')['json']['status']=='failed'
wait(is_failed)
check(sql("SELECT error_code FROM attachment_jobs WHERE id='%s'"%jid)=='source_missing','missing source not permanent')
check(op('jobs','replay','--id',jid,fail=True).returncode!=0,'replay accepted missing source')
op('storage','restore','--id',failed['id'],'--source','/fixtures/sample.png')
check(exists(key),'restored source missing')
# Advance only the synthetic record's age; do not wait a real day or change production retention.
sql("UPDATE attachments SET updated_at=clock_timestamp()-interval '25 hours' WHERE id='%s'"%failed['id'])
wait(lambda:sql("SELECT staging_key FROM attachments WHERE id='%s'"%failed['id'])=='')
check(not exists(key),'expired failed source not retired')
check(op('jobs','replay','--id',jid,fail=True).returncode!=0,'replay accepted retired source')
results.append({'scenario':'failed_retention_and_missing_source_replay','result':'PASS','age_simulated_hours':25})
op('storage','restore','--id',failed['id'],'--source','/fixtures/sample.png');op('jobs','replay','--id',jid);c.wait_ready(bob,failed)
check(c.call('GET','/api/v1/attachments/'+failed['id']+'/content',actor=bob)['bytes']==data,'restored original changed')
results.append({'scenario':'verified_restore_then_replay','result':'PASS','job_id':jid})
(ROOT/'evidence/storage.json').write_text(json.dumps({'results':results},indent=2)+'\n')
print('PASS: active source protected, ready staging removed, failed retention enforced, restore/replay verified')
finally:command('start','worker')
if __name__=='__main__':main()