From 4f0b8e0cfe817797ff0a4cc1e5784a25331d709f Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Tue, 24 Nov 2026 09:50:34 +0000 Subject: [PATCH] ci(molecule): hold the sweep bound to the job timeout and tolerate races A script test now fails when the sweep step's age bound differs from the job's timeout-minutes, since a shorter bound would remove a live run's containers. The final cleanup removes this run's containers one by one and fails only when one is still there, as another run's sweep may have removed it first once this run passed its timeout. Assisted-by: Claude Code:claude-opus-5-5 --- .gitea/workflows/molecule.yml | 17 +++++++++++------ tests/scripts/test_run_molecule_scenarios.py | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.gitea/workflows/molecule.yml b/.gitea/workflows/molecule.yml index ebba2d9..25377f4 100644 --- a/.gitea/workflows/molecule.yml +++ b/.gitea/workflows/molecule.yml @@ -47,7 +47,8 @@ jobs: (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) runs-on: [kamino-molecule] - # Also the age past which the first step removes a run's containers; keep the two equal. + # Also the age past which the sweep step removes a run's containers; a script test holds the + # two equal. timeout-minutes: 30 env: # The tools and collections molecule runs with: the image Lint and Build use. @@ -94,7 +95,8 @@ jobs: run: | # Assigned first so that a failing `docker ps` fails the step instead of looking empty. ids="$(docker ps -aq --filter 'label=net.rkdr.mortis.molecule-run')" - # The job's timeout-minutes: a shorter bound would remove a live run's containers. + # The job's timeout-minutes (a script test holds them equal): a shorter bound would remove a + # live run's containers. cutoff="$(date -d '30 minutes ago' +%s)" for id in $ids; do # a container removed since the listing, by its run or another run's sweep, is skipped @@ -157,7 +159,10 @@ jobs: run: | # Assigned first so that a failing `docker ps` fails the step instead of looking empty. ids="$(docker ps -aq --filter "label=net.rkdr.mortis.molecule-run=$KAMINO_RUN_ID")" - if [ -n "$ids" ]; then - # shellcheck disable=SC2086 # container ids, one word each - docker rm -f $ids - fi + for id in $ids; do + # another run's sweep may have removed it first, once this run passed its timeout + if ! docker rm -f "$id" >/dev/null && docker container inspect "$id" >/dev/null 2>&1; then + echo "::error::could not remove container $id" + exit 1 + fi + done diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index aa9e545..4a5d005 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -360,10 +360,14 @@ def alive(pid): def label_filters(step_name): """The label filters of a workflow step's docker commands, as `--filter label=[=]`.""" + return re.findall(r"--filter ['\"]?label=([^'\"\s]+)", step_run(step_name)) + + +def step_run(step_name): + """The run: block of the Molecule workflow's step named step_name.""" workflow = yaml.safe_load(WORKFLOW.read_text()) steps = [s for job in workflow["jobs"].values() for s in job.get("steps", [])] - run = next(s["run"] for s in steps if s.get("name") == step_name) - return re.findall(r"--filter ['\"]?label=([^'\"\s]+)", run) + return next(s["run"] for s in steps if s.get("name") == step_name) class ScenarioPlatformsTest(unittest.TestCase): @@ -401,6 +405,16 @@ class ScenarioPlatformsTest(unittest.TestCase): [f"{RUN_LABEL}=$KAMINO_RUN_ID"], ) + def test_the_sweep_spares_containers_younger_than_the_job_timeout(self): + # a shorter bound would remove the containers of a run still going + timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][ + "timeout-minutes" + ] + run = step_run("Remove the containers earlier runs left") + self.assertEqual( + re.findall(r"date -d '(\d+) minutes ago'", run), [str(timeout)] + ) + if __name__ == "__main__": unittest.main()