test.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. kube::golang::setup_env
  21. kube::test::find_dirs() {
  22. (
  23. cd ${KUBE_ROOT}
  24. find -L . -not \( \
  25. \( \
  26. -path './_artifacts/*' \
  27. -o -path './_output/*' \
  28. -o -path './_gopath/*' \
  29. -o -path './contrib/podex/*' \
  30. -o -path './output/*' \
  31. -o -path './release/*' \
  32. -o -path './target/*' \
  33. -o -path './test/e2e/*' \
  34. -o -path './test/e2e_node/*' \
  35. -o -path './test/integration/*' \
  36. -o -path './test/component/scheduler/perf/*' \
  37. -o -path './third_party/*' \
  38. -o -path './staging/*' \
  39. -o -path './vendor/*' \
  40. \) -prune \
  41. \) -name '*_test.go' -print0 | xargs -0n1 dirname | sed 's|^\./||' | sort -u
  42. find -L . \
  43. -path './_output' -prune \
  44. -o -path './vendor/k8s.io/client-go/*' \
  45. -name '*_test.go' -print0 | xargs -0n1 dirname | sed 's|^\./||' | sort -u
  46. )
  47. }
  48. KUBE_TIMEOUT=${KUBE_TIMEOUT:--timeout 120s}
  49. KUBE_COVER=${KUBE_COVER:-n} # set to 'y' to enable coverage collection
  50. KUBE_COVERMODE=${KUBE_COVERMODE:-atomic}
  51. # How many 'go test' instances to run simultaneously when running tests in
  52. # coverage mode.
  53. KUBE_COVERPROCS=${KUBE_COVERPROCS:-4}
  54. KUBE_RACE=${KUBE_RACE:-} # use KUBE_RACE="-race" to enable race testing
  55. # Set to the goveralls binary path to report coverage results to Coveralls.io.
  56. KUBE_GOVERALLS_BIN=${KUBE_GOVERALLS_BIN:-}
  57. # Lists of API Versions of each groups that should be tested, groups are
  58. # separated by comma, lists are separated by semicolon. e.g.,
  59. # "v1,compute/v1alpha1,experimental/v1alpha2;v1,compute/v2,experimental/v1alpha3"
  60. # FIXME: due to current implementation of a test client (see: pkg/api/testapi/testapi.go)
  61. # ONLY the last version is tested in each group.
  62. KUBE_TEST_API_VERSIONS=${KUBE_TEST_API_VERSIONS:-"v1,apps/v1alpha1,authentication.k8s.io/v1beta1,authorization.k8s.io/v1beta1,autoscaling/v1,batch/v1,batch/v2alpha1,certificates/v1alpha1,extensions/v1beta1,federation/v1beta1,policy/v1alpha1,rbac.authorization.k8s.io/v1alpha1,imagepolicy.k8s.io/v1alpha1"}
  63. # once we have multiple group supports
  64. # Create a junit-style XML test report in this directory if set.
  65. KUBE_JUNIT_REPORT_DIR=${KUBE_JUNIT_REPORT_DIR:-}
  66. # Set to 'y' to keep the verbose stdout from tests when KUBE_JUNIT_REPORT_DIR is
  67. # set.
  68. KUBE_KEEP_VERBOSE_TEST_OUTPUT=${KUBE_KEEP_VERBOSE_TEST_OUTPUT:-n}
  69. kube::test::usage() {
  70. kube::log::usage_from_stdin <<EOF
  71. usage: $0 [OPTIONS] [TARGETS]
  72. OPTIONS:
  73. -p <number> : number of parallel workers, must be >= 1
  74. EOF
  75. }
  76. isnum() {
  77. [[ "$1" =~ ^[0-9]+$ ]]
  78. }
  79. PARALLEL="${PARALLEL:-1}"
  80. while getopts "hp:i:" opt ; do
  81. case $opt in
  82. h)
  83. kube::test::usage
  84. exit 0
  85. ;;
  86. p)
  87. PARALLEL="$OPTARG"
  88. if ! isnum "${PARALLEL}" || [[ "${PARALLEL}" -le 0 ]]; then
  89. kube::log::usage "'$0': argument to -p must be numeric and greater than 0"
  90. kube::test::usage
  91. exit 1
  92. fi
  93. ;;
  94. i)
  95. kube::log::usage "'$0': use GOFLAGS='-count <num-iterations>'"
  96. kube::test::usage
  97. exit 1
  98. ;;
  99. ?)
  100. kube::test::usage
  101. exit 1
  102. ;;
  103. :)
  104. kube::log::usage "Option -$OPTARG <value>"
  105. kube::test::usage
  106. exit 1
  107. ;;
  108. esac
  109. done
  110. shift $((OPTIND - 1))
  111. # Use eval to preserve embedded quoted strings.
  112. eval "goflags=(${KUBE_GOFLAGS:-})"
  113. eval "testargs=(${KUBE_TEST_ARGS:-})"
  114. # Used to filter verbose test output.
  115. go_test_grep_pattern=".*"
  116. # The go-junit-report tool needs full test case information to produce a
  117. # meaningful report.
  118. if [[ -n "${KUBE_JUNIT_REPORT_DIR}" ]] ; then
  119. goflags+=(-v)
  120. # Show only summary lines by matching lines like "status package/test"
  121. go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
  122. fi
  123. # Filter out arguments that start with "-" and move them to goflags.
  124. testcases=()
  125. for arg; do
  126. if [[ "${arg}" == -* ]]; then
  127. goflags+=("${arg}")
  128. else
  129. testcases+=("${arg}")
  130. fi
  131. done
  132. if [[ ${#testcases[@]} -eq 0 ]]; then
  133. testcases=($(kube::test::find_dirs))
  134. fi
  135. set -- "${testcases[@]+${testcases[@]}}"
  136. junitFilenamePrefix() {
  137. if [[ -z "${KUBE_JUNIT_REPORT_DIR}" ]]; then
  138. echo ""
  139. return
  140. fi
  141. mkdir -p "${KUBE_JUNIT_REPORT_DIR}"
  142. # This filename isn't parsed by anything, and we must avoid
  143. # exceeding 255 character filename limit. KUBE_TEST_API
  144. # barely fits there and in coverage mode test names are
  145. # appended to generated file names, easily exceeding
  146. # 255 chars in length. So let's just sha1sum it.
  147. local KUBE_TEST_API_HASH="$(echo -n "${KUBE_TEST_API//\//-}"|sha1sum|awk '{print $1}')"
  148. echo "${KUBE_JUNIT_REPORT_DIR}/junit_${KUBE_TEST_API_HASH}_$(kube::util::sortable_date)"
  149. }
  150. produceJUnitXMLReport() {
  151. local -r junit_filename_prefix=$1
  152. if [[ -z "${junit_filename_prefix}" ]]; then
  153. return
  154. fi
  155. local test_stdout_filenames
  156. local junit_xml_filename
  157. test_stdout_filenames=$(ls ${junit_filename_prefix}*.stdout)
  158. junit_xml_filename="${junit_filename_prefix}.xml"
  159. if ! command -v go-junit-report >/dev/null 2>&1; then
  160. kube::log::error "go-junit-report not found; please install with " \
  161. "go get -u github.com/jstemmer/go-junit-report"
  162. return
  163. fi
  164. cat ${test_stdout_filenames} | go-junit-report > "${junit_xml_filename}"
  165. if [[ ! ${KUBE_KEEP_VERBOSE_TEST_OUTPUT} =~ ^[yY]$ ]]; then
  166. rm ${test_stdout_filenames}
  167. fi
  168. kube::log::status "Saved JUnit XML test report to ${junit_xml_filename}"
  169. }
  170. runTests() {
  171. local junit_filename_prefix
  172. junit_filename_prefix=$(junitFilenamePrefix)
  173. # If we're not collecting coverage, run all requested tests with one 'go test'
  174. # command, which is much faster.
  175. if [[ ! ${KUBE_COVER} =~ ^[yY]$ ]]; then
  176. kube::log::status "Running tests without code coverage"
  177. # `go test` does not install the things it builds. `go test -i` installs
  178. # the build artifacts but doesn't run the tests. The two together provide
  179. # a large speedup for tests that do not need to be rebuilt.
  180. go test -i "${goflags[@]:+${goflags[@]}}" \
  181. ${KUBE_RACE} ${KUBE_TIMEOUT} "${@+${@/#/${KUBE_GO_PACKAGE}/}}" \
  182. "${testargs[@]:+${testargs[@]}}"
  183. go test "${goflags[@]:+${goflags[@]}}" \
  184. ${KUBE_RACE} ${KUBE_TIMEOUT} "${@+${@/#/${KUBE_GO_PACKAGE}/}}" \
  185. "${testargs[@]:+${testargs[@]}}" \
  186. | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} \
  187. | grep "${go_test_grep_pattern}" && rc=$? || rc=$?
  188. produceJUnitXMLReport "${junit_filename_prefix}"
  189. return ${rc}
  190. fi
  191. # Create coverage report directories.
  192. cover_report_dir="/tmp/k8s_coverage/${KUBE_TEST_API}/$(kube::util::sortable_date)"
  193. cover_profile="coverage.out" # Name for each individual coverage profile
  194. kube::log::status "Saving coverage output in '${cover_report_dir}'"
  195. mkdir -p "${@+${@/#/${cover_report_dir}/}}"
  196. # Run all specified tests, collecting coverage results. Go currently doesn't
  197. # support collecting coverage across multiple packages at once, so we must issue
  198. # separate 'go test' commands for each package and then combine at the end.
  199. # To speed things up considerably, we can at least use xargs -P to run multiple
  200. # 'go test' commands at once.
  201. # To properly parse the test results if generating a JUnit test report, we
  202. # must make sure the output from PARALLEL runs is not mixed. To achieve this,
  203. # we spawn a subshell for each PARALLEL process, redirecting the output to
  204. # separate files.
  205. # cmd/libs/go2idl/generator is fragile when run under coverage, so ignore it for now.
  206. # see: https://github.com/kubernetes/kubernetes/issues/24967
  207. #
  208. # `go test` does not install the things it builds. `go test -i` installs
  209. # the build artifacts but doesn't run the tests. The two together provide
  210. # a large speedup for tests that do not need to be rebuilt.
  211. printf "%s\n" "${@}" | grep -v "cmd/libs/go2idl/generator"| xargs -I{} -n1 -P${KUBE_COVERPROCS} \
  212. bash -c "set -o pipefail; _pkg=\"{}\"; _pkg_out=\${_pkg//\//_}; \
  213. go test -i ${goflags[@]:+${goflags[@]}} \
  214. ${KUBE_RACE} \
  215. ${KUBE_TIMEOUT} \
  216. -cover -covermode=\"${KUBE_COVERMODE}\" \
  217. -coverprofile=\"${cover_report_dir}/\${_pkg}/${cover_profile}\" \
  218. \"${KUBE_GO_PACKAGE}/\${_pkg}\" \
  219. ${testargs[@]:+${testargs[@]}}
  220. go test ${goflags[@]:+${goflags[@]}} \
  221. ${KUBE_RACE} \
  222. ${KUBE_TIMEOUT} \
  223. -cover -covermode=\"${KUBE_COVERMODE}\" \
  224. -coverprofile=\"${cover_report_dir}/\${_pkg}/${cover_profile}\" \
  225. \"${KUBE_GO_PACKAGE}/\${_pkg}\" \
  226. ${testargs[@]:+${testargs[@]}} \
  227. | tee ${junit_filename_prefix:+\"${junit_filename_prefix}-\$_pkg_out.stdout\"} \
  228. | grep \"${go_test_grep_pattern}\"" \
  229. && test_result=$? || test_result=$?
  230. produceJUnitXMLReport "${junit_filename_prefix}"
  231. COMBINED_COVER_PROFILE="${cover_report_dir}/combined-coverage.out"
  232. {
  233. # The combined coverage profile needs to start with a line indicating which
  234. # coverage mode was used (set, count, or atomic). This line is included in
  235. # each of the coverage profiles generated when running 'go test -cover', but
  236. # we strip these lines out when combining so that there's only one.
  237. echo "mode: ${KUBE_COVERMODE}"
  238. # Include all coverage reach data in the combined profile, but exclude the
  239. # 'mode' lines, as there should be only one.
  240. for x in `find "${cover_report_dir}" -name "${cover_profile}"`; do
  241. cat $x | grep -h -v "^mode:" || true
  242. done
  243. } >"${COMBINED_COVER_PROFILE}"
  244. coverage_html_file="${cover_report_dir}/combined-coverage.html"
  245. go tool cover -html="${COMBINED_COVER_PROFILE}" -o="${coverage_html_file}"
  246. kube::log::status "Combined coverage report: ${coverage_html_file}"
  247. return ${test_result}
  248. }
  249. reportCoverageToCoveralls() {
  250. if [[ ${KUBE_COVER} =~ ^[yY]$ ]] && [[ -x "${KUBE_GOVERALLS_BIN}" ]]; then
  251. kube::log::status "Reporting coverage results to Coveralls for service ${CI_NAME:-}"
  252. ${KUBE_GOVERALLS_BIN} -coverprofile="${COMBINED_COVER_PROFILE}" \
  253. ${CI_NAME:+"-service=${CI_NAME}"} \
  254. ${COVERALLS_REPO_TOKEN:+"-repotoken=${COVERALLS_REPO_TOKEN}"} \
  255. || true
  256. fi
  257. }
  258. checkFDs() {
  259. # several unittests panic when httptest cannot open more sockets
  260. # due to the low default files limit on OS X. Warn about low limit.
  261. local fileslimit="$(ulimit -n)"
  262. if [[ $fileslimit -lt 1000 ]]; then
  263. echo "WARNING: ulimit -n (files) should be at least 1000, is $fileslimit, may cause test failure";
  264. fi
  265. }
  266. checkFDs
  267. # Convert the CSVs to arrays.
  268. IFS=';' read -a apiVersions <<< "${KUBE_TEST_API_VERSIONS}"
  269. apiVersionsCount=${#apiVersions[@]}
  270. for (( i=0; i<${apiVersionsCount}; i++ )); do
  271. apiVersion=${apiVersions[i]}
  272. echo "Running tests for APIVersion: $apiVersion"
  273. # KUBE_TEST_API sets the version of each group to be tested.
  274. KUBE_TEST_API="${apiVersion}" runTests "$@"
  275. done
  276. # We might run the tests for multiple versions, but we want to report only
  277. # one of them to coveralls. Here we report coverage from the last run.
  278. reportCoverageToCoveralls