test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. set -e
  12. source ./build
  13. # build tests with vendored dependencies
  14. etcd_setup_gopath
  15. if [ -z "$PASSES" ]; then
  16. PASSES="fmt dep compile build unit"
  17. fi
  18. # TODO: 'client' pkg fails with gosimple from generated files
  19. # TODO: 'rafttest' is failing with unused
  20. GOSIMPLE_UNUSED_PATHS=$(go list ./... | sed -e 's/github.com\/coreos\/etcd\///g' | grep -vE 'cmd|vendor|rafttest|github.com/coreos/etcd$|client$')
  21. # Invoke ./cover for HTML output
  22. COVER=${COVER:-"-cover"}
  23. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  24. IGNORE_PKGS="(cmd|vendor|etcdserverpb|rafttest)"
  25. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  26. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  27. FORMATTABLE=`find . -name \*.go | while read a; do echo $(dirname $a)/"*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  28. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  29. if [ -z "$GOARCH" ]; then
  30. GOARCH=$(go env GOARCH);
  31. fi
  32. # user has not provided PKG override
  33. if [ -z "$PKG" ]; then
  34. TEST=$TESTABLE_AND_FORMATTABLE
  35. FMT=$FORMATTABLE
  36. # user has provided PKG override
  37. else
  38. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  39. TEST=${PKG/#./}
  40. TEST=${TEST/#\//}
  41. TEST=${TEST/%\//}
  42. # only run gofmt on packages provided by user
  43. FMT="$TEST"
  44. fi
  45. # split TEST into an array and prepend REPO_PATH to each local package
  46. split=(${TEST// / })
  47. TEST=${split[@]/#/${REPO_PATH}/}
  48. # determine whether target supports race detection
  49. if [ "$GOARCH" == "amd64" ]; then
  50. RACE="--race"
  51. fi
  52. function unit_pass {
  53. echo "Running unit tests..."
  54. # only -run=Test so examples can run in integration tests
  55. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
  56. }
  57. function integration_pass {
  58. echo "Running integration tests..."
  59. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  60. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  61. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  62. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  63. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  64. }
  65. function e2e_pass {
  66. echo "Running e2e tests..."
  67. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  68. }
  69. function integration_e2e_pass {
  70. echo "Running integration and e2e tests..."
  71. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  72. e2epid="$!"
  73. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  74. intpid="$!"
  75. wait $e2epid
  76. wait $intpid
  77. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  78. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  79. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  80. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  81. }
  82. function grpcproxy_pass {
  83. go test -timeout 10m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/integration
  84. }
  85. function release_pass {
  86. rm -f ./bin/etcd-last-release
  87. # to grab latest patch release; bump this up for every minor release
  88. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.0.*" | head -1)
  89. if [ -n "$MANUAL_VER" ]; then
  90. # in case, we need to test against different version
  91. UPGRADE_VER=$MANUAL_VER
  92. fi
  93. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  94. echo "Downloading $file"
  95. set +e
  96. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  97. local result=$?
  98. set -e
  99. case $result in
  100. 0) ;;
  101. 22) return 0
  102. ;;
  103. *) exit $result
  104. ;;
  105. esac
  106. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  107. mkdir -p ./bin
  108. mv /tmp/etcd ./bin/etcd-last-release
  109. }
  110. function fmt_pass {
  111. toggle_failpoints disable
  112. echo "Checking gofmt..."
  113. fmtRes=$(gofmt -l -s -d $FMT)
  114. if [ -n "${fmtRes}" ]; then
  115. echo -e "gofmt checking failed:\n${fmtRes}"
  116. exit 255
  117. fi
  118. echo "Checking govet..."
  119. vetRes=$(go vet $TEST)
  120. if [ -n "${vetRes}" ]; then
  121. echo -e "govet checking failed:\n${vetRes}"
  122. exit 255
  123. fi
  124. echo "Checking 'go tool vet -shadow'..."
  125. for path in $FMT; do
  126. if [ "${path##*.}" != "go" ]; then
  127. path="${path}/*.go"
  128. fi
  129. vetRes=$(go tool vet -shadow ${path})
  130. if [ -n "${vetRes}" ]; then
  131. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  132. exit 255
  133. fi
  134. done
  135. if which goword >/dev/null; then
  136. echo "Checking goword..."
  137. # get all go files to process
  138. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  139. # ignore tests and protobuf files
  140. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  141. # only check for broken exported godocs
  142. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  143. if [ ! -z "$gowordRes" ]; then
  144. echo -e "goword checking failed:\n${gowordRes}"
  145. exit 255
  146. fi
  147. else
  148. echo "Skipping goword..."
  149. fi
  150. if which gosimple >/dev/null; then
  151. echo "Checking gosimple..."
  152. for path in $GOSIMPLE_UNUSED_PATHS; do
  153. simplResult=`gosimple $REPO_PATH/${path} || true`
  154. if [ -n "${simplResult}" ]; then
  155. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  156. exit 255
  157. fi
  158. done
  159. else
  160. echo "Skipping gosimple..."
  161. fi
  162. if which unused >/dev/null; then
  163. echo "Checking unused..."
  164. for path in $GOSIMPLE_UNUSED_PATHS; do
  165. unusedResult=`unused $REPO_PATH/${path} || true`
  166. if [ -n "${unusedResult}" ]; then
  167. echo -e "unused checking ${path} failed:\n${unusedResult}"
  168. exit 255
  169. fi
  170. done
  171. else
  172. echo "Skipping unused..."
  173. fi
  174. echo "Checking for license header..."
  175. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  176. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  177. done;)
  178. if [ -n "${licRes}" ]; then
  179. echo -e "license header checking failed:\n${licRes}"
  180. exit 255
  181. fi
  182. echo "Checking commit titles..."
  183. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  184. commitMsg=`echo "$l" | cut -f2- -d' '`
  185. if [[ "$commitMsg" == Merge* ]]; then
  186. # ignore "Merge pull" commits
  187. continue
  188. fi
  189. if [[ "$commitMsg" == Revert* ]]; then
  190. # ignore revert commits
  191. continue
  192. fi
  193. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  194. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  195. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  196. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  197. echo "$l"...
  198. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  199. echo "Got: $l"
  200. exit 255
  201. fi
  202. done
  203. }
  204. function dep_pass {
  205. echo "Checking package dependencies..."
  206. # don't pull in etcdserver package
  207. pushd clientv3 >/dev/null
  208. badpkg="(etcdserver|mvcc)"
  209. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  210. popd >/dev/null
  211. if [ ! -z "$deps" ]; then
  212. echo -e "clientv3 has masked dependencies:\n${deps}"
  213. exit 255
  214. fi
  215. }
  216. function compile_pass {
  217. echo "Checking build..."
  218. go build -v ./tools/...
  219. }
  220. # fail fast on static tests
  221. function build_pass {
  222. GO_BUILD_FLAGS="-a -v" etcd_build
  223. }
  224. for pass in $PASSES; do
  225. ${pass}_pass
  226. done
  227. echo "Success"