test 6.4 KB

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