forked from mtusi2026/mtusi2026lab4
127 lines
5.8 KiB
Python
127 lines
5.8 KiB
Python
"""Inject faults on both sides, dry-run, apply, and repeat. Fresh and in-use rows survive."""
|
|
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, check # noqa: E402
|
|
|
|
FIXTURE = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
|
|
|
|
|
def admin(*args) -> dict:
|
|
raw = subprocess.check_output(
|
|
["docker", "exec", "mtusi-api-1", "python", "-m", "app.admin", *args],
|
|
text=True,
|
|
)
|
|
return json.loads(raw)
|
|
|
|
|
|
def redact(report: dict) -> dict:
|
|
cloned = json.loads(json.dumps(report))
|
|
for action in cloned.get("actions", []):
|
|
action.pop("object_key", None)
|
|
return cloned
|
|
|
|
|
|
def main() -> None:
|
|
client = Client("http://127.0.0.1:8080", timeout=30)
|
|
alice = client.user("rec_alice")
|
|
bob = client.user("rec_bob")
|
|
chat = client.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "Сверка", "member_ids": [alice["id"], bob["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
fresh = client.call(
|
|
"POST",
|
|
f"/api/v1/chats/{chat['id']}/attachments?filename=kept.png",
|
|
201,
|
|
actor=alice,
|
|
raw=FIXTURE,
|
|
headers={"Content-Type": "image/png"},
|
|
schema="Attachment",
|
|
)["json"]
|
|
client.message(alice, chat, "", attachments=[fresh["id"]])
|
|
loose = client.call(
|
|
"POST",
|
|
f"/api/v1/chats/{chat['id']}/attachments?filename=loose.png",
|
|
201,
|
|
actor=alice,
|
|
raw=FIXTURE,
|
|
headers={"Content-Type": "image/png"},
|
|
schema="Attachment",
|
|
)["json"]
|
|
faults = admin("inject", "--chat-id", chat["id"], "--owner-id", alice["id"])
|
|
dry = admin("reconcile", "--dry-run")
|
|
notes = {item.get("note") for item in dry["actions"]}
|
|
check("old-orphan" in notes, "dry-run missed the old orphan")
|
|
check("fault-stale-missing" in notes, "dry-run missed stale metadata")
|
|
check("fresh-orphan" not in notes, "dry-run wanted to delete a fresh orphan")
|
|
check("fault-in-use-missing" not in notes, "dry-run wanted to delete an in-use attachment")
|
|
check(dry["skipped"].get("fresh_orphan", 0) >= 1, "fresh orphan was not skipped")
|
|
check(dry["skipped"].get("in_use_missing", 0) >= 1, "in-use missing attachment was not skipped")
|
|
applied = admin("reconcile", "--apply")
|
|
again = admin("reconcile", "--dry-run")
|
|
check(again["action_count"] == 0, "second dry-run was not a no-op")
|
|
old_object = admin("probe", "--object-key", faults["old_orphan"]["object_key"])
|
|
fresh_object = admin("probe", "--object-key", faults["fresh_orphan"]["object_key"])
|
|
stale_meta = admin("probe", "--attachment-id", faults["stale_missing"]["attachment_id"])
|
|
used_meta = admin("probe", "--attachment-id", faults["in_use_missing"]["attachment_id"])
|
|
check(old_object["object"] is False, "old orphan object survived apply")
|
|
check(fresh_object["object"] is True, "fresh orphan object was deleted")
|
|
check(stale_meta["metadata"] is False, "stale unreferenced metadata survived apply")
|
|
check(used_meta["metadata"] is True, "in-use metadata was deleted")
|
|
downloaded = client.call("GET", "/api/v1/attachments/" + fresh["id"] + "/content", actor=bob)
|
|
check(downloaded["bytes"] == FIXTURE, "fresh in-use original changed")
|
|
check(hashlib.sha256(downloaded["bytes"]).hexdigest() == fresh["sha256"], "checksum changed")
|
|
client.call("GET", "/api/v1/attachments/" + loose["id"], actor=bob, schema="Attachment")
|
|
client.call("GET", "/api/v1/attachments/" + faults["in_use_missing"]["attachment_id"], actor=bob, schema="Attachment")
|
|
missing_bytes = client.call(
|
|
"GET",
|
|
"/api/v1/attachments/" + faults["in_use_missing"]["attachment_id"] + "/content",
|
|
404,
|
|
actor=bob,
|
|
)
|
|
check(missing_bytes["json"]["error"]["code"] == "not_found", "missing in-use bytes were not 404")
|
|
client.call("GET", "/api/v1/attachments/" + faults["stale_missing"]["attachment_id"], 404, actor=alice)
|
|
report = {
|
|
"hypothesis": "A full-scan reconciler with a 120s grace period removes only expired orphans and unreferenced metadata whose object is already gone.",
|
|
"method": "Inject a fresh orphan, a 2h orphan, a 2h missing unreferenced row, and a 2h missing row referenced by a message. Dry-run, apply, dry-run again.",
|
|
"dry_run": redact(dry),
|
|
"apply": redact(applied),
|
|
"second_dry_run": redact(again),
|
|
"survived": {
|
|
"fresh_orphan_object": True,
|
|
"in_use_metadata": True,
|
|
"fresh_message_attachment_sha256": fresh["sha256"],
|
|
"loose_unreferenced_attachment": loose["id"],
|
|
},
|
|
"limitations": [
|
|
"Walk cost is one full ListObjects plus one pass over attachment and ledger rows; this stand has one page.",
|
|
"Grace uses the ledger or metadata timestamp, not a backdated S3 LastModified, because MinIO does not let the lab rewrite LastModified.",
|
|
"In-use metadata whose bytes are gone is reported and kept; the reconciler does not invent replacement bytes.",
|
|
"A second dry-run being empty shows the repair is repeatable, not that a continuous watcher is running.",
|
|
],
|
|
}
|
|
dest = ROOT / "evidence" / "reconcile.json"
|
|
dest.parent.mkdir(exist_ok=True)
|
|
dest.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print(json.dumps({"action_count": dry["action_count"], "second": again["action_count"], "duration_ms": dry["duration_ms"], "objects_seen": dry["objects_seen"], "list_pages": dry["list_pages"]}, ensure_ascii=False))
|
|
print("PASS: reconciler dry-run/apply/repeat kept fresh and in-use attachments")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
print("FAIL:", exc)
|
|
raise SystemExit(1)
|