test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. # to grab latest patch release; bump this up for every minor release
  77. UPGRADE_VER=$(git tag -l | grep v3.0 | tail -1)
  78. if [ -n "$MANUAL_VER" ]; then
  79. # in case, we need to test against different version
  80. UPGRADE_VER=$MANUAL_VER
  81. fi
  82. echo "Downloading" etcd $UPGRADE_VER
  83. 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
  84. tar xzvf /tmp/etcd-$UPGRADE_VER-linux-amd64.tar.gz -C /tmp/ --strip-components=1
  85. mv /tmp/etcd ./bin/etcd-last-release
  86. }
  87. function fmt_pass {
  88. toggle_failpoints disable
  89. echo "Checking gofmt..."
  90. fmtRes=$(gofmt -l -s -d $FMT)
  91. if [ -n "${fmtRes}" ]; then
  92. echo -e "gofmt checking failed:\n${fmtRes}"
  93. exit 255
  94. fi
  95. echo "Checking govet..."
  96. vetRes=$(go vet $TEST)
  97. if [ -n "${vetRes}" ]; then
  98. echo -e "govet checking failed:\n${vetRes}"
  99. exit 255
  100. fi
  101. echo "Checking 'go tool vet -shadow'..."
  102. for path in $FMT; do
  103. if [ "${path##*.}" != "go" ]; then
  104. path="${path}/*.go"
  105. fi
  106. vetRes=$(go tool vet -shadow ${path})
  107. if [ -n "${vetRes}" ]; then
  108. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  109. exit 255
  110. fi
  111. done
  112. if which goword >/dev/null; then
  113. echo "Checking goword..."
  114. # get all go files to process
  115. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  116. # ignore tests and protobuf files
  117. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  118. # only check for broken exported godocs
  119. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  120. if [ ! -z "$gowordRes" ]; then
  121. echo -e "goword checking failed:\n${gowordRes}"
  122. exit 255
  123. fi
  124. else
  125. echo "Skipping goword..."
  126. fi
  127. if which gosimple >/dev/null; then
  128. echo "Checking gosimple..."
  129. for path in $GOSIMPLE_UNUSED_PATHS; do
  130. simplResult=`gosimple $REPO_PATH/${path} || true`
  131. if [ -n "${simplResult}" ]; then
  132. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  133. exit 255
  134. fi
  135. done
  136. else
  137. echo "Skipping gosimple..."
  138. fi
  139. if which unused >/dev/null; then
  140. echo "Checking unused..."
  141. for path in $GOSIMPLE_UNUSED_PATHS; do
  142. unusedResult=`unused $REPO_PATH/${path} || true`
  143. if [ -n "${unusedResult}" ]; then
  144. echo -e "unused checking ${path} failed:\n${unusedResult}"
  145. exit 255
  146. fi
  147. done
  148. else
  149. echo "Skipping unused..."
  150. fi
  151. echo "Checking for license header..."
  152. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  153. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  154. done;)
  155. if [ -n "${licRes}" ]; then
  156. echo -e "license header checking failed:\n${licRes}"
  157. exit 255
  158. fi
  159. echo "Checking commit titles..."
  160. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  161. commitMsg=`echo "$l" | cut -f2- -d' '`
  162. if [[ "$commitMsg" == Merge* ]]; then
  163. # ignore "Merge pull" commits
  164. continue
  165. fi
  166. if [[ "$commitMsg" == Revert* ]]; then
  167. # ignore revert commits
  168. continue
  169. fi
  170. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  171. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  172. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  173. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  174. echo "$l"...
  175. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  176. echo "Got: $l"
  177. exit 255
  178. fi
  179. done
  180. }
  181. function dep_pass {
  182. echo "Checking package dependencies..."
  183. # don't pull in etcdserver package
  184. pushd clientv3 >/dev/null
  185. badpkg="(etcdserver|mvcc)"
  186. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  187. popd >/dev/null
  188. if [ ! -z "$deps" ]; then
  189. echo -e "clientv3 has masked dependencies:\n${deps}"
  190. exit 255
  191. fi
  192. }
  193. function compile_pass {
  194. echo "Checking build..."
  195. go build -v ./tools/...
  196. }
  197. # fail fast on static tests
  198. function build_pass {
  199. GO_BUILD_FLAGS="-a -v" etcd_build
  200. }
  201. for pass in $PASSES; do
  202. ${pass}_pass
  203. done
  204. echo "Success"