test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 tests with vendored dependencies
  18. etcd_setup_gopath
  19. if [ -z "$PASSES" ]; then
  20. PASSES="fmt bom dep compile build unit"
  21. fi
  22. USERPKG=${PKG:-}
  23. # Invoke ./cover for HTML output
  24. COVER=${COVER:-"-cover"}
  25. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  26. IGNORE_PKGS="(cmd/|etcdserverpb|rafttest|gopath.proto|v3lockpb|v3electionpb)"
  27. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  28. # all github.com/coreos/etcd/whatever pkgs that are not auto-generated / tools
  29. PKGS=`find . -name \*.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | egrep -v "(tools/|contrib/|e2e|pb)" | sed "s|\.|${REPO_PATH}|g" | xargs echo`
  30. # pkg1,pkg2,pkg3
  31. PKGS_COMMA=${PKGS// /,}
  32. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  33. FORMATTABLE=`find . -name \*.go | while read a; do echo "$(dirname $a)/*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  34. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  35. # check if user provided PKG override
  36. if [ -z "${USERPKG}" ]; then
  37. TEST=$TESTABLE_AND_FORMATTABLE
  38. FMT=$FORMATTABLE
  39. else
  40. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  41. TEST=${USERPKG/#./}
  42. TEST=${TEST/#\//}
  43. TEST=${TEST/%\//}
  44. # only run gofmt on packages provided by user
  45. FMT="$TEST"
  46. fi
  47. # prepend REPO_PATH to each local package
  48. split=$TEST
  49. TEST=""
  50. for a in $split; do TEST="$TEST ${REPO_PATH}/${a}"; done
  51. # TODO: 'client' pkg fails with gosimple from generated files
  52. # TODO: 'rafttest' is failing with unused
  53. STATIC_ANALYSIS_PATHS=`find . -name \*.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | grep -v 'client'`
  54. if [ -z "$GOARCH" ]; then
  55. GOARCH=$(go env GOARCH);
  56. fi
  57. # determine whether target supports race detection
  58. if [ "$GOARCH" == "amd64" ]; then
  59. RACE="--race"
  60. fi
  61. function unit_pass {
  62. echo "Running unit tests..."
  63. # only -run=Test so examples can run in integration tests
  64. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
  65. }
  66. function integration_pass {
  67. echo "Running integration tests..."
  68. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  69. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  70. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  71. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  72. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  73. }
  74. function cov_pass {
  75. echo "Running code coverage..."
  76. # install gocovmerge before running code coverage from github.com/wadey/gocovmerge
  77. # gocovmerge merges coverage files
  78. if ! which gocovmerge >/dev/null; then
  79. echo "gocovmerge not installed"
  80. exit 255
  81. fi
  82. if [ -z "$COVERDIR" ]; then
  83. echo "COVERDIR undeclared"
  84. exit 255
  85. fi
  86. if [ ! -f "bin/etcd_test" ]; then
  87. echo "etcd_test binary not found"
  88. exit 255
  89. fi
  90. mkdir -p "$COVERDIR"
  91. # run code coverage for unit and integration tests
  92. GOCOVFLAGS="-covermode=set -coverpkg $PKGS_COMMA -v -timeout 15m"
  93. failed=""
  94. for t in `echo "${TEST_PKGS}" | egrep -v "(e2e|functional-tester)"`; do
  95. tf=`echo $t | tr / _`
  96. # cache package compilation data for faster repeated builds
  97. go test $GOCOVFLAGS -i ${REPO_PATH}/$t || true
  98. # uses -run=Test to skip examples because clientv3/ example tests will leak goroutines
  99. go test $GOCOVFLAGS -run=Test -coverprofile "$COVERDIR/${tf}.coverprofile" ${REPO_PATH}/$t || failed="$failed $t"
  100. done
  101. # proxy tests
  102. go test -tags cluster_proxy $GOCOVFLAGS -coverprofile "$COVERDIR/proxy_integration.coverprofile" ${REPO_PATH}/integration || failed="$failed proxy-integration"
  103. go test -tags cluster_proxy $GOCOVFLAGS -coverprofile "$COVERDIR/proxy_clientv3.coverprofile" ${REPO_PATH}/clientv3/integration || failed="$failed proxy-clientv3/integration"
  104. # run code coverage for e2e tests
  105. # use 30m timeout because e2e coverage takes longer
  106. # due to many tests cause etcd process to wait
  107. # on leadership transfer timeout during gracefully shutdown
  108. go test -tags cov -timeout 30m -v ${REPO_PATH}"/e2e" || failed="$failed e2e"
  109. # incrementally merge to get coverage data even if some coverage files are corrupted
  110. # optimistically assume etcdserver package's coverage file is OK since gocovmerge
  111. # expects to start with a non-empty file
  112. cp "$COVERDIR"/etcdserver.coverprofile "$COVERDIR"/cover.out
  113. for f in "$COVERDIR"/*.coverprofile; do
  114. gocovmerge $f "$COVERDIR"/cover.out >"$COVERDIR"/cover.tmp || failed="$failed $f"
  115. if [ -s "$COVERDIR"/cover.tmp ]; then
  116. mv "$COVERDIR"/cover.tmp "$COVERDIR"/cover.out
  117. fi
  118. done
  119. # strip out generated files (using GNU-style sed)
  120. sed --in-place '/generated.go/d' "$COVERDIR"/cover.out || true
  121. # held failures to generate the full coverage file, now fail
  122. if [ -n "$failed" ]; then
  123. for f in $failed; do
  124. echo FAIL $f
  125. done
  126. exit 255
  127. fi
  128. }
  129. function e2e_pass {
  130. echo "Running e2e tests..."
  131. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  132. }
  133. function integration_e2e_pass {
  134. echo "Running integration and e2e tests..."
  135. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  136. e2epid="$!"
  137. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  138. intpid="$!"
  139. wait $e2epid
  140. wait $intpid
  141. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  142. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  143. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  144. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  145. }
  146. function grpcproxy_pass {
  147. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/integration
  148. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  149. }
  150. function release_pass {
  151. rm -f ./bin/etcd-last-release
  152. # to grab latest patch release; bump this up for every minor release
  153. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.2.*" | head -1)
  154. if [ -n "$MANUAL_VER" ]; then
  155. # in case, we need to test against different version
  156. UPGRADE_VER=$MANUAL_VER
  157. fi
  158. if [[ -z ${UPGRADE_VER} ]]; then
  159. UPGRADE_VER="v3.2.0"
  160. echo "fallback to" ${UPGRADE_VER}
  161. fi
  162. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  163. echo "Downloading $file"
  164. set +e
  165. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  166. local result=$?
  167. set -e
  168. case $result in
  169. 0) ;;
  170. *) echo "FAIL with" ${result}
  171. exit $result
  172. ;;
  173. esac
  174. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  175. mkdir -p ./bin
  176. mv /tmp/etcd ./bin/etcd-last-release
  177. }
  178. function fmt_pass {
  179. toggle_failpoints disable
  180. echo "Checking gofmt..."
  181. fmtRes=$(gofmt -l -s -d $FMT)
  182. if [ -n "${fmtRes}" ]; then
  183. echo -e "gofmt checking failed:\n${fmtRes}"
  184. exit 255
  185. fi
  186. echo "Checking govet..."
  187. vetRes=$(go vet $TEST)
  188. if [ -n "${vetRes}" ]; then
  189. echo -e "govet checking failed:\n${vetRes}"
  190. exit 255
  191. fi
  192. echo "Checking 'go tool vet -all -shadow'..."
  193. fmtpkgs=$(echo $FMT | xargs dirname | sort | uniq | sed '/\./d')
  194. vetRes=$(go tool vet -all -shadow ${fmtpkgs} 2>&1 | grep -v '/gw/' || true)
  195. if [ -n "${vetRes}" ]; then
  196. echo -e "govet -all -shadow checking failed:\n${vetRes}"
  197. exit 255
  198. fi
  199. if which shellcheck >/dev/null; then
  200. echo "Checking shellcheck..."
  201. shellcheckResult=$(shellcheck -fgcc build test scripts/* 2>&1 || true)
  202. if [ -n "${shellcheckResult}" ]; then
  203. # mask the most common ones; fix later
  204. SHELLCHECK_MASK="SC(2086|2006|2068|2196|2035|2162|2076)"
  205. errs=$(echo "${shellcheckResult}" | egrep -v "${SHELLCHECK_MASK}" || true)
  206. if [ -n "${errs}" ]; then
  207. echo -e "shellcheck checking failed:\n${shellcheckResult}\n===\nFailed:\n${errs}"
  208. exit 255
  209. fi
  210. suppressed=$(echo "${shellcheckResult}" | cut -f4- -d':' | sort | uniq -c | sort -n)
  211. echo -e "shellcheck suppressed warnings:\n${suppressed}"
  212. fi
  213. fi
  214. echo "Checking documentation style..."
  215. # eschew you
  216. yous=`find . -name \*.md -exec egrep --color "[Yy]ou[r]?[ '.,;]" {} + | grep -v /v2/ || true`
  217. if [ ! -z "$yous" ]; then
  218. echo -e "found 'you' in documentation:\n${yous}"
  219. exit 255
  220. fi
  221. # TODO: check other markdown files when marker handles headers with '[]'
  222. if which marker >/dev/null; then
  223. echo "Checking marker to find broken links..."
  224. markerResult=`marker --skip-http --root ./Documentation 2>&1 || true`
  225. if [ -n "${markerResult}" ]; then
  226. echo -e "marker checking failed:\n${markerResult}"
  227. exit 255
  228. fi
  229. else
  230. echo "Skipping marker..."
  231. fi
  232. if which goword >/dev/null; then
  233. echo "Checking goword..."
  234. # get all go files to process
  235. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  236. # ignore tests and protobuf files
  237. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  238. # only check for broken exported godocs
  239. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  240. if [ ! -z "$gowordRes" ]; then
  241. echo -e "goword checking failed:\n${gowordRes}"
  242. exit 255
  243. fi
  244. else
  245. echo "Skipping goword..."
  246. fi
  247. if which gosimple >/dev/null; then
  248. echo "Checking gosimple..."
  249. gosimpleResult=`gosimple ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  250. if [ -n "${gosimpleResult}" ]; then
  251. # TODO: resolve these after go1.8 migration
  252. SIMPLE_CHECK_MASK="S(1024)"
  253. if echo "${gosimpleResult}" | egrep -v "$SIMPLE_CHECK_MASK"; then
  254. echo -e "gosimple checking failed:\n${gosimpleResult}"
  255. exit 255
  256. else
  257. echo -e "gosimple warning:\n${gosimpleResult}"
  258. fi
  259. fi
  260. else
  261. echo "Skipping gosimple..."
  262. fi
  263. if which unused >/dev/null; then
  264. echo "Checking unused..."
  265. unusedResult=`unused ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  266. if [ -n "${unusedResult}" ]; then
  267. echo -e "unused checking failed:\n${unusedResult}"
  268. exit 255
  269. fi
  270. else
  271. echo "Skipping unused..."
  272. fi
  273. if which staticcheck >/dev/null; then
  274. echo "Checking staticcheck..."
  275. staticcheckResult=`staticcheck ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  276. if [ -n "${staticcheckResult}" ]; then
  277. # TODO: resolve these after go1.8 migration
  278. # See https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck
  279. STATIC_CHECK_MASK="SA(1019|2002)"
  280. if echo "${staticcheckResult}" | egrep -v "$STATIC_CHECK_MASK"; then
  281. echo -e "staticcheck checking failed:\n${staticcheckResult}"
  282. exit 255
  283. else
  284. suppressed=`echo "${staticcheckResult}" | sed 's/ /\n/g' | grep "(SA" | sort | uniq -c`
  285. echo -e "staticcheck suppressed warnings:\n${suppressed}"
  286. fi
  287. fi
  288. else
  289. echo "Skipping staticcheck..."
  290. fi
  291. echo "Checking for license header..."
  292. licRes=""
  293. files=$(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*')
  294. for file in $files; do
  295. if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
  296. licRes="${licRes}"$(echo -e " ${file}")
  297. fi
  298. done
  299. if [ -n "${licRes}" ]; then
  300. echo -e "license header checking failed:\n${licRes}"
  301. exit 255
  302. fi
  303. echo "Checking commit titles..."
  304. git log --oneline "$(git merge-base HEAD master)"...HEAD | while read l; do
  305. commitMsg=`echo "$l" | cut -f2- -d' '`
  306. if [[ "$commitMsg" == Merge* ]]; then
  307. # ignore "Merge pull" commits
  308. continue
  309. fi
  310. if [[ "$commitMsg" == Revert* ]]; then
  311. # ignore revert commits
  312. continue
  313. fi
  314. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  315. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  316. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  317. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  318. echo "$l"...
  319. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  320. echo "Got: $l"
  321. exit 255
  322. fi
  323. done
  324. }
  325. function bom_pass {
  326. if ! which license-bill-of-materials >/dev/null; then
  327. return
  328. fi
  329. echo "Checking bill of materials..."
  330. license-bill-of-materials \
  331. --override-file bill-of-materials.override.json \
  332. github.com/coreos/etcd github.com/coreos/etcd/etcdctl >bom-now.json || true
  333. if ! diff bill-of-materials.json bom-now.json; then
  334. echo "vendored licenses do not match given bill of materials"
  335. exit 255
  336. fi
  337. rm bom-now.json
  338. }
  339. function dep_pass {
  340. echo "Checking package dependencies..."
  341. # don't pull in etcdserver package
  342. pushd clientv3 >/dev/null
  343. badpkg="(etcdserver$|mvcc$|backend$|grpc-gateway)"
  344. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" || echo ""`
  345. popd >/dev/null
  346. if [ ! -z "$deps" ]; then
  347. echo -e "clientv3 has masked dependencies:\n${deps}"
  348. exit 255
  349. fi
  350. }
  351. function build_cov_pass {
  352. out="bin"
  353. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  354. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcd_test
  355. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcdctl_test ${REPO_PATH}/etcdctl
  356. }
  357. function compile_pass {
  358. echo "Checking build..."
  359. go build -v ./tools/...
  360. }
  361. # fail fast on static tests
  362. function build_pass {
  363. GO_BUILD_FLAGS="-a -v" etcd_build
  364. }
  365. for pass in $PASSES; do
  366. ${pass}_pass $@
  367. done
  368. echo "Success"