verify-test-featuregates.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script checks whether mutable global feature gate is invocated correctly
  16. # in `*_test.go` files.
  17. # Usage: `hack/verify-test-featuregates.sh`.
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  22. source "${KUBE_ROOT}/hack/lib/init.sh"
  23. cd "${KUBE_ROOT}"
  24. rc=0
  25. # find test files accessing the mutable global feature gate or interface
  26. direct_sets=$(find -L . -name '*_test.go' -exec grep -Hn 'MutableFeatureGate' {} \; 2>/dev/null) || true
  27. if [[ -n "${direct_sets}" ]]; then
  28. echo "Test files may not access mutable global feature gates directly:" >&2
  29. echo "${direct_sets}" >&2
  30. echo >&2
  31. echo "Use this invocation instead:" >&2
  32. echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
  33. echo >&2
  34. rc=1
  35. fi
  36. # find test files calling SetFeatureGateDuringTest and not calling the result
  37. missing_defers=$(find -L . -name '*_test.go' -exec grep -Hn 'SetFeatureGateDuringTest' {} \; 2>/dev/null | grep -E -v "defer .*\\)\\(\\)$") || true
  38. if [[ -n "${missing_defers}" ]]; then
  39. echo "Invalid invocations of featuregatetesting.SetFeatureGateDuringTest():" >&2
  40. echo "${missing_defers}" >&2
  41. echo >&2
  42. echo "Always make a deferred call to the returned function to ensure the feature gate is reset:" >&2
  43. echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
  44. echo >&2
  45. rc=1
  46. fi
  47. exit $rc