verify-test-featuregates.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. cd "${KUBE_ROOT}"
  21. rc=0
  22. # find test files accessing the mutable global feature gate or interface
  23. direct_sets=$(grep -n --include '*_test.go' -R 'MutableFeatureGate' . 2>/dev/null) || true
  24. if [[ -n "${direct_sets}" ]]; then
  25. echo "Test files may not access mutable global feature gates directly:" >&2
  26. echo "${direct_sets}" >&2
  27. echo >&2
  28. echo "Use this invocation instead:" >&2
  29. echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
  30. echo >&2
  31. rc=1
  32. fi
  33. # find test files calling SetFeatureGateDuringTest and not calling the result
  34. missing_defers=$(grep -n --include '*_test.go' -R 'SetFeatureGateDuringTest' . 2>/dev/null | grep -E -v "defer .*\\)\\(\\)$") || true
  35. if [[ -n "${missing_defers}" ]]; then
  36. echo "Invalid invocations of featuregatetesting.SetFeatureGateDuringTest():" >&2
  37. echo "${missing_defers}" >&2
  38. echo >&2
  39. echo "Always make a deferred call to the returned function to ensure the feature gate is reset:" >&2
  40. echo " defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()" >&2
  41. echo >&2
  42. rc=1
  43. fi
  44. exit $rc