verify-test-code.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env bash
  2. # Copyright 2019 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. all_e2e_files=()
  22. # NOTE: This checks e2e test code without the e2e framework which contains Expect().To(HaveOccurred())
  23. kube::util::read-array all_e2e_files < <(find test/e2e{,_node,_kubeadm} -name '*.go' | grep -v 'test/e2e/framework/')
  24. errors_expect_no_error=()
  25. for file in "${all_e2e_files[@]}"
  26. do
  27. if grep -E "Expect\(.*\)\.(NotTo|ToNot)\(.*HaveOccurred\(\)" "${file}" > /dev/null
  28. then
  29. errors_expect_no_error+=( "${file}" )
  30. fi
  31. done
  32. errors_expect_error=()
  33. for file in "${all_e2e_files[@]}"
  34. do
  35. if grep "Expect(.*)\.To(.*HaveOccurred()" "${file}" > /dev/null
  36. then
  37. errors_expect_error+=( "${file}" )
  38. fi
  39. done
  40. errors_expect_equal=()
  41. for file in "${all_e2e_files[@]}"
  42. do
  43. if grep -E "Expect\(.*\)\.To\((gomega\.Equal|Equal)" "${file}" > /dev/null
  44. then
  45. errors_expect_equal+=( "${file}" )
  46. fi
  47. done
  48. if [ ${#errors_expect_no_error[@]} -ne 0 ]; then
  49. {
  50. echo "Errors:"
  51. for err in "${errors_expect_no_error[@]}"; do
  52. echo "$err"
  53. done
  54. echo
  55. echo 'The above files need to use framework.ExpectNoError(err) instead of '
  56. echo 'Expect(err).NotTo(HaveOccurred()) or gomega.Expect(err).NotTo(gomega.HaveOccurred())'
  57. echo
  58. } >&2
  59. exit 1
  60. fi
  61. if [ ${#errors_expect_error[@]} -ne 0 ]; then
  62. {
  63. echo "Errors:"
  64. for err in "${errors_expect_error[@]}"; do
  65. echo "$err"
  66. done
  67. echo
  68. echo 'The above files need to use framework.ExpectError(err) instead of '
  69. echo 'Expect(err).To(HaveOccurred()) or gomega.Expect(err).To(gomega.HaveOccurred())'
  70. echo
  71. } >&2
  72. exit 1
  73. fi
  74. if [ ${#errors_expect_equal[@]} -ne 0 ]; then
  75. {
  76. echo "Errors:"
  77. for err in "${errors_expect_equal[@]}"; do
  78. echo "$err"
  79. done
  80. echo
  81. echo 'The above files need to use framework.ExpectEqual(foo, bar) instead of '
  82. echo 'Expect(foo).To(Equal(bar)) or gomega.Expect(foo).To(gomega.Equal(bar))'
  83. echo
  84. } >&2
  85. exit 1
  86. fi
  87. echo 'Congratulations! All e2e test source files are valid.'