ci(molecule): run the selected scenarios side by side in four lanes
A full run executes 15 scenarios one after another, about 1100 s in total, with the slowest (traefik/default) at about 216 s. The scenarios share nothing on the runner's Docker daemon but the daemon itself: their container names are unique. So scripts/run_molecule_scenarios.sh now runs MOLECULE_LANES (default 4, a positive integer) of them at once inside the one job, with the slowest scenario as the floor; the saving will be measured in CI. Each scenario gets its own ANSIBLE_HOME under the job tree. With Runtime(isolated=False), ansible-compat uses ANSIBLE_HOME as molecule's cache dir: molecule's prerun installs the collection under test there with `ansible-galaxy collection install --force` (which first deletes the installed copy), and the scenario's ephemeral directory (inventory, ansible.cfg, state) lives under it. A shared one would let a scenario delete the copy another is running. Each scenario's output goes to its own log, printed afterwards in the selection order inside its own group with its wall time, plus a timing summary. The failure list, the ran-N-of-M guard, </dev/null stdin, the role-directory cwd and the per-scenario container removal are kept. A TERM, INT or HUP to the script stops each lane's whole process tree, including the process groups of ansible's detached workers (they call setsid), terminates it, ignores further signals until done, and exits 128 plus the first signal. A kill of the step's process group reaches the lanes and molecule but not ansible's detached workers. Either way a cancelled job leaves no molecule behind to recreate a container after the next run's cleanup. The logic moved from the workflow into a script so a unit test with stub molecule and docker can hold it; the test also fails a scenario whose platform name is not unique or not matched by the stale-container filter, which is what makes running side by side safe. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
f77586dc60
commit
1ab01b6e3a
4 files changed
+450
-58
No files matched your search
@@ -84,10 +84,13 @@ issue.
|
||||
too, until #166 removes that trigger. *Why:* a pull request runs only its selection, so
|
||||
drift in the other roles shows up here. *Held:* review.
|
||||
- **A new commit cancels the pull request's running Molecule run, and the run step first removes
|
||||
the containers a cancelled run left.** Two runs never overlap only because the runner has a
|
||||
single slot, and the scenarios' container names are fixed; raising the runner's capacity needs
|
||||
per-run container names first (#166). *Why:* a cancelled run is killed before it can destroy
|
||||
its instances, and two runs would collide on the same names. *Held:* review.
|
||||
the containers a cancelled run left.** The scenarios of one run overlap
|
||||
(`scripts/run_molecule_scenarios.sh`, `MOLECULE_LANES` at a time), so no two scenarios may share a
|
||||
platform name. Two runs never overlap only because the runner has a single slot, and the
|
||||
scenarios' container names are fixed; raising the runner's capacity needs per-run container
|
||||
names first (#166). *Why:* a cancelled run is killed before it can remove its instances, and
|
||||
two overlapping scenarios or runs would collide on the same names. *Held:* review; that no two
|
||||
scenarios share a platform name is held by the test the `test_sequence` rule below names.
|
||||
- **Molecule tests the pull request's pins, as Lint and Build do.** *Why:* the pull request that
|
||||
raises the ansible-core floor must be behaviour-tested on the new core before it automerges.
|
||||
*Held:* aspirational until #225 runs Molecule from the `ci` image with the pin comparison
|
||||
@@ -96,7 +99,9 @@ issue.
|
||||
steps, and names its platforms literally in its `molecule.yml`;** copy it into a new scenario.
|
||||
*Why:* the workflow clears stale containers first and removes each scenario's containers by
|
||||
those names as soon as it ends, which skips the driver's destroy wait, and the molecule state
|
||||
lives in the job's own `ANSIBLE_HOME`, so a killed run leaves none behind. *Held:* review.
|
||||
lives in each scenario's own `ANSIBLE_HOME` in the job tree, so a killed run leaves none
|
||||
behind. *Held:* control: `tests/scripts/test_run_molecule_scenarios.py` fails a `destroy` in a
|
||||
sequence, and a platform name that is not unique or not matched by the stale-container filter.
|
||||
- **Scenarios live in `roles/*/molecule/*/` and `extensions/molecule/*/`.** A role scenario that
|
||||
must pass on Ubuntu 24.04 and 26.04 takes `image: ${MOLECULE_IMAGE}` or
|
||||
`${MOLECULE_IMAGE_2604}` per platform; the image-smoke scenarios are described in
|
||||
|
||||
@@ -44,10 +44,11 @@ permissions:
|
||||
|
||||
# A pull request run is cancelled when a newer commit is pushed to the same pull request (the
|
||||
# same rule as lint.yml and build.yml); a run on main is never cancelled, so its group is the
|
||||
# commit SHA. Scenarios use fixed container names on the runner's shared Docker daemon, so two
|
||||
# runs must not overlap. The group no longer guarantees that (it is per pull request now): the
|
||||
# runner's single slot does. Raise the runner's capacity and this needs a lock or a shared
|
||||
# group again.
|
||||
# commit SHA. The scenarios of one run overlap (scripts/run_molecule_scenarios.sh), which is safe
|
||||
# because no two share a container name; but the names are fixed on the runner's shared Docker
|
||||
# daemon, so two runs must not overlap. The group no longer guarantees that (it is per pull
|
||||
# request now): the runner's single slot does. Raise the runner's capacity and this needs
|
||||
# per-run container names first (#166), or a lock or a shared group again.
|
||||
concurrency:
|
||||
group: molecule-${{ github.event_name == 'pull_request' && github.ref || github.sha }}
|
||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||
@@ -127,12 +128,9 @@ jobs:
|
||||
echo "KAMINO_COLLECTIONS=$dest" >> "$GITHUB_ENV"
|
||||
|
||||
# A pull request runs only the scenarios of the roles it changes (scripts/select_molecule_scenarios.py
|
||||
# explains the rules); a push to main and a manual dispatch run all of them.
|
||||
# The scenarios leave out molecule's destroy (see their test_sequence): the step removes each
|
||||
# scenario's containers itself, by the platform names in its molecule.yml, as soon as it ends.
|
||||
# That skips the driver's destroy playbook: its deletion wait is about 5.6 s per scenario (about
|
||||
# 84 s over 15), and the whole destroy phase measured about 103 s a full run. A cancelled run
|
||||
# is killed before it can, so the step first removes what one left.
|
||||
# explains the rules); a push to main and a manual dispatch run all of them, a few at a time.
|
||||
# scripts/run_molecule_scenarios.sh runs them and removes each scenario's containers as soon as
|
||||
# it ends; a cancelled run is killed before it can, so the step first removes what one left.
|
||||
- name: Run the scenarios
|
||||
working-directory: ac/ansible_collections/mortis/kamino
|
||||
env:
|
||||
@@ -150,12 +148,11 @@ jobs:
|
||||
# molecule calls ansible-config and ansible-playbook by name
|
||||
export PATH="$MOLECULE_VENV/bin:$PATH"
|
||||
# The collection under test first, then the pinned collections the roles use. molecule also
|
||||
# installs the collection under test into ANSIBLE_HOME; keep that inside the
|
||||
# job tree so no run resolves a copy another run left in the runner user's
|
||||
# home (the version stays 0.1.0, so a stale copy would look current). The rm
|
||||
# installs the collection under test into ANSIBLE_HOME, which the run script gives each
|
||||
# scenario inside the job tree, so no run resolves a copy another run left in the runner
|
||||
# user's home (the version stays 0.1.0, so a stale copy would look current). The rm
|
||||
# clears a copy left there by runs before this was set.
|
||||
ac_root="$(cd ../../.. && pwd)"
|
||||
export ANSIBLE_HOME="$ac_root/.ansible"
|
||||
export ANSIBLE_COLLECTIONS_PATH="$ac_root:$KAMINO_COLLECTIONS"
|
||||
rm -rf "$HOME/.ansible/collections/ansible_collections/mortis"
|
||||
# The selector comes from the base branch, so a pull request cannot edit the rules it is
|
||||
@@ -171,42 +168,4 @@ jobs:
|
||||
echo "::notice::no base copy of $sel; running this checkout's copy"
|
||||
fi
|
||||
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
|
||||
# Removes the containers of the scenario in directory $1 by its platform names (literal in
|
||||
# molecule.yml), whether or not they exist; a failure fails the step.
|
||||
remove_instances() {
|
||||
local names ids
|
||||
local py='import sys, yaml; print(*(p["name"] for p in yaml.safe_load(open(sys.argv[1]))["platforms"]))'
|
||||
names="$("$MOLECULE_VENV/bin/python3" -I -c "$py" "$1/molecule.yml")" && [ -n "$names" ] || return 1
|
||||
# shellcheck disable=SC2046,SC2086 # names and ids are one word each
|
||||
ids="$(docker ps -aq $(printf -- '--filter name=^/%s$ ' $names))" || return 1
|
||||
# shellcheck disable=SC2086
|
||||
[ -z "$ids" ] || docker rm -f $ids >/dev/null
|
||||
}
|
||||
failed=""
|
||||
ran=0
|
||||
while IFS= read -r dir; do
|
||||
[ -n "$dir" ] || continue
|
||||
name="$(basename "$dir")"
|
||||
case "$dir" in
|
||||
extensions/*)
|
||||
label="$name"
|
||||
echo "::group::molecule test: $label"
|
||||
molecule test -s "$name" </dev/null || failed="$failed $label"
|
||||
;;
|
||||
*)
|
||||
role="${dir#roles/}"
|
||||
role="${role%%/*}"
|
||||
label="$role/$name"
|
||||
echo "::group::molecule test: $label"
|
||||
(cd "roles/$role" && molecule test -s "$name" </dev/null) || failed="$failed $label"
|
||||
;;
|
||||
esac
|
||||
remove_instances "$dir" || failed="$failed $label(removal)"
|
||||
ran=$((ran + 1))
|
||||
echo "::endgroup::"
|
||||
done <<< "$selected"
|
||||
echo "Scenarios run: $ran"
|
||||
# A scenario that swallowed the rest of the list must not pass quietly.
|
||||
want="$(grep -c . <<< "$selected" || true)"
|
||||
test "$ran" -eq "$want" || { echo "::error::ran $ran of $want selected scenarios"; exit 1; }
|
||||
test -z "$failed" || { echo "::error::Failed:$failed"; exit 1; }
|
||||
scripts/run_molecule_scenarios.sh "$ac_root/molecule-runs" <<< "$selected"
|
||||
Reference in new issue
Block a user