test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. echo "Running integration tests..."
  51. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  52. e2epid="$!"
  53. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  54. intpid="$!"
  55. wait $e2epid
  56. wait $intpid
  57. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  58. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  59. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  60. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  61. }
  62. function fmt_tests {
  63. echo "Checking gofmt..."
  64. fmtRes=$(gofmt -l -s -d $FMT)
  65. if [ -n "${fmtRes}" ]; then
  66. echo -e "gofmt checking failed:\n${fmtRes}"
  67. exit 255
  68. fi
  69. echo "Checking govet..."
  70. vetRes=$(go vet $TEST)
  71. if [ -n "${vetRes}" ]; then
  72. echo -e "govet checking failed:\n${vetRes}"
  73. exit 255
  74. fi
  75. echo "Checking 'go tool vet -shadow'..."
  76. for path in $FMT; do
  77. if [ "${path##*.}" != "go" ]; then
  78. path="${path}/*.go"
  79. fi
  80. vetRes=$(go tool vet -shadow ${path})
  81. if [ -n "${vetRes}" ]; then
  82. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  83. exit 255
  84. fi
  85. done
  86. if which goword >/dev/null; then
  87. echo "Checking goword..."
  88. # get all go files to process
  89. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  90. # ignore tests and protobuf files
  91. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  92. # only check for broken exported godocs
  93. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  94. if [ ! -z "$gowordRes" ]; then
  95. echo -e "goword checking failed:\n${gowordRes}"
  96. exit 255
  97. fi
  98. else
  99. echo "Skipping goword..."
  100. fi
  101. if which gosimple >/dev/null; then
  102. echo "Checking gosimple..."
  103. for path in $GOSIMPLE_UNUSED_PATHS; do
  104. simplResult=`gosimple $REPO_PATH/${path} || true`
  105. if [ -n "${simplResult}" ]; then
  106. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  107. exit 255
  108. fi
  109. done
  110. else
  111. echo "Skipping gosimple..."
  112. fi
  113. if which unused >/dev/null; then
  114. echo "Checking unused..."
  115. for path in $GOSIMPLE_UNUSED_PATHS; do
  116. unusedResult=`unused $REPO_PATH/${path} || true`
  117. if [ -n "${unusedResult}" ]; then
  118. echo -e "unused checking ${path} failed:\n${unusedResult}"
  119. exit 255
  120. fi
  121. done
  122. else
  123. echo "Skipping unused..."
  124. fi
  125. echo "Checking for license header..."
  126. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  127. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  128. done;)
  129. if [ -n "${licRes}" ]; then
  130. echo -e "license header checking failed:\n${licRes}"
  131. exit 255
  132. fi
  133. echo "Checking commit titles..."
  134. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  135. commitMsg=`echo "$l" | cut -f2- -d' '`
  136. if [[ "$commitMsg" == Merge* ]]; then
  137. # ignore "Merge pull" commits
  138. continue
  139. fi
  140. if [[ "$commitMsg" == Revert* ]]; then
  141. # ignore revert commits
  142. continue
  143. fi
  144. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  145. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  146. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  147. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  148. echo "$l"...
  149. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  150. echo "Got: $l"
  151. exit 255
  152. fi
  153. done
  154. }
  155. function dep_tests {
  156. echo "Checking package dependencies..."
  157. # don't pull in etcdserver package
  158. pushd clientv3 >/dev/null
  159. badpkg="(etcdserver|mvcc)"
  160. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  161. popd >/dev/null
  162. if [ ! -z "$deps" ]; then
  163. echo -e "clientv3 has masked dependencies:\n${deps}"
  164. exit 255
  165. fi
  166. }
  167. function compile_tests {
  168. echo "Checking build..."
  169. go build -v ./tools/...
  170. }
  171. # Set up gopath so tests use vendored dependencies
  172. export GOPATH=${PWD}/gopath
  173. rm -rf $GOPATH/src
  174. mkdir -p $GOPATH
  175. ln -s ${PWD}/cmd/vendor $GOPATH/src
  176. # fail fast on static tests
  177. toggle_failpoints disable
  178. fmt_tests
  179. dep_tests
  180. compile_tests
  181. # fail fast on static tests
  182. GO_BUILD_FLAGS="-a -v" etcd_build
  183. unit_tests
  184. if [ -n "$INTEGRATION" ]; then
  185. integration_tests
  186. fi
  187. echo "Success"