Files
mtusi2026lab7/tests/retry_limit.py
T
Бахрам 1df2202403
Skeleton contracts / contracts (pull_request) Canceled after 0s
Complete lab 7 TLS and secret management
2026-12-01 22:44:28 +03:00

31 lines
1.8 KiB
Python

"""Inject a corrupt staged object and prove bounded failure is visible."""
import subprocess
import time
from client import Client, arguments, check, run
def main():
args=arguments('Bounded retry and safe failed state')
c=Client(args.base_url,args.ca_file,timeout=10)
alice,bob,eve,chat=c.bootstrap()
subprocess.run(['docker','compose','stop','worker'],check=True,stdout=subprocess.DEVNULL)
try:
att,original=c.attachment(alice,chat)
code="from app.worker import s3,BUCKET; import os,psycopg; c=psycopg.connect(os.environ['DATABASE_URL']); k=c.execute('SELECT stage_key FROM attachments WHERE id=%s',(\""+att['id']+"\",)).fetchone()[0]; s3().put_object(Bucket=BUCKET,Key=k,Body=b'corrupt image data',ContentType='image/png'); c.close()"
subprocess.run(['docker','compose','exec','-T','api1','python','-c',code],check=True,stdout=subprocess.DEVNULL)
finally:
subprocess.run(['docker','compose','up','-d','worker'],check=True,stdout=subprocess.DEVNULL)
deadline=time.monotonic()+60
while time.monotonic()<deadline:
att=c.call('GET','/api/v1/attachments/'+att['id'],actor=bob,schema='Attachment')['json']
if att['status']=='failed': break
time.sleep(.5)
check(att['status']=='failed','corrupt image did not reach bounded failed state')
check(att['error_code'] in ('invalid_image','retry_exhausted'),'unexpected public failure code')
c.call('GET','/api/v1/attachments/'+att['id']+'/content',409,actor=bob)
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',409,actor=alice,
body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':'failed-attachment-test'})
print('PASS: corrupt job ended as failed after bounded retries; content and message attachment are rejected')
if __name__=='__main__': run(main)