|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Run integration tests, optionally sharded across CI runners. |
| 3 | +# |
| 4 | +# Honors: |
| 5 | +# CREATE_JUNIT_REPORT Emit JUnit XML when set. |
| 6 | +# SHARD_INDEX 1-based shard index (used with SHARD_TOTAL). |
| 7 | +# SHARD_TOTAL Total number of shards. When set, this run executes |
| 8 | +# only the tests assigned to SHARD_INDEX (round-robin |
| 9 | +# partition by sorted test name). |
| 10 | +# RUN Override: pass directly to `go test -run`. |
| 11 | +# |
| 12 | +# Sharding is disabled when SHARD_TOTAL is unset or empty, so the script also |
| 13 | +# acts as a drop-in replacement for the unsharded `go test ./...` invocation. |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +cd "$(dirname "$0")/../test/integration" |
| 18 | + |
| 19 | +JUNIT_FLAG=() |
| 20 | +if [ -n "${CREATE_JUNIT_REPORT:-}" ]; then |
| 21 | + JUNIT_FLAG=(--junitfile ../../test-integration-results.xml) |
| 22 | +fi |
| 23 | + |
| 24 | +if [ "$(uname)" = "Darwin" ]; then |
| 25 | + export LSTK_KEYRING=file |
| 26 | +fi |
| 27 | + |
| 28 | +RUN_FLAG=() |
| 29 | +if [ -n "${SHARD_TOTAL:-}" ]; then |
| 30 | + IDX="${SHARD_INDEX:-1}" |
| 31 | + # `go test -list` enumerates top-level tests in each package. Run it as a |
| 32 | + # standalone step (no stderr suppression) so compile errors surface loudly |
| 33 | + # under `set -e` instead of being swallowed into an empty test list. The |
| 34 | + # subsequent `go test -run` reuses the compiled test binary from Go's build |
| 35 | + # cache, so this isn't a duplicate compile. |
| 36 | + LIST_OUTPUT=$(go test -list '.*' ./...) |
| 37 | + # Filter to lines matching /^Test/ to drop the trailing "ok pkg" summary |
| 38 | + # lines, sort for stable partitioning, and pick every N-th entry. |
| 39 | + # SHARD_INDEX is 1-based for human-friendly CI labels (shard 1/4, 2/4, ...). |
| 40 | + TESTS=$(echo "$LIST_OUTPUT" | awk '/^Test/' | sort \ |
| 41 | + | awk -v idx="$IDX" -v total="$SHARD_TOTAL" '((NR-1) % total) + 1 == idx') |
| 42 | + if [ -z "$TESTS" ]; then |
| 43 | + echo "no tests for shard $IDX/$SHARD_TOTAL — skipping" |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | + REGEX="^($(echo "$TESTS" | paste -sd '|' -))\$" |
| 47 | + RUN_FLAG=(-run "$REGEX") |
| 48 | +elif [ -n "${RUN:-}" ]; then |
| 49 | + RUN_FLAG=(-run "$RUN") |
| 50 | +fi |
| 51 | + |
| 52 | +# Bash 3 (macOS default) treats `${arr[@]}` on an empty array as unbound under |
| 53 | +# `set -u`. The `${arr[@]+"${arr[@]}"}` idiom expands to nothing when the |
| 54 | +# array is empty and to the array's contents otherwise. |
| 55 | +exec go run gotest.tools/gotestsum@latest --format testname \ |
| 56 | + ${JUNIT_FLAG[@]+"${JUNIT_FLAG[@]}"} \ |
| 57 | + -- -count=1 -timeout 15m \ |
| 58 | + ${RUN_FLAG[@]+"${RUN_FLAG[@]}"} \ |
| 59 | + ./... |
0 commit comments