test 4.7 KB

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