Files
Kamino/scripts/run_molecule_scenarios.sh
Defame1297 e34a0eb2e0 test(molecule): derive the order test from the duration list
Part of #432. The start-order test now reads its scenarios and their seconds
from the run script's list, so a refreshed list does not break it; the
empty-lanes test only asserts the value is accepted, and the script's header
and duration-list comments are rewrapped and say the list is keyed by
scenario directory.

Assisted-by: Claude Code
2026-11-28 08:45:05 +00:00

170 lines
6.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# The Molecule workflow's "Run the scenarios" step: `molecule test` for each scenario directory on
# stdin, MOLECULE_LANES (default 4, also when empty) at a time, longest first, each with its own
# log and ANSIBLE_HOME under <work-dir>/<n>/ (no collection is installed there:
# .config/molecule/config.yml sets prerun false).
# Exit 1 on a failure.
#
# scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs
set -euo pipefail
lanes="${MOLECULE_LANES:-4}"
if ! [[ "$lanes" =~ ^[1-9][0-9]*$ ]]; then
echo "MOLECULE_LANES must be a positive integer, not '$lanes'" >&2
exit 2
fi
if [ "$#" -ne 1 ]; then
echo "usage: $0 <work-dir> < scenario-dirs" >&2
exit 2
fi
work="$1"
label_of() {
case "$1" in
extensions/*) echo "${1##*/}" ;;
*) local role="${1#roles/}"; echo "${role%%/*}/${1##*/}" ;;
esac
}
# 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
import sys
import docker
import yaml
from molecule.interpolation import Interpolator, TemplateWithDefaults
with open(sys.argv[1]) as f:
text = Interpolator(TemplateWithDefaults, os.environ).interpolate(f.read())
names = [p["name"] for p in yaml.safe_load(text)["platforms"]]
if not names:
sys.exit("no platform names in " + sys.argv[1])
containers = docker.from_env().containers
for name in names:
for container in containers.list(all=True, filters={"name": f"^/{name}$"}):
if container.name == name:
container.remove(force=True)
EOF
}
# Runs scenario $2 into $work/$1/. Its outcome is the rc file, which a lane that died lacks.
run_one() {
local out="$work/$1" dir="$2" start=$SECONDS rc=0
mkdir -p "$out/ansible"
(
export ANSIBLE_HOME="$out/ansible"
# a role scenario runs from its role's directory, as molecule expects
case "$dir" in roles/*) cd "${dir%/molecule/*}" || exit 1 ;; esac
molecule test -s "${dir##*/}"
) >"$out/log" 2>&1 </dev/null || rc=$?
if ! remove_instances "$dir" >>"$out/log" 2>&1; then
echo "could not remove the containers of $dir" >>"$out/log"
if [ "$rc" -eq 0 ]; then rc=1; fi
fi
echo "$((SECONDS - start))" >"$out/seconds"
echo "$rc" >"$out/rc"
echo "finished $(label_of "$dir"): exit $rc after $((SECONDS - start)) s"
}
dirs=()
while IFS= read -r dir; do
if [ -n "$dir" ]; then dirs+=("$dir"); fi
done
mkdir -p "$work"
# Seconds each scenario took in the last full run, so the longest start first and none started
# late sets the end of the run. Keyed by scenario directory, not label: refresh from a full run's
# `finished <label>: exit 0 after S s` lines, each label mapped back to its directory. A scenario
# missing here is assumed long and starts first.
declare -A last_seconds=(
[roles/system_baseline/molecule/default]=1127
[roles/ufw/molecule/default]=907
[roles/traefik/molecule/default]=886
[roles/gitea_runner/molecule/default]=762
[roles/cloudflared/molecule/default]=673
[roles/docker/molecule/default]=612
[roles/tailscale/molecule/default]=509
[roles/fail2ban/molecule/default]=337
[roles/systemd_hardening/molecule/default]=335
[roles/gitea/molecule/default]=301
[roles/user_management/molecule/default]=270
[roles/_binary_update/molecule/default]=248
[roles/pihole/molecule/default]=228
[roles/sshd/molecule/default]=163
[roles/pve_lxc/molecule/default]=157
[roles/network_interfaces/molecule/default]=129
[roles/_binary_update/molecule/check_mode]=81
[extensions/molecule/image-smoke]=47
[extensions/molecule/image-smoke-2604]=46
)
# The indices of dirs in start order; ties keep the selection order (a stable sort).
mapfile -t start_order < <(
for n in "${!dirs[@]}"; do printf '%s %s\n' "${last_seconds[${dirs[$n]}]:-999999}" "$n"; done |
sort -s -k1,1nr | cut -d' ' -f2
)
# A cancelled job must not leave lanes behind that recreate containers after the next run's
# cleanup. A signal to this script stops each lane's tree so it cannot fork, then terminates it,
# with the whole group of any descendant that leads one: ansible's workers detach into their own.
# A kill of the step's process group reaches the lanes and molecule, not those detached workers.
kill_tree() {
local child leader=""
kill -STOP "$1" 2>/dev/null || return 0
# never a group this script or its caller is in: only one a descendant leads
if [ "$(ps -o pgid= -p "$1" | tr -d ' ')" = "$1" ] && [ "$1" != "$own_group" ]; then
leader=1
kill -STOP -- "-$1" 2>/dev/null || true
fi
for child in $(ps -o pid= --ppid "$1"); do kill_tree "$child"; done
if [ -n "$leader" ]; then
kill -TERM -- "-$1" 2>/dev/null || true
kill -CONT -- "-$1" 2>/dev/null || true
fi
kill -TERM "$1" 2>/dev/null || true
kill -CONT "$1" 2>/dev/null || true
}
stop_lanes() {
# a second signal must not abort the cleanup and leave a lane stopped
trap '' TERM INT HUP
local pid
for pid in $(jobs -rp); do kill_tree "$pid"; done
wait || true
exit "$1"
}
own_group="$(ps -o pgid= -p "$$" | tr -d ' ')"
trap 'stop_lanes 143' TERM
trap 'stop_lanes 130' INT
trap 'stop_lanes 129' HUP
for n in "${start_order[@]}"; do
while [ "$(jobs -rp | wc -l)" -ge "$lanes" ]; do wait -n || true; done
run_one "$n" "${dirs[$n]}" </dev/null &
done
wait
failed=""
ran=0
times=""
for n in "${!dirs[@]}"; do
label="$(label_of "${dirs[$n]}")"
seconds="$(cat "$work/$n/seconds" 2>/dev/null || echo '?')"
times="$times $label: $seconds s"$'\n'
echo "::group::molecule test: $label ($seconds s)"
cat "$work/$n/log" 2>/dev/null || true
echo "::endgroup::"
if [ -f "$work/$n/rc" ]; then
ran=$((ran + 1))
if [ "$(cat "$work/$n/rc")" != 0 ]; then failed="$failed $label"; fi
fi
done
printf 'Scenarios run: %s\n%s' "$ran" "$times"
# A lane that died without a result must not pass quietly.
test "$ran" -eq "${#dirs[@]}" || { echo "::error::ran $ran of ${#dirs[@]} selected scenarios"; exit 1; }
test -z "$failed" || { echo "::error::Failed:$failed"; exit 1; }