chore(migrate) moved codebase from lab4

This commit is contained in:
Mikhail Verkovykh
2026-11-14 23:17:36 +03:00
parent 276dbdad8a
commit 28d68dc30b
123 changed files with 11677 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
#!/usr/bin/env python3
"""Two-step attachment durability probe used by verify-attachment-failover.sh.
Step 1 (`upload`) uploads a fixture and writes a short-lived state file. The
caller then stops the replica named in `instance_id`. Step 2 (`download`) uses
the same session token through the load balancer, retries transient errors and
checks that another replica returns the exact original bytes.
The state file contains a test session token. It lives in a private temporary
directory and is never copied into evidence.
"""
import argparse
import hashlib
import json
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402
def upload(args):
c = Client(args.base_url, args.ca_file)
alice, _, _, chat = c.bootstrap()
content = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
response = c.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png",
201,
actor=alice,
raw=content,
headers={"Content-Type": "image/png"},
schema="Attachment",
)
attachment = response["json"]
check(attachment["status"] == "ready", "upload is not ready")
check(
attachment["sha256"] == hashlib.sha256(content).hexdigest(),
"upload checksum mismatch",
)
state = {
"attachment_id": attachment["id"],
"sha256": attachment["sha256"],
"size_bytes": attachment["size_bytes"],
"instance_id": response["headers"].get("X-Instance-Id"),
"token": alice["token"],
"chat_id": chat["id"],
"content_length": len(content),
}
check(bool(state["instance_id"]), "upload response has no instance id")
args.state.write_text(json.dumps(state))
print(
json.dumps(
{
"uploaded_by": state["instance_id"],
"attachment_id": state["attachment_id"],
"size_bytes": state["size_bytes"],
}
)
)
def download(args):
state = json.loads(args.state.read_text())
expected_sha = state["sha256"]
expected_size = state["size_bytes"]
actor = {"token": state["token"]}
c = Client(args.base_url, args.ca_file)
deadline = time.monotonic() + args.timeout
attempts = 0
errors = []
while time.monotonic() < deadline:
attempts += 1
try:
response = c.call(
"GET",
f"/api/v1/attachments/{state['attachment_id']}/content",
actor=actor,
)
except Failure as error:
errors.append(str(error))
time.sleep(0.5)
continue
served_by = response["headers"].get("X-Instance-Id")
check(served_by is not None, "download response has no instance id")
check(served_by != state["instance_id"], "download was served by the stopped replica")
check(
len(response["bytes"]) == expected_size,
f"download size {len(response['bytes'])} != {expected_size}",
)
check(
hashlib.sha256(response["bytes"]).hexdigest() == expected_sha,
"download checksum differs from the uploaded original",
)
print(
json.dumps(
{
"downloaded_by": served_by,
"stopped_replica": state["instance_id"],
"attempts": attempts,
"transient_errors": len(errors),
"size_bytes": len(response["bytes"]),
"sha256": expected_sha,
}
)
)
return
raise Failure(
f"download did not recover within {args.timeout}s after {attempts} attempts: "
+ "; ".join(errors[-3:])
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--action", choices=("upload", "download"), required=True)
parser.add_argument("--state", type=Path, required=True)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
parser.add_argument("--timeout", type=float, default=30)
args = parser.parse_args()
if args.action == "upload":
upload(args)
else:
download(args)
if __name__ == "__main__":
main()
+168
View File
@@ -0,0 +1,168 @@
#!/usr/bin/env python3
"""Grade-4 negative checks for the attachment path.
The public smoke covers the happy path. This test adds the failure cases the
assignment requires: an empty body, an oversized body, a corrupted image, a
MIME spoof, a foreign attachment id and an attachment that belongs to another
chat. Every client mistake must remain a 4xx response with the contract error
envelope, not a 500.
"""
import argparse
import hashlib
import sys
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
FIXTURE = ROOT / "tests" / "fixtures" / "sample.png"
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
def upload(client, actor, chat_id, raw, filename, content_type):
headers = {} if content_type is None else {"Content-Type": content_type}
return client.call(
"POST",
f"/api/v1/chats/{chat_id}/attachments?filename={filename}",
expected=(201, 400, 404, 413, 415),
actor=actor,
raw=raw,
headers=headers,
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
args = parser.parse_args()
c = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = c.bootstrap()
png = FIXTURE.read_bytes()
# Happy path first, so the negative checks can refer to a real attachment.
good = c.attachment(alice, chat)[0]
check(good["status"] == "ready", "valid upload is not ready")
check(good["error_code"] is None, "ready attachment carries an error")
# 1. One byte over the contract limit must be a 413, decided before decode.
oversized = b"\x00" * (MAX_UPLOAD_BYTES + 1)
response = upload(c, alice, chat["id"], oversized, "big.png", "image/png")
check(response["status"] == 413, "oversized upload did not return 413")
# 2. A file with a valid PNG signature but a broken body must be a 415.
corrupt = png[:8] + b"\x00" * 64
response = upload(c, alice, chat["id"], corrupt, "broken.png", "image/png")
check(response["status"] == 415, "corrupted image did not return 415")
# 3. The declared type disagrees with the actual PNG bytes: MIME spoof.
response = upload(c, alice, chat["id"], png, "spoof.jpg", "image/jpeg")
check(response["status"] == 415, "MIME spoof did not return 415")
# 4. A non-image content type is rejected without touching the decoder.
response = upload(c, alice, chat["id"], b"hello", "file.txt", "text/plain")
check(response["status"] == 415, "non-image Content-Type did not return 415")
# 4b. An empty body is rejected before validation and leaves no metadata.
response = upload(c, alice, chat["id"], b"", "empty.png", "image/png")
check(response["status"] == 415, "empty upload did not return 415")
# 5. Path separators in the display name are rejected before I/O.
for bad_name in ("../escape.png", "dir/photo.png", "back\\slash.png"):
response = upload(c, alice, chat["id"], png, bad_name, "image/png")
check(response["status"] == 400, f"filename {bad_name!r} did not return 400")
# 6. Eve is not a chat member: both metadata and bytes are hidden as 404.
for suffix in ("", "/content"):
response = c.call(
"GET", f"/api/v1/attachments/{good['id']}{suffix}", expected=404, actor=eve
)
check(
response["json"]["error"]["code"] == "not_found",
"foreign attachment error code mismatch",
)
# 7. An attachment of another chat cannot be linked to this chat.
other = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
other_attachment = c.attachment(alice, other)[0]
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
404,
actor=alice,
body={"text": "wrong chat", "attachment_ids": [other_attachment["id"]]},
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{other['id']}/messages",
404,
actor=alice,
body={"text": "wrong chat", "attachment_ids": [good["id"]]},
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
)
# 8. Unknown ids, duplicates and an oversized list are cheap 400/404s.
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
404,
actor=alice,
body={"text": "unknown", "attachment_ids": [str(uuid.uuid4())]},
headers={"Idempotency-Key": "unknown-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body={"text": "duplicate", "attachment_ids": [good["id"], good["id"]]},
headers={"Idempotency-Key": "duplicate-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body={"text": "many", "attachment_ids": [str(uuid.uuid4())] * 11},
headers={"Idempotency-Key": "many-" + uuid.uuid4().hex},
)
# 9. Uploading into a chat the caller does not belong to is a 404, and the
# response body must not become a 500 even though authorization wins
# before the body is read.
response = upload(c, eve, chat["id"], png, "foreign.png", "image/png")
check(response["status"] == 404, "foreign-chat upload did not return 404")
# 10. A valid image attached to a message is visible to the other member
# with the exact original checksum, independently of the uploader.
message = c.message(alice, chat, "", attachments=[good["id"]])["json"]
check(message["attachment_ids"] == [good["id"]], "message lost attachment id")
download = c.call(
"GET", f"/api/v1/attachments/{good['id']}/content", actor=bob
)
check(download["bytes"] == png, "member download differs from original")
check(
hashlib.sha256(download["bytes"]).hexdigest() == good["sha256"],
"member download checksum mismatch",
)
print(
"PASS: graded negative cases (413/415/404/400), no 5xx, "
"cross-member download checksum matches"
)
if __name__ == "__main__":
main()
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
"""Upload ten or more large-but-valid PNGs at the same time.
The payload is generated in memory and shared read-only between threads, so the
client itself does not multiply the image. This keeps the measurement focused on
API memory: streaming temp files plus bounded image decoding.
"""
import argparse
import hashlib
import json
import os
import struct
import sys
import threading
import time
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
def chunk(kind, payload):
return (
struct.pack(">I", len(payload))
+ kind
+ payload
+ struct.pack(">I", zlib.crc32(kind + payload) & 0xFFFFFFFF)
)
def make_png(width, height):
compressor = zlib.compressobj(1)
rows = bytearray()
for _ in range(height):
rows += b"\x00" + os.urandom(width * 3)
body = compressor.compress(bytes(rows)) + compressor.flush()
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
+ chunk(b"IDAT", body)
+ chunk(b"IEND", b"")
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
parser.add_argument("--uploads", type=int, default=10)
parser.add_argument("--width", type=int, default=1800)
parser.add_argument("--height", type=int, default=1400)
args = parser.parse_args()
c = Client(args.base_url, args.ca_file)
alice, _, _, chat = c.bootstrap()
png = make_png(args.width, args.height)
digest = hashlib.sha256(png).hexdigest()
check(len(png) <= 10 * 1024 * 1024, "generated fixture exceeds the upload limit")
barrier = threading.Barrier(args.uploads)
results = []
errors = []
lock = threading.Lock()
def worker(index):
client = Client(args.base_url, args.ca_file)
try:
barrier.wait(timeout=30)
response = client.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename=concurrent-{index}.png",
201,
actor=alice,
raw=png,
headers={"Content-Type": "image/png"},
schema="Attachment",
)
with lock:
results.append(
{
"id": response["json"]["id"],
"instance": response["headers"].get("X-Instance-Id"),
}
)
except Exception as error: # noqa: BLE001 - report every worker failure
with lock:
errors.append(f"{type(error).__name__}: {error}")
threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.uploads)]
started = time.monotonic()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
elapsed = time.monotonic() - started
check(not errors, "concurrent uploads failed: " + "; ".join(errors[:3]))
check(len(results) == args.uploads, "not all concurrent uploads completed")
instances = sorted({item["instance"] for item in results if item["instance"]})
check(bool(instances), "no X-Instance-Id was observed")
# Read one uploaded object back and compare the exact bytes before reporting
# success; ten 201 responses alone are not enough.
download = c.call(
"GET", f"/api/v1/attachments/{results[0]['id']}/content", actor=alice
)
check(
hashlib.sha256(download["bytes"]).hexdigest() == digest,
"downloaded object differs from the generated original",
)
report = {
"uploads": args.uploads,
"width": args.width,
"height": args.height,
"image_bytes": len(png),
"sha256": digest,
"elapsed_seconds": round(elapsed, 3),
"instances": instances,
"download_verified": True,
}
print(json.dumps(report))
if __name__ == "__main__":
main()
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
# The public smoke checks sequential replay;
# this test checks the cross-replica race.
import argparse
import sys
import threading
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402
def main():
parser = argparse.ArgumentParser(
description="Concurrent idempotency check through the load balancer"
)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
parser.add_argument("--requests", type=int, default=12)
args = parser.parse_args()
setup = Client(args.base_url, args.ca_file)
alice, _, _, chat = setup.bootstrap()
key = "concurrent-" + uuid.uuid4().hex
body = {"text": "one concurrent message"}
path = f"/api/v1/chats/{chat['id']}/messages"
results = []
lock = threading.Lock()
def worker():
client = Client(args.base_url, args.ca_file)
try:
response = client.call(
"POST",
path,
201,
actor=alice,
body=body,
headers={"Idempotency-Key": key},
schema="Message",
)
with lock:
results.append(
(response["json"], response["headers"].get("X-Instance-Id"))
)
except Failure as error:
with lock:
results.append((error, None))
threads = [threading.Thread(target=worker) for _ in range(args.requests)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
check(len(results) == args.requests, "not every concurrent request finished")
check(
all(isinstance(message, dict) for message, _ in results),
"a concurrent request failed",
)
ids = {message["id"] for message, _ in results}
check(len(ids) == 1, "concurrent requests returned different message IDs")
instances = {instance for _, instance in results if instance}
check(
len(instances) >= 2, "expected the load balancer to use at least two replicas"
)
page = setup.call("GET", path + "?limit=100", actor=alice, schema="MessagePage")[
"json"
]
stored = [message for message in page["items"] if message["id"] == next(iter(ids))]
check(len(stored) == 1, "concurrent requests stored more than one message")
setup.call(
"POST",
path,
409,
actor=alice,
body={"text": "different body"},
headers={"Idempotency-Key": key},
)
print(
f"PASS: {args.requests} concurrent requests used {len(instances)} replicas and stored one message"
)
if __name__ == "__main__":
main()