forked from mtusi2026/mtusi2026lab5
Выполнение ЛР5 на 5 (трек 2)
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
"""Helpers for student tests: synthetic images (stdlib only), operator CLI and compose control."""
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Failure, check # noqa: E402
|
||||
from image_probe import jpeg_size # noqa: E402
|
||||
|
||||
FIXTURES = Path(__file__).resolve().parent / "fixtures"
|
||||
SAMPLE_PNG = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
|
||||
|
||||
def fixture(name):
|
||||
return (FIXTURES / name).read_bytes()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- images
|
||||
def _chunk(kind, data):
|
||||
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def png(width, height, row=None):
|
||||
blank = bytes(width * 3)
|
||||
comp = zlib.compressobj(6)
|
||||
parts = [comp.compress(b"\x00" + (row(y) if row else blank)) for y in range(height)]
|
||||
parts.append(comp.flush())
|
||||
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
||||
return b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", ihdr) + _chunk(b"IDAT", b"".join(parts)) + _chunk(b"IEND", b"")
|
||||
|
||||
|
||||
def png_header_only(width, height):
|
||||
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
||||
return b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", ihdr) + _chunk(b"IDAT", zlib.compress(bytes(1000))) + _chunk(b"IEND", b"")
|
||||
|
||||
|
||||
def corrupt_png(width=64, height=64):
|
||||
"""Valid signature, IHDR and chunk CRCs (passes the synchronous API checks), but IDAT is not zlib:
|
||||
only a full decode in the worker can detect it -> failed / invalid_image, without retries."""
|
||||
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
||||
garbage = bytes((i * 37 + 11) % 256 for i in range(4000))
|
||||
return b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", ihdr) + _chunk(b"IDAT", garbage) + _chunk(b"IEND", b"")
|
||||
|
||||
|
||||
def webp_size(data):
|
||||
check(data[:4] == b"RIFF" and data[8:12] == b"WEBP", "not a WebP RIFF container")
|
||||
kind = data[12:16]
|
||||
if kind == b"VP8X":
|
||||
w = 1 + int.from_bytes(data[24:27], "little")
|
||||
h = 1 + int.from_bytes(data[27:30], "little")
|
||||
return w, h
|
||||
if kind == b"VP8 ":
|
||||
w, h = struct.unpack("<HH", data[26:30])
|
||||
return w & 0x3FFF, h & 0x3FFF
|
||||
if kind == b"VP8L":
|
||||
b = int.from_bytes(data[21:25], "little")
|
||||
return (b & 0x3FFF) + 1, ((b >> 14) & 0x3FFF) + 1
|
||||
raise Failure("unknown WebP chunk")
|
||||
|
||||
|
||||
def has_exif(data):
|
||||
return b"Exif\x00\x00" in data[:65536]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- compose / operator CLI
|
||||
def dc(*args, check_rc=True, timeout=180, input=None, env=None):
|
||||
full_env = dict(os.environ, **env) if env else None
|
||||
out = subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, text=True, timeout=timeout,
|
||||
input=input, env=full_env)
|
||||
if check_rc and out.returncode != 0:
|
||||
raise Failure("docker compose %s failed: %s" % (" ".join(args[:2]), out.stderr.strip()[-400:]))
|
||||
return out
|
||||
|
||||
|
||||
def ops(*args, check_rc=True):
|
||||
return dc("exec", "-T", "ops", "python", "-m", "app.ops", *args, check_rc=check_rc)
|
||||
|
||||
|
||||
def inspect(att_id):
|
||||
return json.loads(ops("inspect", att_id).stdout)
|
||||
|
||||
|
||||
def stats():
|
||||
return json.loads(ops("stats").stdout)
|
||||
|
||||
|
||||
def arm(point, action, attachment=None, count=1):
|
||||
args = ["fault", "arm", point, action, "--count", str(count)]
|
||||
if attachment:
|
||||
args += ["--attachment", attachment]
|
||||
ops(*args)
|
||||
|
||||
|
||||
def pixel(data, x, y):
|
||||
"""Decode one pixel of an image with Pillow inside the ops container (the host has only the stdlib)."""
|
||||
code = ("import sys,io;from PIL import Image;im=Image.open(io.BytesIO(sys.stdin.buffer.read())).convert('RGB');"
|
||||
"print(*im.getpixel((%d,%d)))" % (x, y))
|
||||
out = subprocess.run(["docker", "compose", "exec", "-T", "ops", "python", "-c", code], cwd=ROOT,
|
||||
input=data, capture_output=True, timeout=60)
|
||||
check(out.returncode == 0, "pixel probe failed")
|
||||
return tuple(int(v) for v in out.stdout.split())
|
||||
|
||||
|
||||
def wait_for(predicate, timeout, what, interval=0.5):
|
||||
deadline = time.monotonic() + timeout
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
last = predicate()
|
||||
if last:
|
||||
return last
|
||||
time.sleep(interval)
|
||||
raise Failure("timeout waiting for " + what)
|
||||
Reference in New Issue
Block a user