verify.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/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. KRATOS_ROOT=$(dirname "${BASH_SOURCE}")/../..
  19. source "${KRATOS_ROOT}/hack/lib/util.sh"
  20. # include shell2junit library
  21. # source "${KRATOS_ROOT}/third_party/forked/shell2junit/sh2ju.sh"
  22. # Excluded check patterns are always skipped.
  23. EXCLUDED_PATTERNS=(
  24. "verify-all.sh" # this script calls the make rule and would cause a loop
  25. "verify-linkcheck.sh" # runs in separate Jenkins job once per day due to high network usage
  26. "verify-test-owners.sh" # TODO(rmmh): figure out how to avoid endless conflicts
  27. "verify-*-dockerized.sh" # Don't run any scripts that intended to be run dockerized
  28. "verify-typecheck.sh" # runs in separate typecheck job
  29. )
  30. # Only run whitelisted fast checks in quick mode.
  31. # These run in <10s each on enisoc's workstation, assuming that
  32. # `make` and `hack/godep-restore.sh` had already been run.
  33. QUICK_PATTERNS+=(
  34. "verify-api-groups.sh"
  35. "verify-bazel.sh"
  36. "verify-boilerplate.sh"
  37. "verify-generated-files-remake"
  38. "verify-godep-licenses.sh"
  39. "verify-gofmt.sh"
  40. "verify-imports.sh"
  41. "verify-pkg-names.sh"
  42. "verify-readonly-packages.sh"
  43. "verify-spelling.sh"
  44. "verify-staging-client-go.sh"
  45. "verify-test-images.sh"
  46. "verify-test-owners.sh"
  47. )
  48. EXCLUDED_CHECKS=$(ls ${EXCLUDED_PATTERNS[@]/#/${KRATOS_ROOT}\/hack\/} 2>/dev/null || true)
  49. QUICK_CHECKS=$(ls ${QUICK_PATTERNS[@]/#/${KRATOS_ROOT}\/hack\/} 2>/dev/null || true)
  50. function is-excluded {
  51. for e in ${EXCLUDED_CHECKS[@]}; do
  52. if [[ $1 -ef "$e" ]]; then
  53. return
  54. fi
  55. done
  56. return 1
  57. }
  58. function is-quick {
  59. for e in ${QUICK_CHECKS[@]}; do
  60. if [[ $1 -ef "$e" ]]; then
  61. return
  62. fi
  63. done
  64. return 1
  65. }
  66. function is-explicitly-chosen {
  67. local name="${1#verify-}"
  68. name="${name%.*}"
  69. for e in ${WHAT}; do
  70. if [[ $e == "$name" ]]; then
  71. return
  72. fi
  73. done
  74. return 1
  75. }
  76. function run-cmd {
  77. local filename="${2##*/verify-}"
  78. local testname="${filename%%.*}"
  79. local output="${KRATOS_JUNIT_REPORT_DIR:-/tmp/junit-results}"
  80. local tr
  81. if ${SILENT}; then
  82. juLog -output="${output}" -class="verify" -name="${testname}" "$@" &> /dev/null
  83. tr=$?
  84. else
  85. juLog -output="${output}" -class="verify" -name="${testname}" "$@"
  86. tr=$?
  87. fi
  88. return ${tr}
  89. }
  90. # Collect Failed tests in this Array , initialize it to nil
  91. FAILED_TESTS=()
  92. function print-failed-tests {
  93. echo -e "========================"
  94. echo -e "${color_red}FAILED TESTS${color_norm}"
  95. echo -e "========================"
  96. for t in ${FAILED_TESTS[@]}; do
  97. echo -e "${color_red}${t}${color_norm}"
  98. done
  99. }
  100. function run-checks {
  101. local -r pattern=$1
  102. local -r runner=$2
  103. local t
  104. for t in $(ls ${pattern})
  105. do
  106. local check_name="$(basename "${t}")"
  107. if [[ ! -z ${WHAT:-} ]]; then
  108. if ! is-explicitly-chosen "${check_name}"; then
  109. continue
  110. fi
  111. else
  112. if is-excluded "${t}" ; then
  113. echo "Skipping ${check_name}"
  114. continue
  115. fi
  116. if ${QUICK} && ! is-quick "${t}" ; then
  117. echo "Skipping ${check_name} in quick mode"
  118. continue
  119. fi
  120. fi
  121. echo -e "Verifying ${check_name}"
  122. local start=$(date +%s)
  123. run-cmd "${runner}" "${t}" && tr=$? || tr=$?
  124. local elapsed=$(($(date +%s) - ${start}))
  125. if [[ ${tr} -eq 0 ]]; then
  126. echo -e "${color_green}SUCCESS${color_norm} ${check_name}\t${elapsed}s"
  127. else
  128. echo -e "${color_red}FAILED${color_norm} ${check_name}\t${elapsed}s"
  129. ret=1
  130. FAILED_TESTS+=(${t})
  131. fi
  132. done
  133. }
  134. SILENT=${SILENT:-false}
  135. QUICK=${QUICK:-false}
  136. if ${SILENT} ; then
  137. echo "Running in silent mode, run with SILENT=false if you want to see script logs."
  138. fi
  139. if ${QUICK} ; then
  140. echo "Running in quick mode (QUICK=true). Only fast checks will run."
  141. fi
  142. ret=0
  143. run-checks "${KRATOS_ROOT}/hack/verify-*.sh" bash
  144. run-checks "${KRATOS_ROOT}/hack/verify-*.py" python
  145. if [[ ${ret} -eq 1 ]]; then
  146. print-failed-tests
  147. fi
  148. exit ${ret}
  149. # ex: ts=2 sw=2 et filetype=sh