test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. PKGS=`ls pkg/*/*go | cut -f1,2 -d/ | sort | uniq`
  26. TESTABLE_AND_FORMATTABLE="auth client clientv3 discovery error etcdctl/ctlv2 etcdctl/ctlv3 etcdmain etcdserver etcdserver/auth etcdserver/api/v2http etcdserver/api/v2http/httptypes etcdserver/api/v3rpc etcdserver/api/v3rpc/rpctypes $PKGS proxy/httpproxy proxy/tcpproxy raft snap mvcc mvcc/backend store version wal rafthttp"
  27. FORMATTABLE="$TESTABLE_AND_FORMATTABLE *.go etcdctl/ integration clientv3/integration e2e alarm"
  28. # user has not provided PKG override
  29. if [ -z "$PKG" ]; then
  30. TEST=$TESTABLE_AND_FORMATTABLE
  31. FMT=$FORMATTABLE
  32. # user has provided PKG override
  33. else
  34. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  35. TEST=${PKG/#./}
  36. TEST=${TEST/#\//}
  37. TEST=${TEST/%\//}
  38. # only run gofmt on packages provided by user
  39. FMT="$TEST"
  40. fi
  41. # split TEST into an array and prepend REPO_PATH to each local package
  42. split=(${TEST// / })
  43. TEST=${split[@]/#/${REPO_PATH}/}
  44. MACHINE_TYPE=$(uname -m)
  45. if [ $MACHINE_TYPE != "armv7l" ]; then
  46. RACE="--race"
  47. fi
  48. function unit_tests {
  49. echo "Running tests..."
  50. # only -run=Test so examples can run in integration tests
  51. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
  52. }
  53. function integration_tests {
  54. echo "Running integration tests..."
  55. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  56. e2epid="$!"
  57. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  58. intpid="$!"
  59. wait $e2epid
  60. wait $intpid
  61. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  62. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  63. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  64. }
  65. function fmt_tests {
  66. echo "Checking gofmt..."
  67. fmtRes=$(gofmt -l -s -d $FMT)
  68. if [ -n "${fmtRes}" ]; then
  69. echo -e "gofmt checking failed:\n${fmtRes}"
  70. exit 255
  71. fi
  72. echo "Checking govet..."
  73. vetRes=$(go vet $TEST)
  74. if [ -n "${vetRes}" ]; then
  75. echo -e "govet checking failed:\n${vetRes}"
  76. exit 255
  77. fi
  78. echo "Checking 'go tool vet -shadow'..."
  79. for path in $FMT; do
  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. }
  134. function dep_tests {
  135. echo "Checking package dependencies..."
  136. # don't pull in etcdserver package
  137. pushd clientv3 >/dev/null
  138. badpkg="(etcdserver|mvcc)"
  139. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  140. popd >/dev/null
  141. if [ ! -z "$deps" ]; then
  142. echo -e "clientv3 has masked dependencies:\n${deps}"
  143. exit 255
  144. fi
  145. }
  146. # fail fast on static tests
  147. fmt_tests
  148. dep_tests
  149. unit_tests
  150. if [ -n "$INTEGRATION" ]; then
  151. integration_tests
  152. fi
  153. echo "Success"