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