test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/usr/bin/env bash
  2. #
  3. # Run all etcd tests
  4. # ./test
  5. # ./test -v
  6. #
  7. # Run tests for one package
  8. #
  9. # PKG=./wal ./test
  10. # PKG=snap ./test
  11. #
  12. # Run code coverage
  13. # COVERDIR must either be a absolute path or a relative path to the etcd root
  14. # COVERDIR=coverage PASSES="build_cov cov" ./test
  15. set -e
  16. source ./build
  17. # build before setting up test GOPATH
  18. if [[ "${PASSES}" == *"functional"* ]]; then
  19. ./tools/functional-tester/build
  20. fi
  21. # build tests with vendored dependencies
  22. etcd_setup_gopath
  23. if [ -z "$PASSES" ]; then
  24. PASSES="fmt bom dep compile build unit"
  25. fi
  26. USERPKG=${PKG:-}
  27. # Invoke ./cover for HTML output
  28. COVER=${COVER:-"-cover"}
  29. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  30. IGNORE_PKGS="(cmd/|etcdserverpb|rafttest|gopath.proto|v3lockpb|v3electionpb)"
  31. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  32. # all github.com/coreos/etcd/whatever pkgs that are not auto-generated / tools
  33. PKGS=$(find . -name \*.go | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | grep -vE "(tools/|contrib/|e2e|pb)" | sed "s|\.|${REPO_PATH}|g" | xargs echo)
  34. # pkg1,pkg2,pkg3
  35. PKGS_COMMA=${PKGS// /,}
  36. TEST_PKGS=$(find . -name \*_test.go | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | sed "s|\./||g")
  37. FORMATTABLE=$(find . -name \*.go | while read -r a; do echo "$(dirname "$a")/*.go"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | sed "s|\./||g")
  38. TESTABLE_AND_FORMATTABLE=$(echo "$TEST_PKGS" | grep -vE "$INTEGRATION_PKGS")
  39. # check if user provided PKG override
  40. if [ -z "${USERPKG}" ]; then
  41. TEST=$TESTABLE_AND_FORMATTABLE
  42. FMT=$FORMATTABLE
  43. else
  44. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  45. TEST=${USERPKG/#./}
  46. TEST=${TEST/#\//}
  47. TEST=${TEST/%\//}
  48. # only run gofmt on packages provided by user
  49. FMT="$TEST"
  50. fi
  51. FMT=($FMT)
  52. # prepend REPO_PATH to each local package
  53. split=$TEST
  54. TEST=""
  55. for a in $split; do TEST="$TEST ${REPO_PATH}/${a}"; done
  56. TEST=($TEST)
  57. # TODO: 'client' pkg fails with gosimple from generated files
  58. # TODO: 'rafttest' is failing with unused
  59. STATIC_ANALYSIS_PATHS=$(find . -name \*.go | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | grep -v 'client')
  60. STATIC_ANALYSIS_PATHS=($STATIC_ANALYSIS_PATHS)
  61. if [ -z "$GOARCH" ]; then
  62. GOARCH=$(go env GOARCH);
  63. fi
  64. # determine whether target supports race detection
  65. if [ "$GOARCH" == "amd64" ]; then
  66. RACE="--race"
  67. fi
  68. function unit_pass {
  69. echo "Running unit tests..."
  70. GO_TEST_FLAG=""
  71. if [ "${VERBOSE}" == "1" ]; then
  72. GO_TEST_FLAG="-v"
  73. fi
  74. if [ "${VERBOSE}" == "2" ]; then
  75. GO_TEST_FLAG="-v"
  76. export CLIENT_DEBUG=1
  77. fi
  78. # only -run=Test so examples can run in integration tests
  79. go test ${GO_TEST_FLAG} -timeout 5m "${COVER}" ${RACE} -cpu 1,2,4 -run=Test "$@" "${TEST[@]}"
  80. }
  81. function integration_pass {
  82. echo "Running integration tests..."
  83. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  84. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  85. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  86. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  87. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  88. }
  89. function functional_pass {
  90. # Clean up any data and logs from previous runs
  91. rm -rf ./agent-*
  92. for a in 1 2 3; do
  93. mkdir -p ./agent-$a
  94. ./bin/etcd-agent -etcd-path ./bin/etcd -etcd-log-dir "./agent-$a" -port ":${a}9027" -use-root=false &
  95. pid="$!"
  96. agent_pids="${agent_pids} $pid"
  97. done
  98. for a in 1 2 3; do
  99. echo "Waiting for 'etcd-agent' on ${a}9027..."
  100. while ! nc -z localhost ${a}9027; do
  101. sleep 1
  102. done
  103. done
  104. echo "Starting 'etcd-tester'"
  105. ./bin/etcd-tester \
  106. -agent-endpoints "127.0.0.1:19027,127.0.0.1:29027,127.0.0.1:39027" \
  107. -client-ports 12379,22379,32379 \
  108. -peer-ports 12380,22380,32380 \
  109. -limit 1 \
  110. -schedule-cases "0 1 2 3 4 5" \
  111. -exit-on-failure && echo "'etcd-tester' succeeded"
  112. ETCD_TESTER_EXIT_CODE=$?
  113. echo "ETCD_TESTER_EXIT_CODE:" ${ETCD_TESTER_EXIT_CODE}
  114. echo "Waiting for processes to exit"
  115. kill -s TERM ${agent_pids}
  116. for a in ${agent_pids}; do wait $a || true; done
  117. if [[ "${ETCD_TESTER_EXIT_CODE}" -ne "0" ]]; then
  118. echo "--- FAIL: exit code" ${ETCD_TESTER_EXIT_CODE}
  119. exit ${ETCD_TESTER_EXIT_CODE}
  120. fi
  121. }
  122. function e2e_pass {
  123. echo "Running e2e tests..."
  124. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  125. }
  126. function integration_extra {
  127. go test -timeout 15m -v ${RACE} -cpu 1,2,4 "$@" "${REPO_PATH}/client/integration"
  128. go test -timeout 20m -v ${RACE} -cpu 1,2,4 "$@" "${REPO_PATH}/clientv3/integration"
  129. }
  130. function integration_e2e_pass {
  131. echo "Running integration and e2e tests..."
  132. go test -timeout 15m -v -cpu 1,2,4 "$@" "${REPO_PATH}/e2e" &
  133. e2epid="$!"
  134. go test -timeout 15m -v -cpu 1,2,4 "$@" "${REPO_PATH}/integration" &
  135. intpid="$!"
  136. wait $e2epid
  137. wait $intpid
  138. integration_extra "$@"
  139. }
  140. function grpcproxy_pass {
  141. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/integration
  142. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  143. }
  144. function release_pass {
  145. rm -f ./bin/etcd-last-release
  146. # to grab latest patch release; bump this up for every minor release
  147. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.2.*" | head -1)
  148. if [ -n "$MANUAL_VER" ]; then
  149. # in case, we need to test against different version
  150. UPGRADE_VER=$MANUAL_VER
  151. fi
  152. if [[ -z ${UPGRADE_VER} ]]; then
  153. UPGRADE_VER="v3.2.0"
  154. echo "fallback to" ${UPGRADE_VER}
  155. fi
  156. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  157. echo "Downloading $file"
  158. set +e
  159. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  160. local result=$?
  161. set -e
  162. case $result in
  163. 0) ;;
  164. *) echo "--- FAIL:" ${result}
  165. exit $result
  166. ;;
  167. esac
  168. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  169. mkdir -p ./bin
  170. mv /tmp/etcd ./bin/etcd-last-release
  171. }
  172. function fmt_pass {
  173. toggle_failpoints disable
  174. echo "Checking gofmt..."
  175. fmtRes=$(gofmt -l -s -d $FMT)
  176. if [ -n "${fmtRes}" ]; then
  177. echo -e "gofmt checking failed:\n${fmtRes}"
  178. exit 255
  179. fi
  180. echo "Checking govet..."
  181. vetRes=$(go vet $TEST)
  182. if [ -n "${vetRes}" ]; then
  183. echo -e "govet checking failed:\n${vetRes}"
  184. exit 255
  185. fi
  186. echo "Checking documentation style..."
  187. # eschew you
  188. yous=`find . -name \*.md -exec egrep --color "[Yy]ou[r]?[ '.,;]" {} + | grep -v /v2/ || true`
  189. if [ ! -z "$yous" ]; then
  190. echo -e "found 'you' in documentation:\n${yous}"
  191. exit 255
  192. fi
  193. # TODO: check other markdown files when marker handles headers with '[]'
  194. if which marker >/dev/null; then
  195. echo "Checking marker to find broken links..."
  196. markerResult=`marker --skip-http --root ./Documentation 2>&1 || true`
  197. if [ -n "${markerResult}" ]; then
  198. echo -e "marker checking failed:\n${markerResult}"
  199. exit 255
  200. fi
  201. else
  202. echo "Skipping marker..."
  203. fi
  204. if which gosimple >/dev/null; then
  205. echo "Checking gosimple..."
  206. gosimpleResult=`gosimple ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  207. if [ -n "${gosimpleResult}" ]; then
  208. # TODO: resolve these after go1.8 migration
  209. SIMPLE_CHECK_MASK="S(1024)"
  210. if echo "${gosimpleResult}" | egrep -v "$SIMPLE_CHECK_MASK"; then
  211. echo -e "gosimple checking failed:\n${gosimpleResult}"
  212. exit 255
  213. else
  214. echo -e "gosimple warning:\n${gosimpleResult}"
  215. fi
  216. fi
  217. else
  218. echo "Skipping gosimple..."
  219. fi
  220. if which unused >/dev/null; then
  221. echo "Checking unused..."
  222. unusedResult=`unused ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  223. if [ -n "${unusedResult}" ]; then
  224. echo -e "unused checking failed:\n${unusedResult}"
  225. exit 255
  226. fi
  227. else
  228. echo "Skipping unused..."
  229. fi
  230. if which staticcheck >/dev/null; then
  231. echo "Checking staticcheck..."
  232. staticcheckResult=`staticcheck ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  233. if [ -n "${staticcheckResult}" ]; then
  234. # TODO: resolve these after go1.8 migration
  235. # See https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck
  236. STATIC_CHECK_MASK="SA(1019|2002)"
  237. if echo "${staticcheckResult}" | egrep -v "$STATIC_CHECK_MASK"; then
  238. echo -e "staticcheck checking failed:\n${staticcheckResult}"
  239. exit 255
  240. else
  241. suppressed=`echo "${staticcheckResult}" | sed 's/ /\n/g' | grep "(SA" | sort | uniq -c`
  242. echo -e "staticcheck suppressed warnings:\n${suppressed}"
  243. fi
  244. fi
  245. else
  246. echo "Skipping staticcheck..."
  247. fi
  248. echo "Checking for license header..."
  249. licRes=""
  250. files=$(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*')
  251. for file in $files; do
  252. if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
  253. licRes="${licRes}"$(echo -e " ${file}")
  254. fi
  255. done
  256. if [ -n "${licRes}" ]; then
  257. echo -e "license header checking failed:\n${licRes}"
  258. exit 255
  259. fi
  260. echo "Checking commit titles..."
  261. git log --oneline "$(git merge-base HEAD master)"...HEAD | while read l; do
  262. commitMsg=`echo "$l" | cut -f2- -d' '`
  263. if [[ "$commitMsg" == Merge* ]]; then
  264. # ignore "Merge pull" commits
  265. continue
  266. fi
  267. if [[ "$commitMsg" == Revert* ]]; then
  268. # ignore revert commits
  269. continue
  270. fi
  271. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  272. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  273. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  274. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  275. echo "$l"...
  276. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  277. echo "Got: $l"
  278. exit 255
  279. fi
  280. done
  281. }
  282. function bom_pass {
  283. if ! which license-bill-of-materials >/dev/null; then
  284. return
  285. fi
  286. echo "Checking bill of materials..."
  287. license-bill-of-materials \
  288. --override-file bill-of-materials.override.json \
  289. github.com/coreos/etcd github.com/coreos/etcd/etcdctl >bom-now.json || true
  290. if ! diff bill-of-materials.json bom-now.json; then
  291. echo "vendored licenses do not match given bill of materials"
  292. exit 255
  293. fi
  294. rm bom-now.json
  295. }
  296. function dep_pass {
  297. echo "Checking package dependencies..."
  298. # don't pull in etcdserver package
  299. pushd clientv3 >/dev/null
  300. badpkg="(etcdserver$|mvcc$|backend$|grpc-gateway)"
  301. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" || echo ""`
  302. popd >/dev/null
  303. if [ ! -z "$deps" ]; then
  304. echo -e "clientv3 has masked dependencies:\n${deps}"
  305. exit 255
  306. fi
  307. }
  308. function build_cov_pass {
  309. out="bin"
  310. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  311. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcd_test
  312. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcdctl_test ${REPO_PATH}/etcdctl
  313. }
  314. function compile_pass {
  315. echo "Checking build..."
  316. go build -v ./tools/...
  317. }
  318. # fail fast on static tests
  319. function build_pass {
  320. GO_BUILD_FLAGS="-a -v" etcd_build
  321. }
  322. for pass in $PASSES; do
  323. ${pass}_pass $@
  324. done
  325. echo "Success"