|
1 | | -.PHONY: manifests assets prepare-env prepare-env-container unit-tests helm-unit-tests bats-unit-tests bats-unit-files-check rd-presets-check migrations-target-check test test-controllers preflight |
2 | | - |
3 | | -# Before any include, the last file make has read is this one: MAKEFILES and |
4 | | -# -f files given before it are earlier in the list, -f files after it are not |
5 | | -# read yet. |
6 | | -ROOT_MAKEFILE := $(lastword $(MAKEFILE_LIST)) |
| 1 | +.PHONY: manifests assets prepare-env prepare-env-container unit-tests helm-unit-tests bats-unit-tests bats-unit-files-check bats-posix-compat-tests print-bats-unit-files print-bats-posix-compat-files print-bats-jobs rd-presets-check migrations-target-check test test-controllers preflight |
7 | 2 |
|
8 | 3 | include hack/common-envs.mk |
9 | 4 |
|
@@ -122,7 +117,7 @@ test: |
122 | 117 | make -C packages/core/testing apply |
123 | 118 | make -C packages/core/testing e2e |
124 | 119 |
|
125 | | -unit-tests: helm-unit-tests bats-unit-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness migrations-target-check |
| 120 | +unit-tests: helm-unit-tests bats-unit-tests bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness migrations-target-check |
126 | 121 |
|
127 | 122 | helm-unit-tests: |
128 | 123 | hack/helm-unit-tests.sh |
@@ -174,52 +169,91 @@ test-controllers: |
174 | 169 | test-check-readiness: |
175 | 170 | go test ./test/check-readiness/ -count=1 |
176 | 171 |
|
177 | | -# Discover every hack/*.bats file that is NOT an e2e test and run it |
178 | | -# through cozytest.sh. This glob is one level deep: live-cluster suites under |
179 | | -# hack/e2e-* or in subdirectories need an invocation in packages/core/testing. |
180 | | -# hack/bats-runner-coverage.bats reports files that neither runner selects. |
181 | | -# Park a suite with the .bats.disabled suffix to exclude it from the audit. |
| 172 | +# Unit discovery is one level deep. Live-cluster files need an invocation in |
| 173 | +# packages/core/testing; hack/bats-runner-coverage.bats checks that boundary. |
| 174 | +# Bats supplies TAP and JUnit output; the live-cluster runner keeps its tracing. |
182 | 175 | # |
183 | 176 | # Caveat: $(wildcard ...) returns space-separated names, so a filename |
184 | 177 | # containing a literal space would split into multiple tokens here. All |
185 | 178 | # current bats files use hyphen-separated names; if the project ever |
186 | 179 | # introduces whitespace-bearing filenames this recipe must be rewritten |
187 | 180 | # (e.g. to use `find ... -print0 | xargs -0`). |
188 | 181 | BATS_UNIT_FILES := $(filter-out hack/e2e-%.bats,$(wildcard hack/*.bats)) |
189 | | -BATS_UNIT_TARGETS := $(patsubst hack/%.bats,bats-unit-%,$(BATS_UNIT_FILES)) |
190 | | - |
191 | | -# Quiet by default: cozytest.sh streams every test's xtrace live, which over |
192 | | -# this many suites buried the one failing assertion under ~240k lines of |
193 | | -# trace in CI. COZYTEST_TRACE=0 keeps the per-test start/end lines and still dumps |
194 | | -# the full trace of whichever test FAILS -- but not of one that is killed |
195 | | -# instead, a hang reaped by the job timeout among them, since the dump runs |
196 | | -# after the test returns. See the COZYTEST_TRACE comment in hack/cozytest.sh. |
197 | | -# Override to watch a run line by |
198 | | -# line: `COZYTEST_TRACE=1 make bats-unit-tests`, or invoke the runner |
199 | | -# directly (`hack/cozytest.sh hack/foo.bats [pattern]`), which stays verbose. |
200 | | -# The e2e call sites in packages/core/testing/Makefile are untouched and keep |
201 | | -# the live stream -- see the COZYTEST_TRACE comment in hack/cozytest.sh. |
202 | | -COZYTEST_TRACE ?= 0 |
203 | 182 |
|
204 | | -# A sub-make with --keep-going, so one red file does not stop make from |
205 | | -# scheduling the files after it; the sub-make still exits non-zero if any |
206 | | -# file failed (hack/bats-unit-keep-going.bats covers both). It shares the |
207 | | -# caller's -j slots and --output-sync through MAKEFLAGS. |
208 | | -bats-unit-tests: bats-unit-files-check |
209 | | - @$(MAKE) --file=$(ROOT_MAKEFILE) --no-print-directory --keep-going $(BATS_UNIT_TARGETS) |
| 183 | +# The same list, one file per line, for hack/bats-strict-setup.bats. Every unit |
| 184 | +# file has to load hack/test_helper.bash to get `set -u` back, and that audit is |
| 185 | +# only worth anything if the set it walks is the set that actually runs -- so it |
| 186 | +# asks here rather than keeping a second copy of the filter above. |
| 187 | +print-bats-unit-files: |
| 188 | + @printf '%s\n' $(BATS_UNIT_FILES) |
| 189 | + |
| 190 | +# `bats -j` needs GNU parallel and exits non-zero rather than degrading when it |
| 191 | +# is missing. moreutils also ships a command named parallel, so identify the GNU |
| 192 | +# implementation from its version output before enabling concurrency. Resolve |
| 193 | +# the probe once per make process; `?=` would keep the `$(shell ...)` recursive |
| 194 | +# and rerun it at every expansion. |
| 195 | +ifndef BATS_JOBS |
| 196 | +BATS_JOBS := $(shell parallel --version 2>/dev/null | grep -q '^GNU parallel ' && nproc 2>/dev/null || echo 1) |
| 197 | +endif |
| 198 | + |
| 199 | +print-bats-jobs: |
| 200 | + @printf '%s\n' "$(BATS_JOBS)" |
| 201 | + |
| 202 | +# JUnit XML is preserved as a CI artifact for inspection. _out is gitignored. |
| 203 | +BATS_REPORT_DIR ?= _out/test-reports |
210 | 204 |
|
211 | 205 | bats-unit-files-check: |
212 | 206 | @if [ -z "$(BATS_UNIT_FILES)" ]; then \ |
213 | 207 | echo "ERROR: no hack/*.bats unit test files found"; \ |
214 | 208 | exit 1; \ |
215 | 209 | fi |
| 210 | +bats-unit-tests: bats-unit-files-check |
| 211 | + @command -v bats >/dev/null 2>&1 || { \ |
| 212 | + echo "ERROR: bats not found. Install bats-core >= 1.5 — https://bats-core.readthedocs.io"; \ |
| 213 | + exit 1; \ |
| 214 | + } |
| 215 | + @mkdir -p "$(BATS_REPORT_DIR)" |
| 216 | + bats -j $(BATS_JOBS) --report-formatter junit -o "$(BATS_REPORT_DIR)" $(BATS_UNIT_FILES) |
| 217 | + |
| 218 | +# Real Bats is authoritative. Unit files that source production code whose |
| 219 | +# contract is POSIX sh retain a compatibility pass through cozytest.sh's |
| 220 | +# /bin/sh translator. Discover literal .sh dependencies wherever they live, |
| 221 | +# including hack/lib and package migration helpers, so a new non-Chainsaw |
| 222 | +# helper test cannot run only under Bash. The explicit entries retain reviewed |
| 223 | +# shell-facing tests, including files whose production path is held in a |
| 224 | +# variable and therefore cannot be identified from the source line alone. |
| 225 | +BATS_SOURCED_SH_FILES := $(shell grep -El '^[[:space:]]*(\.|source)[[:space:]]+.*\.sh' $(BATS_UNIT_FILES)) |
| 226 | +BATS_POSIX_COMPAT_FILES := $(sort \ |
| 227 | + $(BATS_SOURCED_SH_FILES) \ |
| 228 | + hack/capture-dataplane.bats \ |
| 229 | + hack/capture-previous-logs.bats \ |
| 230 | + hack/cilium-leak-healer_test.bats \ |
| 231 | + hack/container-lane-capacity_test.bats \ |
| 232 | + hack/cozyreport-talos.bats \ |
| 233 | + hack/cozyreport.bats \ |
| 234 | + hack/cozystack-version-stamp.bats \ |
| 235 | + hack/nightly-mirror_test.bats \ |
| 236 | + hack/pod-label-census_test.bats \ |
| 237 | + hack/promote-rewrite-tags_test.bats \ |
| 238 | + hack/runner-identity.bats \ |
| 239 | + hack/seaweedfs-naming-audit.bats \ |
| 240 | +) |
| 241 | + |
| 242 | +# CI's /bin/sh is dash. Keep the interpreter overridable so contributors can |
| 243 | +# run the same compatibility lane explicitly on hosts whose /bin/sh differs. |
| 244 | +BATS_POSIX_SHELL ?= /bin/sh |
| 245 | +COZYTEST_TRACE ?= 0 |
| 246 | + |
| 247 | +print-bats-posix-compat-files: |
| 248 | + @printf '%s\n' $(BATS_POSIX_COMPAT_FILES) |
216 | 249 |
|
217 | | -# Each file is its own target of the sub-make so `make -jN` can schedule them, |
218 | | -# and the trace switch rides along per target. |
219 | | -.PHONY: $(BATS_UNIT_TARGETS) |
220 | | -$(BATS_UNIT_TARGETS): bats-unit-%: hack/%.bats |
221 | | - @echo "--- running $< ---" |
222 | | - @COZYTEST_TRACE=$(COZYTEST_TRACE) hack/cozytest.sh "$<" |
| 250 | +bats-posix-compat-tests: |
| 251 | + @status=0; \ |
| 252 | + for f in $(BATS_POSIX_COMPAT_FILES); do \ |
| 253 | + echo "--- running POSIX compatibility: $$f ---"; \ |
| 254 | + COZYTEST_TRACE=$(COZYTEST_TRACE) "$(BATS_POSIX_SHELL)" hack/cozytest.sh "$$f" || status=1; \ |
| 255 | + done; \ |
| 256 | + exit $$status |
223 | 257 |
|
224 | 258 | # Operator-facing host preflight check. Warns about a standalone |
225 | 259 | # containerd.service or docker.service running alongside the embedded |
|
0 commit comments