ci(molecule): fail the Molecule result when select's output is lost
An empty select output meant "nothing selected", so an output lost on the way from select to result also passed the required check. select now writes `none` for an empty selection; scenarios runs for neither `none` nor an empty output, and result passes a skipped scenarios only for `none`. The result-logic test runs under sh, as act runs a job container's step without a shell, and a new test parses what select writes to GITHUB_OUTPUT. The fork-guard test compares each job with the literal guard. Assisted-by: Claude Code
This commit is contained in:
1 parent
2d1f142662
commit
479efe1ddc
3 files changed
+61
-23
No files matched your search
@@ -79,10 +79,11 @@ issue.
|
|||||||
pin pull request. *Held:* review.
|
pin pull request. *Held:* review.
|
||||||
- **Molecule has three jobs: `select`, `scenarios` and `result`.** `select` runs the selector in
|
- **Molecule has three jobs: `select`, `scenarios` and `result`.** `select` runs the selector in
|
||||||
a job container on `kamino-docker`, in the `molecule-controller` pin (never the `ci` pin, whose
|
a job container on `kamino-docker`, in the `molecule-controller` pin (never the `ci` pin, whose
|
||||||
bumps would then select every scenario), and outputs the selected directories. `scenarios`
|
bumps would then select every scenario), and outputs the selected directories, or `none`.
|
||||||
runs them on `kamino-molecule` and is skipped when the list is empty. `result` (check
|
`scenarios` runs them on `kamino-molecule` and is skipped for `none` or an empty output.
|
||||||
`Molecule / Result (pull_request)`) is the required check: it runs `if: always()` and fails
|
`result` (check `Molecule / Result (pull_request)`) is the required check: it runs
|
||||||
unless `select` passed and `scenarios` passed or was skipped for an empty list. The
|
`if: always()` and fails unless `select` passed and `scenarios` passed or was skipped for
|
||||||
|
`none`, so a lost output fails it. The
|
||||||
controller pin is written in all three jobs, because `container.image` cannot read `env`.
|
controller pin is written in all three jobs, because `container.image` cannot read `env`.
|
||||||
*Why:* a pull request that selects nothing takes no Molecule runner slot, and a skipped job
|
*Why:* a pull request that selects nothing takes no Molecule runner slot, and a skipped job
|
||||||
counts as passing in Gitea, so only a job that always runs can be required. *Held:* control:
|
counts as passing in Gitea, so only a job that always runs can be required. *Held:* control:
|
||||||
|
|||||||
@@ -110,19 +110,18 @@ jobs:
|
|||||||
echo "::notice::no base copy of $sel; running this checkout's copy"
|
echo "::notice::no base copy of $sel; running this checkout's copy"
|
||||||
fi
|
fi
|
||||||
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
|
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
|
||||||
# One directory per line. A random delimiter, so no line of the list can end the value;
|
# `none` marks an empty selection, so a lost or empty output fails result, never passes.
|
||||||
# an empty selection writes no line, so the output is the empty string.
|
if [ -z "$selected" ]; then selected=none; fi
|
||||||
|
# One directory per line; a random delimiter, so no line of the list can end the value.
|
||||||
delim="scenarios-$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
|
delim="scenarios-$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
|
||||||
{
|
printf 'scenarios<<%s\n%s\n%s\n' "$delim" "$selected" "$delim" >> "$GITHUB_OUTPUT"
|
||||||
echo "scenarios<<$delim"
|
|
||||||
if [ -n "$selected" ]; then printf '%s\n' "$selected"; fi
|
|
||||||
echo "$delim"
|
|
||||||
} >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
scenarios:
|
scenarios:
|
||||||
needs: select
|
needs: select
|
||||||
# Skipped, not failed, when select fails or selects nothing; result tells the two apart.
|
# Skipped, not failed, when select fails, selects nothing (`none`) or loses its output (empty);
|
||||||
|
# result passes only the second.
|
||||||
if: >-
|
if: >-
|
||||||
|
needs.select.outputs.scenarios != 'none' &&
|
||||||
needs.select.outputs.scenarios != '' &&
|
needs.select.outputs.scenarios != '' &&
|
||||||
github.repository == 'mortis/kamino' &&
|
github.repository == 'mortis/kamino' &&
|
||||||
(github.event_name != 'pull_request' ||
|
(github.event_name != 'pull_request' ||
|
||||||
@@ -248,7 +247,7 @@ jobs:
|
|||||||
github.event.pull_request.head.repo.full_name == github.repository)
|
github.event.pull_request.head.repo.full_name == github.repository)
|
||||||
runs-on: kamino-docker
|
runs-on: kamino-docker
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
# Any small image does; the controller pin is reused so there is no other pin to bump.
|
# The controller pin again, so this job adds no pin of its own to bump.
|
||||||
container:
|
container:
|
||||||
image: git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8
|
image: git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8
|
||||||
steps:
|
steps:
|
||||||
@@ -261,8 +260,8 @@ jobs:
|
|||||||
if [ "$SELECT_RESULT" != success ]; then
|
if [ "$SELECT_RESULT" != success ]; then
|
||||||
echo "::error::select ended as $SELECT_RESULT"; exit 1
|
echo "::error::select ended as $SELECT_RESULT"; exit 1
|
||||||
fi
|
fi
|
||||||
# A skip is a pass only for an empty selection: a cancelled or failed run is not.
|
# A skip is a pass only for select's `none`: an empty output means it was lost.
|
||||||
if [ "$SCENARIOS_RESULT" = skipped ] && [ -z "$SELECTED" ]; then
|
if [ "$SCENARIOS_RESULT" = skipped ] && [ "$SELECTED" = none ]; then
|
||||||
echo "No scenario selected."
|
echo "No scenario selected."
|
||||||
elif [ "$SCENARIOS_RESULT" != success ]; then
|
elif [ "$SCENARIOS_RESULT" != success ]; then
|
||||||
echo "::error::scenarios ended as $SCENARIOS_RESULT"; exit 1
|
echo "::error::scenarios ended as $SCENARIOS_RESULT"; exit 1
|
||||||
|
|||||||
@@ -540,12 +540,38 @@ class WorkflowJobsTest(unittest.TestCase):
|
|||||||
SELECTED=selected,
|
SELECTED=selected,
|
||||||
)
|
)
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
["bash", "-e", "-c", step["run"]],
|
# act runs a job container's step with sh when the step sets no shell
|
||||||
|
["sh", "-e", "-c", step["run"]],
|
||||||
check=False,
|
check=False,
|
||||||
env=env,
|
env=env,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
).returncode
|
).returncode
|
||||||
|
|
||||||
|
def written_output(self, selected):
|
||||||
|
"""The `scenarios` output select's run step writes for that selector output."""
|
||||||
|
step = next(s for s in self.JOBS["select"]["steps"] if s.get("id") == "select")
|
||||||
|
block = step["run"].split("|| exit 1\n", 1)[1]
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
out = pathlib.Path(tmp, "output")
|
||||||
|
subprocess.run(
|
||||||
|
["sh", "-e", "-c", 'selected="$1"\n' + block, "sh", selected],
|
||||||
|
check=True,
|
||||||
|
env=dict(os.environ, GITHUB_OUTPUT=str(out)),
|
||||||
|
)
|
||||||
|
lines = out.read_text().splitlines()
|
||||||
|
# GITHUB_OUTPUT's `name<<delimiter` form: the value is every line up to the delimiter
|
||||||
|
name, delim = lines[0].split("<<", 1)
|
||||||
|
self.assertEqual((name, lines[-1]), ("scenarios", delim))
|
||||||
|
self.assertNotIn(delim, lines[1:-1])
|
||||||
|
return "\n".join(lines[1:-1])
|
||||||
|
|
||||||
|
def test_select_writes_none_for_an_empty_selection(self):
|
||||||
|
self.assertEqual(self.written_output(""), "none")
|
||||||
|
|
||||||
|
def test_select_writes_the_selection_one_directory_per_line(self):
|
||||||
|
selected = "extensions/molecule/image-smoke\nroles/a/molecule/default"
|
||||||
|
self.assertEqual(self.written_output(selected), selected)
|
||||||
|
|
||||||
def test_three_jobs_each_on_its_runner(self):
|
def test_three_jobs_each_on_its_runner(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
{name: job["runs-on"] for name, job in self.JOBS.items()},
|
{name: job["runs-on"] for name, job in self.JOBS.items()},
|
||||||
@@ -563,15 +589,25 @@ class WorkflowJobsTest(unittest.TestCase):
|
|||||||
self.assertNotIn("container", self.JOBS["scenarios"])
|
self.assertNotIn("container", self.JOBS["scenarios"])
|
||||||
|
|
||||||
def test_every_job_has_the_fork_guard(self):
|
def test_every_job_has_the_fork_guard(self):
|
||||||
guard = self.JOBS["select"]["if"]
|
guard = (
|
||||||
self.assertIn("github.repository == 'mortis/kamino'", guard)
|
"github.repository == 'mortis/kamino' && "
|
||||||
|
"(github.event_name != 'pull_request' || "
|
||||||
|
"github.event.pull_request.head.repo.full_name == github.repository)"
|
||||||
|
)
|
||||||
|
self.assertEqual(self.JOBS["select"]["if"], guard)
|
||||||
for name, job in self.JOBS.items():
|
for name, job in self.JOBS.items():
|
||||||
self.assertTrue(job["if"].endswith(guard), name)
|
self.assertTrue(job["if"].endswith(guard), name)
|
||||||
|
|
||||||
def test_the_scenarios_run_only_for_a_selection(self):
|
def test_the_scenarios_run_only_for_a_selection(self):
|
||||||
job = self.JOBS["scenarios"]
|
job = self.JOBS["scenarios"]
|
||||||
self.assertEqual(job["needs"], "select")
|
self.assertEqual(job["needs"], "select")
|
||||||
self.assertTrue(job["if"].startswith("needs.select.outputs.scenarios != '' &&"))
|
# false for `none` and for an empty or missing output alike
|
||||||
|
self.assertTrue(
|
||||||
|
job["if"].startswith(
|
||||||
|
"needs.select.outputs.scenarios != 'none' && "
|
||||||
|
"needs.select.outputs.scenarios != '' &&"
|
||||||
|
)
|
||||||
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.JOBS["select"]["outputs"]["scenarios"],
|
self.JOBS["select"]["outputs"]["scenarios"],
|
||||||
"${{ steps.select.outputs.scenarios }}",
|
"${{ steps.select.outputs.scenarios }}",
|
||||||
@@ -587,11 +623,13 @@ class WorkflowJobsTest(unittest.TestCase):
|
|||||||
self.assertEqual(job["needs"], ["select", "scenarios"])
|
self.assertEqual(job["needs"], ["select", "scenarios"])
|
||||||
self.assertTrue(job["if"].startswith("always() &&"))
|
self.assertTrue(job["if"].startswith("always() &&"))
|
||||||
|
|
||||||
def test_the_result_passes_a_run_or_an_empty_selection_only(self):
|
def test_the_result_passes_a_run_or_a_none_selection_only(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.result("success", "success", "roles/a/molecule/default"), 0
|
self.result("success", "success", "roles/a/molecule/default"), 0
|
||||||
)
|
)
|
||||||
self.assertEqual(self.result("success", "skipped", ""), 0)
|
self.assertEqual(self.result("success", "skipped", "none"), 0)
|
||||||
|
# an empty output was lost, not an empty selection
|
||||||
|
self.assertNotEqual(self.result("success", "skipped", ""), 0)
|
||||||
self.assertNotEqual(
|
self.assertNotEqual(
|
||||||
self.result("success", "skipped", "roles/a/molecule/default"), 0
|
self.result("success", "skipped", "roles/a/molecule/default"), 0
|
||||||
)
|
)
|
||||||
@@ -601,8 +639,8 @@ class WorkflowJobsTest(unittest.TestCase):
|
|||||||
self.assertNotEqual(
|
self.assertNotEqual(
|
||||||
self.result("success", "cancelled", "roles/a/molecule/default"), 0
|
self.result("success", "cancelled", "roles/a/molecule/default"), 0
|
||||||
)
|
)
|
||||||
self.assertNotEqual(self.result("failure", "skipped", ""), 0)
|
self.assertNotEqual(self.result("failure", "skipped", "none"), 0)
|
||||||
self.assertNotEqual(self.result("skipped", "skipped", ""), 0)
|
self.assertNotEqual(self.result("skipped", "skipped", "none"), 0)
|
||||||
|
|
||||||
|
|
||||||
class ScenarioSequenceTest(unittest.TestCase):
|
class ScenarioSequenceTest(unittest.TestCase):
|
||||||
|
|||||||
Reference in new issue
Block a user