test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  159. echo "Downloading $file"
  160. set +e
  161. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  162. local result=$?
  163. set -e
  164. case $result in
  165. 0) ;;
  166. 22) return 0
  167. ;;
  168. *) exit $result
  169. ;;
  170. esac
  171. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  172. mkdir -p ./bin
  173. mv /tmp/etcd ./bin/etcd-last-release
  174. }
  175. function fmt_pass {
  176. toggle_failpoints disable
  177. echo "Checking gofmt..."
  178. fmtRes=$(gofmt -l -s -d $FMT)
  179. if [ -n "${fmtRes}" ]; then
  180. echo -e "gofmt checking failed:\n${fmtRes}"
  181. exit 255
  182. fi
  183. echo "Checking govet..."
  184. vetRes=$(go vet $TEST)
  185. if [ -n "${vetRes}" ]; then
  186. echo -e "govet checking failed:\n${vetRes}"
  187. exit 255
  188. fi
  189. echo "Checking 'go tool vet -all -shadow'..."
  190. fmtpkgs=$(echo $FMT | xargs dirname | sort | uniq | sed '/\./d')
  191. vetRes=$(go tool vet -all -shadow ${fmtpkgs} 2>&1 | grep -v '/gw/' || true)
  192. if [ -n "${vetRes}" ]; then
  193. echo -e "govet -all -shadow checking failed:\n${vetRes}"
  194. exit 255
  195. fi
  196. if which shellcheck >/dev/null; then
  197. echo "Checking shellcheck..."
  198. shellcheckResult=$(shellcheck -fgcc build test scripts/* 2>&1 || true)
  199. if [ -n "${shellcheckResult}" ]; then
  200. # mask the most common ones; fix later
  201. SHELLCHECK_MASK="SC(2086|2006|2068|2196|2035|2162|2076)"
  202. errs=$(echo "${shellcheckResult}" | egrep -v "${SHELLCHECK_MASK}" || true)
  203. if [ -n "${errs}" ]; then
  204. echo -e "shellcheck checking failed:\n${shellcheckResult}\n===\nFailed:\n${errs}"
  205. exit 255
  206. fi
  207. suppressed=$(echo "${shellcheckResult}" | cut -f4- -d':' | sort | uniq -c | sort -n)
  208. echo -e "shellcheck suppressed warnings:\n${suppressed}"
  209. fi
  210. fi
  211. echo "Checking documentation style..."
  212. # eschew you
  213. yous=`find . -name \*.md -exec egrep --color "[Yy]ou[r]?[ '.,;]" {} + | grep -v /v2/ || true`
  214. if [ ! -z "$yous" ]; then
  215. echo -e "found 'you' in documentation:\n${yous}"
  216. exit 255
  217. fi
  218. # TODO: check other markdown files when marker handles headers with '[]'
  219. if which marker >/dev/null; then
  220. echo "Checking marker to find broken links..."
  221. markerResult=`marker --skip-http --root ./Documentation 2>&1 || true`
  222. if [ -n "${markerResult}" ]; then
  223. echo -e "marker checking failed:\n${markerResult}"
  224. exit 255
  225. fi
  226. else
  227. echo "Skipping marker..."
  228. fi
  229. if which goword >/dev/null; then
  230. echo "Checking goword..."
  231. # get all go files to process
  232. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  233. # ignore tests and protobuf files
  234. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  235. # only check for broken exported godocs
  236. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  237. if [ ! -z "$gowordRes" ]; then
  238. echo -e "goword checking failed:\n${gowordRes}"
  239. exit 255
  240. fi
  241. else
  242. echo "Skipping goword..."
  243. fi
  244. if which gosimple >/dev/null; then
  245. echo "Checking gosimple..."
  246. gosimpleResult=`gosimple ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  247. if [ -n "${gosimpleResult}" ]; then
  248. # TODO: resolve these after go1.8 migration
  249. SIMPLE_CHECK_MASK="S(1024)"
  250. if echo "${gosimpleResult}" | egrep -v "$SIMPLE_CHECK_MASK"; then
  251. echo -e "gosimple checking failed:\n${gosimpleResult}"
  252. exit 255
  253. else
  254. echo -e "gosimple warning:\n${gosimpleResult}"
  255. fi
  256. fi
  257. else
  258. echo "Skipping gosimple..."
  259. fi
  260. if which unused >/dev/null; then
  261. echo "Checking unused..."
  262. unusedResult=`unused ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  263. if [ -n "${unusedResult}" ]; then
  264. echo -e "unused checking failed:\n${unusedResult}"
  265. exit 255
  266. fi
  267. else
  268. echo "Skipping unused..."
  269. fi
  270. if which staticcheck >/dev/null; then
  271. echo "Checking staticcheck..."
  272. staticcheckResult=`staticcheck ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  273. if [ -n "${staticcheckResult}" ]; then
  274. # TODO: resolve these after go1.8 migration
  275. # See https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck
  276. STATIC_CHECK_MASK="SA(1019|2002)"
  277. if echo "${staticcheckResult}" | egrep -v "$STATIC_CHECK_MASK"; then
  278. echo -e "staticcheck checking failed:\n${staticcheckResult}"
  279. exit 255
  280. else
  281. suppressed=`echo "${staticcheckResult}" | sed 's/ /\n/g' | grep "(SA" | sort | uniq -c`
  282. echo -e "staticcheck suppressed warnings:\n${suppressed}"
  283. fi
  284. fi
  285. else
  286. echo "Skipping staticcheck..."
  287. fi
  288. echo "Checking for license header..."
  289. licRes=""
  290. files=$(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*')
  291. for file in $files; do
  292. if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
  293. licRes="${licRes}"$(echo -e " ${file}")
  294. fi
  295. done
  296. if [ -n "${licRes}" ]; then
  297. echo -e "license header checking failed:\n${licRes}"
  298. exit 255
  299. fi
  300. echo "Checking commit titles..."
  301. git log --oneline "$(git merge-base HEAD master)"...HEAD | while read l; do
  302. commitMsg=`echo "$l" | cut -f2- -d' '`
  303. if [[ "$commitMsg" == Merge* ]]; then
  304. # ignore "Merge pull" commits
  305. continue
  306. fi
  307. if [[ "$commitMsg" == Revert* ]]; then
  308. # ignore revert commits
  309. continue
  310. fi
  311. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  312. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  313. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  314. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  315. echo "$l"...
  316. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  317. echo "Got: $l"
  318. exit 255
  319. fi
  320. done
  321. }
  322. function bom_pass {
  323. if ! which license-bill-of-materials >/dev/null; then
  324. return
  325. fi
  326. echo "Checking bill of materials..."
  327. license-bill-of-materials \
  328. --override-file bill-of-materials.override.json \
  329. github.com/coreos/etcd github.com/coreos/etcd/etcdctl >bom-now.json || true
  330. if ! diff bill-of-materials.json bom-now.json; then
  331. echo "vendored licenses do not match given bill of materials"
  332. exit 255
  333. fi
  334. rm bom-now.json
  335. }
  336. function dep_pass {
  337. echo "Checking package dependencies..."
  338. # don't pull in etcdserver package
  339. pushd clientv3 >/dev/null
  340. badpkg="(etcdserver$|mvcc$|backend$|grpc-gateway)"
  341. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" || echo ""`
  342. popd >/dev/null
  343. if [ ! -z "$deps" ]; then
  344. echo -e "clientv3 has masked dependencies:\n${deps}"
  345. exit 255
  346. fi
  347. }
  348. function build_cov_pass {
  349. out="bin"
  350. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  351. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcd_test
  352. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcdctl_test ${REPO_PATH}/etcdctl
  353. }
  354. function compile_pass {
  355. echo "Checking build..."
  356. go build -v ./tools/...
  357. }
  358. # fail fast on static tests
  359. function build_pass {
  360. GO_BUILD_FLAGS="-a -v" etcd_build
  361. }
  362. for pass in $PASSES; do
  363. ${pass}_pass $@
  364. done
  365. echo "Success"