scripts/run_molecule_in_ci_image.sh runs inside the molecule-controller image, not the ci image, so its name pointed readers at the wrong image. Its test file and class follow, and every reference is updated. ADR 0003 keeps the old name: it records what that change touched. main's selector sees the old and the new path (it diffs with --no-renames) and neither is on IGNORED, so this pull request runs every scenario through the renamed script of its own checkout. Assisted-by: Claude Code
205 lines
8.0 KiB
Python
205 lines
8.0 KiB
Python
# SPDX-FileCopyrightText: Defame1297 and contributors
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Tests for scripts/run_molecule_in_controller.sh with stub tools in place of the molecule-controller
|
|
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_controller.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" \\
|
|
"inject=${ANSIBLE_INJECT_FACT_VARS-unset} lanes=${MOLECULE_LANES-unset}" \\
|
|
"stdin=[$(cat | tr '\\n' ' ')]" >> "$STUB_LOG"
|
|
"""
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("bash"), "bash is required")
|
|
class RunMoleculeInControllerTest(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/molecule-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_scenarios_run_with_fact_injection_off(self):
|
|
# A role that reads a bare ansible_<fact> must fail its scenario here (#364), whichever way the pins went.
|
|
for fail in ("", "1"):
|
|
with self.subTest(pins_differ=bool(fail)):
|
|
self.log.write_text("")
|
|
result = self.run_script(STUB_FAIL_sha256sum=fail)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
(runner,) = self.calls("runner")
|
|
self.assertIn(" inject=False ", 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-molecule.lock"
|
|
" -r requirements-molecule.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_lane_count_reaches_the_scenario_runner(self):
|
|
result = self.run_script(MOLECULE_LANES="3")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
(runner,) = self.calls("runner")
|
|
self.assertIn(" lanes=3 ", 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()
|