test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  17. TESTABLE_AND_FORMATTABLE="client clientv3 discovery error etcdctl/ctlv2 etcdctl/ctlv3 etcdmain etcdserver etcdserver/auth etcdserver/etcdhttp etcdserver/etcdhttp/httptypes pkg/fileutil pkg/flags pkg/idutil pkg/ioutil pkg/netutil pkg/osutil pkg/pbutil pkg/types pkg/transport pkg/wait proxy raft snap storage storage/backend store version wal"
  18. # TODO: add it to race testing when the issue is resolved
  19. # https://github.com/golang/go/issues/9946
  20. NO_RACE_TESTABLE="rafthttp"
  21. FORMATTABLE="$TESTABLE_AND_FORMATTABLE $NO_RACE_TESTABLE *.go etcdctl/ integration clientv3/integration e2e alarm"
  22. # user has not provided PKG override
  23. if [ -z "$PKG" ]; then
  24. TEST=$TESTABLE_AND_FORMATTABLE
  25. NO_RACE_TEST=$NO_RACE_TESTABLE
  26. FMT=$FORMATTABLE
  27. # user has provided PKG override
  28. else
  29. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  30. TEST=${PKG/#./}
  31. TEST=${TEST/#\//}
  32. TEST=${TEST/%\//}
  33. # only run gofmt on packages provided by user
  34. FMT="$TEST"
  35. fi
  36. # split TEST into an array and prepend REPO_PATH to each local package
  37. split=(${TEST// / })
  38. TEST=${split[@]/#/${REPO_PATH}/}
  39. split=(${NO_RACE_TEST// / })
  40. NO_RACE_TEST=${split[@]/#/${REPO_PATH}/}
  41. function unit_tests {
  42. echo "Running tests..."
  43. MACHINE_TYPE=$(uname -m)
  44. if [ $MACHINE_TYPE != "armv7l" ]; then
  45. RACE="--race"
  46. fi
  47. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 $@ ${TEST}
  48. go test -timeout 3m ${COVER} -cpu 1,2,4 $@ ${NO_RACE_TEST}
  49. }
  50. function integration_tests {
  51. echo "Running integration tests..."
  52. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  53. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  54. go test -timeout 10m -v -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  55. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  56. }
  57. function fmt_tests {
  58. echo "Checking gofmt..."
  59. fmtRes=$(gofmt -l -s -d $FMT)
  60. if [ -n "${fmtRes}" ]; then
  61. echo -e "gofmt checking failed:\n${fmtRes}"
  62. exit 255
  63. fi
  64. echo "Checking govet..."
  65. vetRes=$(go vet $TEST)
  66. if [ -n "${vetRes}" ]; then
  67. echo -e "govet checking failed:\n${vetRes}"
  68. exit 255
  69. fi
  70. echo "Checking govet -shadow..."
  71. for path in $FMT; do
  72. vetRes=$(go tool vet -shadow ${path})
  73. if [ -n "${vetRes}" ]; then
  74. echo -e "govet checking ${path} failed:\n${vetRes}"
  75. exit 255
  76. fi
  77. done
  78. echo "Checking goword..."
  79. # get all go files to process
  80. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  81. # ignore tests and protobuf files
  82. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  83. # only check for broken exported godocs
  84. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  85. if [ ! -z "$gowordRes" ]; then
  86. echo -e "goword checking failed:\n${gowordRes}"
  87. exit 255
  88. fi
  89. echo "Checking for license header..."
  90. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './vendor/*'); do
  91. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  92. done;)
  93. if [ -n "${licRes}" ]; then
  94. echo -e "license header checking failed:\n${licRes}"
  95. exit 255
  96. fi
  97. }
  98. function dep_tests {
  99. echo "Checking package dependencies..."
  100. # don't pull in etcdserver package
  101. pushd clientv3 >/dev/null
  102. badpkg="(etcdserver|storage)"
  103. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  104. popd >/dev/null
  105. if [ ! -z "$deps" ]; then
  106. echo -e "clientv3 has masked dependencies:\n${deps}"
  107. exit 255
  108. fi
  109. }
  110. # fail fast on static tests
  111. fmt_tests
  112. dep_tests
  113. unit_tests
  114. if [ -n "$INTEGRATION" ]; then
  115. integration_tests
  116. fi
  117. echo "Success"