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()