test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if [ -z "$GOARCH" ]; then
  30. GOARCH=$(go env GOARCH);
  31. fi
  32. # user has not provided PKG override
  33. if [ -z "$PKG" ]; then
  34. TEST=$TESTABLE_AND_FORMATTABLE
  35. FMT=$FORMATTABLE
  36. # user has provided PKG override
  37. else
  38. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  39. TEST=${PKG/#./}
  40. TEST=${TEST/#\//}
  41. TEST=${TEST/%\//}
  42. # only run gofmt on packages provided by user
  43. FMT="$TEST"
  44. fi
  45. # split TEST into an array and prepend REPO_PATH to each local package
  46. split=(${TEST// / })
  47. TEST=${split[@]/#/${REPO_PATH}/}
  48. # determine whether target supports race detection
  49. if [ "$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. rm -f ./bin/etcd-last-release
  75. # to grab latest patch release; bump this up for every minor release
  76. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.0.*" | head -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. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  82. echo "Downloading $file"
  83. set +e
  84. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  85. local result=$?
  86. set -e
  87. case $result in
  88. 0) ;;
  89. 22) return 0
  90. ;;
  91. *) exit $result
  92. ;;
  93. esac
  94. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  95. mkdir -p ./bin
  96. mv /tmp/etcd ./bin/etcd-last-release
  97. }
  98. function fmt_pass {
  99. toggle_failpoints disable
  100. echo "Checking gofmt..."
  101. fmtRes=$(gofmt -l -s -d $FMT)
  102. if [ -n "${fmtRes}" ]; then
  103. echo -e "gofmt checking failed:\n${fmtRes}"
  104. exit 255
  105. fi
  106. echo "Checking govet..."
  107. vetRes=$(go vet $TEST)
  108. if [ -n "${vetRes}" ]; then
  109. echo -e "govet checking failed:\n${vetRes}"
  110. exit 255
  111. fi
  112. echo "Checking 'go tool vet -shadow'..."
  113. for path in $FMT; do
  114. if [ "${path##*.}" != "go" ]; then
  115. path="${path}/*.go"
  116. fi
  117. vetRes=$(go tool vet -shadow ${path})
  118. if [ -n "${vetRes}" ]; then
  119. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  120. exit 255
  121. fi
  122. done
  123. if which goword >/dev/null; then
  124. echo "Checking goword..."
  125. # get all go files to process
  126. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  127. # ignore tests and protobuf files
  128. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  129. # only check for broken exported godocs
  130. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  131. if [ ! -z "$gowordRes" ]; then
  132. echo -e "goword checking failed:\n${gowordRes}"
  133. exit 255
  134. fi
  135. else
  136. echo "Skipping goword..."
  137. fi
  138. if which gosimple >/dev/null; then
  139. echo "Checking gosimple..."
  140. for path in $GOSIMPLE_UNUSED_PATHS; do
  141. simplResult=`gosimple $REPO_PATH/${path} || true`
  142. if [ -n "${simplResult}" ]; then
  143. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  144. exit 255
  145. fi
  146. done
  147. else
  148. echo "Skipping gosimple..."
  149. fi
  150. if which unused >/dev/null; then
  151. echo "Checking unused..."
  152. for path in $GOSIMPLE_UNUSED_PATHS; do
  153. unusedResult=`unused $REPO_PATH/${path} || true`
  154. if [ -n "${unusedResult}" ]; then
  155. echo -e "unused checking ${path} failed:\n${unusedResult}"
  156. exit 255
  157. fi
  158. done
  159. else
  160. echo "Skipping unused..."
  161. fi
  162. echo "Checking for license header..."
  163. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  164. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  165. done;)
  166. if [ -n "${licRes}" ]; then
  167. echo -e "license header checking failed:\n${licRes}"
  168. exit 255
  169. fi
  170. echo "Checking commit titles..."
  171. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  172. commitMsg=`echo "$l" | cut -f2- -d' '`
  173. if [[ "$commitMsg" == Merge* ]]; then
  174. # ignore "Merge pull" commits
  175. continue
  176. fi
  177. if [[ "$commitMsg" == Revert* ]]; then
  178. # ignore revert commits
  179. continue
  180. fi
  181. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  182. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  183. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  184. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  185. echo "$l"...
  186. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  187. echo "Got: $l"
  188. exit 255
  189. fi
  190. done
  191. }
  192. function dep_pass {
  193. echo "Checking package dependencies..."
  194. # don't pull in etcdserver package
  195. pushd clientv3 >/dev/null
  196. badpkg="(etcdserver|mvcc)"
  197. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  198. popd >/dev/null
  199. if [ ! -z "$deps" ]; then
  200. echo -e "clientv3 has masked dependencies:\n${deps}"
  201. exit 255
  202. fi
  203. }
  204. function compile_pass {
  205. echo "Checking build..."
  206. go build -v ./tools/...
  207. }
  208. # fail fast on static tests
  209. function build_pass {
  210. GO_BUILD_FLAGS="-a -v" etcd_build
  211. }
  212. for pass in $PASSES; do
  213. ${pass}_pass
  214. done
  215. echo "Success"