ЛР 4 - БПИ2302 - Щербацевич - на 4

This commit is contained in:
2026-11-13 00:14:23 +03:00
parent ec540a1a72
commit 18f3986cc6
68 changed files with 5246 additions and 14 deletions
+101
View File
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""Orphan object after S3 PUT without DB row is removed after the grace period."""
import json
import os
import subprocess
import sys
import time
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check, run
EVIDENCE = ROOT / "evidence"
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
BUCKET = os.environ.get("S3_BUCKET", "messenger-attachments")
def compose(*args, timeout=120):
proc = subprocess.run(
["docker", "compose", "--env-file", ".env", *args],
cwd=str(ROOT),
capture_output=True,
text=True,
timeout=timeout,
encoding="utf-8",
errors="replace",
)
if proc.returncode != 0:
raise Failure("docker compose failed: " + (proc.stderr or proc.stdout)[-400:])
return proc.stdout
def main():
client = Client(BASE, timeout=20)
alice, _bob, _eve, chat = client.bootstrap()
att, _content = client.attachment(alice, chat)
orphan = "att/orphan-%s" % uuid.uuid4().hex
script = """
set -eu
ROOT_USER=$(cat /run/secrets/minio_root_user)
ROOT_PASS=$(cat /run/secrets/minio_root_password)
mc alias set local http://minio:9000 "$ROOT_USER" "$ROOT_PASS" >/dev/null
printf 'orphan' | mc pipe local/%s/%s
mc ls local/%s/%s >/dev/null
""" % (BUCKET, orphan, BUCKET, orphan)
compose("run", "--rm", "--no-deps", "--entrypoint", "/bin/sh", "minio-init", "-c", script)
time.sleep(2)
compose(
"run",
"--rm",
"--no-deps",
"-e",
"APP_ROLE=gc",
"-e",
"GC_ONCE=1",
"-e",
"ATTACHMENT_GC_GRACE_SECONDS=1",
"gc",
timeout=180,
)
missing = compose(
"run",
"--rm",
"--no-deps",
"--entrypoint",
"/bin/sh",
"minio-init",
"-c",
"""
set -eu
ROOT_USER=$(cat /run/secrets/minio_root_user)
ROOT_PASS=$(cat /run/secrets/minio_root_password)
mc alias set local http://minio:9000 "$ROOT_USER" "$ROOT_PASS" >/dev/null
if mc ls local/%s/%s >/dev/null 2>&1; then exit 7; fi
"""
% (BUCKET, orphan),
)
del missing
client.call("GET", "/api/v1/attachments/%s" % att["id"], actor=alice, schema="Attachment")
EVIDENCE.mkdir(exist_ok=True)
(EVIDENCE / "gc_orphans.json").write_text(
json.dumps(
{
"date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"orphan_removed": True,
"live_attachment_kept": att["id"],
"grace_seconds": 1,
},
indent=2,
)
+ "\n",
encoding="utf-8",
)
print("PASS: orphan object removed, live attachment kept")
if __name__ == "__main__":
run(main)