diff --git a/infra/github-backup/bin/drain_queue.py b/infra/github-backup/bin/drain_queue.py index 6e5de0ed94..c7b4c08eff 100644 --- a/infra/github-backup/bin/drain_queue.py +++ b/infra/github-backup/bin/drain_queue.py @@ -45,7 +45,7 @@ from datetime import datetime, timezone sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) -from ghb_common import load_config, backup_root +from ghb_common import load_config, backup_root, load_plan, load_state, report_problems import fetch_mirrors COALESCE_S = 15 * 60 @@ -214,7 +214,8 @@ def main(): cfg = load_config() root = backup_root(cfg) state_path = os.path.join(root, "state.json") - state = json.load(open(state_path)) if os.path.exists(state_path) else {} + state, problems = load_state(root) + report_problems(problems) events = queue_events(cfg["worker"]["queue_poll_path"]) if not events: print("queue empty") @@ -222,12 +223,9 @@ def main(): now = time.time() stamp = time.strftime("%Y-%m-%dT%H:%M:%S%z") log_path = os.path.join(root, "fetch.log") - plan_path = os.path.join(root, "plan.json") - planned = ( - {p["repo"] for p in json.load(open(plan_path))} - if os.path.exists(plan_path) - else set() - ) + plan, problems = load_plan(root) + report_problems(problems) + planned = {p["repo"] for p in plan} def log(line): with open(log_path, "a") as f: diff --git a/infra/github-backup/tests/test_drain_queue.py b/infra/github-backup/tests/test_drain_queue.py index 13fed4deb5..bde114c9d7 100644 --- a/infra/github-backup/tests/test_drain_queue.py +++ b/infra/github-backup/tests/test_drain_queue.py @@ -38,10 +38,10 @@ def iso_age(seconds): class Harness: """One drain run against a stub Worker + stub fetch.""" - def __init__(self, events, planned, fetch_results, initial_state=None): + def __init__(self, events, planned, fetch_results, initial_state=None, plan_raw=None): self.root = tempfile.mkdtemp(prefix="ghb-drain-test-") with open(os.path.join(self.root, "plan.json"), "w") as f: - json.dump([{"repo": r} for r in planned], f) + json.dump(plan_raw if plan_raw is not None else [{"repo": r} for r in planned], f) if initial_state is not None: with open(os.path.join(self.root, "state.json"), "w") as f: json.dump(initial_state, f) @@ -530,4 +530,67 @@ def _edge_req(path, payload=None): len(got) == drain_queue.MAX_QUEUE_PAGES, ) + +# 20. poison plan entries (#6835): string/int entries are named + skipped, +# the valid planned repo still drains +h = Harness( + [ev("e1", "owner/repo")], + [], + {"owner/repo": "ok"}, + plan_raw=["o/r1", 123, {"repo": "owner/repo"}], +) +check("poison plan entries: exit 0", h.run() == 0) +check("poison plan entries: fetched + acked", h.fetch_calls == ["owner/repo"] and h.acked_ids() == ["e1"]) +check("poison plan entries: coalesce stamp set", "drain_last::owner/repo" in h.state()) + +# 21. plan top level is a dict (#6835): whole file treated as empty, +# the repo drains as unplanned instead of crashing the tick +h = Harness( + [ev("e1", "owner/repo")], + [], + {}, + plan_raw={"repo": "owner/repo"}, +) +check("dict plan: exit 0", h.run() == 0) +check("dict plan: event acked as unplanned", h.acked_ids() == ["e1"] and h.fetch_calls == []) +check("dict plan: fetch.log note", "unplanned repo" in h.fetch_log()) + +# 22. state top level is a list (#6835): whole file treated as empty, +# state.json is rewritten as a clean object +h = Harness( + [ev("e1", "owner/repo")], + ["owner/repo"], + {"owner/repo": "ok"}, + initial_state=[], +) +check("list state: exit 0", h.run() == 0) +check("list state: fetched + acked", h.fetch_calls == ["owner/repo"] and h.acked_ids() == ["e1"]) +check("list state: rewritten as object with stamp", isinstance(h.state(), dict) and "drain_last::owner/repo" in h.state()) + +# 23. drain_last wrong shape (#6835): string/dict stamps are dropped and +# treated as absent — the coalesce window does not apply, fetch runs +for bad_stamp in ("soon", {"t": 1}): + h = Harness( + [ev("e1", "owner/repo")], + ["owner/repo"], + {"owner/repo": "ok"}, + initial_state={"drain_last::owner/repo": bad_stamp}, + ) + check(f"drain_last {type(bad_stamp).__name__}: exit 0", h.run() == 0) + check(f"drain_last {type(bad_stamp).__name__}: fetched + acked", h.fetch_calls == ["owner/repo"] and h.acked_ids() == ["e1"]) + check(f"drain_last {type(bad_stamp).__name__}: stamp rewritten numeric", isinstance(h.state()["drain_last::owner/repo"], float)) + +# 24. drain_fail wrong shape (#6835): int/string counters are dropped and +# the failure count starts fresh instead of crashing the tick +for bad_counter in (3, "many"): + h = Harness( + [ev("e1", "owner/repo")], + ["owner/repo"], + {"owner/repo": "fail"}, + initial_state={"drain_fail::owner/repo": bad_counter}, + ) + check(f"drain_fail {type(bad_counter).__name__}: exit 0", h.run() == 0) + check(f"drain_fail {type(bad_counter).__name__}: event NOT acked", h.acked_ids() == []) + check(f"drain_fail {type(bad_counter).__name__}: count starts fresh", h.state()["drain_fail::owner/repo"]["count"] == 1) + print(f"\n{sum(1 for _, ok in PASS if ok)}/{len(PASS)} checks passed")