forked from mtusi2026/mtusi2026lab7
ЛР 7: Безопасность, mTLS и OpenBao
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exercise a queued upload across API/broker restarts and duplicate delivery."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
from urllib.parse import quote
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def compose(*args):
|
||||
command = [
|
||||
"docker", "compose", "--project-directory", str(ROOT),
|
||||
"--env-file", str(ROOT / ".env"), *args,
|
||||
]
|
||||
subprocess.run(
|
||||
command,
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=120,
|
||||
)
|
||||
|
||||
|
||||
def republish_completed_job(attachment_id):
|
||||
source = r"""
|
||||
import sys
|
||||
from datetime import timezone
|
||||
import psycopg
|
||||
from app import database_conninfo
|
||||
from queueing import publish_event
|
||||
|
||||
with psycopg.connect(database_conninfo()) as conn:
|
||||
job = conn.execute(
|
||||
"SELECT id, attachment_id, pipeline_version, attempt, created_at "
|
||||
"FROM image_jobs WHERE attachment_id = %s",
|
||||
(sys.argv[1],),
|
||||
).fetchone()
|
||||
if job is None:
|
||||
raise SystemExit("job not found")
|
||||
created_at = job[4].astimezone(timezone.utc)
|
||||
publish_event({
|
||||
"schema_version": 1,
|
||||
"job_id": str(job[0]),
|
||||
"type": "attachment.process.v1",
|
||||
"attachment_id": str(job[1]),
|
||||
"pipeline_version": job[2],
|
||||
"attempt": job[3],
|
||||
"created_at": created_at.isoformat(timespec="microseconds").replace("+00:00", "Z"),
|
||||
})
|
||||
"""
|
||||
command = [
|
||||
"docker", "compose", "--project-directory", str(ROOT),
|
||||
"--env-file", str(ROOT / ".env"), "run", "--rm", "--no-deps",
|
||||
"api", "python", "-c", source, attachment_id,
|
||||
]
|
||||
subprocess.run(
|
||||
command,
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Verify queued job recovery and idempotent redelivery")
|
||||
client = Client(args.base_url, args.ca_file, timeout=10)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
content = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
||||
upload_path = (
|
||||
"/api/v1/chats/" + chat["id"]
|
||||
+ "/attachments?filename=" + quote("sample.png", safe="")
|
||||
)
|
||||
|
||||
compose("stop", "worker")
|
||||
try:
|
||||
uploaded = client.call(
|
||||
"POST",
|
||||
upload_path,
|
||||
202,
|
||||
actor=alice,
|
||||
raw=content,
|
||||
headers={"Content-Type": "image/png"},
|
||||
schema="Attachment",
|
||||
)["json"]
|
||||
check(uploaded["status"] == "queued", "stopped worker upload was not queued")
|
||||
client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + uploaded["id"] + "/content",
|
||||
409,
|
||||
actor=bob,
|
||||
)
|
||||
linked = client.message(
|
||||
alice, chat, "", attachments=[uploaded["id"]]
|
||||
)["json"]
|
||||
check(linked["attachment_ids"] == [uploaded["id"]],
|
||||
"queued attachment could not be linked to a message")
|
||||
|
||||
compose("restart", "db", "s3", "broker")
|
||||
compose("up", "-d", "--wait", "db", "s3", "broker")
|
||||
compose("run", "--rm", "s3-init")
|
||||
compose("restart", "api")
|
||||
compose("up", "-d", "--wait", "--scale", "api=2", "api", "lb")
|
||||
compose("start", "worker")
|
||||
|
||||
ready = client.wait_ready(alice, uploaded, wait_seconds=60)
|
||||
check(ready["status"] == "ready", "queued work did not recover to ready")
|
||||
original = client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + uploaded["id"] + "/content",
|
||||
actor=bob,
|
||||
)
|
||||
check(original["bytes"] == content, "recovered original bytes changed")
|
||||
thumbnail = client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + uploaded["id"] + "/content?variant=thumbnail",
|
||||
actor=bob,
|
||||
)
|
||||
variant = ready["variants"][0]
|
||||
check(
|
||||
hashlib.sha256(thumbnail["bytes"]).hexdigest() == variant["sha256"],
|
||||
"thumbnail changed after recovery",
|
||||
)
|
||||
|
||||
republish_completed_job(uploaded["id"])
|
||||
time.sleep(1)
|
||||
after_duplicate = client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + uploaded["id"],
|
||||
actor=bob,
|
||||
schema="Attachment",
|
||||
)["json"]
|
||||
check(after_duplicate["status"] == "ready",
|
||||
"duplicate delivery changed a completed attachment state")
|
||||
check(len(after_duplicate["variants"]) == 1,
|
||||
"duplicate delivery created duplicate variants")
|
||||
print("PASS: queued upload survived API/DB/S3/broker restart; duplicate was idempotent")
|
||||
finally:
|
||||
compose("up", "-d", "--scale", "api=2", "api", "worker", "lb")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user