Files
mtusi2026lab7/tests/test_pipeline.py
T
wanderinho 7ee5192cbd
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР7 - БПИ2303 - Стругов - на 4
2026-12-06 23:11:49 +03:00

56 lines
2.6 KiB
Python

"""Local operator test; requires Compose project running. No credentials printed."""
import hashlib
import os
import subprocess
import time
from pathlib import Path
from client import Client, check
ROOT=Path(__file__).resolve().parents[1]
CA_FILE=os.environ.get('CA_FILE', str(ROOT/'secrets/pki/ca.crt'))
def compose(*args):
subprocess.run(['docker','compose',*args],cwd=ROOT,check=True)
def verify(c,actor,att,original):
ready=c.wait_ready(actor,att,wait_seconds=180)
variants={v['name']:v for v in ready['variants']}
check(set(variants)=={'thumbnail','crop','optimized'},'missing variants')
for name, dims, ct in [('thumbnail',(256,160),'image/jpeg'),('crop',(128,128),'image/jpeg'),('optimized',(320,200),'image/webp')]:
v=variants[name]; check((v['width'],v['height'])==dims,'wrong dimensions')
response=c.call('GET',f"/api/v1/attachments/{att['id']}/content?variant={name}",actor=actor)
check(response['headers'].get_content_type()==ct,'wrong MIME')
check(hashlib.sha256(response['bytes']).hexdigest()==v['sha256'],'wrong checksum')
check(c.call('GET',f"/api/v1/attachments/{att['id']}/content",actor=actor)['bytes']==original,'original changed')
return ready
def main():
c=Client(ca_file=CA_FILE,timeout=30)
alice,bob,_,chat=c.bootstrap()
compose('stop','worker')
try:
att,raw=c.attachment(alice,chat)
c.message(alice,chat,'',attachments=[att['id']])
c.call('GET',f"/api/v1/attachments/{att['id']}/content",409,actor=bob)
compose('restart','api','broker')
compose('up','-d','--wait','--scale','api=2')
verify(c,bob,att,raw)
print('PASS: worker stopped, accepted job, API/broker restarted, variants verified')
finally: compose('up','-d','worker')
for marker in ('before-publish','after-object','after-commit'):
(ROOT/'faults'/marker).touch()
att,raw=c.attachment(alice,chat)
verify(c,bob,att,raw)
deadline=time.monotonic()+180
while time.monotonic()<deadline:
pending=subprocess.check_output([
'docker','compose','exec','-T','worker','python','-c',
"import pipeline as p; print(p.broker().xpending(p.STREAM, p.GROUP)['pending'])",
],cwd=ROOT,text=True).splitlines()
if not (ROOT/'faults'/marker).exists() and pending and pending[0]=='0': break
time.sleep(1)
else: raise AssertionError('fault not triggered or pending not acknowledged')
verify(c,bob,att,raw)
print('PASS:',marker,att['id'])
if __name__=='__main__': main()