From 186106d2e95daf618f24856d157631eb77a3a8dd Mon Sep 17 00:00:00 2001 From: Myasnikov Daniil Date: Thu, 26 Nov 2026 11:29:41 +0000 Subject: [PATCH] test(tests): run backported Bats suites under bats-core Fixes picked from main now bring unit suites written for bats-core: they load hack/test_helper.bash and rely on Bats calling setup() and setting BATS_TEST_TMPDIR. release-1.6 runs every hack/*.bats through cozytest.sh, which has no load, so such a suite fails with "load: not found" and keeps the backport batch red. Main moved every unit suite to Bats in #3849, which also edited each existing suite. Only the runner part comes over here: the helper, the pinned bats-core install in the checks job, and a bats-unit-tests target that runs the suites loading the helper under bats. The other suites keep running through cozytest.sh under /bin/sh and nounset, so the 49 suites release-1.6 has today run exactly as before, and the 13 of them that source production shell scripts keep their dash coverage. Tested with make bats-unit-tests and dash as the recipe shell, on release-1.6 alone (49 suites, 527 tests through cozytest.sh, none under bats, the same as before) and with the open backport batch and its conflict resolutions on top (50 suites, 532 tests through cozytest.sh, 10 suites, 73 tests under bats, all passing). Refs: #3849 Assisted-By: LLM Signed-off-by: Myasnikov Daniil --- .github/workflows/pull-requests.yaml | 16 +++++++++++++++- Makefile | 24 ++++++++++++++++++++---- hack/test_helper.bash | 10 ++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 hack/test_helper.bash diff --git a/.github/workflows/pull-requests.yaml b/.github/workflows/pull-requests.yaml index 613641b6e..7e929891d 100644 --- a/.github/workflows/pull-requests.yaml +++ b/.github/workflows/pull-requests.yaml @@ -205,11 +205,25 @@ jobs: with: fetch-depth: 0 # Ephemeral runners don't carry the helm-unittest plugin the persistent - # self-hosted runner accreted. Install if absent (idempotent). + # self-hosted runner accreted. Install if absent (idempotent). bats-core + # runs the unit suites that load hack/test_helper.bash; pinned like on + # main, and the tag's commit is verified because archive bytes can change. - name: Set up test toolchain + env: + BATS_VERSION: "1.14.0" + BATS_COMMIT: eb7f42f8d608ac693d7a4b67474f6714ea68cfc5 run: | helm plugin list 2>/dev/null | grep -q unittest \ || helm plugin install https://github.com/helm-unittest/helm-unittest + if ! bats --version 2>/dev/null | grep -qx "Bats $BATS_VERSION"; then + bats_parent=$(mktemp -d "$RUNNER_TEMP/bats-core.XXXXXX") + bats_source="$bats_parent/source" + git clone --quiet --depth=1 --branch "v$BATS_VERSION" \ + https://github.com/bats-core/bats-core.git "$bats_source" + test "$(git -C "$bats_source" rev-parse HEAD)" = "$BATS_COMMIT" + sudo "$bats_source/install.sh" /usr/local + fi + bats --version | grep -qx "Bats $BATS_VERSION" - name: Run unit tests run: make unit-tests - name: Run controller Go tests diff --git a/Makefile b/Makefile index 80936a5be..0a9a5499c 100644 --- a/Makefile +++ b/Makefile @@ -153,9 +153,15 @@ test-controllers: test-check-readiness: go test ./test/check-readiness/ -count=1 -# Discover every hack/*.bats file that is NOT an e2e test and run it -# through cozytest.sh. Drop a new *.bats file in hack/ and it is picked -# up automatically on the next `make unit-tests` run. +# Discover every hack/*.bats file that is NOT an e2e test. Drop a new +# *.bats file in hack/ and it is picked up automatically on the next +# `make unit-tests` run. +# +# Suites that load hack/test_helper.bash were written for bats-core on main +# (#3849) and reach this line through backports: they need its setup() hook +# and BATS_TEST_TMPDIR, which cozytest.sh does not provide, so they run under +# bats. Every other suite keeps running through cozytest.sh, under /bin/sh +# and nounset, as it always has here. # # Caveat: $(wildcard ...) returns space-separated names, so a filename # containing a literal space would split into multiple tokens here. All @@ -163,16 +169,26 @@ test-check-readiness: # introduces whitespace-bearing filenames this recipe must be rewritten # (e.g. to use `find ... -print0 | xargs -0`). BATS_UNIT_FILES := $(filter-out hack/e2e-%.bats,$(wildcard hack/*.bats)) +BATS_NATIVE_FILES := $(if $(BATS_UNIT_FILES),$(shell grep -l '^[[:space:]]*load[[:space:]][[:space:]]*test_helper[[:space:]]*$$' $(BATS_UNIT_FILES))) +BATS_COZYTEST_FILES := $(filter-out $(BATS_NATIVE_FILES),$(BATS_UNIT_FILES)) bats-unit-tests: @if [ -z "$(BATS_UNIT_FILES)" ]; then \ echo "ERROR: no hack/*.bats unit test files found"; \ exit 1; \ fi - @for f in $(BATS_UNIT_FILES); do \ + @for f in $(BATS_COZYTEST_FILES); do \ echo "--- running $$f ---"; \ hack/cozytest.sh "$$f" || exit 1; \ done + @if [ -n "$(BATS_NATIVE_FILES)" ]; then \ + command -v bats >/dev/null 2>&1 || { \ + echo "ERROR: bats not found. Install bats-core >= 1.5 — https://bats-core.readthedocs.io"; \ + exit 1; \ + }; \ + echo "--- running under bats: $(BATS_NATIVE_FILES) ---"; \ + bats $(BATS_NATIVE_FILES); \ + fi # Operator-facing host preflight check. Warns about a standalone # containerd.service or docker.service running alongside the embedded diff --git a/hack/test_helper.bash b/hack/test_helper.bash new file mode 100644 index 000000000..05411d3d1 --- /dev/null +++ b/hack/test_helper.bash @@ -0,0 +1,10 @@ +# Bats supplies errexit but not nounset. Enable nounset in each test process +# without exporting shell options to production scripts launched by the tests. +# A file-local setup() must call strict_setup first. +strict_setup() { + set -u +} + +setup() { + strict_setup +}