verify.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env bash
  2. # Copyright 2014 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/util.sh"
  20. # If KUBE_JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
  21. if [[ -z "${KUBE_JUNIT_REPORT_DIR:-}" && -n "${ARTIFACTS:-}" ]]; then
  22. export KUBE_JUNIT_REPORT_DIR="${ARTIFACTS}"
  23. fi
  24. # include shell2junit library
  25. source "${KUBE_ROOT}/third_party/forked/shell2junit/sh2ju.sh"
  26. # Excluded check patterns are always skipped.
  27. EXCLUDED_PATTERNS=(
  28. "verify-all.sh" # this script calls the make rule and would cause a loop
  29. "verify-linkcheck.sh" # runs in separate Jenkins job once per day due to high network usage
  30. "verify-test-owners.sh" # TODO(rmmh): figure out how to avoid endless conflicts
  31. "verify-*-dockerized.sh" # Don't run any scripts that intended to be run dockerized
  32. )
  33. # Exclude typecheck in certain cases, if they're running in a separate job.
  34. if [[ ${EXCLUDE_TYPECHECK:-} =~ ^[yY]$ ]]; then
  35. EXCLUDED_PATTERNS+=(
  36. "verify-typecheck.sh" # runs in separate typecheck job
  37. )
  38. fi
  39. # Exclude vendor checks in certain cases, if they're running in a separate job.
  40. if [[ ${EXCLUDE_GODEP:-} =~ ^[yY]$ ]]; then
  41. EXCLUDED_PATTERNS+=(
  42. "verify-vendor.sh" # runs in separate godeps job
  43. "verify-vendor-licenses.sh" # runs in separate godeps job
  44. )
  45. fi
  46. # Exclude readonly package check in certain cases, aka, in periodic jobs we don't care and a readonly package won't be touched
  47. if [[ ${EXCLUDE_READONLY_PACKAGE:-} =~ ^[yY]$ ]]; then
  48. EXCLUDED_PATTERNS+=(
  49. "verify-readonly-packages.sh" # skip in CI, if env is set
  50. )
  51. fi
  52. # Only run whitelisted fast checks in quick mode.
  53. # These run in <10s each on enisoc's workstation, assuming that
  54. # `make` had already been run.
  55. QUICK_PATTERNS+=(
  56. "verify-api-groups.sh"
  57. "verify-bazel.sh"
  58. "verify-boilerplate.sh"
  59. "verify-vendor-licenses.sh"
  60. "verify-gofmt.sh"
  61. "verify-imports.sh"
  62. "verify-pkg-names.sh"
  63. "verify-readonly-packages.sh"
  64. "verify-spelling.sh"
  65. "verify-staging-client-go.sh"
  66. "verify-staging-meta-files.sh"
  67. "verify-test-featuregates.sh"
  68. "verify-test-images.sh"
  69. "verify-test-owners.sh"
  70. )
  71. while IFS='' read -r line; do EXCLUDED_CHECKS+=("$line"); done < <(ls "${EXCLUDED_PATTERNS[@]/#/${KUBE_ROOT}\/hack\/}" 2>/dev/null || true)
  72. while IFS='' read -r line; do QUICK_CHECKS+=("$line"); done < <(ls "${QUICK_PATTERNS[@]/#/${KUBE_ROOT}\/hack\/}" 2>/dev/null || true)
  73. TARGET_LIST=()
  74. IFS=" " read -r -a TARGET_LIST <<< "${WHAT:-}"
  75. function is-excluded {
  76. for e in "${EXCLUDED_CHECKS[@]}"; do
  77. if [[ $1 -ef "${e}" ]]; then
  78. return
  79. fi
  80. done
  81. return 1
  82. }
  83. function is-quick {
  84. for e in "${QUICK_CHECKS[@]}"; do
  85. if [[ $1 -ef "${e}" ]]; then
  86. return
  87. fi
  88. done
  89. return 1
  90. }
  91. function is-explicitly-chosen {
  92. local name="${1#verify-}"
  93. name="${name%.*}"
  94. index=0
  95. for e in "${TARGET_LIST[@]}"; do
  96. if [[ "${e}" == "${name}" ]]; then
  97. TARGET_LIST[${index}]=""
  98. return
  99. fi
  100. index=$((index + 1))
  101. done
  102. return 1
  103. }
  104. function run-cmd {
  105. local filename="${2##*/verify-}"
  106. local testname="${filename%%.*}"
  107. local output="${KUBE_JUNIT_REPORT_DIR:-/tmp/junit-results}"
  108. local tr
  109. if ${SILENT}; then
  110. juLog -output="${output}" -class="verify" -name="${testname}" "$@" &> /dev/null
  111. tr=$?
  112. else
  113. juLog -output="${output}" -class="verify" -name="${testname}" "$@"
  114. tr=$?
  115. fi
  116. return ${tr}
  117. }
  118. # Collect Failed tests in this Array , initialize it to nil
  119. FAILED_TESTS=()
  120. function print-failed-tests {
  121. echo -e "========================"
  122. echo -e "${color_red:?}FAILED TESTS${color_norm:?}"
  123. echo -e "========================"
  124. for t in "${FAILED_TESTS[@]}"; do
  125. echo -e "${color_red}${t}${color_norm}"
  126. done
  127. }
  128. function run-checks {
  129. local -r pattern=$1
  130. local -r runner=$2
  131. local t
  132. for t in ${pattern}
  133. do
  134. local check_name
  135. check_name="$(basename "${t}")"
  136. if [[ -n ${WHAT:-} ]]; then
  137. if ! is-explicitly-chosen "${check_name}"; then
  138. continue
  139. fi
  140. else
  141. if is-excluded "${t}" ; then
  142. echo "Skipping ${check_name}"
  143. continue
  144. fi
  145. if ${QUICK} && ! is-quick "${t}" ; then
  146. echo "Skipping ${check_name} in quick mode"
  147. continue
  148. fi
  149. fi
  150. echo -e "Verifying ${check_name}"
  151. local start
  152. start=$(date +%s)
  153. run-cmd "${runner}" "${t}" && tr=$? || tr=$?
  154. local elapsed=$(($(date +%s) - start))
  155. if [[ ${tr} -eq 0 ]]; then
  156. echo -e "${color_green:?}SUCCESS${color_norm} ${check_name}\t${elapsed}s"
  157. else
  158. echo -e "${color_red}FAILED${color_norm} ${check_name}\t${elapsed}s"
  159. ret=1
  160. FAILED_TESTS+=("${t}")
  161. fi
  162. done
  163. }
  164. # Check invalid targets specified in "WHAT" and mark them as failure cases
  165. function missing-target-checks {
  166. # In case WHAT is not specified
  167. [[ ${#TARGET_LIST[@]} -eq 0 ]] && return
  168. for v in "${TARGET_LIST[@]}"
  169. do
  170. [[ -z "${v}" ]] && continue
  171. FAILED_TESTS+=("${v}")
  172. ret=1
  173. done
  174. }
  175. SILENT=${SILENT:-false}
  176. QUICK=${QUICK:-false}
  177. if ${SILENT} ; then
  178. echo "Running in silent mode, run with SILENT=false if you want to see script logs."
  179. fi
  180. if ${QUICK} ; then
  181. echo "Running in quick mode (QUICK=true). Only fast checks will run."
  182. fi
  183. ret=0
  184. run-checks "${KUBE_ROOT}/hack/verify-*.sh" bash
  185. run-checks "${KUBE_ROOT}/hack/verify-*.py" python
  186. missing-target-checks
  187. if [[ ${ret} -eq 1 ]]; then
  188. print-failed-tests
  189. fi
  190. exit ${ret}
  191. # ex: ts=2 sw=2 et filetype=sh