forked from mtusi2026/mtusi2026lab5
63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
"""Verify worker effects through HTTP and MinIO with a direct job handoff."""
|
|
import hashlib
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from client import Client, arguments, check, run
|
|
from image_probe import jpeg_size
|
|
|
|
|
|
def main():
|
|
args = arguments(__doc__)
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
attachment, original = client.attachment(alice, chat)
|
|
code = """
|
|
from messenger.attachments import AttachmentStorage
|
|
from messenger.config import Config
|
|
from messenger.db import Database
|
|
from messenger.worker import process
|
|
import sys
|
|
db = Database(Config.from_env())
|
|
db.open()
|
|
try:
|
|
with db.cursor() as cur:
|
|
cur.execute('SELECT id AS job_id, attachment_id, pipeline_version, attempt '
|
|
'FROM attachment_jobs WHERE attachment_id=%s', (sys.argv[1],))
|
|
job = cur.fetchone()
|
|
storage = AttachmentStorage()
|
|
process(db, storage, job)
|
|
process(db, storage, job)
|
|
finally:
|
|
db.close()
|
|
"""
|
|
subprocess.run(['docker', 'compose', 'exec', '-T', 'api-a', 'python', '-c',
|
|
code, attachment['id']], cwd=ROOT, check=True, timeout=30)
|
|
path = '/api/v1/attachments/' + attachment['id']
|
|
ready = client.call('GET', path, actor=bob, schema='Attachment')['json']
|
|
check(ready['status'] == 'ready' and len(ready['variants']) == 3, 'expected three variants')
|
|
check(client.call('GET', path + '/content', actor=bob)['bytes'] == original, 'original changed')
|
|
expected = {'thumbnail': (256, 160, 'image/jpeg'), 'crop': (128, 128, 'image/jpeg'),
|
|
'optimized': (320, 200, 'image/webp')}
|
|
for item in ready['variants']:
|
|
width, height, mime = expected[item['name']]
|
|
reply = client.call('GET', path + '/content?variant=' + item['name'], actor=bob)
|
|
data = reply['bytes']
|
|
check((item['width'], item['height'], item['content_type']) == (width, height, mime),
|
|
'incorrect variant metadata')
|
|
check(reply['headers'].get_content_type() == mime and len(data) == item['size_bytes']
|
|
and hashlib.sha256(data).hexdigest() == item['sha256'], 'incorrect variant payload')
|
|
if mime == 'image/jpeg':
|
|
check(jpeg_size(data) == (width, height), 'invalid JPEG dimensions')
|
|
else:
|
|
check(data[:4] == b'RIFF' and data[8:12] == b'WEBP', 'invalid WebP signature')
|
|
print('PASS: original unchanged, thumbnail 256x160, crop 128x128, WebP 320x200')
|
|
print('PASS: processing the same committed job twice keeps one set of variants')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|