test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 dep compile build unit"
  21. fi
  22. # Invoke ./cover for HTML output
  23. COVER=${COVER:-"-cover"}
  24. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  25. IGNORE_PKGS="(cmd|vendor|etcdserverpb|rafttest|gopath.proto)"
  26. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  27. # all github.com/coreos/etcd/whatever pkgs that are not auto-generated / tools
  28. 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"`
  29. # pkg1,pkg2,pkg3
  30. PKGS_COMMA=`echo ${PKGS} | sed 's/ /,/g'`
  31. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  32. FORMATTABLE=`find . -name \*.go | while read a; do echo $(dirname $a)/"*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  33. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  34. # TODO: 'client' pkg fails with gosimple from generated files
  35. # TODO: 'rafttest' is failing with unused
  36. STATIC_ANALYSIS_PATHS=`find . -name \*.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | grep -v 'client'`
  37. if [ -z "$GOARCH" ]; then
  38. GOARCH=$(go env GOARCH);
  39. fi
  40. # user has not provided PKG override
  41. if [ -z "$PKG" ]; then
  42. TEST=$TESTABLE_AND_FORMATTABLE
  43. FMT=$FORMATTABLE
  44. # user has provided PKG override
  45. else
  46. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  47. TEST=${PKG/#./}
  48. TEST=${TEST/#\//}
  49. TEST=${TEST/%\//}
  50. # only run gofmt on packages provided by user
  51. FMT="$TEST"
  52. fi
  53. # split TEST into an array and prepend REPO_PATH to each local package
  54. split=(${TEST// / })
  55. TEST=${split[@]/#/${REPO_PATH}/}
  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. for t in `echo "${TEST_PKGS}" | egrep -v "(e2e|functional-tester)"`; do
  92. tf=`echo $t | tr / _`
  93. # cache package compilation data for faster repeated builds
  94. go test -covermode=set -coverpkg $PKGS_COMMA -i -v ${REPO_PATH}/$t
  95. # uses -run=Test to skip examples because clientv3/ example tests will leak goroutines
  96. go test -covermode=set -coverpkg $PKGS_COMMA -timeout 15m -run=Test -v -coverprofile "$COVERDIR/${tf}.coverprofile" ${REPO_PATH}/$t
  97. done
  98. # proxy tests
  99. go test -tags cluster_proxy -covermode=set -coverpkg $PKGS_COMMA -timeout 15m -v -coverprofile "$COVERDIR/proxy_integration.coverprofile" ${REPO_PATH}/integration
  100. go test -tags cluster_proxy -covermode=set -coverpkg $PKGS_COMMA -timeout 15m -v -coverprofile "$COVERDIR/proxy_clientv3.coverprofile" ${REPO_PATH}/clientv3/integration
  101. # run code coverage for e2e tests
  102. # use 30m timeout because e2e coverage takes longer
  103. # due to many tests cause etcd process to wait
  104. # on leadership transfer timeout during gracefully shutdown
  105. go test -tags cov -timeout 30m -v ${REPO_PATH}"/e2e"
  106. gocovmerge "$COVERDIR"/*.coverprofile >"$COVERDIR"/cover.out
  107. }
  108. function e2e_pass {
  109. echo "Running e2e tests..."
  110. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  111. }
  112. function integration_e2e_pass {
  113. echo "Running integration and e2e tests..."
  114. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  115. e2epid="$!"
  116. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  117. intpid="$!"
  118. wait $e2epid
  119. wait $intpid
  120. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  121. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  122. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  123. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  124. }
  125. function grpcproxy_pass {
  126. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/integration
  127. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  128. }
  129. function release_pass {
  130. rm -f ./bin/etcd-last-release
  131. # to grab latest patch release; bump this up for every minor release
  132. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.1.*" | head -1)
  133. if [ -n "$MANUAL_VER" ]; then
  134. # in case, we need to test against different version
  135. UPGRADE_VER=$MANUAL_VER
  136. fi
  137. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  138. echo "Downloading $file"
  139. set +e
  140. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  141. local result=$?
  142. set -e
  143. case $result in
  144. 0) ;;
  145. 22) return 0
  146. ;;
  147. *) exit $result
  148. ;;
  149. esac
  150. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  151. mkdir -p ./bin
  152. mv /tmp/etcd ./bin/etcd-last-release
  153. }
  154. function fmt_pass {
  155. toggle_failpoints disable
  156. echo "Checking gofmt..."
  157. fmtRes=$(gofmt -l -s -d $FMT)
  158. if [ -n "${fmtRes}" ]; then
  159. echo -e "gofmt checking failed:\n${fmtRes}"
  160. exit 255
  161. fi
  162. echo "Checking govet..."
  163. vetRes=$(go vet $TEST)
  164. if [ -n "${vetRes}" ]; then
  165. echo -e "govet checking failed:\n${vetRes}"
  166. exit 255
  167. fi
  168. echo "Checking 'go tool vet -shadow'..."
  169. for path in $FMT; do
  170. if [ "${path##*.}" != "go" ]; then
  171. path="${path}/*.go"
  172. fi
  173. vetRes=$(go tool vet -shadow ${path})
  174. if [ -n "${vetRes}" ]; then
  175. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  176. exit 255
  177. fi
  178. done
  179. echo "Checking documentation style..."
  180. # eschew you
  181. yous=`find . -name \*.md | xargs egrep --color "[Yy]ou[r]?[ '.,;]" | grep -v /v2/ || true`
  182. if [ ! -z "$yous" ]; then
  183. echo -e "found 'you' in documentation:\n${yous}"
  184. exit 255
  185. fi
  186. if which goword >/dev/null; then
  187. echo "Checking goword..."
  188. # get all go files to process
  189. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  190. # ignore tests and protobuf files
  191. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  192. # only check for broken exported godocs
  193. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  194. if [ ! -z "$gowordRes" ]; then
  195. echo -e "goword checking failed:\n${gowordRes}"
  196. exit 255
  197. fi
  198. else
  199. echo "Skipping goword..."
  200. fi
  201. if which gosimple >/dev/null; then
  202. echo "Checking gosimple..."
  203. gosimpleResult=`gosimple ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  204. if [ ! -n "${gosimpleResult}" ]; then
  205. continue
  206. fi
  207. # TODO: resolve these after go1.8 migration
  208. SIMPLE_CHECK_MASK="S(1024)"
  209. if egrep -v "$SIMPLE_CHECK_MASK" "${gosimpleResult}"; then
  210. echo -e "gosimple checking ${path} failed:\n${gosimpleResult}"
  211. exit 255
  212. else
  213. echo -e "gosimple warning:\n${gosimpleResult}"
  214. fi
  215. else
  216. echo "Skipping gosimple..."
  217. fi
  218. if which unused >/dev/null; then
  219. echo "Checking unused..."
  220. unusedResult=`unused ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  221. if [ -n "${unusedResult}" ]; then
  222. echo -e "unused checking failed:\n${unusedResult}"
  223. exit 255
  224. fi
  225. else
  226. echo "Skipping unused..."
  227. fi
  228. if which staticcheck >/dev/null; then
  229. echo "Checking staticcheck..."
  230. staticcheckResult=`staticcheck ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  231. if [ ! -n "${staticcheckResult}" ]; then
  232. continue
  233. fi
  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(1016|1019|2002)"
  237. if egrep -v "$STATIC_CHECK_MASK" "${staticcheckResult}"; then
  238. echo -e "staticcheck checking ${path} failed:\n${staticcheckResult}"
  239. exit 255
  240. else
  241. echo -e "staticcheck warning:\n${staticcheckResult}"
  242. fi
  243. else
  244. echo "Skipping staticcheck..."
  245. fi
  246. echo "Checking for license header..."
  247. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*'); do
  248. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  249. done;)
  250. if [ -n "${licRes}" ]; then
  251. echo -e "license header checking failed:\n${licRes}"
  252. exit 255
  253. fi
  254. echo "Checking commit titles..."
  255. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  256. commitMsg=`echo "$l" | cut -f2- -d' '`
  257. if [[ "$commitMsg" == Merge* ]]; then
  258. # ignore "Merge pull" commits
  259. continue
  260. fi
  261. if [[ "$commitMsg" == Revert* ]]; then
  262. # ignore revert commits
  263. continue
  264. fi
  265. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  266. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  267. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  268. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  269. echo "$l"...
  270. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  271. echo "Got: $l"
  272. exit 255
  273. fi
  274. done
  275. }
  276. function dep_pass {
  277. echo "Checking package dependencies..."
  278. # don't pull in etcdserver package
  279. pushd clientv3 >/dev/null
  280. badpkg="(etcdserver|mvcc)"
  281. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  282. popd >/dev/null
  283. if [ ! -z "$deps" ]; then
  284. echo -e "clientv3 has masked dependencies:\n${deps}"
  285. exit 255
  286. fi
  287. }
  288. function build_cov_pass {
  289. out="bin"
  290. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  291. go test -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcd_test
  292. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcdctl_test ${REPO_PATH}/etcdctl
  293. }
  294. function compile_pass {
  295. echo "Checking build..."
  296. go build -v ./tools/...
  297. }
  298. # fail fast on static tests
  299. function build_pass {
  300. GO_BUILD_FLAGS="-a -v" etcd_build
  301. }
  302. for pass in $PASSES; do
  303. ${pass}_pass $@
  304. done
  305. echo "Success"