diff --git a/.gitea/workflows/AGENTS.md b/.gitea/workflows/AGENTS.md index ff66a14..6fab069 100644 --- a/.gitea/workflows/AGENTS.md +++ b/.gitea/workflows/AGENTS.md @@ -72,11 +72,24 @@ issue. ## Molecule - **Molecule runs on every same-repository pull request, Renovate's included, with no path - filter.** A fork pull request is skipped by the job's `if:`, which is a convenience, not a + filter.** A fork pull request is skipped by the jobs' shared `if:` guard, which is a convenience, not a boundary: a fork controls its own workflow file. Gitea's approval gate keeps a fork's runs from starting, and the fork rule in [reviewing.md](../../docs/contributing/reviewing.md) keeps it shut. *Why:* a check that always reports can be required, and a new image is tested by its own pin pull request. *Held:* review. +- **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 + bumps would then select every scenario), and outputs the selected directories, or `none`. + `scenarios` runs them on `kamino-molecule` and is skipped for `none` or an empty output. + `result` (check `Molecule / Result (pull_request)`) is the required check: it runs + `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`. + *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: + `WorkflowJobsTest` in `tests/scripts/test_run_molecule_scenarios.py` for the jobs, runners, + guard and result logic, and `test_every_controller_pin_is_the_same` in + `tests/scripts/test_select_molecule_scenarios.py` for the pin copies. - **What a pull request runs is chosen by `scripts/select_molecule_scenarios.py`:** the scenarios of the roles it changes and of the roles that use them (any file naming `mortis.kamino.`, transitively), none for a diff of only IGNORED paths, a failure for a diff --git a/.gitea/workflows/molecule.yml b/.gitea/workflows/molecule.yml index 1d93373..6c56d92 100644 --- a/.gitea/workflows/molecule.yml +++ b/.gitea/workflows/molecule.yml @@ -4,8 +4,13 @@ name: Molecule # extensions/molecule/*/ and roles/*/molecule/*/. extensions/molecule/image-smoke and # image-smoke-2604 test the test images themselves, so they run even before any role exists. # -# It runs on the `kamino-molecule` label, a host executor that uses the runner's own -# Docker daemon; no job container is given the Docker socket. That makes the job +# Three jobs, so that a pull request that selects no scenario takes no Molecule runner slot: +# `select` chooses the scenarios in a job container on `kamino-docker`; `scenarios` runs them on +# `kamino-molecule`, and is skipped when none is selected; `result` is the required check and +# passes only when `select` passed and `scenarios` passed or was skipped for an empty selection. +# +# The scenarios job runs on the `kamino-molecule` label, a host executor that uses the runner's +# own Docker daemon; no job container is given the Docker socket. That makes the job # root-equivalent on the runner, which is disposable and holds no secret from outside # this repository. The `if:` guard is a convenience filter, not a boundary: a fork controls its # own workflow file. The boundaries are write access, which only trusted people have, and Gitea's @@ -16,7 +21,8 @@ name: Molecule # at the same path, so the paths molecule hands the daemon resolve. Like Lint and Build, it installs # the pull request's pins when they differ from the image's. The images are pinned by tag and # digest: bump them here when image.yml publishes a new molecule-controller, molecule-ubuntu2404 or -# molecule-ubuntu2604 version. +# molecule-ubuntu2604 version. The molecule-controller pin is written three times, because +# `container.image` cannot read `env`; a script test holds the copies equal. on: # yamllint disable-line rule:truthy pull_request: branches: @@ -44,15 +50,82 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - molecule: + select: # Same-repo branches only (bot branches included: a Renovate pin pull request is what tests a new # image); the nightly run and a manual dispatch always run. No paths filter on pull requests, so # that Molecule can be a required check: a required check that a pull request never starts would - # block it. A fork pull request is skipped by the guard below. + # block it. A fork pull request is skipped by the guard below, which all three jobs share. if: >- github.repository == 'mortis/kamino' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) + runs-on: kamino-docker + timeout-minutes: 10 + # The molecule-controller pin, as CONTROLLER_IMAGE below: a ci pin here would make every ci + # bump select every scenario (ADR 0003). + container: + image: git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8 + outputs: + scenarios: ${{ steps.select.outputs.scenarios }} + steps: + # The selector's diff needs origin/main and the full history, so a clone, not a fetch of HEAD. + - name: Checkout the commit under test + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraheader + GIT_CONFIG_VALUE_0: "AUTHORIZATION: bearer ${{ secrets.GITHUB_TOKEN }}" + SHA: ${{ github.event.pull_request.head.sha || github.sha }} + REF: ${{ github.ref }} + REPO_URL: ${{ github.server_url }}/${{ github.repository }} + run: | + rm -rf ac + mkdir -p ac/ansible_collections/mortis + git clone "$REPO_URL" ac/ansible_collections/mortis/kamino + cd ac/ansible_collections/mortis/kamino + # A clone has no refs/pull/*: fetch the run's ref when the sha is not on a branch. + git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; } + echo "Checked out $(git rev-parse HEAD)" + + # A pull request runs only the scenarios of the roles it changes (scripts/select_molecule_scenarios.py + # explains the rules); the nightly run and a manual dispatch run all of them, because the + # selector narrows only when EVENT_NAME is pull_request. + - name: Select the scenarios + id: select + working-directory: ac/ansible_collections/mortis/kamino + env: + EVENT_NAME: ${{ github.event_name }} + # The workflow only runs for pull requests to main; lint.yml uses the same constant. + BASE_REF: origin/main + run: | + # The selector comes from the base branch, so a pull request cannot edit the rules it is + # judged by (lint.yml does the same for check_bot_pr_diff.py); the first pull request that + # adds it has no base copy yet and uses its own. + sel=scripts/select_molecule_scenarios.py + if git cat-file -e "$BASE_REF:$sel" 2>/dev/null; then + sel_dir="$(mktemp -d)" + trap 'rm -rf "$sel_dir"' EXIT + git show "$BASE_REF:$sel" > "$sel_dir/select.py" + sel="$sel_dir/select.py" + else + echo "::notice::no base copy of $sel; running this checkout's copy" + fi + selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1 + # `none` marks an empty selection, so a lost or empty output fails result, never passes. + 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')" + printf 'scenarios<<%s\n%s\n%s\n' "$delim" "$selected" "$delim" >> "$GITHUB_OUTPUT" + + scenarios: + needs: select + # Skipped, not failed, when select fails, selects nothing (`none`) or loses its output (empty); + # result passes only the second. + if: >- + needs.select.outputs.scenarios != 'none' && + needs.select.outputs.scenarios != '' && + github.repository == 'mortis/kamino' && + (github.event_name != 'pull_request' || + github.event.pull_request.head.repo.full_name == github.repository) runs-on: [kamino-molecule] # A full run under load takes about 45 minutes (PR #405: 44m53s), so 45 left no margin. Also the # age past which the sweep step removes a run's containers; a script test holds the two equal. @@ -61,7 +134,7 @@ jobs: # The tools and collections molecule runs with, apart from Lint's (ADR 0003), so a tool # bump only Lint uses selects no scenario. CONTROLLER_IMAGE: >- - git.rkdr.net/mortis/molecule-controller:0.1.0@sha256:d4ab16d58851eead751c8f0bada8edd76c9e5dcd5c2acaa1ac93c5f6035b29e2 + git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8 # Used by the scenarios as `image: ${MOLECULE_IMAGE}`; set it the same way to run # one locally. MOLECULE_IMAGE: >- @@ -121,33 +194,15 @@ jobs: for id in $old; do left="$left$(docker ps -aq --filter "id=$id")"; done if [ -n "$left" ]; then echo "::error::could not remove ${left//$'\n'/ }"; exit 1; fi - # A pull request runs only the scenarios of the roles it changes (scripts/select_molecule_scenarios.py - # explains the rules); the nightly run and a manual dispatch run all of them, a few at a time. - # scripts/run_molecule_scenarios.sh runs them and removes each scenario's containers as soon as - # it ends. + # scripts/run_molecule_scenarios.sh runs the selected scenarios, a few at a time, and removes + # each scenario's containers as soon as it ends. - name: Run the scenarios working-directory: ac/ansible_collections/mortis/kamino env: - EVENT_NAME: ${{ github.event_name }} - # The workflow only runs for pull requests to main; lint.yml uses the same constant. - BASE_REF: origin/main + SELECTED: ${{ needs.select.outputs.scenarios }} # Empty outside a dispatch, which the run script reads as its default. MOLECULE_LANES: ${{ inputs.lanes }} run: | - # The selector comes from the base branch, so a pull request cannot edit the rules it is - # judged by (lint.yml does the same for check_bot_pr_diff.py); the first pull request that - # adds it has no base copy yet and uses its own. - sel=scripts/select_molecule_scenarios.py - if git cat-file -e "$BASE_REF:$sel" 2>/dev/null; then - sel_dir="$(mktemp -d)" - trap 'rm -rf "$sel_dir"' EXIT - git show "$BASE_REF:$sel" > "$sel_dir/select.py" - sel="$sel_dir/select.py" - else - echo "::notice::no base copy of $sel; running this checkout's copy" - fi - selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1 - # As the runner user, with the socket's group, so nothing in the job tree is left root-owned. # HOME (ansible's temp and caches) is in the job tree; a mounted passwd entry points the user # at it, because ansible's temp path is `~/.ansible/tmp`. The driver's connection plugin @@ -165,7 +220,7 @@ jobs: -v "$ac_root:$ac_root" -v "$ac_root/passwd:/etc/passwd:ro" -w "$PWD" \ -e "AC_ROOT=$ac_root" -e "HOME=$ac_root/home" -e "USER=$(id -un)" \ -e MOLECULE_IMAGE -e MOLECULE_IMAGE_2604 -e KAMINO_RUN_ID -e MOLECULE_LANES \ - "$CONTROLLER_IMAGE" scripts/run_molecule_in_ci_image.sh <<< "$selected" + "$CONTROLLER_IMAGE" scripts/run_molecule_in_ci_image.sh <<< "$SELECTED" # A cancelled run can leave its containers running when the docker client is killed. - name: Remove this run's containers @@ -178,3 +233,36 @@ jobs: for id in $ids; do docker rm -f "$id" >/dev/null || true; done left="$(docker ps -aq --filter "label=net.rkdr.mortis.molecule-run=$KAMINO_RUN_ID")" if [ -n "$left" ]; then echo "::error::could not remove ${left//$'\n'/ }"; exit 1; fi + + result: + name: Result + needs: [select, scenarios] + # always(): otherwise a failed select would skip this job, and Gitea counts a skipped job as + # passing. The guard is select's, so a fork pull request skips all three jobs alike and the run + # step below never sees a select skipped by it. + if: >- + always() && + github.repository == 'mortis/kamino' && + (github.event_name != 'pull_request' || + github.event.pull_request.head.repo.full_name == github.repository) + runs-on: kamino-docker + timeout-minutes: 5 + # The controller pin again, so this job adds no pin of its own to bump. + container: + image: git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8 + steps: + - name: Check the select and scenarios jobs + env: + SELECT_RESULT: ${{ needs.select.result }} + SCENARIOS_RESULT: ${{ needs.scenarios.result }} + SELECTED: ${{ needs.select.outputs.scenarios }} + run: | + if [ "$SELECT_RESULT" != success ]; then + echo "::error::select ended as $SELECT_RESULT"; exit 1 + fi + # A skip is a pass only for select's `none`: an empty output means it was lost. + if [ "$SCENARIOS_RESULT" = skipped ] && [ "$SELECTED" = none ]; then + echo "No scenario selected." + elif [ "$SCENARIOS_RESULT" != success ]; then + echo "::error::scenarios ended as $SCENARIOS_RESULT"; exit 1 + fi diff --git a/docs/contributing/maintaining.md b/docs/contributing/maintaining.md index 3fbf6e2..bfcdbf2 100644 --- a/docs/contributing/maintaining.md +++ b/docs/contributing/maintaining.md @@ -56,10 +56,10 @@ Every secret the workflows in [.gitea/workflows/](../../.gitea/workflows/) refer A checklist, not rules: what the repository and organisation settings should be. The maintainer applies them and checks them against this list after any change. -- Branch protection on `main`: required checks Lint, Build and Molecule; zero required approvals; - "Require approval from code owners"; "Dismiss stale approvals"; block on rejected and on official - review requests; block on an outdated branch; administrators cannot override; the approvals - allowlist is the maintainer. +- Branch protection on `main`: required checks Lint, Build and `Molecule / Result (pull_request)`; + zero required approvals; "Require approval from code owners"; "Dismiss stale approvals"; block + on rejected and on official review requests; block on an outdated branch; administrators cannot + override; the approvals allowlist is the maintainer. - Merge allowlist on `main`: the maintainer, each trusted contributor and the Renovate bot. - Push allowlist on `release/*`: the maintainer; no rule for `renovate/*`. - Tag protection on `v*`, with the maintainer on its allowlist. diff --git a/requirements-dev.lock b/requirements-dev.lock index 5c5ba0b..500fcd3 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -193,9 +193,9 @@ ansible-builder==3.2.0 \ --hash=sha256:8cd8639011127aaa5046fd60e56ae753eb599061bf493eca2c6b75c54cfc6212 \ --hash=sha256:9cb948dbd221bdf6d0d48e80fa1575ca5f4336a569c05f510e2d406f01897673 # via galaxy-importer -ansible-compat==26.11.0 \ - --hash=sha256:35cbe829e0b5a9acb85d34b8d5da2eb45acaf767f6651f90e0ab2f3a96a060bb \ - --hash=sha256:c06a486b39ced440f9f6fb9a99583c73d9239c94ece822a27ea2242173462dda +ansible-compat==26.11.1 \ + --hash=sha256:d22e0a48d8d79e727b0f56db3573b02bde2189b5bfbef067230a34c0bcbff94a \ + --hash=sha256:ed9717fde1705a58678c1503e46316a36f7392229c2444d2b2f1b6de12be9f25 # via # ansible-lint # molecule @@ -703,9 +703,9 @@ enrich==1.2.7 \ --hash=sha256:0a2ab0d2931dff8947012602d1234d2a3ee002d9a355b5d70be6bf5466008893 \ --hash=sha256:f29b2c8c124b4dbd7c975ab5c3568f6c7a47938ea3b7d2106c8a3bd346545e4f # via molecule -filelock==4.3.0 \ - --hash=sha256:143407310acf479e82d88068e3b9a6486f45fb410c0ed8729fe7359eaabeb6f1 \ - --hash=sha256:f933f39f7e01249a4a49b9abab30edc6d4b874624c3de5dd0b0d137aa8adb979 +filelock==4.3.2 \ + --hash=sha256:02a8801bc6b2a80921bf3d7895c63084d53bae2a3c86783adce5ae2b89a1825b \ + --hash=sha256:4f0a21c70447782453c06bb20a9863ee385ead1abaef22049af79289496851b3 # via # ansible-lint # apm-cli @@ -1431,13 +1431,13 @@ nodeenv==1.12.0 \ --hash=sha256:4ea7f88339310afebc3253f2c7a91b94bacf97d73a36a5c108962abdfaca501e \ --hash=sha256:80644d65c10cc2be12e3e4b54b3367ff288c7ad24ae75c2dcc65454fc7449d4d # via pre-commit -openai==3.57.0 \ - --hash=sha256:5a0a89d6a542efc85e2a8cec701ca9265b795d578c777828e10fafdf80e333b6 \ - --hash=sha256:f97bc74024c06ced8428b56c4a3594066709d3082779bf9d0b4b6092fbf587a8 +openai==3.60.0 \ + --hash=sha256:077657587f7d2d55b943a135c3e183f87cf7dc66644caa3c0bbc2bde1fe65de9 \ + --hash=sha256:476e79f256a4ce74848b7abae9f074352a69dca6e7e8987d565ee883a3b7e496 # via llm -packaging==26.3 \ - --hash=sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79 \ - --hash=sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c +packaging==26.4 \ + --hash=sha256:81383611a09c1bc0b08b76e0fd5829d7c6824c65b27a6aee339da133f4233ea2 \ + --hash=sha256:c4b671c41ddb9bf26e543c37723610d28fb6bf7affc4da17cf048d419d2c062a # via # ansible-builder # ansible-compat @@ -1464,9 +1464,9 @@ pbr==7.1.3 \ --hash=sha256:6583e878a1d97cb135fdc509811f31b9235905cde8d4dacd3dbadf9efc45d745 \ --hash=sha256:9a4a85b84e906337708009af0b5f5cdabeeb72d4dc213c9e97974da54fd9acc5 # via bindep -platformdirs==4.14.8 \ - --hash=sha256:1e34af590de4d978631b751eb2202f2fee4497537a17c1720b12fcf57b331392 \ - --hash=sha256:96893d686e84e9e6f358a9ea41a8de544df66dbf2ba6cda250e7dc73acf85b9b +platformdirs==4.14.9 \ + --hash=sha256:c6b188e03ffd4dfe1bf8c4bcb47cd94711af244a9852dbff60297bd753684d5d \ + --hash=sha256:d0d3a23e7ff282cfa94b77f5a22b81030dcb68ba0430f90c74f4bd8d090782ea # via # black # virtualenv @@ -1806,9 +1806,9 @@ python-debian==1.1.1 \ --hash=sha256:f98ae013e8e5310e49041cc3860a7105df73af73d4ff1d8afb474770d328a6ad \ --hash=sha256:fe4fc3dc798dbf1f0ef5865e2b1b4f7cc0352b6a511b25ab7594906c64a73629 # via reuse -python-discovery==1.9.6 \ - --hash=sha256:383aa67858d73a9585a843a37a4383c6be98e4d86a981a031328e393a6eb2632 \ - --hash=sha256:93b10b90363d4151964cdeee2ec6601f3041b17fc2b253a1c3da7d6e7ddd8337 +python-discovery==1.9.8 \ + --hash=sha256:4e0c3349272923b6f58db8c5dad111b09dec9d3e2c97f9b79023f8b2e9acf7ab \ + --hash=sha256:5d66dd004e6972687438a799fa0895b7a900c1b7324a525196f1826bf7cebb03 # via virtualenv python-frontmatter==1.3.0 \ --hash=sha256:9f7dd9260bec99044219159a329f64f039087f9d1a2124c9442556f2fe6f82ec \ @@ -2165,9 +2165,9 @@ sqlite-fts4==1.0.3 \ --hash=sha256:0359edd8dea6fd73c848989e1e2b1f31a50fe5f9d7272299ff0e8dbaa62d035f \ --hash=sha256:78b05eeaf6680e9dbed8986bde011e9c086a06cb0c931b3cf7da94c214e8930c # via sqlite-utils -sqlite-utils==4.2.1 \ - --hash=sha256:76114b6a5414714e6c70e5fa5c4781b301b590f6951b5da39c8cc60c21382ba1 \ - --hash=sha256:7fbaee670713b25eceba05779b844f0895efaf680d9a226fa5ba7dfe165721c5 +sqlite-utils==4.2.2 \ + --hash=sha256:5c54dc4d77260b44e8d0ccf4461c321c40ca1451dd6b6a63398ed058722aff13 \ + --hash=sha256:e92aa6c2bcefa1877cbd56e8da6fe3924ca5b45954f98e7d738c49d19325fa0d # via llm subprocess-tee==0.4.2 \ --hash=sha256:21942e976715af4a19a526918adb03a8a27a8edab959f2d075b777e3d78f532d \ @@ -2183,9 +2183,9 @@ toml==0.10.2 \ --hash=sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b \ --hash=sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f # via apm-cli -tomlkit==0.15.2 \ - --hash=sha256:05562412163a3037b94497385fb7f9204c637bc605af7a48199e41cf2272f437 \ - --hash=sha256:83e32755ba1d20d9340bd069ee97e9f6232ba2346b63ffcba425de01f7e327a6 +tomlkit==0.15.3 \ + --hash=sha256:a83ce6de3b648e68dd9b50aabadad749ba823eb58b1d026f3c0a283c4767a9c1 \ + --hash=sha256:b3c3020ec1a4bd469d12e31fb4da938e7f6ba758cdbec3fd9047db63120eaf81 # via # apm-cli # reuse @@ -2196,9 +2196,9 @@ truststore==0.10.5 \ # apm-cli # httpcore2 # httpx2 -typer==0.29.2 \ - --hash=sha256:b1998dc5f2c3dedafa1517a9521fa944cc4dc1fadcea085d81a3b10ebcaeb075 \ - --hash=sha256:f30731f8ca758368b836b3714872e58e395cef8fcaff918c853b1969ed5274e7 +typer==0.29.3 \ + --hash=sha256:1fd37275046faecc86caa2e0e313978c10db71533fc0fe1a48f4e03a60efc23c \ + --hash=sha256:8189a53bb9684dc551e0b61e71904b323214884f2eb14bfe71aa859ae980d10a # via rstcheck typing-extensions==4.16.0 \ --hash=sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8 \ @@ -2226,9 +2226,9 @@ urllib3==2.8.0 \ # via # docker # requests -virtualenv==21.15.12 \ - --hash=sha256:a3976f886eb6019fcb1d137b223d160adaeff524ab0ad785c729a43c31cc158c \ - --hash=sha256:bbbf764388a18dee8c3532c77af7a29680cd8121911d6dcb3484335d76547323 +virtualenv==21.15.14 \ + --hash=sha256:6c6e7aaf88064767c75c32a9df92eaa425cee5db4cc21caf031a78d89375d57f \ + --hash=sha256:816503d7826a2269abc03f7f0dd48ffd3b63db1958a7a3cbeea5a70dd7fe0ff1 # via pre-commit watchdog==7.1.1 \ --hash=sha256:05e85c22dcd53f7152d1e03e785e241341d586b06a5115be7c0ea86436ccc1f6 \ diff --git a/tests/scripts/test_run_molecule_scenarios.py b/tests/scripts/test_run_molecule_scenarios.py index b1f32a9..d8981a0 100644 --- a/tests/scripts/test_run_molecule_scenarios.py +++ b/tests/scripts/test_run_molecule_scenarios.py @@ -355,7 +355,7 @@ class RunMoleculeScenariosTest(unittest.TestCase): def test_the_default_time_limit_is_inside_the_job_timeout(self): # a limit past the job's own would let a hang take the job and every log with it - timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][ + timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["scenarios"][ "timeout-minutes" ] limit = re.findall(r"MOLECULE_SCENARIO_TIMEOUT:-(\d+)\}", SCRIPT.read_text()) @@ -502,7 +502,7 @@ class ScenarioPlatformsTest(unittest.TestCase): def test_the_sweep_spares_containers_younger_than_the_job_timeout(self): # a shorter bound would remove the containers of a run still going - timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][ + timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["scenarios"][ "timeout-minutes" ] run = step_run("Remove the containers earlier runs left") @@ -519,12 +519,130 @@ class LanesInputTest(unittest.TestCase): # yaml reads the `on:` key as True lanes = workflow[True]["workflow_dispatch"]["inputs"]["lanes"] self.assertEqual((lanes["type"], lanes["default"]), ("string", "")) - steps = workflow["jobs"]["molecule"]["steps"] + steps = workflow["jobs"]["scenarios"]["steps"] step = next(s for s in steps if s.get("name") == "Run the scenarios") self.assertEqual(step["env"]["MOLECULE_LANES"], "${{ inputs.lanes }}") self.assertRegex(step["run"], r"docker run [^<]*-e MOLECULE_LANES\b") +class WorkflowJobsTest(unittest.TestCase): + """A pull request that selects no scenario takes no Molecule runner slot.""" + + JOBS = yaml.safe_load(WORKFLOW.read_text())["jobs"] + + def result(self, select, scenarios, selected): + """The exit code of the result job's check for those needs results and that selection.""" + (step,) = self.JOBS["result"]["steps"] + env = dict( + os.environ, + SELECT_RESULT=select, + SCENARIOS_RESULT=scenarios, + SELECTED=selected, + ) + return subprocess.run( + # act runs a job container's step with sh when the step sets no shell + ["sh", "-e", "-c", step["run"]], + check=False, + env=env, + capture_output=True, + ).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<-\n {v}" for v in (self.pin(key), self.bumped(key)) + ) + self.assertEqual(self.edited(old, new), {key}, key) def test_the_workflow_runs_in_the_controller_image_not_ci(self): # A ci pin bump must touch only Lint and Build, which IGNORED lists (ADR 0003). self.assertIn("/mortis/molecule-controller:", self.pin("CONTROLLER_IMAGE")) self.assertNotIn("/mortis/ci:", self.WORKFLOW) + def test_every_controller_pin_is_the_same(self): + # container.image cannot read env, so the select and result jobs repeat CONTROLLER_IMAGE + jobs = yaml.safe_load(self.WORKFLOW)["jobs"] + pins = [ + jobs["select"]["container"]["image"], + self.pin("CONTROLLER_IMAGE"), + jobs["result"]["container"]["image"], + ] + self.assertEqual(len(set(pins)), 1, pins) + self.assertEqual(self.WORKFLOW.count("/mortis/molecule-controller:"), len(pins)) + # Renovate moves all three, which the selector reads as "any other change": every + # scenario, as for CONTROLLER_IMAGE alone. + bumped = self.WORKFLOW.replace(pins[0], self.bumped("CONTROLLER_IMAGE")) + self.assertIsNone(sel.pin_changes(self.WORKFLOW, bumped)) + 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())