ci(molecule): select no scenario for a galaxy.yml change, guarded by a load check
Scenarios load mortis.kamino from the checkout, not the copy molecule installs from galaxy.yml, so galaxy.yml goes on the selector's IGNORED list. A new check, run by run_molecule_in_ci_image.sh before the scenarios, fails the run when the environment molecule gives ansible-playbook would resolve the collection anywhere else. Closes #355 Assisted-by: Claude Code
This commit is contained in:
1 parent
fcd331be06
commit
03b21a9aef
7 files changed
+274
-5
No files matched your search
@@ -0,0 +1,125 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Tests for scripts/check_scenarios_load_checkout.py with real molecule and ansible-config.
|
||||
|
||||
Run with: python3 -m unittest discover -s tests/scripts
|
||||
"""
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import yaml
|
||||
|
||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||
SCRIPT = REPO / "scripts" / "check_scenarios_load_checkout.py"
|
||||
HAS_TOOLS = (
|
||||
importlib.util.find_spec("molecule") is not None
|
||||
and shutil.which("ansible-config", path=os.path.dirname(sys.executable)) is not None
|
||||
)
|
||||
NEEDS_TOOLS = "the check runs molecule's config and ansible-config"
|
||||
|
||||
|
||||
def collection(root):
|
||||
"""A mortis.kamino collection at <root>/ansible_collections/mortis/kamino."""
|
||||
path = root / "ansible_collections" / "mortis" / "kamino"
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
(path / "galaxy.yml").write_text(
|
||||
"namespace: mortis\nname: kamino\nversion: 1.0.0\n"
|
||||
)
|
||||
return path
|
||||
|
||||
|
||||
@unittest.skipUnless(HAS_TOOLS, NEEDS_TOOLS)
|
||||
class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-loads-")).resolve()
|
||||
self.addCleanup(shutil.rmtree, self.tmp, True)
|
||||
self.ac_root = self.tmp / "ac"
|
||||
self.checkout = collection(self.ac_root)
|
||||
(self.checkout / "scripts").mkdir()
|
||||
shutil.copy(SCRIPT, self.checkout / "scripts")
|
||||
self.other = self.tmp / "other"
|
||||
collection(self.other)
|
||||
self.scenario("roles/alpha/molecule/default")
|
||||
self.scenario("extensions/molecule/image-smoke")
|
||||
self.env = {
|
||||
"PATH": f"{os.path.dirname(sys.executable)}:{os.environ['PATH']}",
|
||||
"HOME": str(self.tmp),
|
||||
"ANSIBLE_HOME": str(self.tmp / "ansible-home"),
|
||||
"ANSIBLE_COLLECTIONS_PATH": f"{self.ac_root}:{self.other}",
|
||||
}
|
||||
|
||||
def scenario(self, path, env=None):
|
||||
d = self.checkout / path
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
config = {"driver": {"name": "default"}, "platforms": [{"name": "instance"}]}
|
||||
if env:
|
||||
config["provisioner"] = {"name": "ansible", "env": env}
|
||||
(d / "molecule.yml").write_text(yaml.safe_dump(config))
|
||||
return d
|
||||
|
||||
def run_check(self, selection="roles/alpha/molecule/default\n", **env):
|
||||
return subprocess.run(
|
||||
[sys.executable, "-I", str(self.checkout / "scripts" / SCRIPT.name)],
|
||||
cwd=self.checkout,
|
||||
env={**self.env, **env},
|
||||
input=selection,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
def assert_refused(self, result, found):
|
||||
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
||||
self.assertIn("::error::", result.stdout)
|
||||
self.assertIn("must load mortis.kamino from the checkout", result.stdout)
|
||||
self.assertIn(found, result.stdout)
|
||||
|
||||
def test_the_checkout_first_passes(self):
|
||||
result = self.run_check(
|
||||
"roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_another_copy_first_fails(self):
|
||||
result = self.run_check(ANSIBLE_COLLECTIONS_PATH=f"{self.other}:{self.ac_root}")
|
||||
self.assert_refused(result, str(self.other))
|
||||
|
||||
def test_a_scenario_env_that_puts_another_copy_first_fails(self):
|
||||
# The inherited path is right; molecule's provisioner env is what ansible-playbook gets.
|
||||
self.scenario(
|
||||
"roles/alpha/molecule/default",
|
||||
env={"ANSIBLE_COLLECTIONS_PATH": str(self.other)},
|
||||
)
|
||||
self.assert_refused(self.run_check(), str(self.other))
|
||||
|
||||
def test_a_copy_next_to_the_playbooks_fails(self):
|
||||
# ansible-playbook searches the playbook's own collections/ directory first.
|
||||
d = self.checkout / "roles/alpha/molecule/default"
|
||||
collection(d / "collections")
|
||||
self.assert_refused(self.run_check(), str(d / "collections"))
|
||||
|
||||
def test_no_copy_on_the_paths_fails(self):
|
||||
result = self.run_check(ANSIBLE_COLLECTIONS_PATH=str(self.tmp / "empty"))
|
||||
self.assert_refused(result, "no mortis.kamino")
|
||||
|
||||
def test_the_failing_scenario_is_named(self):
|
||||
self.scenario(
|
||||
"extensions/molecule/image-smoke",
|
||||
env={"ANSIBLE_COLLECTIONS_PATH": str(self.other)},
|
||||
)
|
||||
result = self.run_check(
|
||||
"roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
|
||||
)
|
||||
self.assert_refused(result, "extensions/molecule/image-smoke")
|
||||
self.assertNotIn("roles/alpha/molecule/default", result.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -33,6 +33,13 @@ mkdir -p "$3/bin"
|
||||
cp "$STUB_SOURCE" "$3/bin/pip"
|
||||
"""
|
||||
|
||||
# The python that runs the load check: records its arguments, paths and stdin.
|
||||
CHECK_STUB = """#!/usr/bin/env bash
|
||||
echo "python3 args=$* acp=$ANSIBLE_COLLECTIONS_PATH stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
|
||||
[ -n "${STUB_FAIL_check:-}" ] && exit 1
|
||||
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 stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
|
||||
@@ -57,6 +64,8 @@ class RunMoleculeInCiImageTest(unittest.TestCase):
|
||||
stub.chmod(0o755)
|
||||
for name in ("sha256sum", "ansible-galaxy", "ansible", "molecule"):
|
||||
shutil.copy(stub, bin_dir / name)
|
||||
(bin_dir / "python3").write_text(CHECK_STUB)
|
||||
(bin_dir / "python3").chmod(0o755)
|
||||
python = self.tmp / "python"
|
||||
python.write_text(PYTHON_STUB)
|
||||
python.chmod(0o755)
|
||||
@@ -142,6 +151,25 @@ class RunMoleculeInCiImageTest(unittest.TestCase):
|
||||
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")
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
(check,) = self.calls("python3")
|
||||
self.assertIn("args=-I scripts/check_scenarios_load_checkout.py ", check)
|
||||
self.assertIn(
|
||||
f"acp={self.ac_root}:{self.pins}/collections:/opt/collections ", check
|
||||
)
|
||||
self.assertIn(
|
||||
"stdin=[roles/alpha/molecule/default extensions/molecule/image-smoke ]",
|
||||
check,
|
||||
)
|
||||
|
||||
def test_a_failing_load_check_runs_no_scenario(self):
|
||||
result = self.run_script(STUB_FAIL_check="1")
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual(self.calls("runner"), [])
|
||||
|
||||
def test_a_failing_install_fails_the_run(self):
|
||||
for stub in ("python", "pip", "ansible-galaxy"):
|
||||
with self.subTest(stub=stub):
|
||||
|
||||
@@ -210,9 +210,14 @@ class SelectTest(unittest.TestCase):
|
||||
chosen, _reason = self.select(["docs/x.md", "roles/docker/tasks/main.yml"])
|
||||
self.assertEqual(chosen, ["roles/docker/molecule/default"])
|
||||
|
||||
def test_galaxy_yml_selects_nothing_and_does_not_mask_a_role(self):
|
||||
chosen, _reason = self.select(["galaxy.yml"])
|
||||
self.assertEqual(chosen, [])
|
||||
chosen, _reason = self.select(["galaxy.yml", "roles/docker/tasks/main.yml"])
|
||||
self.assertEqual(chosen, ["roles/docker/molecule/default"])
|
||||
|
||||
def test_anything_else_selects_everything(self):
|
||||
for path in (
|
||||
"galaxy.yml",
|
||||
"meta/runtime.yml",
|
||||
"plugins/modules/x.py",
|
||||
"extensions/molecule/requirements.yml",
|
||||
|
||||
Reference in new issue
Block a user