test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. # TODO: 'client' pkg fails with gosimple from generated files
  13. # TODO: 'rafttest' is failing with unused
  14. GOSIMPLE_UNUSED_PATHS=$(go list ./... | sed -e 's/github.com\/coreos\/etcd\///g' | grep -vE 'cmd|vendor|rafttest|github.com/coreos/etcd$|client$')
  15. # Invoke ./cover for HTML output
  16. COVER=${COVER:-"-cover"}
  17. GO_BUILD_FLAGS="-a -v"
  18. source ./build
  19. # Set up gopath so tests use vendored dependencies
  20. export GOPATH=${PWD}/gopath
  21. rm -rf $GOPATH/src
  22. mkdir -p $GOPATH
  23. ln -s ${PWD}/cmd/vendor $GOPATH/src
  24. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  25. IGNORE_PKGS="(cmd|vendor|etcdserverpb|rafttest)"
  26. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  27. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  28. FORMATTABLE=`find . -name \*.go | while read a; do echo $(dirname $a)/"*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  29. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  30. # user has not provided PKG override
  31. if [ -z "$PKG" ]; then
  32. TEST=$TESTABLE_AND_FORMATTABLE
  33. FMT=$FORMATTABLE
  34. # user has provided PKG override
  35. else
  36. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  37. TEST=${PKG/#./}
  38. TEST=${TEST/#\//}
  39. TEST=${TEST/%\//}
  40. # only run gofmt on packages provided by user
  41. FMT="$TEST"
  42. fi
  43. # split TEST into an array and prepend REPO_PATH to each local package
  44. split=(${TEST// / })
  45. TEST=${split[@]/#/${REPO_PATH}/}
  46. MACHINE_TYPE=$(uname -m)
  47. if [ $MACHINE_TYPE != "armv7l" ]; then
  48. RACE="--race"
  49. fi
  50. function unit_tests {
  51. echo "Running tests..."
  52. # only -run=Test so examples can run in integration tests
  53. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
  54. }
  55. function integration_tests {
  56. echo "Running integration tests..."
  57. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  58. e2epid="$!"
  59. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  60. intpid="$!"
  61. wait $e2epid
  62. wait $intpid
  63. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  64. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  65. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  66. }
  67. function fmt_tests {
  68. echo "Checking gofmt..."
  69. fmtRes=$(gofmt -l -s -d $FMT)
  70. if [ -n "${fmtRes}" ]; then
  71. echo -e "gofmt checking failed:\n${fmtRes}"
  72. exit 255
  73. fi
  74. echo "Checking govet..."
  75. vetRes=$(go vet $TEST)
  76. if [ -n "${vetRes}" ]; then
  77. echo -e "govet checking failed:\n${vetRes}"
  78. exit 255
  79. fi
  80. echo "Checking 'go tool vet -shadow'..."
  81. for path in $FMT; do
  82. if [ "${path##*.}" != "go" ]; then
  83. path="${path}/*.go"
  84. fi
  85. vetRes=$(go tool vet -shadow ${path})
  86. if [ -n "${vetRes}" ]; then
  87. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  88. exit 255
  89. fi
  90. done
  91. if which goword >/dev/null; then
  92. echo "Checking goword..."
  93. # get all go files to process
  94. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  95. # ignore tests and protobuf files
  96. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  97. # only check for broken exported godocs
  98. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  99. if [ ! -z "$gowordRes" ]; then
  100. echo -e "goword checking failed:\n${gowordRes}"
  101. exit 255
  102. fi
  103. else
  104. echo "Skipping goword..."
  105. fi
  106. if which gosimple >/dev/null; then
  107. echo "Checking gosimple..."
  108. for path in $GOSIMPLE_UNUSED_PATHS; do
  109. simplResult=`gosimple $REPO_PATH/${path} || true`
  110. if [ -n "${simplResult}" ]; then
  111. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  112. exit 255
  113. fi
  114. done
  115. else
  116. echo "Skipping gosimple..."
  117. fi
  118. if which unused >/dev/null; then
  119. echo "Checking unused..."
  120. for path in $GOSIMPLE_UNUSED_PATHS; do
  121. unusedResult=`unused $REPO_PATH/${path} || true`
  122. if [ -n "${unusedResult}" ]; then
  123. echo -e "unused checking ${path} failed:\n${unusedResult}"
  124. exit 255
  125. fi
  126. done
  127. else
  128. echo "Skipping unused..."
  129. fi
  130. echo "Checking for license header..."
  131. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  132. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  133. done;)
  134. if [ -n "${licRes}" ]; then
  135. echo -e "license header checking failed:\n${licRes}"
  136. exit 255
  137. fi
  138. }
  139. function dep_tests {
  140. echo "Checking package dependencies..."
  141. # don't pull in etcdserver package
  142. pushd clientv3 >/dev/null
  143. badpkg="(etcdserver|mvcc)"
  144. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  145. popd >/dev/null
  146. if [ ! -z "$deps" ]; then
  147. echo -e "clientv3 has masked dependencies:\n${deps}"
  148. exit 255
  149. fi
  150. }
  151. # fail fast on static tests
  152. fmt_tests
  153. dep_tests
  154. unit_tests
  155. if [ -n "$INTEGRATION" ]; then
  156. integration_tests
  157. fi
  158. echo "Success"