Skip to content

Commit e3458ad

Browse files
committed
can run unit tests via makefile
1 parent cb32216 commit e3458ad

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

.github/workflows/affected-tests.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ jobs:
7777
else
7878
echo "- (none)";
7979
fi;
80-
echo "\n### Affected e2e tests";
80+
echo;
81+
echo "### Affected e2e tests";
8182
if [ -s /tmp/affected-e2e.txt ]; then
8283
sed 's/^/- /' /tmp/affected-e2e.txt;
8384
else
8485
echo "- (none)";
8586
fi;
86-
} >> "$GITHUB_STEP_SUMMARY"
87+
} | tee -a "$GITHUB_STEP_SUMMARY"
8788
8889
# 3) Run filtered tests only
8990
- name: Run unit tests for affected packages
@@ -93,11 +94,11 @@ jobs:
9394
# If the script output contains './...' then run all tests
9495
if grep -qx "./..." /tmp/affected.txt; then
9596
echo "Module files changed; running all tests"
96-
go test -race -count=1 ./...
97+
make test
9798
else
9899
echo "Running tests for affected packages"
99-
# xargs will pass the package list as arguments to go test
100-
xargs -a /tmp/affected.txt go test -race -count=1 -v
100+
pkgs=$(tr '\n' ' ' < /tmp/affected.txt)
101+
PACKAGES="$pkgs" make test-packages
101102
fi
102103
103104
- name: Run preflight e2e (filtered)
@@ -106,7 +107,7 @@ jobs:
106107
set -euo pipefail
107108
if [ -s /tmp/preflight-tests.txt ]; then
108109
regex="$(tr '\n' '|' < /tmp/preflight-tests.txt | sed 's/|$//')"
109-
go test -v -count=1 ./test/e2e/preflight -run "^((${regex}))$"
110+
RUN="^((${regex}))$" make support-bundle-e2e-go-test
110111
else
111112
echo "No preflight e2e changes"
112113
fi
@@ -117,7 +118,7 @@ jobs:
117118
set -euo pipefail
118119
if [ -s /tmp/support-tests.txt ]; then
119120
regex="$(tr '\n' '|' < /tmp/support-tests.txt | sed 's/|$//')"
120-
go test -v -count=1 ./test/e2e/support-bundle -run "^((${regex}))$"
121+
RUN="^((${regex}))$" make support-bundle-e2e-go-test
121122
else
122123
echo "No support-bundle e2e changes"
123124
fi

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ test: generate fmt vet
5555
go test ${BUILDFLAGS} ${BUILDPATHS} ${TESTFLAGS}; \
5656
fi
5757

58+
# Run unit tests only for a provided list of packages.
59+
# Usage: make test-packages PACKAGES="pkg/a pkg/b cmd/foo"
60+
.PHONY: test-packages
61+
test-packages:
62+
@if [ -z "$(PACKAGES)" ]; then \
63+
echo "No PACKAGES provided; nothing to test."; \
64+
exit 0; \
65+
fi
66+
@echo "Running unit tests for packages: $(PACKAGES)"
67+
go test ${BUILDFLAGS} $(PACKAGES) ${TESTFLAGS}
68+
5869
# Go tests that require a K8s instance
5970
# TODOLATER: merge with test, so we get unified coverage reports? it'll add 21~sec to the test job though...
6071
.PHONY: test-integration

scripts/run-affected.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# 0) Preconditions (one-time)
5+
export PATH="$(go env GOPATH)/bin:$PATH"
6+
go install sigs.k8s.io/controller-tools/cmd/[email protected] >/dev/null
7+
go install k8s.io/code-generator/cmd/[email protected] >/dev/null
8+
git fetch origin main --depth=1 || true
9+
10+
# 1) Compute base (robust to unrelated histories)
11+
BASE="$(git merge-base HEAD origin/main 2>/dev/null || true)"
12+
if [ -z "${BASE}" ]; then
13+
echo "No merge-base with origin/main → running full set"
14+
PKGS="./..."
15+
E2E_OUT="$(go run ./scripts/affected-packages.go -mode=suites -changed-files go.mod || true)"
16+
else
17+
PKGS="$(go run ./scripts/affected-packages.go -base "${BASE}")"
18+
E2E_OUT="$(go run ./scripts/affected-packages.go -mode=suites -base "${BASE}")"
19+
fi
20+
21+
# 2) Print what will run
22+
echo "=== Affected unit packages ==="
23+
if [ -n "${PKGS}" ]; then echo "${PKGS}"; else echo "(none)"; fi
24+
echo
25+
echo "=== Affected e2e tests ==="
26+
if [ -n "${E2E_OUT}" ]; then echo "${E2E_OUT}"; else echo "(none)"; fi
27+
echo
28+
29+
# 3) Unit tests via Makefile (inherits required build tags)
30+
if [ "${PKGS}" = "./..." ]; then
31+
echo "Running: make test (all)"
32+
make test
33+
elif [ -n "${PKGS}" ]; then
34+
echo "Running: make test-packages for affected pkgs"
35+
PACKAGES="$(echo "${PKGS}" | xargs)" make test-packages
36+
else
37+
echo "No affected unit packages"
38+
fi
39+
40+
# 4) E2E tests via Makefile (filtered by regex)
41+
PRE="$(echo "${E2E_OUT}" | awk -F: '$1=="preflight"{print $2}' | paste -sd'|' -)"
42+
SB="$( echo "${E2E_OUT}" | awk -F: '$1=="support-bundle"{print $2}' | paste -sd'|' -)"
43+
44+
if [ -n "${PRE}" ]; then
45+
echo "Running preflight e2e: ${PRE}"
46+
RUN="^((${PRE}))$" make support-bundle-e2e-go-test
47+
fi
48+
if [ -n "${SB}" ]; then
49+
echo "Running support-bundle e2e: ${SB}"
50+
RUN="^((${SB}))$" make support-bundle-e2e-go-test
51+
fi
File renamed without changes.

0 commit comments

Comments
 (0)