test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  58. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  59. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  60. }
  61. function fmt_tests {
  62. echo "Checking gofmt..."
  63. fmtRes=$(gofmt -l -s -d $FMT)
  64. if [ -n "${fmtRes}" ]; then
  65. echo -e "gofmt checking failed:\n${fmtRes}"
  66. exit 255
  67. fi
  68. echo "Checking govet..."
  69. vetRes=$(go vet $TEST)
  70. if [ -n "${vetRes}" ]; then
  71. echo -e "govet checking failed:\n${vetRes}"
  72. exit 255
  73. fi
  74. echo "Checking 'go tool vet -shadow'..."
  75. for path in $FMT; do
  76. if [ "${path##*.}" != "go" ]; then
  77. path="${path}/*.go"
  78. fi
  79. vetRes=$(go tool vet -shadow ${path})
  80. if [ -n "${vetRes}" ]; then
  81. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  82. exit 255
  83. fi
  84. done
  85. if which goword >/dev/null; then
  86. echo "Checking goword..."
  87. # get all go files to process
  88. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  89. # ignore tests and protobuf files
  90. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  91. # only check for broken exported godocs
  92. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  93. if [ ! -z "$gowordRes" ]; then
  94. echo -e "goword checking failed:\n${gowordRes}"
  95. exit 255
  96. fi
  97. else
  98. echo "Skipping goword..."
  99. fi
  100. if which gosimple >/dev/null; then
  101. echo "Checking gosimple..."
  102. for path in $GOSIMPLE_UNUSED_PATHS; do
  103. simplResult=`gosimple $REPO_PATH/${path} || true`
  104. if [ -n "${simplResult}" ]; then
  105. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  106. exit 255
  107. fi
  108. done
  109. else
  110. echo "Skipping gosimple..."
  111. fi
  112. if which unused >/dev/null; then
  113. echo "Checking unused..."
  114. for path in $GOSIMPLE_UNUSED_PATHS; do
  115. unusedResult=`unused $REPO_PATH/${path} || true`
  116. if [ -n "${unusedResult}" ]; then
  117. echo -e "unused checking ${path} failed:\n${unusedResult}"
  118. exit 255
  119. fi
  120. done
  121. else
  122. echo "Skipping unused..."
  123. fi
  124. echo "Checking for license header..."
  125. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  126. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  127. done;)
  128. if [ -n "${licRes}" ]; then
  129. echo -e "license header checking failed:\n${licRes}"
  130. exit 255
  131. fi
  132. }
  133. function dep_tests {
  134. echo "Checking package dependencies..."
  135. # don't pull in etcdserver package
  136. pushd clientv3 >/dev/null
  137. badpkg="(etcdserver|mvcc)"
  138. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  139. popd >/dev/null
  140. if [ ! -z "$deps" ]; then
  141. echo -e "clientv3 has masked dependencies:\n${deps}"
  142. exit 255
  143. fi
  144. }
  145. # Set up gopath so tests use vendored dependencies
  146. export GOPATH=${PWD}/gopath
  147. rm -rf $GOPATH/src
  148. mkdir -p $GOPATH
  149. ln -s ${PWD}/cmd/vendor $GOPATH/src
  150. # fail fast on static tests
  151. toggle_failpoints disable
  152. fmt_tests
  153. dep_tests
  154. # fail fast on static tests
  155. GO_BUILD_FLAGS="-a -v" etcd_build
  156. unit_tests
  157. if [ -n "$INTEGRATION" ]; then
  158. integration_tests
  159. fi
  160. echo "Success"