test 14 KB

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