Files
mtusi2026lab4-fork/student-tests/orphan_sweep_check.py
T
2026-11-01 01:18:40 +03:00

41 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Docker-stack check of the orphan sweeper: fresh survives, old orphan removed, referenced object never removed.
Run on the local synthetic compose stack: python3 student-tests/orphan_sweep_check.py"""
import subprocess, sys, time
from pathlib import Path
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / "tests")]
from client import Client, check, run
def compose(container: str, *cmd: str) -> str:
return subprocess.check_output(
["docker", "compose", "run", "--rm", "--no-deps", "-T", container, *cmd],
text=True,
)
def main():
c = Client(timeout=30)
alice, bob, eve, chat = c.bootstrap()
att, content = c.attachment(alice, chat)
# injector → api (app-креды, есть PutObject)
key = compose("api", "python", "scripts/s3_fault_inject.py").strip().splitlines()[-1]
# sweeper → s3_sweeper (sweeper-креды, есть ListBucket)
out = compose("s3_sweeper", "python", "scripts/s3_sweep.py", "--apply", "--grace-seconds", "3600")
check("deleted=0" in out, "fresh orphan was deleted inside the grace period: " + out)
time.sleep(6)
out = compose("s3_sweeper", "python", "scripts/s3_sweep.py", "--grace-seconds", "5")
check("WOULD-DELETE " + key in out and "deleted=0" in out, "dry-run must report the orphan and delete nothing: " + out)
out = compose("s3_sweeper", "python", "scripts/s3_sweep.py", "--apply", "--grace-seconds", "5")
check("DELETE " + key in out, "orphan not deleted: " + out)
check(c.call("GET", f"/api/v1/attachments/{att['id']}/content", actor=bob)["bytes"] == content, "referenced attachment damaged by sweeper")
out = compose("s3_sweeper", "python", "scripts/s3_sweep.py", "--apply", "--grace-seconds", "5")
check("orphans=0" in out, "second run must be a no-op: " + out)
print("PASS: grace period, dry-run, orphan removal, referenced object preserved, idempotent re-run")
if __name__ == "__main__":
run(main)