From 6c2e05c95a9c6c55e40db2144e777e32d74fb5ab Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Tue, 24 Nov 2026 09:42:03 +0000 Subject: [PATCH] ci(molecule): harden the stale-container sweep and correct comments The sweep now fails the step when a timestamp cannot be parsed or a container it removes is still there, while tolerating one another run's sweep removed first. Its 30-minute bound and the job's timeout point at each other, since a shorter bound would remove a live run's containers. The concurrency comments of Lint and Build now say that runs on one ref queue rather than cancel, and the scenarios' comment names the run script as what removes their containers. The workflow test checks the label filters of the sweep and cleanup steps by name. Assisted-by: Claude Code:claude-opus-5-5 --- .gitea/workflows/build.yml | 4 ++-- .gitea/workflows/lint.yml | 4 ++-- .gitea/workflows/molecule.yml | 11 +++++++++-- .../molecule/image-smoke-2604/molecule.yml | 5 ++--- extensions/molecule/image-smoke/molecule.yml | 5 ++--- .../molecule/check_mode/molecule.yml | 5 ++--- .../molecule/default/molecule.yml | 5 ++--- .../cloudflared/molecule/default/molecule.yml | 5 ++--- roles/docker/molecule/default/molecule.yml | 5 ++--- roles/fail2ban/molecule/default/molecule.yml | 5 ++--- roles/gitea/molecule/default/molecule.yml | 5 ++--- .../gitea_runner/molecule/default/molecule.yml | 5 ++--- .../molecule/default/molecule.yml | 5 ++--- roles/sshd/molecule/default/molecule.yml | 5 ++--- .../molecule/default/molecule.yml | 5 ++--- .../molecule/default/molecule.yml | 5 ++--- roles/traefik/molecule/default/molecule.yml | 5 ++--- roles/ufw/molecule/default/molecule.yml | 5 ++--- .../molecule/default/molecule.yml | 5 ++--- scripts/run_molecule_scenarios.sh | 10 +++++----- tests/scripts/test_run_molecule_scenarios.py | 18 +++++++++++------- 21 files changed, 61 insertions(+), 66 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index a93643c..1960b95 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -16,8 +16,8 @@ on: # yamllint disable-line rule:truthy permissions: contents: read -# A newer push to the same pull request cancels the running run. A tag's ref differs from every -# branch's, so a tag run has a group of its own and is never cancelled. +# A newer push to the same pull request cancels the running run. Other runs on one ref (a tag, +# a dispatched branch) queue instead: a newer one waits and replaces one still waiting. concurrency: group: build-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index 7d51990..eaaa263 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -17,8 +17,8 @@ on: # yamllint disable-line rule:truthy permissions: contents: read -# A newer push to the same pull request makes the running run obsolete; a manual dispatch never -# cancels another run. +# A newer push to the same pull request makes the running run obsolete. Other runs on one ref +# queue instead: a newer one waits for the running one and replaces one still waiting. concurrency: group: lint-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} diff --git a/.gitea/workflows/molecule.yml b/.gitea/workflows/molecule.yml index 213a8a1..ebba2d9 100644 --- a/.gitea/workflows/molecule.yml +++ b/.gitea/workflows/molecule.yml @@ -47,6 +47,7 @@ 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. timeout-minutes: 30 env: # The tools and collections molecule runs with: the image Lint and Build use. @@ -93,11 +94,17 @@ 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. cutoff="$(date -d '30 minutes ago' +%s)" for id in $ids; do - # a container removed since the listing is skipped + # a container removed since the listing, by its run or another run's sweep, is skipped created="$(docker inspect --format '{{.Created}}' "$id")" || continue - if [ "$(date -d "$created" +%s)" -lt "$cutoff" ]; then docker rm -f "$id"; fi + created="$(date -d "$created" +%s)" + if [ "$created" -ge "$cutoff" ]; then continue; fi + 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 # A pull request runs only the scenarios of the roles it changes (scripts/select_molecule_scenarios.py diff --git a/extensions/molecule/image-smoke-2604/molecule.yml b/extensions/molecule/image-smoke-2604/molecule.yml index cabbc9b..3b7c388 100644 --- a/extensions/molecule/image-smoke-2604/molecule.yml +++ b/extensions/molecule/image-smoke-2604/molecule.yml @@ -28,9 +28,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/extensions/molecule/image-smoke/molecule.yml b/extensions/molecule/image-smoke/molecule.yml index d71414c..04c670b 100644 --- a/extensions/molecule/image-smoke/molecule.yml +++ b/extensions/molecule/image-smoke/molecule.yml @@ -28,9 +28,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/_binary_update/molecule/check_mode/molecule.yml b/roles/_binary_update/molecule/check_mode/molecule.yml index e745df8..277d8e5 100644 --- a/roles/_binary_update/molecule/check_mode/molecule.yml +++ b/roles/_binary_update/molecule/check_mode/molecule.yml @@ -28,9 +28,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/_binary_update/molecule/default/molecule.yml b/roles/_binary_update/molecule/default/molecule.yml index d8cc228..8af128b 100644 --- a/roles/_binary_update/molecule/default/molecule.yml +++ b/roles/_binary_update/molecule/default/molecule.yml @@ -28,9 +28,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/cloudflared/molecule/default/molecule.yml b/roles/cloudflared/molecule/default/molecule.yml index 8e17d0f..f07fe0f 100644 --- a/roles/cloudflared/molecule/default/molecule.yml +++ b/roles/cloudflared/molecule/default/molecule.yml @@ -73,9 +73,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/docker/molecule/default/molecule.yml b/roles/docker/molecule/default/molecule.yml index 1c1488c..3771cce 100644 --- a/roles/docker/molecule/default/molecule.yml +++ b/roles/docker/molecule/default/molecule.yml @@ -29,9 +29,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/fail2ban/molecule/default/molecule.yml b/roles/fail2ban/molecule/default/molecule.yml index d36e0e1..af71173 100644 --- a/roles/fail2ban/molecule/default/molecule.yml +++ b/roles/fail2ban/molecule/default/molecule.yml @@ -27,9 +27,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/gitea/molecule/default/molecule.yml b/roles/gitea/molecule/default/molecule.yml index d6863c8..90b0a6a 100644 --- a/roles/gitea/molecule/default/molecule.yml +++ b/roles/gitea/molecule/default/molecule.yml @@ -38,9 +38,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/gitea_runner/molecule/default/molecule.yml b/roles/gitea_runner/molecule/default/molecule.yml index f4b915b..c9d2fe5 100644 --- a/roles/gitea_runner/molecule/default/molecule.yml +++ b/roles/gitea_runner/molecule/default/molecule.yml @@ -48,9 +48,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/network_interfaces/molecule/default/molecule.yml b/roles/network_interfaces/molecule/default/molecule.yml index e7c156a..fbc51ff 100644 --- a/roles/network_interfaces/molecule/default/molecule.yml +++ b/roles/network_interfaces/molecule/default/molecule.yml @@ -27,9 +27,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/sshd/molecule/default/molecule.yml b/roles/sshd/molecule/default/molecule.yml index f56e5b0..c028b47 100644 --- a/roles/sshd/molecule/default/molecule.yml +++ b/roles/sshd/molecule/default/molecule.yml @@ -28,9 +28,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/system_baseline/molecule/default/molecule.yml b/roles/system_baseline/molecule/default/molecule.yml index bce25f6..59f190a 100644 --- a/roles/system_baseline/molecule/default/molecule.yml +++ b/roles/system_baseline/molecule/default/molecule.yml @@ -34,9 +34,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/systemd_hardening/molecule/default/molecule.yml b/roles/systemd_hardening/molecule/default/molecule.yml index 525365e..523ce6b 100644 --- a/roles/systemd_hardening/molecule/default/molecule.yml +++ b/roles/systemd_hardening/molecule/default/molecule.yml @@ -27,9 +27,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/traefik/molecule/default/molecule.yml b/roles/traefik/molecule/default/molecule.yml index 85012ea..bd767a0 100644 --- a/roles/traefik/molecule/default/molecule.yml +++ b/roles/traefik/molecule/default/molecule.yml @@ -48,9 +48,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/ufw/molecule/default/molecule.yml b/roles/ufw/molecule/default/molecule.yml index cd45b2c..762b5c0 100644 --- a/roles/ufw/molecule/default/molecule.yml +++ b/roles/ufw/molecule/default/molecule.yml @@ -27,9 +27,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/roles/user_management/molecule/default/molecule.yml b/roles/user_management/molecule/default/molecule.yml index bec3981..1744f46 100644 --- a/roles/user_management/molecule/default/molecule.yml +++ b/roles/user_management/molecule/default/molecule.yml @@ -35,9 +35,8 @@ provisioner: verifier: name: ansible scenario: - # molecule's default sequence minus both destroys: the workflow removes the container as soon as - # this scenario ends, and a later run removes one a killed run left; molecule's destroy only adds - # the driver's poll wait. After a local run, run `molecule destroy` yourself. + # molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the + # container as soon as this scenario ends. After a local run, run `molecule destroy` yourself. test_sequence: - dependency - cleanup diff --git a/scripts/run_molecule_scenarios.sh b/scripts/run_molecule_scenarios.sh index 3b181c7..da915f8 100755 --- a/scripts/run_molecule_scenarios.sh +++ b/scripts/run_molecule_scenarios.sh @@ -24,11 +24,11 @@ label_of() { esac } -# Removes the scenario's containers by the platform names in its molecule.yml, expanded as molecule -# expands them (they carry ${KAMINO_RUN_ID}), in place of molecule's destroy, which the scenarios -# leave out: its driver waits about 5.6 s per scenario for the deletion (about 84 s over 15), and -# the whole destroy phase measured about 103 s a full run. The ci image has no docker CLI: python3 -# is the one molecule runs on, with molecule, PyYAML and the Docker SDK of molecule's Docker driver. +# Removes the scenario's containers by the platform names in its molecule.yml, expanded by molecule's +# interpolator, in place of molecule's destroy, which the scenarios leave out: its driver waits about +# 5.6 s per scenario for the deletion (about 84 s over 15), and the whole destroy phase measured about +# 103 s a full run. The ci image has no docker CLI: python3 is the one molecule runs on, with PyYAML +# and the Docker SDK of molecule's Docker driver. remove_instances() { python3 -I - "$1/molecule.yml" <<'EOF' import os diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index 22f208d..aa9e545 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -358,12 +358,12 @@ def alive(pid): return stat.rsplit(")", 1)[1].split()[0] != "Z" -def label_filters(): - """The label filters of the workflow's docker commands, as `--filter label=[=]`.""" +def label_filters(step_name): + """The label filters of a workflow step's docker commands, as `--filter label=[=]`.""" workflow = yaml.safe_load(WORKFLOW.read_text()) steps = [s for job in workflow["jobs"].values() for s in job.get("steps", [])] - runs = "\n".join(s.get("run", "") for s in steps) - return re.findall(r"--filter ['\"]?label=([^'\"\s]+)", runs) + run = next(s["run"] for s in steps if s.get("name") == step_name) + return re.findall(r"--filter ['\"]?label=([^'\"\s]+)", run) class ScenarioPlatformsTest(unittest.TestCase): @@ -392,10 +392,14 @@ class ScenarioPlatformsTest(unittest.TestCase): self.assertNotIn("destroy", data["scenario"]["test_sequence"], path) def test_the_workflow_finds_the_containers_by_the_run_label(self): - filters = label_filters() # the sweep of earlier runs filters by the key, the cleanup of this run by key and value - self.assertIn(RUN_LABEL, filters) - self.assertIn(f"{RUN_LABEL}=$KAMINO_RUN_ID", filters) + self.assertEqual( + label_filters("Remove the containers earlier runs left"), [RUN_LABEL] + ) + self.assertEqual( + label_filters("Remove this run's containers"), + [f"{RUN_LABEL}=$KAMINO_RUN_ID"], + ) if __name__ == "__main__":