ci(molecule): select by image pin for a molecule.yml change of only pins or comments
A change to .gitea/workflows/molecule.yml selected every scenario, so a molecule image pin bump or a comment edit ran the full set. The selector now compares the merge-base and HEAD copies of the workflow: - `#` and blank lines outside a block scalar are dropped; inside one (a `run: |` script, where they may be heredoc content) they count; - the line after each CI_IMAGE, MOLECULE_IMAGE and MOLECULE_IMAGE_2604 `>-` key must be its value, deeper than the key and in the shape Renovate writes, in both copies; - comments only select nothing; a MOLECULE_IMAGE* pin selects every scenario with a file naming it, or every scenario when a shared molecule file names it or a file is unreadable or a symlink; - CI_IMAGE, any other difference or a git error selects everything. reason_for() is split out of select() to stay under ruff's complexity limit. The Molecule rule in .gitea/workflows/AGENTS.md describes the new case. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
6e3ea4521b
commit
6eca2f4031
3 files changed
+395
-21
No files matched your search
@@ -452,6 +452,41 @@ class MainTest(unittest.TestCase):
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
|
||||
IMG = "git.rkdr.net/mortis/molecule-ubuntu2604:0.1.1@sha256:" + "a" * 64
|
||||
WORKFLOW_BASE = (
|
||||
f"env:\n # a comment\n MOLECULE_IMAGE_2604: >-\n {IMG}\nrun: x\n"
|
||||
)
|
||||
|
||||
def run_workflow_topic(self, text):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
pathlib.Path(root, ".gitea/workflows").mkdir(parents=True)
|
||||
pathlib.Path(root, sel.WORKFLOW).write_text(self.WORKFLOW_BASE)
|
||||
self.topic_repo(root, {sel.WORKFLOW: text})
|
||||
cwd = os.getcwd()
|
||||
os.chdir(root)
|
||||
try:
|
||||
return self.run_main(["main"], {"EVENT_NAME": "pull_request"})
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
|
||||
def test_a_comment_only_change_selects_nothing(self):
|
||||
code, out, _err = self.run_workflow_topic(
|
||||
self.WORKFLOW_BASE.replace("a comment", "other")
|
||||
)
|
||||
self.assertEqual((code, out), (0, []))
|
||||
|
||||
def test_a_pin_change_selects_by_image_and_another_change_everything(self):
|
||||
code, out, err = self.run_workflow_topic(
|
||||
self.WORKFLOW_BASE.replace(self.IMG, self.IMG.replace("0.1.1", "0.1.2"))
|
||||
)
|
||||
# make_repo's scenarios name no image, so the 2604 pin reaches none of them
|
||||
self.assertEqual((code, out), (0, []))
|
||||
self.assertIn("image pins changed: MOLECULE_IMAGE_2604", err)
|
||||
code, out, _err = self.run_workflow_topic(
|
||||
self.WORKFLOW_BASE.replace("run: x", "run: y")
|
||||
)
|
||||
self.assertEqual((code, len(out)), (0, 3))
|
||||
|
||||
def test_empty_base_ref_argument_runs_all(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
self.make_repo(root)
|
||||
@@ -609,5 +644,202 @@ class MainTest(unittest.TestCase):
|
||||
self.assertTrue(all((REPO / d / "molecule.yml").is_file() for d in found))
|
||||
|
||||
|
||||
class WorkflowPinsTest(unittest.TestCase):
|
||||
WORKFLOW = (REPO / sel.WORKFLOW).read_text()
|
||||
|
||||
def edited(self, old, new):
|
||||
self.assertIn(old, self.WORKFLOW)
|
||||
return sel.pin_changes(self.WORKFLOW, self.WORKFLOW.replace(old, new, 1))
|
||||
|
||||
def pin(self, key):
|
||||
lines = self.WORKFLOW.splitlines()
|
||||
return lines[lines.index(f" {key}: >-") + 1].strip()
|
||||
|
||||
def bumped(self, key):
|
||||
"""The pin of `key` at another digest, as Renovate would write it."""
|
||||
return self.pin(key).split("@sha256:")[0] + "@sha256:" + "0" * 64
|
||||
|
||||
def test_the_workflow_has_each_pin_where_the_selector_looks(self):
|
||||
for key in ("CI_IMAGE", "MOLECULE_IMAGE", "MOLECULE_IMAGE_2604"):
|
||||
self.assertEqual(self.edited(self.pin(key), self.bumped(key)), {key}, key)
|
||||
|
||||
def test_comments_and_blank_lines_change_nothing(self):
|
||||
self.assertEqual(self.edited("# Behavioural tests", "# Other words"), set())
|
||||
self.assertEqual(self.edited("\njobs:\n", "\n\n\njobs:\n"), set())
|
||||
self.assertEqual(
|
||||
self.edited(
|
||||
" # Used by the scenarios", " # Read by the scenarios"
|
||||
),
|
||||
set(),
|
||||
)
|
||||
|
||||
def test_a_hash_line_inside_a_block_scalar_is_content(self):
|
||||
# In a `run: |` script a `#` line may be heredoc content, not a comment.
|
||||
self.assertIsNone(
|
||||
self.edited(" # Assigned first", " # Set first")
|
||||
)
|
||||
|
||||
def test_two_pins(self):
|
||||
changed = sel.pin_changes(
|
||||
self.WORKFLOW,
|
||||
self.WORKFLOW.replace(
|
||||
self.pin("MOLECULE_IMAGE"), self.bumped("MOLECULE_IMAGE")
|
||||
).replace(
|
||||
self.pin("MOLECULE_IMAGE_2604"), self.bumped("MOLECULE_IMAGE_2604")
|
||||
),
|
||||
)
|
||||
self.assertEqual(changed, {"MOLECULE_IMAGE", "MOLECULE_IMAGE_2604"})
|
||||
|
||||
def test_a_pin_value_is_the_next_line_in_renovates_shape(self):
|
||||
value = self.pin("MOLECULE_IMAGE")
|
||||
indent = " " * 8
|
||||
for new in (
|
||||
# YAML folds the comment into the value: '# x git.rkdr.net/...'
|
||||
f"{indent}# x\n{indent}{value}",
|
||||
f"\n{indent}{value}",
|
||||
f"{indent}{value}\n{indent}more",
|
||||
f"{indent}other:1",
|
||||
value,
|
||||
):
|
||||
self.assertIsNone(self.edited(f"{indent}{value}", new), new)
|
||||
|
||||
def test_a_value_line_replaced_by_an_env_key_is_not_narrowed(self):
|
||||
self.assertIsNone(
|
||||
self.edited(
|
||||
" " + self.pin("MOLECULE_IMAGE_2604"),
|
||||
" MOLECULE_IMAGE: other:1",
|
||||
)
|
||||
)
|
||||
|
||||
def test_any_other_change_is_not_narrowed(self):
|
||||
for old, new in (
|
||||
("timeout-minutes: 90", "timeout-minutes: 60"),
|
||||
(" MOLECULE_IMAGE_2604: >-", " MOLECULE_IMAGE_2605: >-"),
|
||||
(" MOLECULE_IMAGE: >-", " MOLECULE_IMAGE: >"),
|
||||
("-e MOLECULE_IMAGE -e", "-e"),
|
||||
(
|
||||
" # Every scenario, nightly",
|
||||
" schedule: []\n # Every scenario, nightly",
|
||||
),
|
||||
("--init", "--init # not a comment line"),
|
||||
):
|
||||
self.assertIsNone(self.edited(old, new), old)
|
||||
|
||||
def test_a_pin_and_another_change_is_not_narrowed(self):
|
||||
text = self.WORKFLOW.replace(
|
||||
self.pin("MOLECULE_IMAGE_2604"), self.bumped("MOLECULE_IMAGE_2604")
|
||||
).replace("timeout-minutes: 90", "timeout-minutes: 60")
|
||||
self.assertIsNone(sel.pin_changes(self.WORKFLOW, text))
|
||||
|
||||
|
||||
SCENARIO_IMAGES = {
|
||||
"extensions/molecule/image-smoke": "image: ${MOLECULE_IMAGE}",
|
||||
"extensions/molecule/image-smoke-2604": "image: ${MOLECULE_IMAGE_2604}",
|
||||
"roles/docker/molecule/default": "${MOLECULE_IMAGE} ${MOLECULE_IMAGE_2604}",
|
||||
"roles/fail2ban/molecule/default": "image: ${MOLECULE_IMAGE}",
|
||||
"roles/system_baseline/molecule/default": "image: ubuntu:24.04",
|
||||
}
|
||||
|
||||
|
||||
class SelectByPinTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(tmp.cleanup)
|
||||
self.root = tmp.name
|
||||
for scenario, text in SCENARIO_IMAGES.items():
|
||||
pathlib.Path(self.root, scenario).mkdir(parents=True)
|
||||
pathlib.Path(self.root, scenario, "molecule.yml").write_text(text)
|
||||
|
||||
def select(self, pins, changed=(sel.WORKFLOW,)):
|
||||
return sel.select(ALL, list(changed), self.root, pins)[0]
|
||||
|
||||
def test_a_2604_pin_selects_the_scenarios_naming_it(self):
|
||||
self.assertEqual(
|
||||
self.select({"MOLECULE_IMAGE_2604"}),
|
||||
["extensions/molecule/image-smoke-2604", "roles/docker/molecule/default"],
|
||||
)
|
||||
|
||||
def test_a_2404_pin_does_not_match_the_2604_name(self):
|
||||
self.assertEqual(
|
||||
self.select({"MOLECULE_IMAGE"}),
|
||||
[
|
||||
"extensions/molecule/image-smoke",
|
||||
"roles/docker/molecule/default",
|
||||
"roles/fail2ban/molecule/default",
|
||||
],
|
||||
)
|
||||
|
||||
def test_comments_only_select_nothing(self):
|
||||
self.assertEqual(self.select(set()), [])
|
||||
|
||||
def test_the_ci_image_or_no_analysis_selects_everything(self):
|
||||
self.assertEqual(self.select({"CI_IMAGE", "MOLECULE_IMAGE"}), ALL)
|
||||
self.assertEqual(self.select(None), ALL)
|
||||
|
||||
def test_pins_do_not_narrow_another_file(self):
|
||||
self.assertEqual(self.select(set(), [sel.WORKFLOW, "meta/runtime.yml"]), ALL)
|
||||
self.assertEqual(self.select(set(), ["meta/runtime.yml"]), ALL)
|
||||
|
||||
def test_a_shared_file_naming_the_pin_selects_everything(self):
|
||||
for shared in (
|
||||
".config/molecule/config.yml",
|
||||
"extensions/molecule/shared/vars.yml",
|
||||
"roles/docker/molecule/resources/prepare.yml",
|
||||
):
|
||||
path = pathlib.Path(self.root, shared)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("image: ${MOLECULE_IMAGE_2604}")
|
||||
try:
|
||||
self.assertEqual(self.select({"MOLECULE_IMAGE_2604"}), ALL, shared)
|
||||
finally:
|
||||
path.unlink()
|
||||
|
||||
def test_a_shared_file_not_naming_the_pin_narrows(self):
|
||||
path = pathlib.Path(self.root, ".config/molecule/config.yml")
|
||||
path.parent.mkdir(parents=True)
|
||||
path.write_text("image: ${MOLECULE_IMAGE}")
|
||||
self.assertEqual(
|
||||
self.select({"MOLECULE_IMAGE_2604"}),
|
||||
["extensions/molecule/image-smoke-2604", "roles/docker/molecule/default"],
|
||||
)
|
||||
|
||||
def test_an_unreadable_scenario_selects_everything(self):
|
||||
shutil.rmtree(pathlib.Path(self.root, "roles/fail2ban/molecule/default"))
|
||||
self.assertEqual(self.select({"MOLECULE_IMAGE_2604"}), ALL)
|
||||
|
||||
def test_a_symlink_selects_everything(self):
|
||||
# os.walk does not follow a directory symlink, so its target would go unread.
|
||||
target = pathlib.Path(self.root, "elsewhere")
|
||||
target.mkdir()
|
||||
(target / "vars.yml").write_text("image: ${MOLECULE_IMAGE_2604}")
|
||||
for link in (
|
||||
"roles/fail2ban/molecule/default/shared",
|
||||
"extensions/molecule/shared",
|
||||
):
|
||||
path = pathlib.Path(self.root, link)
|
||||
path.symlink_to(target, target_is_directory=True)
|
||||
try:
|
||||
self.assertEqual(self.select({"MOLECULE_IMAGE_2604"}), ALL, link)
|
||||
finally:
|
||||
path.unlink()
|
||||
self.assertEqual(
|
||||
self.select({"MOLECULE_IMAGE_2604"}),
|
||||
["extensions/molecule/image-smoke-2604", "roles/docker/molecule/default"],
|
||||
)
|
||||
|
||||
def test_pins_combine_with_a_role(self):
|
||||
self.assertEqual(
|
||||
self.select(
|
||||
{"MOLECULE_IMAGE_2604"},
|
||||
[sel.WORKFLOW, "roles/system_baseline/tasks/main.yml"],
|
||||
),
|
||||
[
|
||||
"extensions/molecule/image-smoke-2604",
|
||||
"roles/docker/molecule/default",
|
||||
"roles/system_baseline/molecule/default",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user