test 13 KB

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