test(tests): run backported Bats suites under bats-core (#5458)

## What this PR does

Backport batch for release-1.6 (#5312) now carries fixes whose unit
tests are written for bats-core: they do `load test_helper` and need
Bats to call `setup()` and set `BATS_TEST_TMPDIR`. On release-1.6 every
`hack/*.bats` still runs through `cozytest.sh`, which has no `load`, so
the batch CI fails with `load: not found` (first one is
`hack/etcd-grpc-alerts.bats`).

Main moved all unit suites to Bats in #3849, and that PR also edited
every existing suite. I take only the runner part:
`hack/test_helper.bash`, bats-core 1.14.0 installed in the checks job
with the same pin and commit check as on main, and `make
bats-unit-tests` runs the suites that load the helper under bats. All
other suites keep running through `cozytest.sh` under `/bin/sh` as
before, so the 49 suites release-1.6 has today don't change, and the 13
of them that source production shell scripts keep their dash coverage.

Tested `make bats-unit-tests` with dash as recipe shell: on release-1.6
alone it runs the same 49 suites and 527 tests through `cozytest.sh` as
before and nothing under bats, and with the batch and its conflict
resolutions on top it runs 50 suites (532 tests) through `cozytest.sh`
and 10 suites (73 tests) under bats, all green.

### Screenshots

N/A

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:

### Release note

```release-note
NONE
```
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-26 18:19:26 +03:00
commit 5de32a5626
3 files changed
+45 -5

No files matched your search

+15 -1
View File
@@ -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
+20 -4
View File
@@ -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
+10
View File
@@ -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
}