test 15 KB

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