test 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #
  12. # Run code coverage
  13. # COVERDIR=coverage PASSES=cov ./test
  14. set -e
  15. source ./build
  16. # build before setting up test GOPATH
  17. if [[ "${PASSES}" == *"functional"* ]]; then
  18. ./tools/functional-tester/build
  19. fi
  20. # build tests with vendored dependencies
  21. etcd_setup_gopath
  22. if [ -z "$PASSES" ]; then
  23. PASSES="fmt dep compile build unit"
  24. fi
  25. # Invoke ./cover for HTML output
  26. COVER=${COVER:-"-cover"}
  27. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  28. IGNORE_PKGS="(cmd|vendor|etcdserverpb|rafttest|gopath.proto)"
  29. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  30. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  31. FORMATTABLE=`find . -name \*.go | while read a; do echo $(dirname $a)/"*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  32. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  33. if [ -z "$GOARCH" ]; then
  34. GOARCH=$(go env GOARCH);
  35. fi
  36. # user has not provided PKG override
  37. if [ -z "$PKG" ]; then
  38. TEST=$TESTABLE_AND_FORMATTABLE
  39. FMT=$FORMATTABLE
  40. # user has provided PKG override
  41. else
  42. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  43. TEST=${PKG/#./}
  44. TEST=${TEST/#\//}
  45. TEST=${TEST/%\//}
  46. # only run gofmt on packages provided by user
  47. FMT="$TEST"
  48. fi
  49. # split TEST into an array and prepend REPO_PATH to each local package
  50. split=(${TEST// / })
  51. TEST=${split[@]/#/${REPO_PATH}/}
  52. # determine whether target supports race detection
  53. if [ "$GOARCH" == "amd64" ]; then
  54. RACE="--race"
  55. fi
  56. function unit_pass {
  57. echo "Running unit tests..."
  58. # only -run=Test so examples can run in integration tests
  59. go test -timeout 3m ${COVER} ${RACE} -cpu 4 -run=Test $@ ${TEST}
  60. }
  61. function integration_pass {
  62. echo "Running integration tests..."
  63. go test -timeout 15m -v -cpu 4 $@ ${REPO_PATH}/integration
  64. go test -timeout 1m -v ${RACE} -cpu 4 $@ ${REPO_PATH}/client/integration
  65. go test -timeout 10m -v ${RACE} -cpu 4 $@ ${REPO_PATH}/clientv3/integration
  66. go test -timeout 1m -v ${RACE} -cpu 4 -run=Example $@ ${TEST}
  67. }
  68. function functional_pass {
  69. # Clean up any data and logs from previous runs
  70. rm -rf ./agent-*
  71. for a in 1 2 3; do
  72. mkdir -p ./agent-$a
  73. ./bin/etcd-agent -etcd-path ./bin/etcd -etcd-log-dir "./agent-$a" -port ":${a}9027" -use-root=false &
  74. pid="$!"
  75. agent_pids="${agent_pids} $pid"
  76. done
  77. for a in 1 2 3; do
  78. echo "Waiting for 'etcd-agent' on ${a}9027..."
  79. while ! nc -z localhost ${a}9027; do
  80. sleep 1
  81. done
  82. done
  83. echo "Starting 'etcd-tester'"
  84. ./bin/etcd-tester \
  85. -agent-endpoints "127.0.0.1:19027,127.0.0.1:29027,127.0.0.1:39027" \
  86. -client-ports 12379,22379,32379 \
  87. -peer-ports 12380,22380,32380 \
  88. -limit 1 \
  89. -schedule-cases "0 1 2 3 4 5 6 7 8 9" \
  90. -exit-on-failure && echo "'etcd-tester' succeeded"
  91. ETCD_TESTER_EXIT_CODE=$?
  92. echo "ETCD_TESTER_EXIT_CODE:" ${ETCD_TESTER_EXIT_CODE}
  93. echo "Waiting for processes to exit"
  94. kill -s TERM ${agent_pids}
  95. for a in ${agent_pids}; do wait $a || true; done
  96. if [[ "${ETCD_TESTER_EXIT_CODE}" -ne "0" ]]; then
  97. echo "--- FAIL: exit code" ${ETCD_TESTER_EXIT_CODE}
  98. exit ${ETCD_TESTER_EXIT_CODE}
  99. fi
  100. }
  101. function cov_pass {
  102. echo "Running code coverage..."
  103. # install gocovmerge before running code coverage from github.com/wadey/gocovmerge
  104. # gocovmerge merges coverage files
  105. if ! which gocovmerge >/dev/null; then
  106. echo "gocovmerge not installed"
  107. exit 255
  108. fi
  109. if [ -z "$COVERDIR" ]; then
  110. echo "COVERDIR undeclared"
  111. exit 255
  112. fi
  113. mkdir -p "$COVERDIR"
  114. # PKGS_DELIM contains all the core etcd pkgs delimited by ',' which will be profiled for code coverage.
  115. # Integration tests will generate code coverage for those pkgs
  116. PKGS_DELIM=$(echo $TEST | sed 's/ /,/g')
  117. # TODO create coverage to e2e test
  118. PKGS=`echo "$TEST_PKGS" | egrep -v "(e2e|functional-tester)"`
  119. for t in ${PKGS}; do
  120. tf=`echo $t | tr / _`
  121. # uses -run=Test to skip examples because clientv3/ example tests will leak goroutines
  122. go test -covermode=set -coverpkg $PKGS_DELIM -timeout 15m -run=Test -v -coverprofile "$COVERDIR/${tf}.coverprofile" ${REPO_PATH}/$t
  123. done
  124. gocovmerge "$COVERDIR"/*.coverprofile >"$COVERDIR"/cover.out
  125. }
  126. function e2e_pass {
  127. echo "Running e2e tests..."
  128. go test -timeout 10m -v -cpu 4 $@ ${REPO_PATH}/e2e
  129. }
  130. function integration_e2e_pass {
  131. echo "Running integration and e2e tests..."
  132. go test -timeout 10m -v -cpu 4 $@ ${REPO_PATH}/e2e &
  133. e2epid="$!"
  134. go test -timeout 15m -v -cpu 4 $@ ${REPO_PATH}/integration &
  135. intpid="$!"
  136. wait $e2epid
  137. wait $intpid
  138. go test -timeout 1m -v ${RACE} -cpu 4 $@ ${REPO_PATH}/client/integration
  139. go test -timeout 10m -v ${RACE} -cpu 4 $@ ${REPO_PATH}/clientv3/integration
  140. }
  141. function grpcproxy_pass {
  142. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 4 $@ ${REPO_PATH}/integration
  143. }
  144. function release_pass {
  145. rm -f ./bin/etcd-last-release
  146. # to grab latest patch release; bump this up for every minor release
  147. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.0.*" | head -1)
  148. if [ -n "$MANUAL_VER" ]; then
  149. # in case, we need to test against different version
  150. UPGRADE_VER=$MANUAL_VER
  151. fi
  152. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  153. echo "Downloading $file"
  154. set +e
  155. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  156. local result=$?
  157. set -e
  158. case $result in
  159. 0) ;;
  160. 22) return 0
  161. ;;
  162. *) exit $result
  163. ;;
  164. esac
  165. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  166. mkdir -p ./bin
  167. mv /tmp/etcd ./bin/etcd-last-release
  168. }
  169. function fmt_pass {
  170. toggle_failpoints disable
  171. echo "Checking gofmt..."
  172. fmtRes=$(gofmt -l -s -d $FMT)
  173. if [ -n "${fmtRes}" ]; then
  174. echo -e "gofmt checking failed:\n${fmtRes}"
  175. exit 255
  176. fi
  177. echo "Checking govet..."
  178. vetRes=$(go vet $TEST)
  179. if [ -n "${vetRes}" ]; then
  180. echo -e "govet checking failed:\n${vetRes}"
  181. exit 255
  182. fi
  183. echo "Checking 'go tool vet -shadow'..."
  184. for path in $FMT; do
  185. if [ "${path##*.}" != "go" ]; then
  186. path="${path}/*.go"
  187. fi
  188. vetRes=$(go tool vet -shadow ${path})
  189. if [ -n "${vetRes}" ]; then
  190. echo -e "govet -shadow checking ${path} failed:\n${vetRes}"
  191. exit 255
  192. fi
  193. done
  194. echo "Checking for license header..."
  195. licRes=$(for file in $(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*'); do
  196. head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
  197. done;)
  198. if [ -n "${licRes}" ]; then
  199. echo -e "license header checking failed:\n${licRes}"
  200. exit 255
  201. fi
  202. echo "Checking commit titles..."
  203. git log --oneline `git merge-base HEAD master`...HEAD | while read l; do
  204. commitMsg=`echo "$l" | cut -f2- -d' '`
  205. if [[ "$commitMsg" == Merge* ]]; then
  206. # ignore "Merge pull" commits
  207. continue
  208. fi
  209. if [[ "$commitMsg" == Revert* ]]; then
  210. # ignore revert commits
  211. continue
  212. fi
  213. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  214. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  215. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  216. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  217. echo "$l"...
  218. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  219. echo "Got: $l"
  220. exit 255
  221. fi
  222. done
  223. }
  224. function dep_pass {
  225. echo "Checking package dependencies..."
  226. # don't pull in etcdserver package
  227. pushd clientv3 >/dev/null
  228. badpkg="(etcdserver|mvcc)"
  229. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" | egrep -v "${badpkg}/" || echo ""`
  230. popd >/dev/null
  231. if [ ! -z "$deps" ]; then
  232. echo -e "clientv3 has masked dependencies:\n${deps}"
  233. exit 255
  234. fi
  235. }
  236. function compile_pass {
  237. echo "Checking build..."
  238. go build -v ./tools/...
  239. }
  240. # fail fast on static tests
  241. function build_pass {
  242. GO_BUILD_FLAGS="-a -v" etcd_build
  243. }
  244. for pass in $PASSES; do
  245. ${pass}_pass $@
  246. done
  247. echo "Success"