Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
90 lines
3.5 KiB
Python
Executable File
90 lines
3.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Lab5: metadata, bytes and dimensions of all contract variants.
|
|
|
|
Run against a live stand:
|
|
python3 student-tests/lab5_variants.py --base-url http://localhost:8080
|
|
"""
|
|
import hashlib
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
from client import Client, Failure, arguments, check, run # noqa: E402
|
|
from image_probe import jpeg_size # noqa: E402
|
|
|
|
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
|
|
|
|
|
|
def webp_size(data):
|
|
check(data[:4] == b'RIFF' and data[8:12] == b'WEBP', 'not a WebP container')
|
|
|
|
chunk = data[12:16]
|
|
if chunk == b'VP8X':
|
|
width = 1 + int.from_bytes(data[24:27], 'little')
|
|
height = 1 + int.from_bytes(data[27:30], 'little')
|
|
return width, height
|
|
if chunk == b'VP8 ':
|
|
start = data.index(b'\x9d\x01\x2a', 12) + 3
|
|
width = int.from_bytes(data[start:start + 2], 'little') & 0x3fff
|
|
height = int.from_bytes(data[start + 2:start + 4], 'little') & 0x3fff
|
|
return width, height
|
|
if chunk == b'VP8L':
|
|
bits = int.from_bytes(data[21:25], 'little')
|
|
width = (bits & 0x3fff) + 1
|
|
height = ((bits >> 14) & 0x3fff) + 1
|
|
return width, height
|
|
raise Failure('unsupported WebP chunk ' + repr(chunk))
|
|
|
|
|
|
def main():
|
|
args = arguments('Lab5 variant contract')
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
|
|
attached, content = client.attachment(alice, chat)
|
|
ready = client.wait_ready(alice, attached)
|
|
|
|
by_name = {variant['name']: variant for variant in ready['variants']}
|
|
check(set(by_name) == {'thumbnail', 'crop', 'optimized'}, 'unexpected variant set: ' + str(sorted(by_name)))
|
|
|
|
expected = {
|
|
'thumbnail': ('image/jpeg', (256, 160)),
|
|
'crop': ('image/jpeg', (128, 128)),
|
|
'optimized': ('image/webp', (320, 200)),
|
|
}
|
|
|
|
for name, (content_type, dims) in expected.items():
|
|
variant = by_name[name]
|
|
response = client.call(
|
|
'GET', '/api/v1/attachments/' + ready['id'] + '/content?variant=' + name, actor=bob,
|
|
)
|
|
data = response['bytes']
|
|
check(response['headers'].get_content_type() == content_type, name + ' content type mismatch')
|
|
check(variant['content_type'] == content_type, name + ' metadata content type mismatch')
|
|
check(len(data) == variant['size_bytes'], name + ' size_bytes mismatch')
|
|
check(hashlib.sha256(data).hexdigest() == variant['sha256'], name + ' sha256 mismatch')
|
|
|
|
if name == 'optimized':
|
|
actual = webp_size(data)
|
|
check(actual == dims, name + ' dimensions ' + str(actual))
|
|
check(b'EXIF' not in data[:64], 'optimized WebP must not carry EXIF')
|
|
else:
|
|
actual = jpeg_size(data)
|
|
check(actual == dims, name + ' dimensions ' + str(actual))
|
|
check('Exif' not in data[:64].decode('latin-1', 'ignore'), name + ' must not carry EXIF')
|
|
check((variant['width'], variant['height']) == dims, name + ' metadata dimensions mismatch')
|
|
|
|
original = client.call('GET', '/api/v1/attachments/' + ready['id'] + '/content', actor=bob)
|
|
check(original['bytes'] == content, 'original bytes changed')
|
|
check(original['headers'].get_content_type() == 'image/png', 'original content type changed')
|
|
|
|
print('variants: thumbnail 256x160, crop 128x128, optimized WebP 320x200; metadata == bytes')
|
|
print('PASS: lab5 variant contract')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|