Files
Kamino/tests/scripts/test_run_molecule_in_ci_image.py
T
Defame1297 03b21a9aef
Build / build (pull_request) Successful in 15s
Lint / lint (pull_request) Successful in 1m7s
Molecule / molecule (pull_request) Successful in 15m19s
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
2026-11-24 15:56:27 +00:00

186 lines
7.1 KiB
Python

# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for scripts/run_molecule_in_ci_image.sh with stub tools in place of the ci image's.
Run with: python3 -m unittest discover -s tests/scripts
"""
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[2]
SCRIPT = REPO / "scripts" / "run_molecule_in_ci_image.sh"
# Each stub appends its name, arguments and the paths it saw to STUB_LOG; STUB_FAIL_<name> fails it.
STUB = """#!/usr/bin/env bash
name="$(basename "$0")"
echo "$name args=$* path=$PATH acp=${ANSIBLE_COLLECTIONS_PATH:-}" >> "$STUB_LOG"
key="STUB_FAIL_$(printf %s "$name" | tr -c 'A-Za-z0-9' _)"
[ -n "${!key:-}" ] && exit 1
exit 0
"""
# The base python: `-m venv <dir>` makes a venv whose bin holds a pip stub.
PYTHON_STUB = """#!/usr/bin/env bash
echo "python args=$*" >> "$STUB_LOG"
[ -n "${STUB_FAIL_python:-}" ] && exit 1
[ "$1 $2" = "-m venv" ] || exit 1
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"
"""
@unittest.skipUnless(shutil.which("bash"), "bash is required")
class RunMoleculeInCiImageTest(unittest.TestCase):
def setUp(self):
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-runci-")).resolve()
self.addCleanup(shutil.rmtree, self.tmp, True)
self.ac_root = self.tmp / "ac"
self.checkout = self.ac_root / "ansible_collections" / "mortis" / "kamino"
(self.checkout / "scripts").mkdir(parents=True)
runner = self.checkout / "scripts" / "run_molecule_scenarios.sh"
runner.write_text(RUNNER_STUB)
runner.chmod(0o755)
bin_dir = self.tmp / "bin"
bin_dir.mkdir()
stub = self.tmp / "stub"
stub.write_text(STUB)
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)
self.log = self.tmp / "stub.log"
self.log.touch()
self.env = {
"PATH": f"{bin_dir}:{os.environ['PATH']}",
"HOME": str(self.tmp),
"AC_ROOT": str(self.ac_root),
"CI_BASE_PYTHON": str(python),
"STUB_LOG": str(self.log),
"STUB_SOURCE": str(stub),
}
self.pins = self.ac_root / "pins"
def run_script(self, selection="roles/alpha/molecule/default\n", **env):
return subprocess.run(
["bash", str(SCRIPT)],
cwd=self.checkout,
env={**self.env, **env},
input=selection,
capture_output=True,
text=True,
check=False,
)
def calls(self, name):
return [
line
for line in self.log.read_text().splitlines()
if line.split(" ", 1)[0] == name
]
def test_matching_pins_use_the_baked_tools(self):
result = self.run_script()
self.assertEqual(result.returncode, 0, result.stderr)
(check,) = self.calls("sha256sum")
self.assertIn("args=--quiet -c /opt/ci-pins.sha256 ", check)
self.assertEqual(self.calls("python"), [])
self.assertEqual(self.calls("ansible-galaxy"), [])
self.assertEqual(len(self.calls("ansible")), 1)
self.assertEqual(len(self.calls("molecule")), 1)
(runner,) = self.calls("runner")
self.assertIn(f"acp={self.ac_root}:/opt/collections ", runner)
self.assertIn(f"args={self.ac_root}/molecule-runs ", runner)
def test_changed_pins_are_installed_and_put_first(self):
result = self.run_script(STUB_FAIL_sha256sum="1")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(
self.calls("python"), [f"python args=-m venv {self.pins}/venv"]
)
(pip,) = self.calls("pip")
self.assertIn(
"args=install --quiet --require-hashes -r requirements-dev.lock"
" -r requirements-dev.txt ",
pip,
)
galaxy = self.calls("ansible-galaxy")
self.assertEqual(len(galaxy), 2)
for line, req in zip(
galaxy, ("requirements.yml", "extensions/molecule/requirements.yml")
):
self.assertIn(
f"args=collection install --force -r {req} -p {self.pins}/collections ",
line,
)
# The target alone, so a baked copy is not taken as already installed.
self.assertTrue(line.endswith(f"acp={self.pins}/collections"), line)
(runner,) = self.calls("runner")
self.assertIn(f"path={self.pins}/venv/bin:", runner)
self.assertIn(
f"acp={self.ac_root}:{self.pins}/collections:/opt/collections ", runner
)
def test_the_selection_reaches_the_scenario_runner(self):
selection = "roles/alpha/molecule/default\nextensions/molecule/image-smoke\n"
result = self.run_script(selection)
self.assertEqual(result.returncode, 0, result.stderr)
(runner,) = self.calls("runner")
self.assertIn(
"stdin=[roles/alpha/molecule/default extensions/molecule/image-smoke ]",
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):
self.log.write_text("")
shutil.rmtree(self.pins, True)
key = "STUB_FAIL_" + stub.replace("-", "_")
result = self.run_script(STUB_FAIL_sha256sum="1", **{key: "1"})
self.assertNotEqual(result.returncode, 0)
self.assertEqual(self.calls("runner"), [])
if __name__ == "__main__":
unittest.main()