Skip to content

Commit 8912df6

Browse files
committed
Use Go workspaces + go list to find test targets
Plain old UNIX find requires us to do all sorts of silly filtering.
1 parent 7d89e9b commit 8912df6

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

hack/make-rules/test.sh

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
2323

2424
kube::golang::setup_env
2525
kube::golang::setup_gomaxprocs
26+
kube::util::require-jq
2627

2728
# start the cache mutation detector by default so that cache mutators will be found
2829
KUBE_CACHE_MUTATION_DETECTOR="${KUBE_CACHE_MUTATION_DETECTOR:-true}"
@@ -32,28 +33,26 @@ export KUBE_CACHE_MUTATION_DETECTOR
3233
KUBE_PANIC_WATCH_DECODE_ERROR="${KUBE_PANIC_WATCH_DECODE_ERROR:-true}"
3334
export KUBE_PANIC_WATCH_DECODE_ERROR
3435

35-
kube::test::find_dirs() {
36+
kube::test::find_go_packages() {
3637
(
3738
cd "${KUBE_ROOT}"
38-
find -L . -not \( \
39-
\( \
40-
-path './_artifacts/*' \
41-
-o -path './_output/*' \
42-
-o -path './cmd/kubeadm/test/*' \
43-
-o -path './contrib/podex/*' \
44-
-o -path './release/*' \
45-
-o -path './target/*' \
46-
-o -path './test/e2e/e2e_test.go' \
47-
-o -path './test/e2e_node/*' \
48-
-o -path './test/e2e_kubeadm/*' \
49-
-o -path './test/integration/*' \
50-
-o -path './third_party/*' \
51-
-o -path './staging/*' \
52-
-o -path './vendor/*' \
53-
\) -prune \
54-
\) -name '*_test.go' -print0 | xargs -0n1 dirname | LC_ALL=C sort -u
55-
56-
find ./staging -name '*_test.go' -not -path '*/test/integration/*' -prune -print0 | xargs -0n1 dirname | LC_ALL=C sort -u
39+
40+
# Get a list of all the modules in this workspace.
41+
local -a workspace_module_patterns
42+
kube::util::read-array workspace_module_patterns < <(go list -m -json | jq -r '.Path + "/..."')
43+
44+
# Get a list of all packages which have test files, but filter out ones
45+
# that we don't want to run by default (i.e. are not unit-tests).
46+
go list -find \
47+
-f '{{if or (gt (len .TestGoFiles) 0) (gt (len .XTestGoFiles) 0)}}{{.ImportPath}}{{end}}' \
48+
"${workspace_module_patterns[@]}" \
49+
| grep -vE \
50+
-e '^k8s.io/kubernetes/third_party(/.*)?$' \
51+
-e '^k8s.io/kubernetes/cmd/kubeadm/test(/.*)?$' \
52+
-e '^k8s.io/kubernetes/test/e2e$' \
53+
-e '^k8s.io/kubernetes/test/e2e_node(/.*)?$' \
54+
-e '^k8s.io/kubernetes/test/e2e_kubeadm(/.*)?$' \
55+
-e '^k8s.io/.*/test/integration(/.*)?$'
5756
)
5857
}
5958

@@ -147,9 +146,6 @@ goflags=()
147146

148147
# Filter out arguments that start with "-" and move them to goflags.
149148
testcases=()
150-
# if the user passed no target in, we can skip slow normalization.
151-
normalize="true"
152-
153149
for arg; do
154150
if [[ "${arg}" == -* ]]; then
155151
goflags+=("${arg}")
@@ -158,11 +154,10 @@ for arg; do
158154
fi
159155
done
160156
if [[ ${#testcases[@]} -eq 0 ]]; then
161-
normalize="false"
162-
kube::util::read-array testcases < <(kube::test::find_dirs)
163-
fi
164-
165-
if [[ "${normalize}" == "true" ]]; then
157+
# If the user passed no targets in, we want ~everything.
158+
kube::util::read-array testcases < <(kube::test::find_go_packages)
159+
else
160+
# If the user passed targets, we should normalize them.
166161
# This can be slow!
167162
kube::log::status "Normalizing Go targets"
168163
kube::util::read-array testcases < <(kube::golang::normalize_go_targets "${testcases[@]}")

0 commit comments

Comments
 (0)