From 7d89e9b4c07030f43695538d4e22e09f1de06018 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 19 Sep 2024 10:44:50 -0700 Subject: [PATCH 1/2] Only normalize user-provided test targets --- hack/make-rules/test.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/hack/make-rules/test.sh b/hack/make-rules/test.sh index aa1617ea2e6..266aaa5db9a 100755 --- a/hack/make-rules/test.sh +++ b/hack/make-rules/test.sh @@ -147,6 +147,9 @@ goflags=() # Filter out arguments that start with "-" and move them to goflags. testcases=() +# if the user passed no target in, we can skip slow normalization. +normalize="true" + for arg; do if [[ "${arg}" == -* ]]; then goflags+=("${arg}") @@ -155,8 +158,15 @@ for arg; do fi done if [[ ${#testcases[@]} -eq 0 ]]; then + normalize="false" kube::util::read-array testcases < <(kube::test::find_dirs) fi + +if [[ "${normalize}" == "true" ]]; then + # This can be slow! + kube::log::status "Normalizing Go targets" + kube::util::read-array testcases < <(kube::golang::normalize_go_targets "${testcases[@]}") +fi set -- "${testcases[@]+${testcases[@]}}" if [[ -n "${KUBE_RACE}" ]] ; then @@ -190,11 +200,6 @@ runTests() { installTools - # Try to normalize input names. This is slow! - local -a targets - kube::log::status "Normalizing Go targets" - kube::util::read-array targets < <(kube::golang::normalize_go_targets "$@") - # Enable coverage data collection? local cover_msg local COMBINED_COVER_PROFILE @@ -229,7 +234,7 @@ runTests() { go test -json \ "${goflags[@]:+${goflags[@]}}" \ "${KUBE_TIMEOUT}" \ - "${targets[@]}" \ + "$@" \ "${testargs[@]:+${testargs[@]}}" \ && rc=$? || rc=$? From 8912df652b1a9474b1eca12ec359406d1c9d5ef4 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 19 Sep 2024 11:00:58 -0700 Subject: [PATCH 2/2] Use Go workspaces + `go list` to find test targets Plain old UNIX find requires us to do all sorts of silly filtering. --- hack/make-rules/test.sh | 49 ++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/hack/make-rules/test.sh b/hack/make-rules/test.sh index 266aaa5db9a..0842ffb4f22 100755 --- a/hack/make-rules/test.sh +++ b/hack/make-rules/test.sh @@ -23,6 +23,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh" kube::golang::setup_env kube::golang::setup_gomaxprocs +kube::util::require-jq # start the cache mutation detector by default so that cache mutators will be found KUBE_CACHE_MUTATION_DETECTOR="${KUBE_CACHE_MUTATION_DETECTOR:-true}" @@ -32,28 +33,26 @@ export KUBE_CACHE_MUTATION_DETECTOR KUBE_PANIC_WATCH_DECODE_ERROR="${KUBE_PANIC_WATCH_DECODE_ERROR:-true}" export KUBE_PANIC_WATCH_DECODE_ERROR -kube::test::find_dirs() { +kube::test::find_go_packages() { ( cd "${KUBE_ROOT}" - find -L . -not \( \ - \( \ - -path './_artifacts/*' \ - -o -path './_output/*' \ - -o -path './cmd/kubeadm/test/*' \ - -o -path './contrib/podex/*' \ - -o -path './release/*' \ - -o -path './target/*' \ - -o -path './test/e2e/e2e_test.go' \ - -o -path './test/e2e_node/*' \ - -o -path './test/e2e_kubeadm/*' \ - -o -path './test/integration/*' \ - -o -path './third_party/*' \ - -o -path './staging/*' \ - -o -path './vendor/*' \ - \) -prune \ - \) -name '*_test.go' -print0 | xargs -0n1 dirname | LC_ALL=C sort -u - find ./staging -name '*_test.go' -not -path '*/test/integration/*' -prune -print0 | xargs -0n1 dirname | LC_ALL=C sort -u + # Get a list of all the modules in this workspace. + local -a workspace_module_patterns + kube::util::read-array workspace_module_patterns < <(go list -m -json | jq -r '.Path + "/..."') + + # Get a list of all packages which have test files, but filter out ones + # that we don't want to run by default (i.e. are not unit-tests). + go list -find \ + -f '{{if or (gt (len .TestGoFiles) 0) (gt (len .XTestGoFiles) 0)}}{{.ImportPath}}{{end}}' \ + "${workspace_module_patterns[@]}" \ + | grep -vE \ + -e '^k8s.io/kubernetes/third_party(/.*)?$' \ + -e '^k8s.io/kubernetes/cmd/kubeadm/test(/.*)?$' \ + -e '^k8s.io/kubernetes/test/e2e$' \ + -e '^k8s.io/kubernetes/test/e2e_node(/.*)?$' \ + -e '^k8s.io/kubernetes/test/e2e_kubeadm(/.*)?$' \ + -e '^k8s.io/.*/test/integration(/.*)?$' ) } @@ -147,9 +146,6 @@ goflags=() # Filter out arguments that start with "-" and move them to goflags. testcases=() -# if the user passed no target in, we can skip slow normalization. -normalize="true" - for arg; do if [[ "${arg}" == -* ]]; then goflags+=("${arg}") @@ -158,11 +154,10 @@ for arg; do fi done if [[ ${#testcases[@]} -eq 0 ]]; then - normalize="false" - kube::util::read-array testcases < <(kube::test::find_dirs) -fi - -if [[ "${normalize}" == "true" ]]; then + # If the user passed no targets in, we want ~everything. + kube::util::read-array testcases < <(kube::test::find_go_packages) +else + # If the user passed targets, we should normalize them. # This can be slow! kube::log::status "Normalizing Go targets" kube::util::read-array testcases < <(kube::golang::normalize_go_targets "${testcases[@]}")