test.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/init.sh"
  20. kube::golang::setup_env
  21. # start the cache mutation detector by default so that cache mutators will be found
  22. KUBE_CACHE_MUTATION_DETECTOR="${KUBE_CACHE_MUTATION_DETECTOR:-true}"
  23. export KUBE_CACHE_MUTATION_DETECTOR
  24. # panic the server on watch decode errors since they are considered coder mistakes
  25. KUBE_PANIC_WATCH_DECODE_ERROR="${KUBE_PANIC_WATCH_DECODE_ERROR:-true}"
  26. export KUBE_PANIC_WATCH_DECODE_ERROR
  27. kube::test::find_dirs() {
  28. (
  29. cd "${KUBE_ROOT}"
  30. find -L . -not \( \
  31. \( \
  32. -path './_artifacts/*' \
  33. -o -path './bazel-*/*' \
  34. -o -path './_output/*' \
  35. -o -path './_gopath/*' \
  36. -o -path './cmd/kubeadm/test/*' \
  37. -o -path './contrib/podex/*' \
  38. -o -path './output/*' \
  39. -o -path './release/*' \
  40. -o -path './target/*' \
  41. -o -path './test/e2e/*' \
  42. -o -path './test/e2e_node/*' \
  43. -o -path './test/e2e_kubeadm/*' \
  44. -o -path './test/integration/*' \
  45. -o -path './third_party/*' \
  46. -o -path './staging/*' \
  47. -o -path './vendor/*' \
  48. \) -prune \
  49. \) -name '*_test.go' -print0 | xargs -0n1 dirname | sed "s|^\./|${KUBE_GO_PACKAGE}/|" | LC_ALL=C sort -u
  50. find ./staging -name '*_test.go' -not -path '*/test/integration/*' -prune -print0 | xargs -0n1 dirname | sed 's|^\./staging/src/|./vendor/|' | LC_ALL=C sort -u
  51. )
  52. }
  53. KUBE_TIMEOUT=${KUBE_TIMEOUT:--timeout=120s}
  54. KUBE_COVER=${KUBE_COVER:-n} # set to 'y' to enable coverage collection
  55. KUBE_COVERMODE=${KUBE_COVERMODE:-atomic}
  56. # The directory to save test coverage reports to, if generating them. If unset,
  57. # a semi-predictable temporary directory will be used.
  58. KUBE_COVER_REPORT_DIR="${KUBE_COVER_REPORT_DIR:-}"
  59. # How many 'go test' instances to run simultaneously when running tests in
  60. # coverage mode.
  61. KUBE_COVERPROCS=${KUBE_COVERPROCS:-4}
  62. KUBE_RACE=${KUBE_RACE:-} # use KUBE_RACE="-race" to enable race testing
  63. # Set to the goveralls binary path to report coverage results to Coveralls.io.
  64. KUBE_GOVERALLS_BIN=${KUBE_GOVERALLS_BIN:-}
  65. # once we have multiple group supports
  66. # Create a junit-style XML test report in this directory if set.
  67. KUBE_JUNIT_REPORT_DIR=${KUBE_JUNIT_REPORT_DIR:-}
  68. # If KUBE_JUNIT_REPORT_DIR is unset, and ARTIFACTS is set, then have them match.
  69. if [[ -z "${KUBE_JUNIT_REPORT_DIR:-}" && -n "${ARTIFACTS:-}" ]]; then
  70. export KUBE_JUNIT_REPORT_DIR="${ARTIFACTS}"
  71. fi
  72. # Set to 'y' to keep the verbose stdout from tests when KUBE_JUNIT_REPORT_DIR is
  73. # set.
  74. KUBE_KEEP_VERBOSE_TEST_OUTPUT=${KUBE_KEEP_VERBOSE_TEST_OUTPUT:-n}
  75. kube::test::usage() {
  76. kube::log::usage_from_stdin <<EOF
  77. usage: $0 [OPTIONS] [TARGETS]
  78. OPTIONS:
  79. -p <number> : number of parallel workers, must be >= 1
  80. EOF
  81. }
  82. isnum() {
  83. [[ "$1" =~ ^[0-9]+$ ]]
  84. }
  85. PARALLEL="${PARALLEL:-1}"
  86. while getopts "hp:i:" opt ; do
  87. case ${opt} in
  88. h)
  89. kube::test::usage
  90. exit 0
  91. ;;
  92. p)
  93. PARALLEL="${OPTARG}"
  94. if ! isnum "${PARALLEL}" || [[ "${PARALLEL}" -le 0 ]]; then
  95. kube::log::usage "'$0': argument to -p must be numeric and greater than 0"
  96. kube::test::usage
  97. exit 1
  98. fi
  99. ;;
  100. i)
  101. kube::log::usage "'$0': use GOFLAGS='-count <num-iterations>'"
  102. kube::test::usage
  103. exit 1
  104. ;;
  105. :)
  106. kube::log::usage "Option -${OPTARG} <value>"
  107. kube::test::usage
  108. exit 1
  109. ;;
  110. ?)
  111. kube::test::usage
  112. exit 1
  113. ;;
  114. esac
  115. done
  116. shift $((OPTIND - 1))
  117. # Use eval to preserve embedded quoted strings.
  118. testargs=()
  119. eval "testargs=(${KUBE_TEST_ARGS:-})"
  120. # Used to filter verbose test output.
  121. go_test_grep_pattern=".*"
  122. # The junit report tool needs full test case information to produce a
  123. # meaningful report.
  124. if [[ -n "${KUBE_JUNIT_REPORT_DIR}" ]] ; then
  125. goflags+=(-v)
  126. goflags+=(-json)
  127. # Show only summary lines by matching lines like "status package/test"
  128. go_test_grep_pattern="^[^[:space:]]\+[[:space:]]\+[^[:space:]]\+/[^[[:space:]]\+"
  129. fi
  130. if [[ -n "${FULL_LOG:-}" ]] ; then
  131. go_test_grep_pattern=".*"
  132. fi
  133. # Filter out arguments that start with "-" and move them to goflags.
  134. testcases=()
  135. for arg; do
  136. if [[ "${arg}" == -* ]]; then
  137. goflags+=("${arg}")
  138. else
  139. testcases+=("${arg}")
  140. fi
  141. done
  142. if [[ ${#testcases[@]} -eq 0 ]]; then
  143. while IFS='' read -r line; do testcases+=("$line"); done < <(kube::test::find_dirs)
  144. fi
  145. set -- "${testcases[@]+${testcases[@]}}"
  146. if [[ -n "${KUBE_RACE}" ]] ; then
  147. goflags+=("${KUBE_RACE}")
  148. fi
  149. junitFilenamePrefix() {
  150. if [[ -z "${KUBE_JUNIT_REPORT_DIR}" ]]; then
  151. echo ""
  152. return
  153. fi
  154. mkdir -p "${KUBE_JUNIT_REPORT_DIR}"
  155. echo "${KUBE_JUNIT_REPORT_DIR}/junit_$(kube::util::sortable_date)"
  156. }
  157. verifyAndSuggestPackagePath() {
  158. local specified_package_path="$1"
  159. local alternative_package_path="$2"
  160. local original_package_path="$3"
  161. local suggestion_package_path="$4"
  162. if [[ "${specified_package_path}" =~ '/...'$ ]]; then
  163. specified_package_path=${specified_package_path::-4}
  164. fi
  165. if ! [ -d "${specified_package_path}" ]; then
  166. # Because k8s sets a localized $GOPATH for testing, seeing the actual
  167. # directory can be confusing. Instead, just show $GOPATH if it exists in the
  168. # $specified_package_path.
  169. local printable_package_path
  170. printable_package_path=${specified_package_path//${GOPATH}/\$\{GOPATH\}}
  171. kube::log::error "specified test path '${printable_package_path}' does not exist"
  172. if [ -d "${alternative_package_path}" ]; then
  173. kube::log::info "try changing \"${original_package_path}\" to \"${suggestion_package_path}\""
  174. fi
  175. exit 1
  176. fi
  177. }
  178. verifyPathsToPackagesUnderTest() {
  179. local packages_under_test=("$@")
  180. for package_path in "${packages_under_test[@]}"; do
  181. local local_package_path="${package_path}"
  182. local go_package_path="${GOPATH}/src/${package_path}"
  183. if [[ "${package_path:0:2}" == "./" ]] ; then
  184. verifyAndSuggestPackagePath "${local_package_path}" "${go_package_path}" "${package_path}" "${package_path:2}"
  185. else
  186. verifyAndSuggestPackagePath "${go_package_path}" "${local_package_path}" "${package_path}" "./${package_path}"
  187. fi
  188. done
  189. }
  190. produceJUnitXMLReport() {
  191. local -r junit_filename_prefix=$1
  192. if [[ -z "${junit_filename_prefix}" ]]; then
  193. return
  194. fi
  195. local junit_xml_filename
  196. junit_xml_filename="${junit_filename_prefix}.xml"
  197. if ! command -v gotestsum >/dev/null 2>&1; then
  198. kube::log::error "gotestsum not found; please install with " \
  199. "GO111MODULE=off go install k8s.io/kubernetes/vendor/gotest.tools/gotestsum"
  200. return
  201. fi
  202. gotestsum --junitfile "${junit_xml_filename}" --raw-command cat "${junit_filename_prefix}"*.stdout
  203. if [[ ! ${KUBE_KEEP_VERBOSE_TEST_OUTPUT} =~ ^[yY]$ ]]; then
  204. rm "${junit_filename_prefix}"*.stdout
  205. fi
  206. kube::log::status "Saved JUnit XML test report to ${junit_xml_filename}"
  207. }
  208. runTests() {
  209. local junit_filename_prefix
  210. junit_filename_prefix=$(junitFilenamePrefix)
  211. verifyPathsToPackagesUnderTest "$@"
  212. # If we're not collecting coverage, run all requested tests with one 'go test'
  213. # command, which is much faster.
  214. if [[ ! ${KUBE_COVER} =~ ^[yY]$ ]]; then
  215. kube::log::status "Running tests without code coverage"
  216. go test "${goflags[@]:+${goflags[@]}}" \
  217. "${KUBE_TIMEOUT}" "${@}" \
  218. "${testargs[@]:+${testargs[@]}}" \
  219. | tee ${junit_filename_prefix:+"${junit_filename_prefix}.stdout"} \
  220. | grep --binary-files=text "${go_test_grep_pattern}" && rc=$? || rc=$?
  221. produceJUnitXMLReport "${junit_filename_prefix}"
  222. return ${rc}
  223. fi
  224. # Create coverage report directories.
  225. if [[ -z "${KUBE_COVER_REPORT_DIR}" ]]; then
  226. cover_report_dir="/tmp/k8s_coverage/$(kube::util::sortable_date)"
  227. else
  228. cover_report_dir="${KUBE_COVER_REPORT_DIR}"
  229. fi
  230. cover_profile="coverage.out" # Name for each individual coverage profile
  231. kube::log::status "Saving coverage output in '${cover_report_dir}'"
  232. mkdir -p "${@+${@/#/${cover_report_dir}/}}"
  233. # Run all specified tests, collecting coverage results. Go currently doesn't
  234. # support collecting coverage across multiple packages at once, so we must issue
  235. # separate 'go test' commands for each package and then combine at the end.
  236. # To speed things up considerably, we can at least use xargs -P to run multiple
  237. # 'go test' commands at once.
  238. # To properly parse the test results if generating a JUnit test report, we
  239. # must make sure the output from PARALLEL runs is not mixed. To achieve this,
  240. # we spawn a subshell for each PARALLEL process, redirecting the output to
  241. # separate files.
  242. # ignore paths:
  243. # vendor/k8s.io/code-generator/cmd/generator: is fragile when run under coverage, so ignore it for now.
  244. # https://github.com/kubernetes/kubernetes/issues/24967
  245. # vendor/k8s.io/client-go/1.4/rest: causes cover internal errors
  246. # https://github.com/golang/go/issues/16540
  247. cover_ignore_dirs="vendor/k8s.io/code-generator/cmd/generator|vendor/k8s.io/client-go/1.4/rest"
  248. for path in ${cover_ignore_dirs//|/ }; do
  249. echo -e "skipped\tk8s.io/kubernetes/${path}"
  250. done
  251. printf "%s\n" "${@}" \
  252. | grep -Ev ${cover_ignore_dirs} \
  253. | xargs -I{} -n 1 -P "${KUBE_COVERPROCS}" \
  254. bash -c "set -o pipefail; _pkg=\"\$0\"; _pkg_out=\${_pkg//\//_}; \
  255. go test ${goflags[*]:+${goflags[*]}} \
  256. ${KUBE_TIMEOUT} \
  257. -cover -covermode=\"${KUBE_COVERMODE}\" \
  258. -coverprofile=\"${cover_report_dir}/\${_pkg}/${cover_profile}\" \
  259. \"\${_pkg}\" \
  260. ${testargs[*]:+${testargs[*]}} \
  261. | tee ${junit_filename_prefix:+\"${junit_filename_prefix}-\$_pkg_out.stdout\"} \
  262. | grep \"${go_test_grep_pattern}\"" \
  263. {} \
  264. && test_result=$? || test_result=$?
  265. produceJUnitXMLReport "${junit_filename_prefix}"
  266. COMBINED_COVER_PROFILE="${cover_report_dir}/combined-coverage.out"
  267. {
  268. # The combined coverage profile needs to start with a line indicating which
  269. # coverage mode was used (set, count, or atomic). This line is included in
  270. # each of the coverage profiles generated when running 'go test -cover', but
  271. # we strip these lines out when combining so that there's only one.
  272. echo "mode: ${KUBE_COVERMODE}"
  273. # Include all coverage reach data in the combined profile, but exclude the
  274. # 'mode' lines, as there should be only one.
  275. while IFS='' read -r x; do
  276. grep -h -v "^mode:" < "${x}" || true
  277. done < <(find "${cover_report_dir}" -name "${cover_profile}")
  278. } >"${COMBINED_COVER_PROFILE}"
  279. coverage_html_file="${cover_report_dir}/combined-coverage.html"
  280. go tool cover -html="${COMBINED_COVER_PROFILE}" -o="${coverage_html_file}"
  281. kube::log::status "Combined coverage report: ${coverage_html_file}"
  282. return ${test_result}
  283. }
  284. reportCoverageToCoveralls() {
  285. if [[ ${KUBE_COVER} =~ ^[yY]$ ]] && [[ -x "${KUBE_GOVERALLS_BIN}" ]]; then
  286. kube::log::status "Reporting coverage results to Coveralls for service ${CI_NAME:-}"
  287. ${KUBE_GOVERALLS_BIN} -coverprofile="${COMBINED_COVER_PROFILE}" \
  288. ${CI_NAME:+"-service=${CI_NAME}"} \
  289. ${COVERALLS_REPO_TOKEN:+"-repotoken=${COVERALLS_REPO_TOKEN}"} \
  290. || true
  291. fi
  292. }
  293. checkFDs() {
  294. # several unittests panic when httptest cannot open more sockets
  295. # due to the low default files limit on OS X. Warn about low limit.
  296. local fileslimit
  297. fileslimit="$(ulimit -n)"
  298. if [[ ${fileslimit} -lt 1000 ]]; then
  299. echo "WARNING: ulimit -n (files) should be at least 1000, is ${fileslimit}, may cause test failure";
  300. fi
  301. }
  302. checkFDs
  303. runTests "$@"
  304. # We might run the tests for multiple versions, but we want to report only
  305. # one of them to coveralls. Here we report coverage from the last run.
  306. reportCoverageToCoveralls