test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 must either be a absolute path or a relative path to the etcd root
  14. # COVERDIR=coverage PASSES="build_cov cov" ./test
  15. set -e
  16. source ./build
  17. # build before setting up test GOPATH
  18. if [[ "${PASSES}" == *"functional"* ]]; then
  19. ./tools/functional-tester/build
  20. fi
  21. # build tests with vendored dependencies
  22. etcd_setup_gopath
  23. if [ -z "$PASSES" ]; then
  24. PASSES="fmt bom dep compile build unit"
  25. fi
  26. USERPKG=${PKG:-}
  27. # Invoke ./cover for HTML output
  28. COVER=${COVER:-"-cover"}
  29. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  30. IGNORE_PKGS="(cmd/|etcdserverpb|rafttest|gopath.proto|v3lockpb|v3electionpb)"
  31. INTEGRATION_PKGS="(integration|e2e|contrib|functional-tester)"
  32. # all github.com/coreos/etcd/whatever pkgs that are not auto-generated / tools
  33. PKGS=`find . -name \*.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | egrep -v "(tools/|contrib/|e2e|pb)" | sed "s|\.|${REPO_PATH}|g" | xargs echo`
  34. # pkg1,pkg2,pkg3
  35. PKGS_COMMA=${PKGS// /,}
  36. TEST_PKGS=`find . -name \*_test.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  37. FORMATTABLE=`find . -name \*.go | while read a; do echo "$(dirname $a)/*.go"; done | sort | uniq | egrep -v "$IGNORE_PKGS" | sed "s|\./||g"`
  38. TESTABLE_AND_FORMATTABLE=`echo "$TEST_PKGS" | egrep -v "$INTEGRATION_PKGS"`
  39. # check if user provided PKG override
  40. if [ -z "${USERPKG}" ]; then
  41. TEST=$TESTABLE_AND_FORMATTABLE
  42. FMT=$FORMATTABLE
  43. else
  44. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  45. TEST=${USERPKG/#./}
  46. TEST=${TEST/#\//}
  47. TEST=${TEST/%\//}
  48. # only run gofmt on packages provided by user
  49. FMT="$TEST"
  50. fi
  51. # split TEST into an array and prepend REPO_PATH to each local package
  52. split=(${TEST// / })
  53. TEST=${split/#/${REPO_PATH}/}
  54. # TODO: 'client' pkg fails with gosimple from generated files
  55. # TODO: 'rafttest' is failing with unused
  56. STATIC_ANALYSIS_PATHS=`find . -name \*.go | while read a; do dirname $a; done | sort | uniq | egrep -v "$IGNORE_PKGS" | grep -v 'client'`
  57. if [ -z "$GOARCH" ]; then
  58. GOARCH=$(go env GOARCH);
  59. fi
  60. # determine whether target supports race detection
  61. if [ "$GOARCH" == "amd64" ]; then
  62. RACE="--race"
  63. fi
  64. function unit_pass {
  65. echo "Running unit tests..."
  66. # only -run=Test so examples can run in integration tests
  67. go test -timeout 3m ${COVER} ${RACE} -cpu 1,2,4 -run=Test $@ ${TEST}
  68. }
  69. function integration_pass {
  70. echo "Running integration tests..."
  71. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration
  72. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  73. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  74. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  75. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  76. }
  77. function functional_pass {
  78. for a in 1 2 3; do
  79. mkdir -p ./agent-$a
  80. ./bin/etcd-agent -etcd-path ./bin/etcd -etcd-log-dir "./agent-$a" -port ":${a}9027" -use-root=false &
  81. pid="$!"
  82. agent_pids="${agent_pids} $pid"
  83. done
  84. for a in 1 2 3; do
  85. echo "Waiting for 'etcd-agent' on ${a}9027..."
  86. while ! nc -z localhost ${a}9027; do
  87. sleep 1
  88. done
  89. done
  90. echo "Starting 'etcd-tester'"
  91. ./bin/etcd-tester \
  92. -agent-endpoints "127.0.0.1:19027,127.0.0.1:29027,127.0.0.1:39027" \
  93. -client-ports 12379,22379,32379 \
  94. -peer-ports 12380,22380,32380 \
  95. -limit 1 \
  96. -schedule-cases "0 1 2 3 4 5" \
  97. -exit-on-failure && echo "'etcd-tester' succeeded"
  98. ETCD_TESTER_EXIT_CODE=$?
  99. echo "ETCD_TESTER_EXIT_CODE:" ${ETCD_TESTER_EXIT_CODE}
  100. echo "Waiting for processes to exit"
  101. kill -s TERM ${agent_pids}
  102. for a in ${agent_pids}; do wait $a || true; done
  103. rm -rf ./agent-*
  104. if [[ "${ETCD_TESTER_EXIT_CODE}" -ne "0" ]]; then
  105. echo "FAIL with exit code" ${ETCD_TESTER_EXIT_CODE}
  106. exit ${ETCD_TESTER_EXIT_CODE}
  107. fi
  108. }
  109. function cov_pass {
  110. echo "Running code coverage..."
  111. # install gocovmerge before running code coverage from github.com/wadey/gocovmerge
  112. # gocovmerge merges coverage files
  113. if ! which gocovmerge >/dev/null; then
  114. echo "gocovmerge not installed"
  115. exit 255
  116. fi
  117. if [ -z "$COVERDIR" ]; then
  118. echo "COVERDIR undeclared"
  119. exit 255
  120. fi
  121. if [ ! -f "bin/etcd_test" ]; then
  122. echo "etcd_test binary not found"
  123. exit 255
  124. fi
  125. mkdir -p "$COVERDIR"
  126. # run code coverage for unit and integration tests
  127. GOCOVFLAGS="-covermode=set -coverpkg $PKGS_COMMA -v -timeout 15m"
  128. failed=""
  129. for t in `echo "${TEST_PKGS}" | egrep -v "(e2e|functional-tester)"`; do
  130. tf=`echo $t | tr / _`
  131. # cache package compilation data for faster repeated builds
  132. go test $GOCOVFLAGS -i ${REPO_PATH}/$t || true
  133. # uses -run=Test to skip examples because clientv3/ example tests will leak goroutines
  134. go test $GOCOVFLAGS -run=Test -coverprofile "$COVERDIR/${tf}.coverprofile" ${REPO_PATH}/$t || failed="$failed $t"
  135. done
  136. # proxy tests
  137. go test -tags cluster_proxy $GOCOVFLAGS -coverprofile "$COVERDIR/proxy_integration.coverprofile" ${REPO_PATH}/integration || failed="$failed proxy-integration"
  138. go test -tags cluster_proxy $GOCOVFLAGS -coverprofile "$COVERDIR/proxy_clientv3.coverprofile" ${REPO_PATH}/clientv3/integration || failed="$failed proxy-clientv3/integration"
  139. # run code coverage for e2e tests
  140. # use 30m timeout because e2e coverage takes longer
  141. # due to many tests cause etcd process to wait
  142. # on leadership transfer timeout during gracefully shutdown
  143. go test -tags cov -timeout 30m -v ${REPO_PATH}"/e2e" || failed="$failed e2e"
  144. gocovmerge "$COVERDIR"/*.coverprofile >"$COVERDIR"/cover.out
  145. # strip out generated files (using GNU-style sed)
  146. sed --in-place '/generated.go/d' "$COVERDIR"/cover.out || true
  147. # held failures to generate the full coverage file, now fail
  148. if [ -n "$failed" ]; then
  149. for f in $failed; do
  150. echo FAIL $f
  151. done
  152. exit 255
  153. fi
  154. }
  155. function e2e_pass {
  156. echo "Running e2e tests..."
  157. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e
  158. }
  159. function integration_e2e_pass {
  160. echo "Running integration and e2e tests..."
  161. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/e2e &
  162. e2epid="$!"
  163. go test -timeout 15m -v -cpu 1,2,4 $@ ${REPO_PATH}/integration &
  164. intpid="$!"
  165. wait $e2epid
  166. wait $intpid
  167. go test -timeout 1m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/client/integration
  168. go test -timeout 10m -v ${RACE} -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  169. go test -timeout 1m -v -cpu 1,2,4 $@ ${REPO_PATH}/contrib/raftexample
  170. go test -timeout 1m -v ${RACE} -cpu 1,2,4 -run=Example $@ ${TEST}
  171. }
  172. function grpcproxy_pass {
  173. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/integration
  174. go test -timeout 15m -v ${RACE} -tags cluster_proxy -cpu 1,2,4 $@ ${REPO_PATH}/clientv3/integration
  175. }
  176. function release_pass {
  177. rm -f ./bin/etcd-last-release
  178. # to grab latest patch release; bump this up for every minor release
  179. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.2.*" | head -1)
  180. if [ -n "$MANUAL_VER" ]; then
  181. # in case, we need to test against different version
  182. UPGRADE_VER=$MANUAL_VER
  183. fi
  184. if [[ -z ${UPGRADE_VER} ]]; then
  185. UPGRADE_VER="v3.2.0"
  186. echo "fallback to" ${UPGRADE_VER}
  187. fi
  188. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  189. echo "Downloading $file"
  190. set +e
  191. curl --fail -L https://github.com/coreos/etcd/releases/download/$UPGRADE_VER/$file -o /tmp/$file
  192. local result=$?
  193. set -e
  194. case $result in
  195. 0) ;;
  196. *) echo "FAIL with" ${result}
  197. exit $result
  198. ;;
  199. esac
  200. tar xzvf /tmp/$file -C /tmp/ --strip-components=1
  201. mkdir -p ./bin
  202. mv /tmp/etcd ./bin/etcd-last-release
  203. }
  204. function fmt_pass {
  205. toggle_failpoints disable
  206. echo "Checking gofmt..."
  207. fmtRes=$(gofmt -l -s -d $FMT)
  208. if [ -n "${fmtRes}" ]; then
  209. echo -e "gofmt checking failed:\n${fmtRes}"
  210. exit 255
  211. fi
  212. echo "Checking govet..."
  213. vetRes=$(go vet $TEST)
  214. if [ -n "${vetRes}" ]; then
  215. echo -e "govet checking failed:\n${vetRes}"
  216. exit 255
  217. fi
  218. echo "Checking 'go tool vet -all -shadow'..."
  219. fmtpkgs=$(echo $FMT | xargs dirname | sort | uniq | sed '/\./d')
  220. vetRes=$(go tool vet -all -shadow ${fmtpkgs} 2>&1 | grep -v '/gw/' || true)
  221. if [ -n "${vetRes}" ]; then
  222. echo -e "govet -all -shadow checking failed:\n${vetRes}"
  223. exit 255
  224. fi
  225. if which shellcheck >/dev/null; then
  226. echo "Checking shellcheck..."
  227. shellcheckResult=$(shellcheck -fgcc build test scripts/* 2>&1 || true)
  228. if [ -n "${shellcheckResult}" ]; then
  229. # mask the most common ones; fix later
  230. SHELLCHECK_MASK="SC(2086|2006|2068|2196|2035|2162|2076)"
  231. errs=$(echo "${shellcheckResult}" | egrep -v "${SHELLCHECK_MASK}" || true)
  232. if [ -n "${errs}" ]; then
  233. echo -e "shellcheck checking failed:\n${shellcheckResult}\n===\nFailed:\n${errs}"
  234. exit 255
  235. fi
  236. suppressed=$(echo "${shellcheckResult}" | cut -f4- -d':' | sort | uniq -c | sort -n)
  237. echo -e "shellcheck suppressed warnings:\n${suppressed}"
  238. fi
  239. fi
  240. echo "Checking documentation style..."
  241. # eschew you
  242. yous=`find . -name \*.md -exec egrep --color "[Yy]ou[r]?[ '.,;]" {} + | grep -v /v2/ || true`
  243. if [ ! -z "$yous" ]; then
  244. echo -e "found 'you' in documentation:\n${yous}"
  245. exit 255
  246. fi
  247. # TODO: check other markdown files when marker handles headers with '[]'
  248. if which marker >/dev/null; then
  249. echo "Checking marker to find broken links..."
  250. markerResult=`marker --skip-http --root ./Documentation 2>&1 || true`
  251. if [ -n "${markerResult}" ]; then
  252. echo -e "marker checking failed:\n${markerResult}"
  253. exit 255
  254. fi
  255. else
  256. echo "Skipping marker..."
  257. fi
  258. if which goword >/dev/null; then
  259. echo "Checking goword..."
  260. # get all go files to process
  261. gofiles=`find $FMT -iname '*.go' 2>/dev/null`
  262. # ignore tests and protobuf files
  263. gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
  264. # only check for broken exported godocs
  265. gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
  266. if [ ! -z "$gowordRes" ]; then
  267. echo -e "goword checking failed:\n${gowordRes}"
  268. exit 255
  269. fi
  270. else
  271. echo "Skipping goword..."
  272. fi
  273. if which gosimple >/dev/null; then
  274. echo "Checking gosimple..."
  275. gosimpleResult=`gosimple ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  276. if [ -n "${gosimpleResult}" ]; then
  277. # TODO: resolve these after go1.8 migration
  278. SIMPLE_CHECK_MASK="S(1024)"
  279. if echo "${gosimpleResult}" | egrep -v "$SIMPLE_CHECK_MASK"; then
  280. echo -e "gosimple checking failed:\n${gosimpleResult}"
  281. exit 255
  282. else
  283. echo -e "gosimple warning:\n${gosimpleResult}"
  284. fi
  285. fi
  286. else
  287. echo "Skipping gosimple..."
  288. fi
  289. if which unused >/dev/null; then
  290. echo "Checking unused..."
  291. unusedResult=`unused ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  292. if [ -n "${unusedResult}" ]; then
  293. echo -e "unused checking failed:\n${unusedResult}"
  294. exit 255
  295. fi
  296. else
  297. echo "Skipping unused..."
  298. fi
  299. if which staticcheck >/dev/null; then
  300. echo "Checking staticcheck..."
  301. staticcheckResult=`staticcheck ${STATIC_ANALYSIS_PATHS} 2>&1 || true`
  302. if [ -n "${staticcheckResult}" ]; then
  303. # TODO: resolve these after go1.8 migration
  304. # See https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck
  305. STATIC_CHECK_MASK="SA(1019|2002)"
  306. if echo "${staticcheckResult}" | egrep -v "$STATIC_CHECK_MASK"; then
  307. echo -e "staticcheck checking failed:\n${staticcheckResult}"
  308. exit 255
  309. else
  310. suppressed=`echo "${staticcheckResult}" | sed 's/ /\n/g' | grep "(SA" | sort | uniq -c`
  311. echo -e "staticcheck suppressed warnings:\n${suppressed}"
  312. fi
  313. fi
  314. else
  315. echo "Skipping staticcheck..."
  316. fi
  317. echo "Checking for license header..."
  318. licRes=""
  319. files=$(find . -type f -iname '*.go' ! -path './cmd/*' ! -path './gopath.proto/*')
  320. for file in $files; do
  321. if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
  322. licRes="${licRes}"$(echo -e " ${file}")
  323. fi
  324. done
  325. if [ -n "${licRes}" ]; then
  326. echo -e "license header checking failed:\n${licRes}"
  327. exit 255
  328. fi
  329. echo "Checking commit titles..."
  330. git log --oneline "$(git merge-base HEAD master)"...HEAD | while read l; do
  331. commitMsg=`echo "$l" | cut -f2- -d' '`
  332. if [[ "$commitMsg" == Merge* ]]; then
  333. # ignore "Merge pull" commits
  334. continue
  335. fi
  336. if [[ "$commitMsg" == Revert* ]]; then
  337. # ignore revert commits
  338. continue
  339. fi
  340. pkgPrefix=`echo "$commitMsg" | cut -f1 -d':'`
  341. spaceCommas=`echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0`
  342. commaSpaces=`echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0`
  343. if [[ `echo $commitMsg | grep -c ":..*"` == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  344. echo "$l"...
  345. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  346. echo "Got: $l"
  347. exit 255
  348. fi
  349. done
  350. }
  351. function bom_pass {
  352. if ! which license-bill-of-materials >/dev/null; then
  353. return
  354. fi
  355. echo "Checking bill of materials..."
  356. license-bill-of-materials \
  357. --override-file bill-of-materials.override.json \
  358. github.com/coreos/etcd github.com/coreos/etcd/etcdctl >bom-now.json || true
  359. if ! diff bill-of-materials.json bom-now.json; then
  360. echo "vendored licenses do not match given bill of materials"
  361. exit 255
  362. fi
  363. rm bom-now.json
  364. }
  365. function dep_pass {
  366. echo "Checking package dependencies..."
  367. # don't pull in etcdserver package
  368. pushd clientv3 >/dev/null
  369. badpkg="(etcdserver$|mvcc$|backend$|grpc-gateway)"
  370. deps=`go list -f '{{ .Deps }}' | sed 's/ /\n/g' | egrep "${badpkg}" || echo ""`
  371. popd >/dev/null
  372. if [ ! -z "$deps" ]; then
  373. echo -e "clientv3 has masked dependencies:\n${deps}"
  374. exit 255
  375. fi
  376. }
  377. function build_cov_pass {
  378. out="bin"
  379. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  380. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcd_test
  381. go test -tags cov -c -covermode=set -coverpkg=$PKGS_COMMA -o ${out}/etcdctl_test ${REPO_PATH}/etcdctl
  382. }
  383. function compile_pass {
  384. echo "Checking build..."
  385. go build -v ./tools/...
  386. }
  387. # fail fast on static tests
  388. function build_pass {
  389. GO_BUILD_FLAGS="-a -v" etcd_build
  390. }
  391. for pass in $PASSES; do
  392. ${pass}_pass $@
  393. done
  394. echo "Success"