ci(molecule): run the in-image step from a tested script and list the fifth pin file
Assisted-by: Claude Code
This commit is contained in:
1 parent
2e32016c43
commit
8c52c91dfd
8 files changed
+215
-46
No files matched your search
@@ -0,0 +1,156 @@
|
||||
# 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"
|
||||
[ "$1 $2" = "-m venv" ] || exit 1
|
||||
mkdir -p "$3/bin"
|
||||
cp "$STUB_SOURCE" "$3/bin/pip"
|
||||
"""
|
||||
|
||||
# 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)
|
||||
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.assertIn(f"acp={self.pins}/collections\n", line + "\n")
|
||||
(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_a_failing_install_fails_the_run(self):
|
||||
for stub in ("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()
|
||||
Reference in new issue
Block a user