from concurrent.futures import ThreadPoolExecutor from pathlib import Path from random import Random import struct import sys from threading import Barrier, Event, Thread import time from urllib.error import HTTPError from urllib.parse import quote from urllib.request import urlopen import uuid import zlib from check_replicas import compose, wait_instances, REPLICAS from client import Client, Failure, check ROOT = Path(__file__).resolve().parents[1] def png(width, height, textured=False): def chunk(kind, data): return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) random = Random(2026) scanlines = b"".join( b"\x00" + (random.randbytes(width * 3) if textured else bytes(width * 3)) for _ in range(min(height, 512)) ) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(scanlines)) + chunk(b"IEND", b"")) def upload(client, owner, chat, data, expected=201, name="image.png", mime="image/png"): path = f"/api/v1/chats/{chat['id']}/attachments?filename={quote(name, safe='')}" return client.call("POST", path, expected, actor=owner, raw=data, headers={"Content-Type": mime}, schema="Attachment" if expected == 201 else None) def memory(): return {name: int(compose("exec", "-T", name, "cat", "/sys/fs/cgroup/memory.current")) for name in REPLICAS} def run_concurrent(owner, chat, data): ready = Barrier(10) stop = Event() baseline = memory() peak = dict(baseline) def sample(): while not stop.is_set(): for name, value in memory().items(): peak[name] = max(peak[name], value) stop.wait(0.1) def send(_): ready.wait(timeout=10) return upload(Client(timeout=15), owner, chat, data)["json"]["id"] sampler = Thread(target=sample) sampler.start() try: with ThreadPoolExecutor(max_workers=10) as pool: ids = list(pool.map(send, range(10))) finally: stop.set() sampler.join() check(len(set(ids)) == 10, "uploads returned duplicate IDs") print(f"PASS: 10 concurrent uploads of {len(data)} bytes; " f"cgroup memory baseline={baseline}, sampled_peak={peak}", flush=True) def main(): compose("start", *REPLICAS) wait_instances(set(REPLICAS)) client = Client(timeout=6) owner, member, outsider, chat = client.bootstrap() data = png(512, 512) sent = upload(client, owner, chat, data) attachment = sent["json"] serving = sent["headers"]["X-Instance-Id"] path = f"/api/v1/attachments/{attachment['id']}" upload(client, owner, chat, data, 400, name="../image.png") upload(client, owner, chat, data, 415, mime="image/jpeg") upload(client, owner, chat, b"broken image", 415) upload(client, owner, chat, b"\0" * (10 * 1024**2 + 1), 413) upload(client, owner, chat, png(10000, 4001), 413) client.call("GET", path, 404, actor=outsider) client.call("GET", path + "/content", 404, actor=outsider) other = client.call("POST", "/api/v1/chats", 201, actor=owner, body={ "title": "Other chat", "member_ids": [owner["id"]], }, schema="Chat")["json"] wrong_path = f"/api/v1/chats/{other['id']}/messages" client.call("POST", wrong_path, 404, actor=owner, body={"text": "", "attachment_ids": [attachment["id"]]}, headers={"Idempotency-Key": uuid.uuid4().hex}) print("PASS: size, pixels, malformed image, MIME, filename and cross-chat access", flush=True) compose("stop", serving) try: survivor = next(name for name in REPLICAS if name != serving) deadline = time.monotonic() + 10 while True: try: result = client.call("GET", path + "/content", actor=member) break except Failure: if time.monotonic() >= deadline: raise time.sleep(0.2) check(result["bytes"] == data and result["headers"]["X-Instance-Id"] == survivor, "original unavailable on surviving replica") finally: compose("start", serving) wait_instances(set(REPLICAS)) print(f"PASS: original bytes read through {survivor} after {serving} stopped", flush=True) compose("exec", "-T", "api-a", "python", "-c", "import os,urllib.error,urllib.request,sys;" "url=os.environ['S3_ENDPOINT']+'/'+os.environ['S3_BUCKET']+'/'+sys.argv[1];" "exec('try:\\n urllib.request.urlopen(url,timeout=3)\\nexcept urllib.error.HTTPError as e:" "\\n assert e.code == 403\\nelse:\\n raise AssertionError()')", f"attachments/{attachment['id']}") print("PASS: anonymous direct access to S3 object returned 403", flush=True) run_concurrent(owner, chat, png(512, 512, textured=True)) compose("stop", "s3") try: status = None deadline = time.monotonic() + 10 while time.monotonic() < deadline: try: with urlopen(client.base + "/health/ready", timeout=2): pass except HTTPError as error: if error.code == 503: status = error.code break except OSError: pass time.sleep(0.1) check(status == 503, "API did not report S3 failure") finally: compose("start", "s3") wait_instances(set(REPLICAS)) check(client.call("GET", path + "/content", actor=owner)["bytes"] == data, "original lost after restarting S3") print("PASS: readiness returned 503 during S3 outage, original survived restart") if __name__ == "__main__": main()