test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/httpproxy proxy/tcpproxy raft snap storage storage/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. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  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 govet -shadow..."
  75. for path in $FMT; do
  76. vetRes=$(go tool vet -shadow ${path})
  77. if [ -n "${vetRes}" ]; then
  78. echo -e "govet checking ${path} failed:\n${vetRes}"
  79. exit 255
  80. fi
  81. done
  82. echo "Checking goword..."
  83. if which goword >/dev/null; then
  84. echo "goword is installed..."
  85. # get all go files to process
  86. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  87. # ignore tests and protobuf files
  88. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  89. # only check for broken exported godocs
  90. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  91. if [ ! -z "$gowordRes" ]; then
  92. echo -e "goword checking failed:\n${gowordRes}"
  93. exit 255
  94. fi
  95. else
  96. echo "gowrod does not exist... skipping..."
  97. fi
  98. echo "Checking gosimple"
  99. if which gosimple >/dev/null; then
  100. echo "gosimple is installed..."
  101. for path in $GOSIMPLE_UNUSED_PATHS; do
  102. simplResult=`gosimple $REPO_PATH/${path} || true`
  103. if [ -n "${simplResult}" ]; then
  104. echo -e "gosimple checking ${path} failed:\n${simplResult}"
  105. exit 255
  106. fi
  107. done
  108. else
  109. echo "gosimple does not exist... skipping..."
  110. fi
  111. echo "Checking unused"
  112. if which unused >/dev/null; then
  113. echo "unused is installed..."
  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 "unused does not exist... skipping..."
  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|storage)"
  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. # fail fast on static tests
  146. fmt_tests
  147. dep_tests
  148. unit_tests
  149. if [ -n "$INTEGRATION" ]; then
  150. integration_tests
  151. fi
  152. echo "Success"