ci(molecule): start the longest scenarios first and pass the lane count in

Part of #432. A long scenario started late sets the end of a full run, so
run_molecule_scenarios.sh now starts scenarios by the durations of the last
full run, longest first, with an unlisted scenario first; the log groups keep
the selection order. A workflow_dispatch input `lanes` reaches the script as
MOLECULE_LANES, so full runs with different lane counts can be compared; an
empty value keeps the default of 4.

The shared molecule config enables ansible.posix.profile_tasks (summary only)
and ansible.posix.timer, so each scenario's log shows where its time goes.

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-11-28 08:36:20 +00:00
1 parent a2ab13a6e1
commit ef840c8952
6 files changed
+123 -8

No files matched your search

@@ -42,7 +42,7 @@ exit 0
# The scenario runner: records its arguments, paths and stdin.
RUNNER_STUB = """#!/usr/bin/env bash
echo "runner args=$* path=$PATH acp=$ANSIBLE_COLLECTIONS_PATH inject=${ANSIBLE_INJECT_FACT_VARS-unset} stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
echo "runner args=$* path=$PATH acp=$ANSIBLE_COLLECTIONS_PATH inject=${ANSIBLE_INJECT_FACT_VARS-unset} lanes=${MOLECULE_LANES-unset} stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
"""
@@ -161,6 +161,12 @@ class RunMoleculeInCiImageTest(unittest.TestCase):
runner,
)
def test_the_lane_count_reaches_the_scenario_runner(self):
result = self.run_script(MOLECULE_LANES="3")
self.assertEqual(result.returncode, 0, result.stderr)
(runner,) = self.calls("runner")
self.assertIn(" lanes=3 ", runner)
def test_the_load_check_sees_the_selection_and_the_final_path(self):
selection = "roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
result = self.run_script(selection, STUB_FAIL_sha256sum="1")
@@ -283,6 +283,52 @@ class RunMoleculeScenariosTest(unittest.TestCase):
self.assertEqual(proc.returncode, 1)
self.assertIn("::error::ran 1 of 2 selected scenarios", proc.stdout)
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
def test_the_longest_scenarios_start_first(self):
# an unlisted scenario counts as longest; the groups keep the selection order
listed = {
"roles/sshd/molecule/default": "sshd-instance",
"roles/ufw/molecule/default": "ufw-instance",
}
for scenario, name in listed.items():
d = self.root / scenario
d.mkdir(parents=True)
platforms = [{"name": f"{RUN_ID}-{name}", "image": "${MOLECULE_IMAGE}"}]
(d / "molecule.yml").write_text(yaml.safe_dump({"platforms": platforms}))
order = [
"extensions/molecule/image-smoke",
"roles/sshd/molecule/default",
"roles/alpha/molecule/default",
"roles/ufw/molecule/default",
]
proc = self.run_script(order, MOLECULE_LANES="1")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
spans = self.events()
self.assertEqual(
sorted(spans, key=lambda label: spans[label][0]),
["alpha/default", "ufw/default", "sshd/default", "image-smoke"],
)
groups = re.findall(
r"^::group::molecule test: (\S+) ", proc.stdout, re.MULTILINE
)
self.assertEqual(
groups, ["image-smoke", "sshd/default", "alpha/default", "ufw/default"]
)
def test_the_duration_list_names_existing_scenarios(self):
# a renamed scenario would otherwise drop out of the list unnoticed
listed = re.findall(r"^ \[([^]]+)\]=\d+$", SCRIPT.read_text(), re.MULTILINE)
self.assertTrue(listed)
for scenario in listed:
self.assertTrue((REPO / scenario / "molecule.yml").is_file(), scenario)
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
def test_empty_lanes_mean_the_default(self):
# the workflow passes an empty value outside a manual dispatch
proc = self.run_script(list(PLATFORMS), MOLECULE_LANES="")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
self.assertIn("Scenarios run: 4", proc.stdout)
def test_lanes_must_be_a_positive_integer(self):
for lanes in ("0", "-1", "two", "04"):
proc = self.run_script(list(PLATFORMS), MOLECULE_LANES=lanes)
@@ -416,6 +462,20 @@ class ScenarioPlatformsTest(unittest.TestCase):
)
class LanesInputTest(unittest.TestCase):
"""A manual dispatch can set the lane count of the run script inside the run container."""
def test_the_dispatch_input_reaches_the_run_container(self):
workflow = yaml.safe_load(WORKFLOW.read_text())
# yaml reads the `on:` key as True
lanes = workflow[True]["workflow_dispatch"]["inputs"]["lanes"]
self.assertEqual((lanes["type"], lanes["default"]), ("string", ""))
steps = workflow["jobs"]["molecule"]["steps"]
step = next(s for s in steps if s.get("name") == "Run the scenarios")
self.assertEqual(step["env"]["MOLECULE_LANES"], "${{ inputs.lanes }}")
self.assertRegex(step["run"], r"docker run [^<]*-e MOLECULE_LANES\b")
class ScenarioSequenceTest(unittest.TestCase):
"""A role scenario previews the fresh host and the configured host with molecule's check step."""