test 14 KB

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