mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
scripts/ci-clear-rolling-cache.py deletes the old rolling entries when the cache warm leg's Post Setup step starts. It used to exit 0 at that point, so a save that was canceled or failed afterwards left no rolling entry while the job passed. The script now keeps polling the leg until Post Setup completes or the leg ends, within the same 160-minute limit, and exits 1 unless the save succeeded. A failure in a later post step does not count once the save has succeeded. The header and the ci.yml comment describe this, and say that the next run then starts cold. Three new tests run main() with recorded API answers: a save that succeeds after the deletion exits 0, and a save that is canceled or fails after it exits 1.
197 lines
10 KiB
Python
197 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Checks for ci-clear-rolling-cache.py with recorded GitHub API answers; no network.
|
|
|
|
python3 scripts/ci-clear-rolling-cache-tests.py
|
|
"""
|
|
import contextlib
|
|
from datetime import datetime, timedelta, timezone
|
|
import importlib.util
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
_spec = importlib.util.spec_from_file_location("clear", Path(__file__).with_name("ci-clear-rolling-cache.py"))
|
|
clear = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(clear)
|
|
|
|
WARM = "gate / cache warm (ubuntu-24.04)"
|
|
ME = [{"name": "cold cache clear", "status": "in_progress"}]
|
|
|
|
|
|
def warm_job(status, conclusion=None, steps=(), started_at="2026-11-28T03:40:00Z"):
|
|
return {
|
|
"name": WARM,
|
|
"status": status,
|
|
"conclusion": conclusion,
|
|
"started_at": started_at,
|
|
"steps": [{"name": n, "status": s, "conclusion": c} for n, s, c in steps],
|
|
}
|
|
|
|
|
|
def earlier(conclusion):
|
|
return [{"name": WARM, "conclusion": conclusion}, {"name": "cold cache clear", "conclusion": "failure"}]
|
|
|
|
|
|
class SaveStep(unittest.TestCase):
|
|
def test_the_leg_is_saving_once_its_save_step_starts(self):
|
|
self.assertFalse(clear.saving(warm_job("in_progress", steps=[("Setup", "completed", "success"), ("Gate", "in_progress", None)])))
|
|
self.assertTrue(clear.saving(warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])))
|
|
|
|
def test_other_post_steps_are_not_the_save(self):
|
|
job = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Harden runner", "in_progress", None)])
|
|
self.assertFalse(clear.saving(job))
|
|
|
|
def test_a_leg_canceled_during_its_save_saves_nothing(self):
|
|
job = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "completed", "cancelled")])
|
|
self.assertTrue(clear.saving(job))
|
|
self.assertTrue(clear.saves_nothing(job))
|
|
|
|
def test_a_leg_that_ended_with_its_save_still_running_saves_nothing(self):
|
|
job = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])
|
|
self.assertTrue(clear.saves_nothing(job))
|
|
|
|
def test_a_running_or_finished_save_counts_as_saved(self):
|
|
running = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Post Setup", "in_progress", None)])
|
|
done = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "success"),
|
|
("Post Harden runner", "completed", "failure")])
|
|
self.assertFalse(clear.saves_nothing(running))
|
|
self.assertFalse(clear.saves_nothing(done))
|
|
|
|
def test_a_failed_step_before_the_save_saves_nothing(self):
|
|
job = lambda c: warm_job("in_progress", steps=[("Gate", "completed", c), ("Post Setup", "in_progress", None)])
|
|
self.assertTrue(clear.saves_nothing(job("failure")))
|
|
self.assertTrue(clear.saves_nothing(job("cancelled")))
|
|
self.assertFalse(clear.saves_nothing(job("success")))
|
|
tolerated = warm_job("in_progress", steps=[("Gate", "completed", "success"), ("Trim the cache entry to this build", "completed", "failure"),
|
|
("Post Setup", "in_progress", None)])
|
|
self.assertFalse(clear.saves_nothing(tolerated))
|
|
|
|
|
|
class Deadline(unittest.TestCase):
|
|
began = datetime(2026, 11, 28, 3, 37, tzinfo=timezone.utc)
|
|
|
|
def test_the_wait_is_timed_from_the_leg_start(self):
|
|
late = warm_job("in_progress", started_at="2026-11-28T04:00:00Z")
|
|
before_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS - 60)
|
|
after_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS + 60)
|
|
self.assertGreater((before_limit - self.began).total_seconds(), clear.GIVE_UP_SECONDS)
|
|
self.assertFalse(clear.gave_up(late, before_limit, self.began))
|
|
self.assertTrue(clear.gave_up(late, after_limit, self.began))
|
|
|
|
def test_a_leg_that_never_starts_is_timed_from_the_script_start(self):
|
|
queued = warm_job("queued", started_at="2026-11-28T03:38:00Z")
|
|
later = self.began + timedelta(seconds=clear.GIVE_UP_SECONDS + 60)
|
|
self.assertFalse(clear.gave_up(None, self.began + timedelta(minutes=10), self.began))
|
|
self.assertTrue(clear.gave_up(None, later, self.began))
|
|
self.assertTrue(clear.gave_up(queued, later, self.began))
|
|
|
|
|
|
class Jobs(unittest.TestCase):
|
|
def test_only_the_warm_leg_counts(self):
|
|
jobs = [{"name": "gate / test 1/5 (ubuntu-24.04)"}, {"name": "gate / build (${{ matrix.os }})"}, {"name": WARM}]
|
|
self.assertEqual(clear.warm_job(jobs), jobs[2])
|
|
|
|
def test_a_rerun_of_this_job_alone_skips_a_leg_that_succeeded(self):
|
|
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("success")}))
|
|
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("success"), 2: [{"name": "cold cache clear"}]}))
|
|
self.assertTrue(clear.not_in_attempt(ME, {1: earlier("failure"), 2: [{"name": "cold cache clear"}], 3: earlier("success")}))
|
|
|
|
def test_a_leg_that_did_not_succeed_is_waited_for(self):
|
|
self.assertFalse(clear.not_in_attempt(ME, {1: earlier("cancelled")}))
|
|
self.assertFalse(clear.not_in_attempt(ME, {}))
|
|
self.assertFalse(clear.not_in_attempt(ME, {1: earlier("success"), 2: earlier("failure")}))
|
|
|
|
def test_a_rerun_of_all_jobs_is_waited_for(self):
|
|
self.assertFalse(clear.not_in_attempt(ME + [{"name": "cache prune", "status": "in_progress"}], {1: earlier("success")}))
|
|
|
|
def test_a_failed_select_or_build_stops_the_wait(self):
|
|
done = lambda n, c: {"name": n, "status": "completed", "conclusion": c}
|
|
self.assertTrue(clear.legs_stopped([done("gate / select legs", "failure")]))
|
|
self.assertTrue(clear.legs_stopped([done("gate / select legs", "success"), done("gate / build (ubuntu-24.04)", "cancelled")]))
|
|
self.assertFalse(clear.legs_stopped([done("gate / select legs", "success"), done("gate / build (${{ matrix.os }})", "skipped")]))
|
|
self.assertFalse(clear.legs_stopped([done("gate / test 1/5 (ubuntu-24.04)", "failure")]))
|
|
|
|
|
|
class Caches(unittest.TestCase):
|
|
def test_every_page_is_read(self):
|
|
entries = [{"id": n, "key": f"v2-rust-a013-gate-crates-Linux-x64-{n}"} for n in range(2 * clear.PAGE_SIZE + 3)]
|
|
pages = []
|
|
|
|
def fetch(page):
|
|
pages.append(page)
|
|
start = (page - 1) * clear.PAGE_SIZE
|
|
return {"actions_caches": entries[start:start + clear.PAGE_SIZE]}
|
|
|
|
self.assertEqual(clear.all_caches(fetch), entries)
|
|
self.assertEqual(pages, [1, 2, 3])
|
|
|
|
def test_only_rolling_crates_entries_are_deleted(self):
|
|
caches = [
|
|
{"key": "v2-rust-a013-gate-crates-Linux-x64-46055934-01d60c35"},
|
|
{"key": "v2-rust-a013-gate-Linux-x64-46055934-01d60c35"},
|
|
{"key": "v1-affinity-rust-a013-gate-Linux-x64-b541f517-01d60c35"},
|
|
{"key": "v0-rust-build-Linux-x64-1fd829d9-01d60c35"},
|
|
]
|
|
self.assertEqual(clear.rolling_entries(caches), caches[:1])
|
|
|
|
def test_entries_created_after_the_run_started_are_kept(self):
|
|
rolling = lambda created: {"key": "v2-rust-ab-gate-crates-Linux-x64-1", "created_at": created}
|
|
old, same_minute_push = rolling("2026-11-27T03:00:00.5Z"), rolling("2026-11-28T03:40:00Z")
|
|
self.assertEqual(clear.created_before([old, same_minute_push], "2026-11-28T03:37:10Z"), [old])
|
|
|
|
|
|
class Clock(datetime):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
return datetime(2026, 11, 28, 4, 0, tzinfo=timezone.utc)
|
|
|
|
|
|
class AfterDeletion(unittest.TestCase):
|
|
SAVING = [("Gate", "completed", "success"), ("Post Setup", "in_progress", None)]
|
|
|
|
def run_main(self, after):
|
|
"""Runs main() with the warm leg's save starting at the first poll and `after` at every later poll."""
|
|
polls = [warm_job("in_progress", steps=self.SAVING), after]
|
|
deleted = []
|
|
|
|
def gh(url):
|
|
if "/jobs?" in url:
|
|
return {"jobs": ME + [polls.pop(0) if len(polls) > 1 else polls[0]]}
|
|
if "/caches?" in url:
|
|
old = {"id": 7, "key": "v2-rust-ab-gate-crates-Linux-x64-1", "created_at": "2026-11-27T03:40:00Z", "size_in_bytes": 4.6e9}
|
|
return {"actions_caches": [old]}
|
|
return {"run_started_at": "2026-11-28T03:37:10Z"}
|
|
|
|
def run(args, **kwargs):
|
|
deleted.append(args[-1])
|
|
return subprocess.CompletedProcess(args, 0, "", "")
|
|
|
|
env = {"GITHUB_REPOSITORY": "storytold/vectorcraft", "GITHUB_RUN_ID": "1", "GITHUB_RUN_ATTEMPT": "1"}
|
|
with mock.patch.object(clear, "gh", gh), mock.patch.object(clear.subprocess, "run", run), mock.patch.object(clear.time, "sleep"), \
|
|
mock.patch.object(clear, "datetime", Clock), mock.patch.dict(os.environ, env), contextlib.redirect_stdout(io.StringIO()):
|
|
status = clear.main()
|
|
self.assertEqual(deleted, ["repos/storytold/vectorcraft/actions/caches/7"])
|
|
return status
|
|
|
|
def test_a_save_that_succeeds_after_the_deletion_exits_0(self):
|
|
saved = warm_job("completed", "success", [("Gate", "completed", "success"), ("Post Setup", "completed", "success")])
|
|
later_post_failure = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "success"),
|
|
("Post Harden runner", "completed", "failure")])
|
|
self.assertFalse(self.run_main(saved))
|
|
self.assertFalse(self.run_main(later_post_failure))
|
|
|
|
def test_a_save_canceled_after_the_deletion_exits_1(self):
|
|
canceled = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "completed", "cancelled")])
|
|
self.assertEqual(self.run_main(canceled), 1)
|
|
|
|
def test_a_save_that_fails_after_the_deletion_exits_1(self):
|
|
failed = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "failure")])
|
|
self.assertEqual(self.run_main(failed), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|