ci(molecule): skip the Molecule runner when no scenario is selected

A pull request that selects no scenario still took the single Molecule
runner slot to pull the controller image, select nothing and exit.

The workflow now has three jobs. select runs the selector from main's
copy in a job container on kamino-docker, in the molecule-controller pin,
and outputs the selected list. scenarios runs it on kamino-molecule and
is skipped when the list is empty. result runs always() and is the one
job to require: it fails unless select passed and scenarios passed or
was skipped for an empty list, because Gitea counts a skipped job as
passing. Scheduled and dispatched runs still select every scenario.

container.image cannot read env, so the controller pin is written in all
three jobs; a test holds the copies equal. The pin moves to 0.1.1, which
has git for the select job's checkout. Branch protection must then
require "Molecule / Result (pull_request)".

Closes #433

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-12-01 23:20:01 +00:00
1 parent 492a68395e
commit 2d1f142662
5 files changed
+236 -37

No files matched your search

+83 -3
View File
@@ -355,7 +355,7 @@ class RunMoleculeScenariosTest(unittest.TestCase):
def test_the_default_time_limit_is_inside_the_job_timeout(self):
# a limit past the job's own would let a hang take the job and every log with it
timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][
timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["scenarios"][
"timeout-minutes"
]
limit = re.findall(r"MOLECULE_SCENARIO_TIMEOUT:-(\d+)\}", SCRIPT.read_text())
@@ -502,7 +502,7 @@ class ScenarioPlatformsTest(unittest.TestCase):
def test_the_sweep_spares_containers_younger_than_the_job_timeout(self):
# a shorter bound would remove the containers of a run still going
timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][
timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["scenarios"][
"timeout-minutes"
]
run = step_run("Remove the containers earlier runs left")
@@ -519,12 +519,92 @@ class LanesInputTest(unittest.TestCase):
# 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"]
steps = workflow["jobs"]["scenarios"]["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 WorkflowJobsTest(unittest.TestCase):
"""A pull request that selects no scenario takes no Molecule runner slot."""
JOBS = yaml.safe_load(WORKFLOW.read_text())["jobs"]
def result(self, select, scenarios, selected):
"""The exit code of the result job's check for those needs results and that selection."""
(step,) = self.JOBS["result"]["steps"]
env = dict(
os.environ,
SELECT_RESULT=select,
SCENARIOS_RESULT=scenarios,
SELECTED=selected,
)
return subprocess.run(
["bash", "-e", "-c", step["run"]],
check=False,
env=env,
capture_output=True,
).returncode
def test_three_jobs_each_on_its_runner(self):
self.assertEqual(
{name: job["runs-on"] for name, job in self.JOBS.items()},
{
"select": "kamino-docker",
"scenarios": ["kamino-molecule"],
"result": "kamino-docker",
},
)
def test_only_the_scenarios_job_runs_on_the_host(self):
# select and result need no Docker daemon, so they take no Molecule runner slot
self.assertIn("container", self.JOBS["select"])
self.assertIn("container", self.JOBS["result"])
self.assertNotIn("container", self.JOBS["scenarios"])
def test_every_job_has_the_fork_guard(self):
guard = self.JOBS["select"]["if"]
self.assertIn("github.repository == 'mortis/kamino'", guard)
for name, job in self.JOBS.items():
self.assertTrue(job["if"].endswith(guard), name)
def test_the_scenarios_run_only_for_a_selection(self):
job = self.JOBS["scenarios"]
self.assertEqual(job["needs"], "select")
self.assertTrue(job["if"].startswith("needs.select.outputs.scenarios != '' &&"))
self.assertEqual(
self.JOBS["select"]["outputs"]["scenarios"],
"${{ steps.select.outputs.scenarios }}",
)
step = next(s for s in job["steps"] if s.get("name") == "Run the scenarios")
self.assertEqual(
step["env"]["SELECTED"], "${{ needs.select.outputs.scenarios }}"
)
self.assertIn('run_molecule_in_ci_image.sh <<< "$SELECTED"', step["run"])
def test_the_result_runs_whatever_the_others_ended_as(self):
job = self.JOBS["result"]
self.assertEqual(job["needs"], ["select", "scenarios"])
self.assertTrue(job["if"].startswith("always() &&"))
def test_the_result_passes_a_run_or_an_empty_selection_only(self):
self.assertEqual(
self.result("success", "success", "roles/a/molecule/default"), 0
)
self.assertEqual(self.result("success", "skipped", ""), 0)
self.assertNotEqual(
self.result("success", "skipped", "roles/a/molecule/default"), 0
)
self.assertNotEqual(
self.result("success", "failure", "roles/a/molecule/default"), 0
)
self.assertNotEqual(
self.result("success", "cancelled", "roles/a/molecule/default"), 0
)
self.assertNotEqual(self.result("failure", "skipped", ""), 0)
self.assertNotEqual(self.result("skipped", "skipped", ""), 0)
class ScenarioSequenceTest(unittest.TestCase):
"""A role scenario previews the fresh host and the configured host with molecule's check step."""
@@ -678,13 +678,31 @@ class WorkflowPinsTest(unittest.TestCase):
def test_the_workflow_has_each_pin_where_the_selector_looks(self):
for key in ("CONTROLLER_IMAGE", "MOLECULE_IMAGE", "MOLECULE_IMAGE_2604"):
self.assertEqual(self.edited(self.pin(key), self.bumped(key)), {key}, key)
old, new = (
f"{key}: >-\n {v}" for v in (self.pin(key), self.bumped(key))
)
self.assertEqual(self.edited(old, new), {key}, key)
def test_the_workflow_runs_in_the_controller_image_not_ci(self):
# A ci pin bump must touch only Lint and Build, which IGNORED lists (ADR 0003).
self.assertIn("/mortis/molecule-controller:", self.pin("CONTROLLER_IMAGE"))
self.assertNotIn("/mortis/ci:", self.WORKFLOW)
def test_every_controller_pin_is_the_same(self):
# container.image cannot read env, so the select and result jobs repeat CONTROLLER_IMAGE
jobs = yaml.safe_load(self.WORKFLOW)["jobs"]
pins = [
jobs["select"]["container"]["image"],
self.pin("CONTROLLER_IMAGE"),
jobs["result"]["container"]["image"],
]
self.assertEqual(len(set(pins)), 1, pins)
self.assertEqual(self.WORKFLOW.count("/mortis/molecule-controller:"), len(pins))
# Renovate moves all three, which the selector reads as "any other change": every
# scenario, as for CONTROLLER_IMAGE alone.
bumped = self.WORKFLOW.replace(pins[0], self.bumped("CONTROLLER_IMAGE"))
self.assertIsNone(sel.pin_changes(self.WORKFLOW, bumped))
def test_comments_and_blank_lines_change_nothing(self):
self.assertEqual(self.edited("# Behavioural tests", "# Other words"), set())
self.assertEqual(self.edited("\njobs:\n", "\n\n\njobs:\n"), set())