91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
import argparse
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import zlib
|
|
|
|
from PIL import Image
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client
|
|
from scripts import lab
|
|
|
|
|
|
def arguments():
|
|
return argparse.Namespace(
|
|
project=os.environ.get("LAB4_PROJECT", "mtusi-lab4"),
|
|
port=int(os.environ.get("LAB4_PORT", "8080")),
|
|
ttl=int(os.environ.get("LAB4_TTL", "3600")), test_port=18082,
|
|
)
|
|
|
|
|
|
def sql(statement):
|
|
args = arguments()
|
|
return lab.compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
|
|
"-v", "ON_ERROR_STOP=1", "-At", "-c", statement, quiet=True).strip()
|
|
|
|
|
|
def s3(code, admin=True):
|
|
args = arguments()
|
|
prelude = "import json; from src.objects import Objects; o=Objects(admin=" + str(admin) + "); c=o.client; b=o.bucket; "
|
|
if admin:
|
|
output = lab.compose(args, "run", "--rm", "--no-deps", "-T", "bootstrap",
|
|
"python", "-c", prelude + code, quiet=True)
|
|
else:
|
|
output = lab.compose(args, "exec", "-T", "api1", "python", "-c", prelude + code, quiet=True)
|
|
return json.loads(output.strip().splitlines()[-1])
|
|
|
|
|
|
def object_exists(key):
|
|
return s3("print(json.dumps(any(x['Key']==" + repr(key)
|
|
+ " for x in c.list_objects_v2(Bucket=b,Prefix=" + repr(key) + ").get('Contents',[]))))")
|
|
|
|
|
|
def wait(predicate, seconds=30):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
if predicate():
|
|
return
|
|
time.sleep(0.1)
|
|
raise AssertionError("Test condition timed out")
|
|
|
|
|
|
def image_bytes(kind="PNG", size=(32, 24), mode="RGB"):
|
|
with Image.new(mode, size, (35, 120, 200, 255) if mode == "RGBA" else (35, 120, 200)) as image:
|
|
output = io.BytesIO()
|
|
image.save(output, format=kind)
|
|
return output.getvalue()
|
|
|
|
|
|
def chunk(kind, data):
|
|
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xffffffff)
|
|
|
|
|
|
def padded_png(size):
|
|
base = image_bytes()
|
|
padding = b"p" * (size - len(base) - 12)
|
|
return base[:-12] + chunk(b"raNd", padding) + base[-12:]
|
|
|
|
|
|
def oversized_pixels():
|
|
header = struct.pack(">IIBBBBB", 8000, 5001, 8, 2, 0, 0, 0)
|
|
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IEND", b"")
|
|
|
|
|
|
def cleanup_once(expected=True):
|
|
args = arguments()
|
|
command = ["docker", "compose", "--project-name", args.project, "--env-file", ".env",
|
|
"run", "--rm", "--no-deps", "-T", "cleanup", "python", "-m", "src.cleanup", "--once"]
|
|
result = subprocess.run(command, cwd=ROOT, env=lab.environment(args), capture_output=True, text=True, encoding="utf-8")
|
|
if (result.returncode == 0) != expected:
|
|
raise AssertionError("Unexpected cleanup exit status")
|
|
return json.loads(result.stdout.strip().splitlines()[-1]) if expected else None
|