Files
mtusi2026lab5/student-tests/unit/test_pipeline.py
T
Alexey Bodanyuk aef6eb1d4c тесты lab4 переведены на 202 + worker; unit-тесты pipeline; кейсы рецензий как 415
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

PNG с фильтром строки 5/0xff, IDAT не zlib, JPEG из 27 байт, SOS с компонентом не из SOF:
415 при загрузке. 40 Мпикс проходит до ready в лимите памяти worker.
2026-10-31 16:12:52 +03:00

225 lines
9.8 KiB
Python

"""Unit tests of lab 5 without services: variant geometry and encoding (imaging.build, the same
code the worker runs in its child process), the job message schema, backoff with jitter and the
pipeline configuration."""
from __future__ import annotations
import io
import json
import sys
import uuid
from pathlib import Path
import pytest
from jsonschema import Draft202012Validator
from PIL import Image
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "src"))
sys.path.insert(0, str(ROOT / "student-tests"))
import imagegen # noqa: E402
from messenger import broker, imaging, jobs # noqa: E402
from messenger.config import ConfigError, Settings # noqa: E402
from messenger.pipeline import PIPELINES, center_square, fit # noqa: E402
SAMPLE = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
SCHEMA = json.loads((ROOT / "contracts" / "jobs.schema.json").read_text(encoding="utf-8"))
LIMIT = 40_000_000
def variants(data: bytes, content_type: str = "image/png", version: int = 1) -> dict[str, imaging.Rendered]:
return {v.name: v for v in imaging.build(data, content_type, version, LIMIT)}
def decoded(v: imaging.Rendered) -> Image.Image:
img = Image.open(io.BytesIO(v.data))
img.load()
return img
# ------------------------------------------------------------------ geometry (contract rules)
@pytest.mark.parametrize("size, box, expected", [
((320, 200), 256, (256, 160)), # public fixture
((200, 320), 256, (160, 256)),
((100, 50), 256, (100, 50)), # never upscale
((256, 256), 256, (256, 256)),
((1000, 1), 256, (256, 1)), # 0.256 px -> minimum 1 px
((1, 1000), 256, (1, 256)),
((257, 255), 256, (256, 254)), # 255 * 256 / 257 = 254.0 (floor)
((3000, 2000), 1280, (1280, 853)), # 853.33 -> 853
])
def test_fit_rounds_down_without_upscaling(size, box, expected):
assert fit(*size, box) == expected
def test_center_square():
assert center_square(320, 200) == (60, 0, 260, 200)
assert center_square(7, 10) == (0, 1, 7, 8)
def test_public_fixture_variants_match_the_contract():
out = variants(SAMPLE)
assert list(out) == ["thumbnail", "crop", "optimized"]
assert (out["thumbnail"].content_type, decoded(out["thumbnail"]).format, decoded(out["thumbnail"]).size) == \
("image/jpeg", "JPEG", (256, 160))
assert (out["crop"].content_type, decoded(out["crop"]).size) == ("image/jpeg", (128, 128))
assert (out["optimized"].content_type, decoded(out["optimized"]).format, decoded(out["optimized"]).size) == \
("image/webp", "WEBP", (320, 200)) # fits 1280 already: no upscale
for v in out.values():
assert (v.width, v.height) == decoded(v).size
def test_small_image_is_not_upscaled_but_crop_is():
out = variants(imagegen.png(40, 30))
assert decoded(out["thumbnail"]).size == (40, 30)
assert decoded(out["optimized"]).size == (40, 30)
assert decoded(out["crop"]).size == (128, 128)
def test_big_image_is_bounded_by_1280():
out = variants(imagegen.png(3000, 2000))
assert decoded(out["optimized"]).size == (1280, 853)
assert decoded(out["thumbnail"]).size == (256, 170)
def test_exif_orientation_is_applied_and_dropped():
# 60x20 stored landscape with Orientation=6 (rotate 90 CW): displayed portrait 20x60.
img = Image.new("RGB", (60, 20), (200, 30, 30))
exif = Image.Exif()
exif[0x0112] = 6
buf = io.BytesIO()
img.save(buf, "JPEG", exif=exif.tobytes(), quality=95)
out = variants(buf.getvalue(), "image/jpeg")
assert decoded(out["thumbnail"]).size == (20, 60)
assert decoded(out["optimized"]).size == (20, 60)
for v in out.values():
assert not decoded(v).getexif(), v.name
def test_alpha_is_composited_on_white_for_jpeg_and_kept_for_webp():
img = Image.new("RGBA", (64, 64), (255, 0, 0, 0)) # fully transparent red
buf = io.BytesIO()
img.save(buf, "PNG")
out = variants(buf.getvalue())
thumb = decoded(out["thumbnail"]).convert("RGB")
assert min(thumb.getpixel((32, 32))) >= 250, "transparent pixels must become white, not red/black"
webp = decoded(out["optimized"])
assert webp.mode == "RGBA" and webp.getpixel((10, 10))[3] == 0
def test_metadata_is_not_copied():
img = Image.new("RGB", (300, 300), (10, 200, 10))
exif = Image.Exif()
exif[0x010F] = "SecretCamera" # Make
buf = io.BytesIO()
img.save(buf, "JPEG", exif=exif.tobytes(), icc_profile=b"\0" * 128, comment=b"private")
out = variants(buf.getvalue(), "image/jpeg")
for name in ("thumbnail", "crop"):
data = out[name].data
assert b"Exif" not in data and b"ICC_PROFILE" not in data and b"SecretCamera" not in data, name
assert b"EXIF" not in out["optimized"].data and b"ICCP" not in out["optimized"].data
@pytest.mark.parametrize("mode", ["RGBA", "P", "1", "L", "LA", "RGB"])
def test_png_modes_are_processed(mode):
out = variants(imagegen.png(33, 17, mode))
assert decoded(out["thumbnail"]).size == (33, 17)
def test_16_bit_grey_is_scaled_not_clipped():
img = Image.new("I;16", (8, 8), 32768) # 50 % grey in 16 bits
buf = io.BytesIO()
img.save(buf, "PNG")
out = variants(buf.getvalue())
value = decoded(out["thumbnail"]).convert("L").getpixel((4, 4))
assert 120 <= value <= 135
def test_pipeline_versions_differ_in_bytes_not_in_geometry():
v1, v2 = variants(SAMPLE, version=1), variants(SAMPLE, version=2)
for name in v1:
assert (v1[name].width, v1[name].height) == (v2[name].width, v2[name].height)
assert v1[name].sha256 != v2[name].sha256
assert variants(SAMPLE, version=1)["thumbnail"].sha256 == v1["thumbnail"].sha256 # deterministic
def test_pixel_limit_is_checked_before_decoding():
# 8001 x 5000 = 40.005 Mpx with only a few rows of data: refused from the header (TooLarge);
# 20000 x 10000 is refused even earlier by Pillow's own bomb check (> 2 x MAX_IMAGE_PIXELS).
with pytest.raises(imaging.TooLarge):
imaging.build(imagegen.png_bomb_header(8001, 5000), "image/png", 1, LIMIT)
with pytest.raises((imaging.TooLarge, Image.DecompressionBombError)):
imaging.build(imagegen.png_bomb_header(), "image/png", 1, LIMIT)
def test_child_process_protocol_round_trip():
out = list(variants(SAMPLE).values())
raw = json.dumps({"variants": [{"name": v.name, "content_type": v.content_type, "width": v.width,
"height": v.height, "size": len(v.data)} for v in out]}).encode() + b"\n"
raw += b"".join(v.data for v in out)
parsed = imaging.parse_output(raw)
assert [(p.name, p.sha256) for p in parsed] == [(v.name, v.sha256) for v in out]
with pytest.raises(imaging.ProcessingError):
imaging.parse_output(raw[:-1])
# ------------------------------------------------------------------ message format
def test_encoded_message_matches_jobs_schema_and_carries_ids_only():
import datetime as dt
raw = broker.encode(str(uuid.uuid4()), str(uuid.uuid4()), 1, 2, dt.datetime.now(dt.timezone.utc))
body = json.loads(raw)
assert not list(Draft202012Validator(SCHEMA, format_checker=Draft202012Validator.FORMAT_CHECKER).iter_errors(body))
assert set(body) == {"schema_version", "job_id", "type", "attachment_id", "pipeline_version", "attempt", "created_at"}
assert broker.decode(raw) == body
@pytest.mark.parametrize("change", [
{"schema_version": 2}, {"type": "other"}, {"job_id": "not-a-uuid"}, {"attempt": 0}, {"attempt": True},
{"pipeline_version": "1"}, {"created_at": "yesterday"}, {"url": "http://evil"}, {"object_key": "a/b"},
{"traceparent": "bad"},
])
def test_invalid_messages_are_rejected(change):
body = json.loads(broker.encode(str(uuid.uuid4()), str(uuid.uuid4()), 1, 1,
__import__("datetime").datetime(2026, 1, 1, tzinfo=__import__("datetime").timezone.utc)))
body.update(change)
with pytest.raises(broker.BadMessage):
broker.decode(json.dumps(body).encode())
# The contract schema refuses the same messages (date-time needs an optional jsonschema extra).
checker = Draft202012Validator.FORMAT_CHECKER
assert list(Draft202012Validator(SCHEMA, format_checker=checker).iter_errors(body)) or "created_at" in change
def test_garbage_is_rejected():
for raw in (b"", b"[]", b"{", b"\xff", b"x" * 5000):
with pytest.raises(broker.BadMessage):
broker.decode(raw)
# ------------------------------------------------------------------ backoff and configuration
def test_backoff_is_full_jitter_and_capped():
assert jobs.backoff_delay(1, 2, 60, rnd=lambda: 1.0) == 2
assert jobs.backoff_delay(3, 2, 60, rnd=lambda: 1.0) == 8
assert jobs.backoff_delay(10, 2, 60, rnd=lambda: 1.0) == 60
assert jobs.backoff_delay(4, 2, 60, rnd=lambda: 0.0) == 0
samples = [jobs.backoff_delay(3, 2, 60) for _ in range(500)]
assert 0 <= min(samples) < 2 and 6 < max(samples) <= 8 # spread over the whole window
def test_pipeline_settings(monkeypatch):
monkeypatch.setenv("DATABASE_URL", "postgresql://x@y/z")
monkeypatch.setenv("S3_ACCESS_KEY_ID", "AKTEST")
monkeypatch.setenv("S3_SECRET_ACCESS_KEY", "secret-for-unit-tests")
s = Settings.from_env()
assert (s.job_max_attempts, s.job_timeout_seconds, s.worker_concurrency, s.staging_retention_seconds) == (5, 60, 2, 86400)
assert s.s3_staging_bucket == "messenger-staging" and s.pipeline_version == 1
monkeypatch.setenv("FAULT_POINTS", "transient_error=3,slow")
s = Settings.from_env()
assert s.fault_points == {"transient_error", "slow"} and s.fault_value("transient_error") == 3
for name, value in (("PIPELINE_VERSION", "9"), ("JOB_MAX_ATTEMPTS", "0"), ("FAULT_POINTS", "slow=x"),
("S3_STAGING_BUCKET", "messenger-attachments"), ("JOB_RETRY_BASE_SECONDS", "100")):
monkeypatch.setenv(name, value)
with pytest.raises(ConfigError):
Settings.from_env()
monkeypatch.delenv(name)
assert set(PIPELINES) == {1, 2, 3}