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,97 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Fail when a molecule scenario would load mortis.kamino from anywhere but this checkout.
|
||||
|
||||
python3 -I scripts/check_scenarios_load_checkout.py < scenario-dirs
|
||||
|
||||
Checks the environment molecule hands ansible-playbook, not only the inherited one: a scenario's
|
||||
own env can change it. The rule it holds is in .gitea/workflows/AGENTS.md (Molecule). Exit 1 when
|
||||
a scenario would load another copy, or none.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from molecule.config import Config
|
||||
|
||||
CHECKOUT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||
RULE = (
|
||||
"scenarios must load mortis.kamino from the checkout ({checkout}), not molecule's installed"
|
||||
" copy: the scenario selector ignores galaxy.yml on that rule"
|
||||
" (.gitea/workflows/AGENTS.md, Molecule)"
|
||||
)
|
||||
|
||||
|
||||
def playbook_env(scenario):
|
||||
"""The environment molecule gives ansible-playbook in `scenario`, as a role scenario runs it."""
|
||||
parts = scenario.split("/")
|
||||
cwd = os.path.join(CHECKOUT, *parts[:2]) if parts[0] == "roles" else CHECKOUT
|
||||
# molecule takes the project from the working directory; each call sets it afresh
|
||||
os.chdir(cwd)
|
||||
config = Config(
|
||||
os.path.relpath(os.path.join(CHECKOUT, scenario, "molecule.yml")),
|
||||
args={},
|
||||
command_args={"subcommand": "converge"},
|
||||
)
|
||||
return dict(config.provisioner.env)
|
||||
|
||||
|
||||
def collection_paths(env, cwd):
|
||||
"""COLLECTIONS_PATHS as ansible-config resolves it in `env`, in search order."""
|
||||
env = dict(env)
|
||||
# molecule writes its ansible.cfg only when a step runs; the environment outranks it anyway
|
||||
if not os.path.isfile(env.get("ANSIBLE_CONFIG", "")):
|
||||
env.pop("ANSIBLE_CONFIG", None)
|
||||
out = subprocess.run(
|
||||
["ansible-config", "dump", "-t", "base", "--format", "json"],
|
||||
cwd=cwd,
|
||||
env=env,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
).stdout
|
||||
(entry,) = [e for e in json.loads(out) if e.get("name") == "COLLECTIONS_PATHS"]
|
||||
return entry["value"]
|
||||
|
||||
|
||||
def loaded_copy(paths):
|
||||
"""The mortis.kamino directory the first of `paths` holding one gives, or None."""
|
||||
for path in paths:
|
||||
# a path may name the ansible_collections directory itself
|
||||
base = (
|
||||
path
|
||||
if os.path.basename(path) == "ansible_collections"
|
||||
else os.path.join(path, "ansible_collections")
|
||||
)
|
||||
found = os.path.join(base, "mortis", "kamino")
|
||||
if os.path.isdir(found):
|
||||
return found
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
failed = False
|
||||
with tempfile.TemporaryDirectory(prefix="kamino-loads-") as home:
|
||||
# molecule's config writes its scratch directories under ANSIBLE_HOME
|
||||
os.environ.setdefault("ANSIBLE_HOME", home)
|
||||
for scenario in filter(None, sys.stdin.read().splitlines()):
|
||||
scenario_dir = os.path.join(CHECKOUT, scenario)
|
||||
paths = [os.path.join(scenario_dir, "collections")]
|
||||
paths += collection_paths(playbook_env(scenario), scenario_dir)
|
||||
found = loaded_copy(paths)
|
||||
if found and os.path.realpath(found) == CHECKOUT:
|
||||
continue
|
||||
failed = True
|
||||
print(
|
||||
f"::error::{scenario}: "
|
||||
+ RULE.format(checkout=CHECKOUT)
|
||||
+ f"; it would load {found or 'no mortis.kamino on ' + ':'.join(paths)}"
|
||||
)
|
||||
return 1 if failed else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -4,7 +4,8 @@
|
||||
#
|
||||
# The Molecule workflow's run inside the ci image, from the checkout root: when a pin file differs
|
||||
# from the image's, install the pull request's pins into $AC_ROOT/pins (/opt is not writable for the
|
||||
# runner user) and put them first, then run the scenarios on stdin. CI_BASE_PYTHON is a test seam.
|
||||
# runner user) and put them first, then check that the scenarios on stdin load this checkout and run
|
||||
# them. CI_BASE_PYTHON is a test seam.
|
||||
#
|
||||
# AC_ROOT=<dir holding ansible_collections> scripts/run_molecule_in_ci_image.sh < scenario-dirs
|
||||
set -euo pipefail
|
||||
@@ -31,4 +32,5 @@ molecule --version
|
||||
# The collection under test first; molecule also installs it into each scenario's ANSIBLE_HOME,
|
||||
# which the run script keeps in the job tree.
|
||||
export ANSIBLE_COLLECTIONS_PATH="$AC_ROOT:${extra_collections}/opt/collections"
|
||||
python3 -I scripts/check_scenarios_load_checkout.py <<< "$selected"
|
||||
exec scripts/run_molecule_scenarios.sh "$AC_ROOT/molecule-runs" <<< "$selected"
|
||||
@@ -17,7 +17,7 @@ Only a pull request is narrowed, and only when <base-ref> is given and EVENT_NAM
|
||||
only (the image-smoke scenarios test the images, not the roles); a shared file such as
|
||||
extensions/molecule/requirements.yml is "any other file";
|
||||
* a file on the IGNORED list (documentation, changelog, repository tooling that no
|
||||
scenario reads) selects nothing;
|
||||
scenario reads; galaxy.yml by a rule in .gitea/workflows/AGENTS.md) selects nothing;
|
||||
* any other file selects every scenario.
|
||||
|
||||
A role uses another when any file under its directory names it as `mortis.kamino.<other>`
|
||||
@@ -62,6 +62,7 @@ IGNORED = (
|
||||
"changelogs/*",
|
||||
"LICENSES/*",
|
||||
"README.md",
|
||||
"galaxy.yml",
|
||||
"CONTRIBUTING.md",
|
||||
"SECURITY.md",
|
||||
"GLOSSARY.md",
|
||||
|
||||
Reference in new issue
Block a user