The Build check failed in ansible-test sanity: pylint reported tests/scripts/test_ansible_lint_wrapper.py:37:13 disallowed-name for `_`, from unpacking the middle of str.partition. Ruff accepts the name, but the pylint configuration of ansible-test, which also scans the Python under tests/, does not. Index the partition result instead. Reproduced locally with ansible-test sanity --test pylint on a copy of the tree under ansible_collections/mortis/kamino: the committed file fails with the same message, this one passes. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
157 lines
6.1 KiB
Python
157 lines
6.1 KiB
Python
# SPDX-FileCopyrightText: Defame1297 and contributors
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Tests for scripts/ansible-lint.sh against a throwaway copy of the script and a stub ansible-lint.
|
|
|
|
Run with: python3 -m unittest discover -s tests/scripts
|
|
"""
|
|
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
SCRIPT = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "ansible-lint.sh"
|
|
|
|
# Prints what the wrapper hands to ansible-lint: its arguments, directory, the two variables and
|
|
# the temporary collections root (the first element of the path) with every link inside it.
|
|
STUB = """#!/bin/sh
|
|
first="${ANSIBLE_COLLECTIONS_PATH%%:*}"
|
|
echo "args=$*"
|
|
echo "cwd=$(pwd -P)"
|
|
echo "home=${ANSIBLE_HOME-unset}"
|
|
echo "path=${ANSIBLE_COLLECTIONS_PATH-unset}"
|
|
echo "first=$first"
|
|
for link in "$first"/ansible_collections/*/*; do
|
|
echo "link=${link#"$first"/ansible_collections/} -> $(readlink "$link")"
|
|
done
|
|
exit "${STUB_EXIT:-0}"
|
|
"""
|
|
|
|
|
|
def parsed(stdout):
|
|
"""{key: value} of the stub's output; `link` may repeat, so it is collected in a list."""
|
|
out = {"link": []}
|
|
for line in stdout.splitlines():
|
|
parts = line.partition("=")
|
|
if parts[0] == "link":
|
|
out["link"].append(parts[2])
|
|
else:
|
|
out[parts[0]] = parts[2]
|
|
return out
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("bash"), "bash is required")
|
|
class AnsibleLintWrapperTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-lintwrap-")).resolve()
|
|
self.addCleanup(shutil.rmtree, self.tmp, True)
|
|
self.repo = self.tmp / "repo"
|
|
(self.repo / "scripts").mkdir(parents=True)
|
|
shutil.copy(SCRIPT, self.repo / "scripts" / "ansible-lint.sh")
|
|
self.galaxy("mortis", "kamino")
|
|
self.bin = self.tmp / "bin"
|
|
self.bin.mkdir()
|
|
stub = self.bin / "ansible-lint"
|
|
stub.write_text(STUB)
|
|
stub.chmod(0o755)
|
|
|
|
def galaxy(self, namespace, name):
|
|
(self.repo / "galaxy.yml").write_text(
|
|
f"---\nnamespace: {namespace}\nname: {name}\nversion: 1.0.0\n"
|
|
)
|
|
|
|
def run_wrapper(self, cwd=None, **env):
|
|
base = {"PATH": f"{self.bin}:{os.environ['PATH']}", "HOME": str(self.tmp)}
|
|
return subprocess.run(
|
|
["bash", str(self.repo / "scripts" / "ansible-lint.sh")],
|
|
env={**base, **env},
|
|
cwd=str(cwd or self.repo),
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
|
|
def install_collections(self):
|
|
(self.repo / ".ansible" / "collections" / "ansible_collections").mkdir(
|
|
parents=True
|
|
)
|
|
|
|
def test_uses_the_clone_collections_when_the_variable_is_unset(self):
|
|
self.install_collections()
|
|
result = self.run_wrapper()
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
seen = parsed(result.stdout)
|
|
self.assertEqual(seen["args"], "--offline --nocolor")
|
|
self.assertEqual(seen["home"], str(self.repo / ".ansible"))
|
|
# The temporary link root comes first, then the clone's pinned collections.
|
|
self.assertEqual(
|
|
seen["path"], f"{seen['first']}:{self.repo / '.ansible' / 'collections'}"
|
|
)
|
|
|
|
def test_leaves_a_preset_collections_path_after_the_link_root(self):
|
|
# CI sets it (the ci image bakes the collections into /opt/collections), even with none in the clone.
|
|
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
seen = parsed(result.stdout)
|
|
self.assertEqual(seen["path"], f"{seen['first']}:/opt/collections")
|
|
self.assertEqual(seen["home"], "unset")
|
|
|
|
def test_links_the_checkout_as_the_collection(self):
|
|
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(
|
|
parsed(result.stdout)["link"], [f"mortis/kamino -> {self.repo}"]
|
|
)
|
|
|
|
def test_takes_the_collection_name_from_galaxy_yml(self):
|
|
self.galaxy("acme", "widgets")
|
|
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(
|
|
parsed(result.stdout)["link"], [f"acme/widgets -> {self.repo}"]
|
|
)
|
|
|
|
def test_fails_without_a_namespace_and_name(self):
|
|
(self.repo / "galaxy.yml").write_text("---\nversion: 1.0.0\n")
|
|
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("galaxy.yml", result.stderr)
|
|
self.assertNotIn("args=", result.stdout)
|
|
|
|
def test_removes_the_link_root_afterwards(self):
|
|
result = self.run_wrapper(ANSIBLE_COLLECTIONS_PATH="/opt/collections")
|
|
first = pathlib.Path(parsed(result.stdout)["first"])
|
|
self.assertNotEqual(first, pathlib.Path("/opt/collections"))
|
|
self.assertFalse(first.exists())
|
|
# The checkout itself is untouched: only the link went.
|
|
self.assertTrue((self.repo / "galaxy.yml").is_file())
|
|
|
|
def test_removes_the_link_root_and_keeps_the_exit_code_when_ansible_lint_fails(
|
|
self,
|
|
):
|
|
result = self.run_wrapper(
|
|
ANSIBLE_COLLECTIONS_PATH="/opt/collections", STUB_EXIT="3"
|
|
)
|
|
self.assertEqual(result.returncode, 3)
|
|
self.assertFalse(pathlib.Path(parsed(result.stdout)["first"]).exists())
|
|
|
|
def test_fails_with_the_fix_when_the_collections_are_not_installed(self):
|
|
result = self.run_wrapper()
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn("scripts/dev-setup.sh", result.stderr)
|
|
self.assertNotIn("args=", result.stdout)
|
|
|
|
def test_runs_from_the_repository_root_whatever_the_caller_directory(self):
|
|
self.install_collections()
|
|
elsewhere = self.tmp / "elsewhere"
|
|
elsewhere.mkdir()
|
|
result = self.run_wrapper(cwd=elsewhere)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(parsed(result.stdout)["cwd"], str(self.repo))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|