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
This commit is contained in:
Defame1297 committed 2026-11-24 09:50:34 +00:00
1 parent 6c2e05c95a
commit 4f0b8e0cfe
2 files changed
+27 -8

No files matched your search

+11 -6
View File
@@ -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
+16 -2
View File
@@ -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=<key>[=<value>]`."""
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()