test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "Skipping gosimple..."
  113. fi
  114. if which unused >/dev/null; then
  115. echo "Checking unused..."
  116. for path in $GOSIMPLE_UNUSED_PATHS; do
  117. unusedResult=`unused $REPO_PATH/${path} || true`
  118. if [ -n "${unusedResult}" ]; then
  119. echo -e "unused checking ${path} failed:\n${unusedResult}"
  120. exit 255
  121. fi
  122. done
  123. else
  124. echo "Skipping unused..."
  125. fi
  126. echo "Checking for license header..."
  127. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  128. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  129. done;)
  130. if [ -n "${licRes}" ]; then
  131. echo -e "license header checking failed:\n${licRes}"
  132. exit 255
  133. fi
  134. echo "Checking commit titles..."
  135. git log master..HEAD --oneline | while read l; do
  136. commitMsg=`echo "$l" | cut -f2- -d' '`
  137. if [[ "$commitMsg" == Merge* ]]; then
  138. # ignore "Merge pull" commits
  139. continue
  140. fi
  141. if [[ "$commitMsg" == Revert* ]]; then
  142. # ignore revert commits
  143. continue
  144. fi
  145. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  146. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  147. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  148. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  149. echo "$l"...
  150. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  151. echo "Got: $l"
  152. exit 255
  153. fi
  154. done
  155. }
  156. function dep_tests {
  157. echo "Checking package dependencies..."
  158. # don't pull in etcdserver package
  159. pushd clientv3 >/dev/null
  160. badpkg="(etcdserver|mvcc)"
  161. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  162. popd >/dev/null
  163. if [ ! -z "$deps" ]; then
  164. echo -e "clientv3 has masked dependencies:\n${deps}"
  165. exit 255
  166. fi
  167. }
  168. # Set up gopath so tests use vendored dependencies
  169. export GOPATH=${PWD}/gopath
  170. rm -rf $GOPATH/src
  171. mkdir -p $GOPATH
  172. ln -s ${PWD}/cmd/vendor $GOPATH/src
  173. # fail fast on static tests
  174. toggle_failpoints disable
  175. fmt_tests
  176. dep_tests
  177. # fail fast on static tests
  178. GO_BUILD_FLAGS="-a -v" etcd_build
  179. unit_tests
  180. if [ -n "$INTEGRATION" ]; then
  181. integration_tests
  182. fi
  183. echo "Success"