forked from mtusi2026/mtusi2026lab7
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Track 1 proof of job durability. Host side, public HTTPS API only, uses the unchanged skeleton test client.
|
|
|
|
jobs_integrity.py submit --n 10 register users, upload N images while the worker is STOPPED, remember their ids in evidence/jobs-ids.json
|
|
jobs_integrity.py verify after the incident script restarted the worker: every attachment must become `ready` (lost = 0)
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
|
sys.path.insert(0, os.path.join(ROOT, "tests"))
|
|
from client import Client # noqa: E402
|
|
|
|
IDS = os.path.join(ROOT, "evidence", "jobs-ids.json")
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("mode", choices=["submit", "verify"])
|
|
ap.add_argument("--n", type=int, default=10)
|
|
ap.add_argument("--base-url", default="https://localhost:8443")
|
|
ap.add_argument("--ca-file", required=True)
|
|
ap.add_argument("--wait", type=int, default=180)
|
|
a = ap.parse_args()
|
|
c = Client(a.base_url, a.ca_file)
|
|
if a.mode == "submit":
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
ids = []
|
|
for _ in range(a.n):
|
|
att, _content = c.attachment(alice, chat) # 202 + status "queued" are asserted by the client
|
|
ids.append(att["id"])
|
|
os.makedirs(os.path.dirname(IDS), exist_ok=True)
|
|
json.dump({"ids": ids, "actor": {"id": alice["id"], "token": alice["token"]}}, open(IDS, "w"))
|
|
print(f"submitted {len(ids)} jobs; they stay queued while the worker is stopped")
|
|
return
|
|
data = json.load(open(IDS))
|
|
actor, pending, ready = data["actor"], list(data["ids"]), set()
|
|
deadline = time.monotonic() + a.wait
|
|
while pending and time.monotonic() < deadline:
|
|
for att_id in list(pending):
|
|
res = c.call("GET", "/api/v1/attachments/" + att_id, actor=actor, schema="Attachment")
|
|
if not res.get("json") or "status" not in res["json"]:
|
|
continue
|
|
st = res["json"]["status"]
|
|
|
|
if st == "failed":
|
|
print(f"attachment {att_id} FAILED")
|
|
sys.exit(1)
|
|
if st == "ready":
|
|
ready.add(att_id)
|
|
pending.remove(att_id)
|
|
if pending:
|
|
time.sleep(2)
|
|
total = len(data["ids"])
|
|
print(f"{len(ready)}/{total} attachments ready; lost={total - len(ready)}")
|
|
if len(ready) == total:
|
|
os.remove(IDS)
|
|
sys.exit(0)
|
|
|
|
print(f"IDs preserved for retry: {IDS}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|