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 +}