test 6.5 KB

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