ci(molecule): isolate runs per run and drop the push trigger on main
Two Molecule runs could not overlap on one Docker daemon because every scenario's container name was fixed; only the runner's single slot kept them apart. Each platform is now named and labelled by KAMINO_RUN_ID (run id and attempt, "local" outside CI), the run script expands those names with molecule's own interpolator before removing them, and the workflow removes its own containers by label and only sweeps labelled leftovers older than the job timeout, so a run still going is never hit. Non-pull-request runs get a concurrency group of their own. Push to main no longer triggers Molecule, Lint or Build: a merged pull request was already tested, and the nightly and manual Molecule runs cover every scenario. Build keeps its v* tag trigger. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
33fc3fd3a5
commit
96cb9246c6
23 files changed
+223
-160
No files matched your search
@@ -5,6 +5,7 @@
|
||||
Run with: python3 -m unittest discover -s tests/scripts
|
||||
"""
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
@@ -21,6 +22,11 @@ import yaml
|
||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||
SCRIPT = REPO / "scripts" / "run_molecule_scenarios.sh"
|
||||
WORKFLOW = REPO / ".gitea" / "workflows" / "molecule.yml"
|
||||
RUN_ID = "${KAMINO_RUN_ID:-local}"
|
||||
RUN_LABEL = "net.rkdr.mortis.molecule-run"
|
||||
# The script expands platform names with molecule's own interpolator, as molecule does.
|
||||
HAS_MOLECULE = importlib.util.find_spec("molecule") is not None
|
||||
NEEDS_MOLECULE = "the run script imports molecule to expand platform names"
|
||||
|
||||
# Records what each run saw, then behaves as STUB_<scenario label> asks: fail, sleep, hang (and
|
||||
# record its pids), detach (as an ansible worker does, into its own process group, whose orphaned
|
||||
@@ -94,8 +100,11 @@ def stub_python(where):
|
||||
).stdout.strip()
|
||||
site = pathlib.Path(site)
|
||||
(site / "docker.py").write_text(DOCKER_SDK_STUB)
|
||||
# PyYAML from the environment running the tests
|
||||
(site / "outer.pth").write_text(str(pathlib.Path(yaml.__file__).parents[1]) + "\n")
|
||||
# PyYAML, and molecule when it is there, from the environment running the tests
|
||||
outer = {pathlib.Path(yaml.__file__).parents[1]}
|
||||
if HAS_MOLECULE:
|
||||
outer.add(pathlib.Path(importlib.util.find_spec("molecule").origin).parents[1])
|
||||
(site / "outer.pth").write_text("".join(f"{p}\n" for p in outer))
|
||||
return where / "bin"
|
||||
|
||||
|
||||
@@ -107,6 +116,11 @@ PLATFORMS = {
|
||||
}
|
||||
|
||||
|
||||
def container_names(run_id):
|
||||
"""Every fixture platform's container name in the run run_id."""
|
||||
return [f"{run_id}-{n}" for names in PLATFORMS.values() for n in names]
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("bash"), "bash is required")
|
||||
class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
@classmethod
|
||||
@@ -126,7 +140,9 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
for scenario, names in PLATFORMS.items():
|
||||
d = self.root / scenario
|
||||
d.mkdir(parents=True)
|
||||
platforms = [{"name": n, "image": "${MOLECULE_IMAGE}"} for n in names]
|
||||
platforms = [
|
||||
{"name": f"{RUN_ID}-{n}", "image": "${MOLECULE_IMAGE}"} for n in names
|
||||
]
|
||||
(d / "molecule.yml").write_text(yaml.safe_dump({"platforms": platforms}))
|
||||
bin_dir = self.tmp / "bin"
|
||||
bin_dir.mkdir()
|
||||
@@ -163,6 +179,7 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
times.setdefault(label, {})[what] = float(stamp)
|
||||
return {k: (v["start"], v.get("end")) for k, v in times.items()}
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_groups_follow_the_selection_order_with_times(self):
|
||||
order = list(PLATFORMS)
|
||||
proc = self.run_script(order, STUB_alpha_default="sleep")
|
||||
@@ -185,6 +202,7 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
self.assertIn("Scenarios run: 4", proc.stdout)
|
||||
self.assertIn(" image-smoke: ", proc.stdout)
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_runs_in_lanes_and_isolates_each_scenario(self):
|
||||
order = list(PLATFORMS)
|
||||
stubs = {
|
||||
@@ -211,13 +229,14 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
smoke = next(s for s in starts if s.startswith("image-smoke "))
|
||||
self.assertIn(f"cwd={self.root} args=test -s image-smoke", smoke)
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_a_failure_is_reported_and_its_containers_still_go(self):
|
||||
proc = self.run_script(list(PLATFORMS), STUB_beta_default="fail")
|
||||
self.assertEqual(proc.returncode, 1)
|
||||
self.assertIn("::error::Failed: beta/default\n", proc.stdout)
|
||||
self.assertIn("Scenarios run: 4", proc.stdout)
|
||||
docker = self.docker_log.read_text().splitlines()
|
||||
for name in (n for names in PLATFORMS.values() for n in names):
|
||||
for name in container_names("local"):
|
||||
self.assertIn(f"list {name} all=True", docker)
|
||||
self.assertIn(f"remove {name} force=True", docker)
|
||||
# a filter match by another name is left alone
|
||||
@@ -225,12 +244,25 @@ class RunMoleculeScenariosTest(unittest.TestCase):
|
||||
[line for line in docker if line.endswith("-other force=True")]
|
||||
)
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_the_containers_removed_are_the_run_s_own(self):
|
||||
proc = self.run_script(list(PLATFORMS), KAMINO_RUN_ID="r7")
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
removed = [
|
||||
line.split()[1]
|
||||
for line in self.docker_log.read_text().splitlines()
|
||||
if line.startswith("remove ")
|
||||
]
|
||||
self.assertEqual(sorted(removed), sorted(container_names("r7")))
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_molecule_gets_no_stdin(self):
|
||||
proc = self.run_script(list(PLATFORMS), STUB_alpha_default="stdin")
|
||||
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
||||
self.assertIn("stdin of alpha/default: []", proc.stdout)
|
||||
self.assertIn("Scenarios run: 4", proc.stdout)
|
||||
|
||||
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
|
||||
def test_a_failed_removal_fails_the_scenario(self):
|
||||
proc = self.run_script(["roles/alpha/molecule/default"], STUB_RM_FAIL="1")
|
||||
self.assertEqual(proc.returncode, 1)
|
||||
@@ -326,32 +358,32 @@ def alive(pid):
|
||||
return stat.rsplit(")", 1)[1].split()[0] != "Z"
|
||||
|
||||
|
||||
def stale_filters():
|
||||
"""The name filters of the workflow's stale-container cleanup, as docker matches /<name>."""
|
||||
def label_filters():
|
||||
"""The label filters of the workflow's docker commands, as `--filter label=<key>[=<value>]`."""
|
||||
workflow = yaml.safe_load(WORKFLOW.read_text())
|
||||
steps = [s for job in workflow["jobs"].values() for s in job.get("steps", [])]
|
||||
run = next(s["run"] for s in steps if s.get("name") == "Run the scenarios")
|
||||
stale = next(line for line in run.splitlines() if line.strip().startswith("stale="))
|
||||
return re.findall(r"--filter 'name=([^']+)'", stale)
|
||||
runs = "\n".join(s.get("run", "") for s in steps)
|
||||
return re.findall(r"--filter ['\"]?label=([^'\"\s]+)", runs)
|
||||
|
||||
|
||||
class ScenarioPlatformNamesTest(unittest.TestCase):
|
||||
"""The run script removes containers by literal platform name, and scenarios run side by side."""
|
||||
class ScenarioPlatformsTest(unittest.TestCase):
|
||||
"""Scenarios run side by side and runs overlap, so each container is named and labelled by its run."""
|
||||
|
||||
def test_platform_names_are_literal_unique_and_cleared_before_a_run(self):
|
||||
def test_platforms_are_named_and_labelled_by_the_run(self):
|
||||
files = sorted(REPO.glob("roles/*/molecule/*/molecule.yml"))
|
||||
files += sorted(REPO.glob("extensions/molecule/*/molecule.yml"))
|
||||
self.assertTrue(files)
|
||||
seen = {}
|
||||
stale = stale_filters()
|
||||
self.assertTrue(stale)
|
||||
for path in files:
|
||||
where = path.relative_to(REPO)
|
||||
data = yaml.safe_load(path.read_text())
|
||||
for platform in data["platforms"]:
|
||||
name = platform["name"]
|
||||
where = path.relative_to(REPO)
|
||||
self.assertTrue(
|
||||
any(re.search(f, f"/{name}") for f in stale), f"{where}: {name}"
|
||||
self.assertTrue(name.startswith(f"{RUN_ID}-"), f"{where}: {name}")
|
||||
self.assertEqual(
|
||||
platform.get("labels", {}).get(RUN_LABEL),
|
||||
RUN_ID,
|
||||
f"{where}: {name}",
|
||||
)
|
||||
self.assertNotIn(
|
||||
name, seen, f"{where} and {seen.get(name)} share {name}"
|
||||
@@ -359,6 +391,12 @@ class ScenarioPlatformNamesTest(unittest.TestCase):
|
||||
seen[name] = where
|
||||
self.assertNotIn("destroy", data["scenario"]["test_sequence"], path)
|
||||
|
||||
def test_the_workflow_finds_the_containers_by_the_run_label(self):
|
||||
filters = label_filters()
|
||||
# the sweep of earlier runs filters by the key, the cleanup of this run by key and value
|
||||
self.assertIn(RUN_LABEL, filters)
|
||||
self.assertIn(f"{RUN_LABEL}=$KAMINO_RUN_ID", filters)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user