ci(molecule): fail the run when a scenario has molecule's prerun on #479
No files matched your search
@@ -96,7 +96,9 @@ issue.
|
||||
proves the collection builds; they had caused about a third of full pull request runs and their
|
||||
minutes (#338). *Held:* control: `scripts/check_scenarios_load_checkout.py`, which
|
||||
`run_molecule_in_ci_image.sh` runs on the selected scenarios before they start, fails the run
|
||||
when the environment molecule gives `ansible-playbook` resolves `mortis.kamino` anywhere else.
|
||||
when the environment molecule gives `ansible-playbook` resolves `mortis.kamino` anywhere else,
|
||||
or when `prerun` is not off once the base config is merged as molecule's CLI merges it (a
|
||||
scenario turns it on, or the base config is moved or turns it on).
|
||||
A pull request that selects no scenario runs no check; the next one that does, and the nightly
|
||||
run, catch a change that broke the rule.
|
||||
- **A new file outside `roles/<role>/` runs every scenario until it is added to IGNORED, and it
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
python3 -I scripts/check_scenarios_load_checkout.py < scenario-dirs
|
||||
|
||||
Checks the environment molecule hands ansible-playbook, not only the inherited one: a scenario's
|
||||
own env can change it. The rule it holds is in .gitea/workflows/AGENTS.md (Molecule). Exit 1 when
|
||||
a scenario would load another copy, or none.
|
||||
own env can change it. Also checks that molecule's prerun, its install of the collection, is off
|
||||
once the base config is merged as molecule's CLI merges it. The rule it holds is in
|
||||
.gitea/workflows/AGENTS.md (Molecule). Exit 1 when a scenario would load another copy, or none,
|
||||
or has prerun on.
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -16,6 +18,8 @@ import sys
|
||||
import tempfile
|
||||
|
||||
from molecule.config import Config
|
||||
from molecule.shell import LOCAL_CONFIG_SEARCH
|
||||
from molecule.util import lookup_config_file
|
||||
|
||||
CHECKOUT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||
RULE = (
|
||||
@@ -23,20 +27,25 @@ RULE = (
|
||||
" copy: the scenario selector ignores galaxy.yml on that rule"
|
||||
" (.gitea/workflows/AGENTS.md, Molecule)"
|
||||
)
|
||||
PRERUN = (
|
||||
"prerun must be off, so molecule does not install mortis.kamino again: set `prerun: false`"
|
||||
" in the base config {base} and do not turn it on in the scenario (.gitea/workflows/AGENTS.md, Molecule)"
|
||||
)
|
||||
|
||||
|
||||
def playbook_env(scenario):
|
||||
"""The environment molecule gives ansible-playbook in `scenario`, as a role scenario runs it."""
|
||||
def scenario_config(scenario):
|
||||
"""Molecule's config of `scenario` as `molecule converge` builds it there, and its base config."""
|
||||
parts = scenario.split("/")
|
||||
cwd = os.path.join(CHECKOUT, *parts[:2]) if parts[0] == "roles" else CHECKOUT
|
||||
# molecule takes the project from the working directory; each call sets it afresh
|
||||
# molecule takes the project and the base config from the working directory; set it afresh
|
||||
os.chdir(cwd)
|
||||
base = lookup_config_file(LOCAL_CONFIG_SEARCH)
|
||||
config = Config(
|
||||
os.path.relpath(os.path.join(CHECKOUT, scenario, "molecule.yml")),
|
||||
args={},
|
||||
args={"base_config": [base] if base else []},
|
||||
command_args={"subcommand": "converge"},
|
||||
)
|
||||
return dict(config.provisioner.env)
|
||||
return config, base
|
||||
|
||||
|
||||
def collection_paths(env, cwd):
|
||||
@@ -79,8 +88,16 @@ def main():
|
||||
os.environ.setdefault("ANSIBLE_HOME", home)
|
||||
for scenario in filter(None, sys.stdin.read().splitlines()):
|
||||
scenario_dir = os.path.join(CHECKOUT, scenario)
|
||||
config, base = scenario_config(scenario)
|
||||
# molecule's worker reads this key to decide whether to install the collection
|
||||
if config.config_data["prerun"] is not False:
|
||||
failed = True
|
||||
base = (
|
||||
base or os.path.join(CHECKOUT, LOCAL_CONFIG_SEARCH) + " (not found)"
|
||||
)
|
||||
print(f"::error::{scenario}: " + PRERUN.format(base=base))
|
||||
paths = [os.path.join(scenario_dir, "collections")]
|
||||
paths += collection_paths(playbook_env(scenario), scenario_dir)
|
||||
paths += collection_paths(dict(config.provisioner.env), scenario_dir)
|
||||
found = loaded_copy(paths)
|
||||
if found and os.path.realpath(found) == CHECKOUT:
|
||||
continue
|
||||
|
||||
@@ -30,7 +30,7 @@ fi
|
||||
ansible --version
|
||||
molecule --version
|
||||
# The collection under test first; molecule does not install it again (prerun is off in
|
||||
# .config/molecule/config.yml).
|
||||
# .config/molecule/config.yml, which check_scenarios_load_checkout.py holds).
|
||||
export ANSIBLE_COLLECTIONS_PATH="$AC_ROOT:${extra_collections}/opt/collections"
|
||||
# Facts are not injected as variables (deprecated in ansible-core 2.22, removal scheduled for 2.24, see #364), so a
|
||||
# role that reads a bare `ansible_<fact>` fails its scenario here instead of in a consumer's inventory.
|
||||
|
||||
@@ -43,6 +43,11 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
||||
self.ac_root = self.tmp / "ac"
|
||||
self.checkout = collection(self.ac_root)
|
||||
(self.checkout / "scripts").mkdir()
|
||||
# molecule's CLI finds the base config from the repository root, as in the real checkout
|
||||
subprocess.run(["git", "init", "-q", str(self.checkout)], check=True)
|
||||
self.base_config = self.checkout / ".config" / "molecule" / "config.yml"
|
||||
self.base_config.parent.mkdir(parents=True)
|
||||
self.base_config.write_text("prerun: false\n")
|
||||
shutil.copy(SCRIPT, self.checkout / "scripts")
|
||||
self.other = self.tmp / "other"
|
||||
collection(self.other)
|
||||
@@ -55,10 +60,14 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
||||
"ANSIBLE_COLLECTIONS_PATH": f"{self.ac_root}:{self.other}",
|
||||
}
|
||||
|
||||
def scenario(self, path, env=None):
|
||||
def scenario(self, path, env=None, **keys):
|
||||
d = self.checkout / path
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
config = {"driver": {"name": "default"}, "platforms": [{"name": "instance"}]}
|
||||
config = {
|
||||
"driver": {"name": "default"},
|
||||
"platforms": [{"name": "instance"}],
|
||||
**keys,
|
||||
}
|
||||
if env:
|
||||
config["provisioner"] = {"name": "ansible", "env": env}
|
||||
(d / "molecule.yml").write_text(yaml.safe_dump(config))
|
||||
@@ -81,7 +90,14 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
||||
self.assertIn("must load mortis.kamino from the checkout", result.stdout)
|
||||
self.assertIn(found, result.stdout)
|
||||
|
||||
def assert_prerun_refused(self, result, scenario="roles/alpha/molecule/default"):
|
||||
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
||||
self.assertIn(f"::error::{scenario}: ", result.stdout)
|
||||
self.assertIn("prerun", result.stdout)
|
||||
self.assertIn(".config/molecule/config.yml", result.stdout)
|
||||
|
||||
def test_the_checkout_first_passes(self):
|
||||
# the scenarios inherit prerun off from the base config
|
||||
result = self.run_check(
|
||||
"roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
|
||||
)
|
||||
@@ -120,6 +136,18 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
||||
self.assert_refused(result, "extensions/molecule/image-smoke")
|
||||
self.assertNotIn("roles/alpha/molecule/default", result.stdout)
|
||||
|
||||
def test_a_scenario_with_prerun_on_fails(self):
|
||||
self.scenario("roles/alpha/molecule/default", prerun=True)
|
||||
self.assert_prerun_refused(self.run_check())
|
||||
|
||||
def test_a_missing_base_config_fails(self):
|
||||
self.base_config.unlink()
|
||||
self.assert_prerun_refused(self.run_check())
|
||||
|
||||
def test_a_base_config_with_prerun_on_fails(self):
|
||||
self.base_config.write_text("prerun: true\n")
|
||||
self.assert_prerun_refused(self.run_check())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user