forked from mtusi2026/mtusi2026lab4
all for lab4
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from client import Client
|
||||
|
||||
|
||||
def check(condition, message):
|
||||
if not condition:
|
||||
raise RuntimeError(message)
|
||||
|
||||
|
||||
def docker_script(script, *arguments, input_bytes=None):
|
||||
command = [
|
||||
"docker", "compose", "exec", "-T",
|
||||
"minio",
|
||||
"sh", "-ec", script,
|
||||
"cleanup-check",
|
||||
*arguments,
|
||||
]
|
||||
|
||||
result = subprocess.run(
|
||||
command,
|
||||
input=input_bytes,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(
|
||||
"Команда MinIO завершилась ошибкой. "
|
||||
"Проверь доступность minio и название bucket."
|
||||
)
|
||||
|
||||
return result.stdout.decode("utf-8")
|
||||
|
||||
|
||||
def configure_minio():
|
||||
docker_script(
|
||||
'''
|
||||
mc alias set cleanupcheck http://localhost:9000 \
|
||||
"$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null
|
||||
'''
|
||||
)
|
||||
|
||||
|
||||
def put_orphan(bucket, key):
|
||||
docker_script(
|
||||
'mc pipe "cleanupcheck/$1/$2" >/dev/null',
|
||||
bucket,
|
||||
key,
|
||||
input_bytes=b"synthetic cleanup test object\n",
|
||||
)
|
||||
|
||||
|
||||
def list_test_objects(bucket, prefix):
|
||||
output = docker_script(
|
||||
'mc ls --json "cleanupcheck/$1/$2"',
|
||||
bucket,
|
||||
prefix,
|
||||
)
|
||||
|
||||
names = set()
|
||||
|
||||
for line in output.splitlines():
|
||||
if not line.strip():
|
||||
continue
|
||||
|
||||
item = json.loads(line)
|
||||
|
||||
check(
|
||||
item.get("status") != "error",
|
||||
"MinIO вернул ошибку при получении списка объектов",
|
||||
)
|
||||
|
||||
key = item.get("key")
|
||||
|
||||
if key:
|
||||
names.add(key.rstrip("/").split("/")[-1])
|
||||
|
||||
return names
|
||||
|
||||
|
||||
def remove_test_object(bucket, key):
|
||||
docker_script(
|
||||
'mc rm --force "cleanupcheck/$1/$2" >/dev/null',
|
||||
bucket,
|
||||
key,
|
||||
)
|
||||
|
||||
|
||||
def check_download(client, actor, attachment, original):
|
||||
result = client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
actor=actor,
|
||||
)
|
||||
|
||||
check(
|
||||
result["bytes"] == original,
|
||||
"Корректное вложение потеряно или изменилось",
|
||||
)
|
||||
|
||||
check(
|
||||
hashlib.sha256(result["bytes"]).hexdigest()
|
||||
== attachment["sha256"],
|
||||
"SHA-256 корректного вложения не совпадает",
|
||||
)
|
||||
|
||||
|
||||
def wait_until(deadline):
|
||||
remaining = deadline - time.monotonic()
|
||||
|
||||
if remaining > 0:
|
||||
time.sleep(remaining)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--base-url",
|
||||
default="http://localhost:8080",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--bucket",
|
||||
default="messenger-attachments",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
check(
|
||||
re.fullmatch(r"[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]", args.bucket),
|
||||
"Некорректное название bucket",
|
||||
)
|
||||
|
||||
client = Client(args.base_url, None)
|
||||
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
|
||||
# Создаётся через API: в S3 и PostgreSQL.
|
||||
attachment, original = client.attachment(alice, chat)
|
||||
|
||||
check_download(client, bob, attachment, original)
|
||||
|
||||
configure_minio()
|
||||
|
||||
prefix = "originals/cleanup-test-" + uuid.uuid4().hex + "/"
|
||||
old_key = prefix + "old.bin"
|
||||
fresh_key = prefix + "fresh.bin"
|
||||
|
||||
try:
|
||||
put_orphan(args.bucket, old_key)
|
||||
old_created = time.monotonic()
|
||||
|
||||
names = list_test_objects(args.bucket, prefix)
|
||||
|
||||
check(
|
||||
"old.bin" in names,
|
||||
"Тестовый объект не появился в S3",
|
||||
)
|
||||
|
||||
print("Created API attachment and an untracked S3 object", flush=True)
|
||||
|
||||
wait_until(old_created + 25)
|
||||
|
||||
names = list_test_objects(args.bucket, prefix)
|
||||
|
||||
check(
|
||||
"old.bin" in names,
|
||||
"Объект удалён раньше grace period",
|
||||
)
|
||||
|
||||
print("PASS: object younger than grace period is preserved", flush=True)
|
||||
|
||||
wait_until(old_created + 65)
|
||||
|
||||
put_orphan(args.bucket, fresh_key)
|
||||
fresh_created = time.monotonic()
|
||||
|
||||
print("Waiting for cleanup of the old object...", flush=True)
|
||||
|
||||
deadline = fresh_created + 30
|
||||
|
||||
while True:
|
||||
names = list_test_objects(args.bucket, prefix)
|
||||
|
||||
check(
|
||||
"fresh.bin" in names,
|
||||
"Свежий объект удалён раньше grace period",
|
||||
)
|
||||
|
||||
if "old.bin" not in names:
|
||||
break
|
||||
|
||||
check(
|
||||
time.monotonic() < deadline,
|
||||
"Старый объект не удалён. Проверь настройки и логи очистки",
|
||||
)
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
print("PASS: old object without metadata is deleted", flush=True)
|
||||
print("PASS: fresh object without metadata is preserved", flush=True)
|
||||
|
||||
client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + attachment["id"],
|
||||
actor=bob,
|
||||
schema="Attachment",
|
||||
)
|
||||
|
||||
check_download(client, bob, attachment, original)
|
||||
|
||||
print("PASS: attachment with metadata and its bytes are preserved", flush=True)
|
||||
print("PASS: cleanup scenario completed", flush=True)
|
||||
|
||||
finally:
|
||||
|
||||
for key in (old_key, fresh_key):
|
||||
try:
|
||||
remove_test_object(args.bucket, key)
|
||||
except Exception as error:
|
||||
print(
|
||||
f"WARNING: final cleanup failed for {key}: {error}",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as error:
|
||||
print("FAIL:", error, flush=True)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user