test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. # Invoke ./cover for HTML output
  13. COVER=${COVER:-"-cover"}
  14. GO_BUILD_FLAGS=-a
  15. source ./build
  16. # Set up gopath so tests use vendored dependencies
  17. export GOPATH=${PWD}/gopath
  18. rm -f $GOPATH/src
  19. mkdir -p $GOPATH
  20. ln -s ${PWD}/cmd/vendor $GOPATH/src
  21. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  22. PKGS=`ls pkg/*/*go | cut -f1,2 -d/ | sort | uniq`
  23. 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"
  24. # TODO: add it to race testing when the issue is resolved
  25. # https://github.com/golang/go/issues/9946
  26. NO_RACE_TESTABLE="rafthttp"
  27. FORMATTABLE="$TESTABLE_AND_FORMATTABLE $NO_RACE_TESTABLE *.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. NO_RACE_TEST=$NO_RACE_TESTABLE
  32. FMT=$FORMATTABLE
  33. # user has provided PKG override
  34. else
  35. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  36. TEST=${PKG/#./}
  37. TEST=${TEST/#\//}
  38. TEST=${TEST/%\//}
  39. # only run gofmt on packages provided by user
  40. FMT="$TEST"
  41. fi
  42. # split TEST into an array and prepend REPO_PATH to each local package
  43. split=(${TEST// / })
  44. TEST=${split[@]/#/${REPO_PATH}/}
  45. split=(${NO_RACE_TEST// / })
  46. NO_RACE_TEST=${split[@]/#/${REPO_PATH}/}
  47. MACHINE_TYPE=$(uname -m)
  48. if [ $MACHINE_TYPE != "armv7l" ]; then
  49. RACE="--race"
  50. fi
  51. function unit_tests {
  52. echo "Running tests..."
  53. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 $@ ${TEST}
  54. go test -timeout 3m ${COVER} -cpu 1,2,4 $@ ${NO_RACE_TEST}
  55. }
  56. function integration_tests {
  57. echo "Running integration tests..."
  58. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  59. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  60. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  61. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  62. }
  63. function fmt_tests {
  64. echo "Checking gofmt..."
  65. fmtRes=$(gofmt -l -s -d $FMT)
  66. if [ -n "${fmtRes}" ]; then
  67. echo -e "gofmt checking failed:\n${fmtRes}"
  68. exit 255
  69. fi
  70. echo "Checking govet..."
  71. vetRes=$(go vet $TEST)
  72. if [ -n "${vetRes}" ]; then
  73. echo -e "govet checking failed:\n${vetRes}"
  74. exit 255
  75. fi
  76. echo "Checking govet -shadow..."
  77. for path in $FMT; do
  78. vetRes=$(go tool vet -shadow ${path})
  79. if [ -n "${vetRes}" ]; then
  80. echo -e "govet checking ${path} failed:\n${vetRes}"
  81. exit 255
  82. fi
  83. done
  84. echo "Checking goword..."
  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. echo "Checking for license header..."
  96. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*'); do
  97. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  98. done;)
  99. if [ -n "${licRes}" ]; then
  100. echo -e "license header checking failed:\n${licRes}"
  101. exit 255
  102. fi
  103. }
  104. function dep_tests {
  105. echo "Checking package dependencies..."
  106. # don't pull in etcdserver package
  107. pushd clientv3 >/dev/null
  108. badpkg="(etcdserver|storage)"
  109. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  110. popd >/dev/null
  111. if [ ! -z "$deps" ]; then
  112. echo -e "clientv3 has masked dependencies:\n${deps}"
  113. exit 255
  114. fi
  115. }
  116. # fail fast on static tests
  117. fmt_tests
  118. dep_tests
  119. unit_tests
  120. if [ -n "$INTEGRATION" ]; then
  121. integration_tests
  122. fi
  123. echo "Success"