From e3458ad2978c5432444d44e51ce6e6caa6fdbedd Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Mon, 13 Oct 2025 11:29:58 -0500 Subject: [PATCH] can run unit tests via makefile --- .github/workflows/affected-tests.yml | 15 +++--- Makefile | 11 ++++ scripts/run-affected.sh | 51 +++++++++++++++++++ .../test-affected-detection.sh | 0 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100755 scripts/run-affected.sh rename test-affected-detection.sh => scripts/test-affected-detection.sh (100%) diff --git a/.github/workflows/affected-tests.yml b/.github/workflows/affected-tests.yml index d32ba362..89926c63 100644 --- a/.github/workflows/affected-tests.yml +++ b/.github/workflows/affected-tests.yml @@ -77,13 +77,14 @@ jobs: else echo "- (none)"; fi; - echo "\n### Affected e2e tests"; + echo; + echo "### Affected e2e tests"; if [ -s /tmp/affected-e2e.txt ]; then sed 's/^/- /' /tmp/affected-e2e.txt; else echo "- (none)"; fi; - } >> "$GITHUB_STEP_SUMMARY" + } | tee -a "$GITHUB_STEP_SUMMARY" # 3) Run filtered tests only - name: Run unit tests for affected packages @@ -93,11 +94,11 @@ jobs: # If the script output contains './...' then run all tests if grep -qx "./..." /tmp/affected.txt; then echo "Module files changed; running all tests" - go test -race -count=1 ./... + make test else echo "Running tests for affected packages" - # xargs will pass the package list as arguments to go test - xargs -a /tmp/affected.txt go test -race -count=1 -v + pkgs=$(tr '\n' ' ' < /tmp/affected.txt) + PACKAGES="$pkgs" make test-packages fi - name: Run preflight e2e (filtered) @@ -106,7 +107,7 @@ jobs: set -euo pipefail if [ -s /tmp/preflight-tests.txt ]; then regex="$(tr '\n' '|' < /tmp/preflight-tests.txt | sed 's/|$//')" - go test -v -count=1 ./test/e2e/preflight -run "^((${regex}))$" + RUN="^((${regex}))$" make support-bundle-e2e-go-test else echo "No preflight e2e changes" fi @@ -117,7 +118,7 @@ jobs: set -euo pipefail if [ -s /tmp/support-tests.txt ]; then regex="$(tr '\n' '|' < /tmp/support-tests.txt | sed 's/|$//')" - go test -v -count=1 ./test/e2e/support-bundle -run "^((${regex}))$" + RUN="^((${regex}))$" make support-bundle-e2e-go-test else echo "No support-bundle e2e changes" fi diff --git a/Makefile b/Makefile index 2588f3bf..b13b8672 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,17 @@ test: generate fmt vet go test ${BUILDFLAGS} ${BUILDPATHS} ${TESTFLAGS}; \ fi +# Run unit tests only for a provided list of packages. +# Usage: make test-packages PACKAGES="pkg/a pkg/b cmd/foo" +.PHONY: test-packages +test-packages: + @if [ -z "$(PACKAGES)" ]; then \ + echo "No PACKAGES provided; nothing to test."; \ + exit 0; \ + fi + @echo "Running unit tests for packages: $(PACKAGES)" + go test ${BUILDFLAGS} $(PACKAGES) ${TESTFLAGS} + # Go tests that require a K8s instance # TODOLATER: merge with test, so we get unified coverage reports? it'll add 21~sec to the test job though... .PHONY: test-integration diff --git a/scripts/run-affected.sh b/scripts/run-affected.sh new file mode 100755 index 00000000..0cfa92b8 --- /dev/null +++ b/scripts/run-affected.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -euo pipefail + +# 0) Preconditions (one-time) +export PATH="$(go env GOPATH)/bin:$PATH" +go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.19.0 >/dev/null +go install k8s.io/code-generator/cmd/client-gen@v0.34.0 >/dev/null +git fetch origin main --depth=1 || true + +# 1) Compute base (robust to unrelated histories) +BASE="$(git merge-base HEAD origin/main 2>/dev/null || true)" +if [ -z "${BASE}" ]; then + echo "No merge-base with origin/main → running full set" + PKGS="./..." + E2E_OUT="$(go run ./scripts/affected-packages.go -mode=suites -changed-files go.mod || true)" +else + PKGS="$(go run ./scripts/affected-packages.go -base "${BASE}")" + E2E_OUT="$(go run ./scripts/affected-packages.go -mode=suites -base "${BASE}")" +fi + +# 2) Print what will run +echo "=== Affected unit packages ===" +if [ -n "${PKGS}" ]; then echo "${PKGS}"; else echo "(none)"; fi +echo +echo "=== Affected e2e tests ===" +if [ -n "${E2E_OUT}" ]; then echo "${E2E_OUT}"; else echo "(none)"; fi +echo + +# 3) Unit tests via Makefile (inherits required build tags) +if [ "${PKGS}" = "./..." ]; then + echo "Running: make test (all)" + make test +elif [ -n "${PKGS}" ]; then + echo "Running: make test-packages for affected pkgs" + PACKAGES="$(echo "${PKGS}" | xargs)" make test-packages +else + echo "No affected unit packages" +fi + +# 4) E2E tests via Makefile (filtered by regex) +PRE="$(echo "${E2E_OUT}" | awk -F: '$1=="preflight"{print $2}' | paste -sd'|' -)" +SB="$( echo "${E2E_OUT}" | awk -F: '$1=="support-bundle"{print $2}' | paste -sd'|' -)" + +if [ -n "${PRE}" ]; then + echo "Running preflight e2e: ${PRE}" + RUN="^((${PRE}))$" make support-bundle-e2e-go-test +fi +if [ -n "${SB}" ]; then + echo "Running support-bundle e2e: ${SB}" + RUN="^((${SB}))$" make support-bundle-e2e-go-test +fi \ No newline at end of file diff --git a/test-affected-detection.sh b/scripts/test-affected-detection.sh similarity index 100% rename from test-affected-detection.sh rename to scripts/test-affected-detection.sh