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
This commit is contained in:
Defame1297 committed 2026-11-28 08:45:05 +00:00
1 parent ef840c8952
commit e34a0eb2e0
2 files changed
+33 -23

No files matched your search

+6 -4
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# The Molecule workflow's "Run the scenarios" step: `molecule test` for each scenario directory on # 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 # stdin, MOLECULE_LANES (default 4, also when empty) at a time, longest first, each with its own
# <work-dir>/<n>/ (no collection is installed there: .config/molecule/config.yml sets prerun false). # 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. # Exit 1 on a failure.
# #
# scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs # scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs
@@ -78,8 +79,9 @@ done
mkdir -p "$work" mkdir -p "$work"
# Seconds each scenario took in the last full run, so the longest start first and none started # 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 <label>: exit 0 after S s` # late sets the end of the run. Keyed by scenario directory, not label: refresh from a full run's
# lines; a scenario missing here is assumed long and starts first. # `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=( declare -A last_seconds=(
[roles/system_baseline/molecule/default]=1127 [roles/system_baseline/molecule/default]=1127
[roles/ufw/molecule/default]=907 [roles/ufw/molecule/default]=907
+27 -19
View File
@@ -286,48 +286,42 @@ class RunMoleculeScenariosTest(unittest.TestCase):
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE) @unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
def test_the_longest_scenarios_start_first(self): def test_the_longest_scenarios_start_first(self):
# an unlisted scenario counts as longest; the groups keep the selection order # an unlisted scenario counts as longest; the groups keep the selection order
listed = { seconds = listed_seconds()
"roles/sshd/molecule/default": "sshd-instance", roles = [d for d in seconds if d.startswith("roles/")]
"roles/ufw/molecule/default": "ufw-instance", shortest, longest = min(roles, key=seconds.get), max(roles, key=seconds.get)
} for scenario in (shortest, longest):
for scenario, name in listed.items():
d = self.root / scenario d = self.root / scenario
d.mkdir(parents=True) d.mkdir(parents=True)
platforms = [{"name": f"{RUN_ID}-{name}", "image": "${MOLECULE_IMAGE}"}] name = f"{RUN_ID}-{scenario.replace('/', '-')}"
platforms = [{"name": name, "image": "${MOLECULE_IMAGE}"}]
(d / "molecule.yml").write_text(yaml.safe_dump({"platforms": platforms})) (d / "molecule.yml").write_text(yaml.safe_dump({"platforms": platforms}))
order = [ smoke = "extensions/molecule/image-smoke"
"extensions/molecule/image-smoke", order = [smoke, shortest, "roles/alpha/molecule/default", longest]
"roles/sshd/molecule/default", listed = sorted((smoke, shortest, longest), key=seconds.get, reverse=True)
"roles/alpha/molecule/default",
"roles/ufw/molecule/default",
]
proc = self.run_script(order, MOLECULE_LANES="1") proc = self.run_script(order, MOLECULE_LANES="1")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
spans = self.events() spans = self.events()
self.assertEqual( self.assertEqual(
sorted(spans, key=lambda label: spans[label][0]), sorted(spans, key=lambda label: spans[label][0]),
["alpha/default", "ufw/default", "sshd/default", "image-smoke"], [label(d) for d in ["roles/alpha/molecule/default", *listed]],
) )
groups = re.findall( groups = re.findall(
r"^::group::molecule test: (\S+) ", proc.stdout, re.MULTILINE r"^::group::molecule test: (\S+) ", proc.stdout, re.MULTILINE
) )
self.assertEqual( self.assertEqual(groups, [label(d) for d in order])
groups, ["image-smoke", "sshd/default", "alpha/default", "ufw/default"]
)
def test_the_duration_list_names_existing_scenarios(self): def test_the_duration_list_names_existing_scenarios(self):
# a renamed scenario would otherwise drop out of the list unnoticed # a renamed scenario would otherwise drop out of the list unnoticed
listed = re.findall(r"^ \[([^]]+)\]=\d+$", SCRIPT.read_text(), re.MULTILINE) listed = listed_seconds()
self.assertTrue(listed) self.assertTrue(listed)
for scenario in listed: for scenario in listed:
self.assertTrue((REPO / scenario / "molecule.yml").is_file(), scenario) self.assertTrue((REPO / scenario / "molecule.yml").is_file(), scenario)
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE) @unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
def test_empty_lanes_mean_the_default(self): def test_empty_lanes_are_accepted(self):
# the workflow passes an empty value outside a manual dispatch # the workflow passes an empty value outside a manual dispatch
proc = self.run_script(list(PLATFORMS), MOLECULE_LANES="") proc = self.run_script(list(PLATFORMS), MOLECULE_LANES="")
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) 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): def test_lanes_must_be_a_positive_integer(self):
for lanes in ("0", "-1", "two", "04"): for lanes in ("0", "-1", "two", "04"):
@@ -395,6 +389,20 @@ class RunMoleculeScenariosTest(unittest.TestCase):
self.assertEqual(self.log.read_text(), "") self.assertEqual(self.log.read_text(), "")
def listed_seconds():
"""{scenario directory: seconds} from the run script's duration list."""
pairs = re.findall(r"^ \[([^]]+)\]=(\d+)$", SCRIPT.read_text(), re.MULTILINE)
return {d: int(s) for d, s in pairs}
def label(scenario):
"""The run script's label of a scenario directory, as its log groups print it."""
if scenario.startswith("extensions/"):
return scenario.rsplit("/", 1)[1]
parts = scenario.split("/")
return f"{parts[1]}/{parts[3]}"
def alive(pid): def alive(pid):
"""Whether pid is a running process, not gone or a zombie.""" """Whether pid is a running process, not gone or a zombie."""
try: try: