#!/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 # // (no collection is installed there: .config/molecule/config.yml sets prerun false). # Exit 1 on a failure. # # scripts/run_molecule_scenarios.sh < 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 < 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 >"$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. Refresh from a full run's `finished