verify.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-*-dockerized.sh" # Don't run any scripts that intended to be run dockerized
  31. )
  32. # Exclude typecheck in certain cases, if they're running in a separate job.
  33. if [[ ${EXCLUDE_TYPECHECK:-} =~ ^[yY]$ ]]; then
  34. EXCLUDED_PATTERNS+=(
  35. "verify-typecheck.sh" # runs in separate typecheck job
  36. "verify-typecheck-providerless.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. )
  70. while IFS='' read -r line; do EXCLUDED_CHECKS+=("$line"); done < <(ls "${EXCLUDED_PATTERNS[@]/#/${KUBE_ROOT}\/hack\/}" 2>/dev/null || true)
  71. while IFS='' read -r line; do QUICK_CHECKS+=("$line"); done < <(ls "${QUICK_PATTERNS[@]/#/${KUBE_ROOT}\/hack\/}" 2>/dev/null || true)
  72. TARGET_LIST=()
  73. IFS=" " read -r -a TARGET_LIST <<< "${WHAT:-}"
  74. function is-excluded {
  75. for e in "${EXCLUDED_CHECKS[@]}"; do
  76. if [[ $1 -ef "${e}" ]]; then
  77. return
  78. fi
  79. done
  80. return 1
  81. }
  82. function is-quick {
  83. for e in "${QUICK_CHECKS[@]}"; do
  84. if [[ $1 -ef "${e}" ]]; then
  85. return
  86. fi
  87. done
  88. return 1
  89. }
  90. function is-explicitly-chosen {
  91. local name="${1#verify-}"
  92. name="${name%.*}"
  93. index=0
  94. for e in "${TARGET_LIST[@]}"; do
  95. if [[ "${e}" == "${name}" ]]; then
  96. TARGET_LIST[${index}]=""
  97. return
  98. fi
  99. index=$((index + 1))
  100. done
  101. return 1
  102. }
  103. function run-cmd {
  104. local filename="${2##*/verify-}"
  105. local testname="${filename%%.*}"
  106. local output="${KUBE_JUNIT_REPORT_DIR:-/tmp/junit-results}"
  107. local tr
  108. if ${SILENT}; then
  109. juLog -output="${output}" -class="verify" -name="${testname}" "$@" &> /dev/null
  110. tr=$?
  111. else
  112. juLog -output="${output}" -class="verify" -name="${testname}" "$@"
  113. tr=$?
  114. fi
  115. return ${tr}
  116. }
  117. # Collect Failed tests in this Array , initialize it to nil
  118. FAILED_TESTS=()
  119. function print-failed-tests {
  120. echo -e "========================"
  121. echo -e "${color_red:?}FAILED TESTS${color_norm:?}"
  122. echo -e "========================"
  123. for t in "${FAILED_TESTS[@]}"; do
  124. echo -e "${color_red}${t}${color_norm}"
  125. done
  126. }
  127. function run-checks {
  128. local -r pattern=$1
  129. local -r runner=$2
  130. local t
  131. for t in ${pattern}
  132. do
  133. local check_name
  134. check_name="$(basename "${t}")"
  135. if [[ -n ${WHAT:-} ]]; then
  136. if ! is-explicitly-chosen "${check_name}"; then
  137. continue
  138. fi
  139. else
  140. if is-excluded "${t}" ; then
  141. echo "Skipping ${check_name}"
  142. continue
  143. fi
  144. if ${QUICK} && ! is-quick "${t}" ; then
  145. echo "Skipping ${check_name} in quick mode"
  146. continue
  147. fi
  148. fi
  149. echo -e "Verifying ${check_name}"
  150. local start
  151. start=$(date +%s)
  152. run-cmd "${runner}" "${t}" && tr=$? || tr=$?
  153. local elapsed=$(($(date +%s) - start))
  154. if [[ ${tr} -eq 0 ]]; then
  155. echo -e "${color_green:?}SUCCESS${color_norm} ${check_name}\t${elapsed}s"
  156. else
  157. echo -e "${color_red}FAILED${color_norm} ${check_name}\t${elapsed}s"
  158. ret=1
  159. FAILED_TESTS+=("${t}")
  160. fi
  161. done
  162. }
  163. # Check invalid targets specified in "WHAT" and mark them as failure cases
  164. function missing-target-checks {
  165. # In case WHAT is not specified
  166. [[ ${#TARGET_LIST[@]} -eq 0 ]] && return
  167. for v in "${TARGET_LIST[@]}"
  168. do
  169. [[ -z "${v}" ]] && continue
  170. FAILED_TESTS+=("${v}")
  171. ret=1
  172. done
  173. }
  174. SILENT=${SILENT:-false}
  175. QUICK=${QUICK:-false}
  176. if ${SILENT} ; then
  177. echo "Running in silent mode, run with SILENT=false if you want to see script logs."
  178. fi
  179. if ${QUICK} ; then
  180. echo "Running in quick mode (QUICK=true). Only fast checks will run."
  181. fi
  182. ret=0
  183. run-checks "${KUBE_ROOT}/hack/verify-*.sh" bash
  184. run-checks "${KUBE_ROOT}/hack/verify-*.py" python
  185. missing-target-checks
  186. if [[ ${ret} -eq 1 ]]; then
  187. print-failed-tests
  188. fi
  189. exit ${ret}
  190. # ex: ts=2 sw=2 et filetype=sh