test 9.9 KB

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