test 6.4 KB

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