test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  18. source ./build
  19. # Set up gopath so tests use vendored dependencies
  20. export GOPATH=${PWD}/gopath
  21. rm -f $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. PKGS=`ls pkg/*/*go | cut -f1,2 -d/ | sort | uniq`
  26. TESTABLE_AND_FORMATTABLE="client clientv3 discovery error etcdctl/ctlv2 etcdctl/ctlv3 etcdmain etcdserver etcdserver/auth etcdserver/api/v2http etcdserver/api/v2http/httptypes $PKGS proxy raft snap storage storage/backend store version wal"
  27. # TODO: add it to race testing when the issue is resolved
  28. # https://github.com/golang/go/issues/9946
  29. NO_RACE_TESTABLE="rafthttp"
  30. FORMATTABLE="$TESTABLE_AND_FORMATTABLE $NO_RACE_TESTABLE *.go etcdctl/ integration clientv3/integration e2e alarm"
  31. # user has not provided PKG override
  32. if [ -z "$PKG" ]; then
  33. TEST=$TESTABLE_AND_FORMATTABLE
  34. NO_RACE_TEST=$NO_RACE_TESTABLE
  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. split=(${NO_RACE_TEST// / })
  49. NO_RACE_TEST=${split[@]/#/${REPO_PATH}/}
  50. MACHINE_TYPE=$(uname -m)
  51. if [ $MACHINE_TYPE != "armv7l" ]; then
  52. RACE="--race"
  53. fi
  54. function unit_tests {
  55. echo "Running tests..."
  56. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 $@ ${TEST}
  57. go test -timeout 3m ${COVER} -cpu 1,2,4 $@ ${NO_RACE_TEST}
  58. }
  59. function integration_tests {
  60. echo "Running integration tests..."
  61. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  62. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  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. }
  66. function fmt_tests {
  67. echo "Checking gofmt..."
  68. fmtRes=$(gofmt -l -s -d $FMT)
  69. if [ -n "${fmtRes}" ]; then
  70. echo -e "gofmt checking failed:\n${fmtRes}"
  71. exit 255
  72. fi
  73. echo "Checking govet..."
  74. vetRes=$(go vet $TEST)
  75. if [ -n "${vetRes}" ]; then
  76. echo -e "govet checking failed:\n${vetRes}"
  77. exit 255
  78. fi
  79. echo "Checking govet -shadow..."
  80. for path in $FMT; do
  81. vetRes=$(go tool vet -shadow ${path})
  82. if [ -n "${vetRes}" ]; then
  83. echo -e "govet checking ${path} failed:\n${vetRes}"
  84. exit 255
  85. fi
  86. done
  87. echo "Checking goword..."
  88. if which goword >/dev/null; then
  89. echo "goword is installed..."
  90. # get all go files to process
  91. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  92. # ignore tests and protobuf files
  93. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  94. # only check for broken exported godocs
  95. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  96. if [ ! -z "$gowordRes" ]; then
  97. echo -e "goword checking failed:\n${gowordRes}"
  98. exit 255
  99. fi
  100. else
  101. echo "gowrod does not exist... skipping..."
  102. fi
  103. echo "Checking gosimple"
  104. if which gosimple >/dev/null; then
  105. echo "gosimple is installed..."
  106. for path in $GOSIMPLE_UNUSED_PATHS; do
  107. simplResult=$(gosimple $REPO_PATH/${path})
  108. if [ -n "${simplResult}" ]; then
  109. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  110. exit 255
  111. fi
  112. done
  113. else
  114. echo "gosimple does not exist... skipping..."
  115. fi
  116. echo "Checking unused"
  117. if which unused >/dev/null; then
  118. echo "unused is installed..."
  119. for path in $GOSIMPLE_UNUSED_PATHS; do
  120. unusedResult=$(unused $REPO_PATH/${path})
  121. if [ -n "${unusedResult}" ]; then
  122. echo -e "unused checking ${path} failed:\n${unusedResult}"
  123. exit 255
  124. fi
  125. done
  126. else
  127. echo "unused does not exist... skipping..."
  128. fi
  129. echo "Checking for license header..."
  130. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  131. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  132. done;)
  133. if [ -n "${licRes}" ]; then
  134. echo -e "license header checking failed:\n${licRes}"
  135. exit 255
  136. fi
  137. }
  138. function dep_tests {
  139. echo "Checking package dependencies..."
  140. # don't pull in etcdserver package
  141. pushd clientv3 >/dev/null
  142. badpkg="(etcdserver|storage)"
  143. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  144. popd >/dev/null
  145. if [ ! -z "$deps" ]; then
  146. echo -e "clientv3 has masked dependencies:\n${deps}"
  147. exit 255
  148. fi
  149. }
  150. # fail fast on static tests
  151. fmt_tests
  152. dep_tests
  153. unit_tests
  154. if [ -n "$INTEGRATION" ]; then
  155. integration_tests
  156. fi
  157. echo "Success"