test 6.5 KB

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