test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #!/usr/bin/env bash
  2. #
  3. # Run all etcd tests
  4. # ./test
  5. # ./test -v
  6. #
  7. #
  8. # Run specified test pass
  9. #
  10. # $ PASSES=unit ./test
  11. # $ PASSES=integration ./test
  12. #
  13. #
  14. # Run tests for one package
  15. # Each pass has different default timeout, if you just run tests in one package or 1 test case then you can set TIMEOUT
  16. # flag for different expectation
  17. #
  18. # $ PASSES=unit PKG=./wal TIMEOUT=1m ./test
  19. # $ PASSES=integration PKG=client/integration TIMEOUT=1m ./test
  20. #
  21. #
  22. # Run specified unit tests in one package
  23. # To run all the tests with prefix of "TestNew", set "TESTCASE=TestNew ";
  24. # to run only "TestNew", set "TESTCASE="\bTestNew\b""
  25. #
  26. # $ PASSES=unit PKG=./wal TESTCASE=TestNew TIMEOUT=1m ./test
  27. # $ PASSES=unit PKG=./wal TESTCASE="\bTestNew\b" TIMEOUT=1m ./test
  28. # $ PASSES=integration PKG=client/integration TESTCASE="\bTestV2NoRetryEOF\b" TIMEOUT=1m ./test
  29. #
  30. #
  31. # Run code coverage
  32. # COVERDIR must either be a absolute path or a relative path to the etcd root
  33. # $ COVERDIR=coverage PASSES="build_cov cov" ./test
  34. set -e
  35. source ./build
  36. # build before setting up test GOPATH
  37. if [[ "${PASSES}" == *"functional"* ]]; then
  38. ./functional/build
  39. fi
  40. if [ -z "$PASSES" ]; then
  41. PASSES="fmt bom dep build unit"
  42. fi
  43. USERPKG=${PKG:-}
  44. # Invoke ./tests/cover.test.bash for HTML output
  45. COVER=${COVER:-"-cover"}
  46. # Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
  47. IGNORE_PKGS="(vendor/|etcdserverpb|rafttest|gopath.proto|v3lockpb|v3electionpb)"
  48. INTEGRATION_PKGS="(integration|tests/e2e|contrib|functional)"
  49. # all github.com/etcd-io/etcd/whatever pkgs that are not auto-generated / tools
  50. # shellcheck disable=SC1117
  51. PKGS=$(find . -name \*.go | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | grep -vE "(tools/|contrib/|tests/e2e|pb)" | sed "s|\.|${REPO_PATH}|g" | xargs echo)
  52. # pkg1,pkg2,pkg3
  53. PKGS_COMMA=${PKGS// /,}
  54. # shellcheck disable=SC1117
  55. TEST_PKGS=$(find . -name \*_test.go | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | sed "s|\./||g")
  56. # shellcheck disable=SC1117
  57. FORMATTABLE=$(find . -name \*.go | while read -r a; do echo "$(dirname "$a")/*.go"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | sed "s|\./||g")
  58. TESTABLE_AND_FORMATTABLE=$(echo "$TEST_PKGS" | grep -vE "$INTEGRATION_PKGS")
  59. # check if user provided PKG override
  60. if [ -z "${USERPKG}" ]; then
  61. TEST=$TESTABLE_AND_FORMATTABLE
  62. FMT=$FORMATTABLE
  63. else
  64. # strip out leading dotslashes and trailing slashes from PKG=./foo/
  65. TEST=${USERPKG/#./}
  66. TEST=${TEST/#\//}
  67. TEST=${TEST/%\//}
  68. # only run gofmt on packages provided by user
  69. FMT="$TEST"
  70. fi
  71. # shellcheck disable=SC2206
  72. FMT=($FMT)
  73. if [ "${VERBOSE}" == "1" ]; then
  74. # shellcheck disable=SC2128
  75. echo "Running with FMT:" "${FMT[@]}"
  76. fi
  77. # prepend REPO_PATH to each local package
  78. split=$TEST
  79. TEST=""
  80. for a in $split; do TEST="$TEST ${REPO_PATH}/${a}"; done
  81. # shellcheck disable=SC2206
  82. TEST=($TEST)
  83. if [ "${VERBOSE}" == "1" ]; then
  84. # shellcheck disable=SC2128
  85. echo "Running with TEST:" "${TEST[@]}"
  86. fi
  87. # TODO: 'client' pkg fails with gosimple from generated files
  88. # TODO: 'rafttest' is failing with unused
  89. STATIC_ANALYSIS_PATHS=$(find . -name \*.go ! -path './vendor/*' ! -path './gopath.proto/*' ! -path '*pb/*' | while read -r a; do dirname "$a"; done | sort | uniq | grep -vE "$IGNORE_PKGS" | grep -v 'client')
  90. # shellcheck disable=SC2206
  91. STATIC_ANALYSIS_PATHS=($STATIC_ANALYSIS_PATHS)
  92. if [ "${VERBOSE}" == "1" ]; then
  93. # shellcheck disable=SC2128
  94. echo "Running with STATIC_ANALYSIS_PATHS:" "${STATIC_ANALYSIS_PATHS[@]}"
  95. fi
  96. if [ -z "$GOARCH" ]; then
  97. GOARCH=$(go env GOARCH);
  98. fi
  99. # determine the number of CPUs to use for Go tests
  100. TEST_CPUS="1,2,4"
  101. if [ ! -z "${CPU}" ]; then
  102. TEST_CPUS="${CPU}"
  103. fi
  104. echo "Running with TEST_CPUS:" "${TEST_CPUS}"
  105. # determine whether target supports race detection
  106. if [ "$GOARCH" == "amd64" ]; then
  107. RACE="--race"
  108. fi
  109. RUN_ARG=""
  110. if [ ! -z "${TESTCASE}" ]; then
  111. RUN_ARG="-run=${TESTCASE}"
  112. fi
  113. function unit_pass {
  114. echo "Running unit tests..."
  115. GO_TEST_FLAG=""
  116. if [ "${VERBOSE}" == "1" ]; then
  117. GO_TEST_FLAG="-v"
  118. fi
  119. if [ "${VERBOSE}" == "2" ]; then
  120. GO_TEST_FLAG="-v"
  121. export CLIENT_DEBUG=1
  122. fi
  123. if [ "${RUN_ARG}" == "" ]; then
  124. RUN_ARG="-run=Test"
  125. fi
  126. # check if user provided time out, especially useful when just run one test case
  127. # expectation could be different
  128. USERTIMEOUT=""
  129. if [ -z "${TIMEOUT}" ]; then
  130. USERTIMEOUT="3m"
  131. else
  132. USERTIMEOUT="${TIMEOUT}"
  133. fi
  134. go test ${GO_TEST_FLAG} -timeout "${USERTIMEOUT}" "${COVER}" ${RACE} -cpu "${TEST_CPUS}" ${RUN_ARG} "$@" "${TEST[@]}"
  135. }
  136. function integration_pass {
  137. echo "Running integration tests..."
  138. # check if user provided time out, especially useful when just run one test case
  139. # expectation could be different
  140. USERTIMEOUT=""
  141. if [ -z "${TIMEOUT}" ]; then
  142. USERTIMEOUT="20m"
  143. else
  144. USERTIMEOUT="${TIMEOUT}"
  145. fi
  146. # if TESTCASE and PKG set, run specified test case in specified PKG
  147. # if TESTCASE set, PKG not set, run specified test case in all integration and integration_extra packages
  148. # if TESTCASE not set, PKG set, run all test cases in specified package
  149. # if TESTCASE not set, PKG not set, run all tests in all integration and integration_extra packages
  150. if [ -z "${TESTCASE}" ] && [ -z "${USERPKG}" ]; then
  151. go test -timeout "${USERTIMEOUT}" -v -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/integration"
  152. integration_extra "$@"
  153. else
  154. if [ -z "${USERPKG}" ]; then
  155. INTEGTESTPKG=("${REPO_PATH}/integration"
  156. "${REPO_PATH}/client/integration"
  157. "${REPO_PATH}/clientv3/integration"
  158. "${REPO_PATH}/contrib/raftexample"
  159. "${REPO_PATH}/store")
  160. else
  161. INTEGTESTPKG=("${TEST[@]}")
  162. fi
  163. go test -timeout "${USERTIMEOUT}" -v -cpu "${TEST_CPUS}" "${RUN_ARG}" "$@" "${INTEGTESTPKG[@]}"
  164. fi
  165. }
  166. function integration_extra {
  167. go test -timeout 1m -v ${RACE} -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/client/integration"
  168. go test -timeout 25m -v ${RACE} -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/clientv3/integration"
  169. go test -timeout 1m -v -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/contrib/raftexample"
  170. go test -timeout 5m -v ${RACE} -tags v2v3 "$@" "${REPO_PATH}/etcdserver/api/v2store"
  171. go test -timeout 1m -v ${RACE} -cpu "${TEST_CPUS}" -run=Example "$@" "${TEST[@]}"
  172. }
  173. function functional_pass {
  174. # Clean up any data and logs from previous runs
  175. rm -rf /tmp/etcd-functional-* /tmp/etcd-functional-*.backup
  176. for a in 1 2 3; do
  177. ./bin/etcd-agent --network tcp --address 127.0.0.1:${a}9027 &
  178. pid="$!"
  179. agent_pids="${agent_pids} $pid"
  180. done
  181. for a in 1 2 3; do
  182. echo "Waiting for 'etcd-agent' on ${a}9027..."
  183. while ! nc -z localhost ${a}9027; do
  184. sleep 1
  185. done
  186. done
  187. echo "functional test START!"
  188. ./bin/etcd-tester --config ./functional.yaml && echo "'etcd-tester' succeeded"
  189. ETCD_TESTER_EXIT_CODE=$?
  190. echo "ETCD_TESTER_EXIT_CODE:" ${ETCD_TESTER_EXIT_CODE}
  191. # shellcheck disable=SC2206
  192. agent_pids=($agent_pids)
  193. kill -s TERM "${agent_pids[@]}" || true
  194. if [[ "${ETCD_TESTER_EXIT_CODE}" -ne "0" ]]; then
  195. echo "--- FAIL: exit code" ${ETCD_TESTER_EXIT_CODE}
  196. exit ${ETCD_TESTER_EXIT_CODE}
  197. fi
  198. echo "functional test PASS!"
  199. }
  200. function cov_pass {
  201. echo "Running code coverage..."
  202. # install gocovmerge before running code coverage from github.com/wadey/gocovmerge
  203. # gocovmerge merges coverage files
  204. if ! which gocovmerge >/dev/null; then
  205. echo "gocovmerge not installed"
  206. exit 255
  207. fi
  208. if [ -z "$COVERDIR" ]; then
  209. echo "COVERDIR undeclared"
  210. exit 255
  211. fi
  212. if [ ! -f "bin/etcd_test" ]; then
  213. echo "etcd_test binary not found"
  214. exit 255
  215. fi
  216. mkdir -p "$COVERDIR"
  217. # run code coverage for unit and integration tests
  218. GOCOVFLAGS="-covermode=set -coverpkg ${PKGS_COMMA} -v -timeout 20m"
  219. # shellcheck disable=SC2206
  220. GOCOVFLAGS=($GOCOVFLAGS)
  221. failed=""
  222. for t in $(echo "${TEST_PKGS}" | grep -vE "(tests/e2e|functional)"); do
  223. tf=$(echo "$t" | tr / _)
  224. # cache package compilation data for faster repeated builds
  225. go test "${GOCOVFLAGS[@]}" -i "${REPO_PATH}/$t" || true
  226. # uses -run=Test to skip examples because clientv3/ example tests will leak goroutines
  227. go test "${GOCOVFLAGS[@]}" -run=Test -coverprofile "$COVERDIR/${tf}.coverprofile" "${REPO_PATH}/$t" || failed="$failed $t"
  228. done
  229. # v2v3 tests
  230. go test -tags v2v3 "${GOCOVFLAGS[@]}" -coverprofile "$COVERDIR/store-v2v3.coverprofile" "${REPO_PATH}/clientv3/integration" || failed="$failed store-v2v3"
  231. # proxy tests
  232. go test -tags cluster_proxy "${GOCOVFLAGS[@]}" -coverprofile "$COVERDIR/proxy_integration.coverprofile" "${REPO_PATH}/integration" || failed="$failed proxy-integration"
  233. go test -tags cluster_proxy "${GOCOVFLAGS[@]}" -coverprofile "$COVERDIR/proxy_clientv3.coverprofile" "${REPO_PATH}/clientv3/integration" || failed="$failed proxy-clientv3/integration"
  234. # run code coverage for e2e tests
  235. # use 30m timeout because e2e coverage takes longer
  236. # due to many tests cause etcd process to wait
  237. # on leadership transfer timeout during gracefully shutdown
  238. echo Testing tests/e2e without proxy...
  239. go test -tags cov -timeout 30m -v "${REPO_PATH}/tests/e2e" || failed="$failed tests/e2e"
  240. echo Testing tests/e2e with proxy...
  241. go test -tags "cov cluster_proxy" -timeout 30m -v "${REPO_PATH}/tests/e2e" || failed="$failed tests/e2e-proxy"
  242. # incrementally merge to get coverage data even if some coverage files are corrupted
  243. # optimistically assume etcdserver package's coverage file is OK since gocovmerge
  244. # expects to start with a non-empty file
  245. cp "$COVERDIR"/etcdserver.coverprofile "$COVERDIR"/cover.out
  246. for f in "$COVERDIR"/*.coverprofile; do
  247. echo "merging test coverage file ${f}"
  248. gocovmerge "$f" "$COVERDIR"/cover.out >"$COVERDIR"/cover.tmp || failed="$failed $f"
  249. if [ -s "$COVERDIR"/cover.tmp ]; then
  250. mv "$COVERDIR"/cover.tmp "$COVERDIR"/cover.out
  251. fi
  252. done
  253. # strip out generated files (using GNU-style sed)
  254. sed --in-place '/generated.go/d' "$COVERDIR"/cover.out || true
  255. # held failures to generate the full coverage file, now fail
  256. if [ -n "$failed" ]; then
  257. for f in $failed; do
  258. echo "--- FAIL:" "$f"
  259. done
  260. exit 255
  261. fi
  262. }
  263. function e2e_pass {
  264. echo "Running e2e tests..."
  265. # check if user provided time out, especially useful when just run one test case
  266. # expectation could be different
  267. USERTIMEOUT=""
  268. if [ -z "${TIMEOUT}" ]; then
  269. USERTIMEOUT="20m"
  270. else
  271. USERTIMEOUT="${TIMEOUT}"
  272. fi
  273. go test -timeout "${USERTIMEOUT}" -v -cpu "${TEST_CPUS}" "${RUN_ARG}" "$@" "${REPO_PATH}/tests/e2e"
  274. }
  275. function integration_e2e_pass {
  276. echo "Running integration and e2e tests..."
  277. go test -timeout 20m -v -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/tests/e2e" &
  278. e2epid="$!"
  279. go test -timeout 20m -v -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/integration" &
  280. intpid="$!"
  281. wait $e2epid
  282. wait $intpid
  283. integration_extra "$@"
  284. }
  285. function grpcproxy_pass {
  286. go test -timeout 20m -v ${RACE} -tags cluster_proxy -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/integration"
  287. go test -timeout 20m -v ${RACE} -tags cluster_proxy -cpu "${TEST_CPUS}" "$@" "${REPO_PATH}/clientv3/integration"
  288. go test -timeout 20m -v -tags cluster_proxy "$@" "${REPO_PATH}/tests/e2e"
  289. }
  290. function release_pass {
  291. rm -f ./bin/etcd-last-release
  292. # to grab latest patch release; bump this up for every minor release
  293. UPGRADE_VER=$(git tag -l --sort=-version:refname "v3.3.*" | head -1)
  294. if [ -n "$MANUAL_VER" ]; then
  295. # in case, we need to test against different version
  296. UPGRADE_VER=$MANUAL_VER
  297. fi
  298. if [[ -z ${UPGRADE_VER} ]]; then
  299. UPGRADE_VER="v3.3.0"
  300. echo "fallback to" ${UPGRADE_VER}
  301. fi
  302. local file="etcd-$UPGRADE_VER-linux-$GOARCH.tar.gz"
  303. echo "Downloading $file"
  304. set +e
  305. curl --fail -L "https://github.com/etcd-io/etcd/releases/download/$UPGRADE_VER/$file" -o "/tmp/$file"
  306. local result=$?
  307. set -e
  308. case $result in
  309. 0) ;;
  310. *) echo "--- FAIL:" ${result}
  311. exit $result
  312. ;;
  313. esac
  314. tar xzvf "/tmp/$file" -C /tmp/ --strip-components=1
  315. mkdir -p ./bin
  316. mv /tmp/etcd ./bin/etcd-last-release
  317. }
  318. function shellcheck_pass {
  319. if which shellcheck >/dev/null; then
  320. shellcheckResult=$(shellcheck -fgcc build test scripts/*.sh 2>&1 || true)
  321. if [ -n "${shellcheckResult}" ]; then
  322. echo -e "shellcheck checking failed:\\n${shellcheckResult}"
  323. exit 255
  324. fi
  325. fi
  326. }
  327. function markdown_you_pass {
  328. # eschew you
  329. yous=$(find . -name \*.md ! -path './vendor/*' ! -path './Documentation/v2/*' ! -path './gopath.proto/*' -exec grep -E --color "[Yy]ou[r]?[ '.,;]" {} + || true)
  330. if [ ! -z "$yous" ]; then
  331. echo -e "found 'you' in documentation:\\n${yous}"
  332. exit 255
  333. fi
  334. }
  335. function markdown_marker_pass {
  336. # TODO: check other markdown files when marker handles headers with '[]'
  337. if which marker >/dev/null; then
  338. markerResult=$(marker --skip-http --root ./Documentation 2>&1 || true)
  339. if [ -n "${markerResult}" ]; then
  340. echo -e "marker checking failed:\\n${markerResult}"
  341. exit 255
  342. fi
  343. else
  344. echo "Skipping marker..."
  345. fi
  346. }
  347. function goword_pass {
  348. if which goword >/dev/null; then
  349. # get all go files to process
  350. gofiles=$(find "${FMT[@]}" -iname '*.go' 2>/dev/null)
  351. # shellcheck disable=SC2206
  352. gofiles_all=($gofiles)
  353. # ignore tests and protobuf files
  354. # shellcheck disable=SC1117
  355. gofiles=$(echo "${gofiles_all[@]}" | sort | uniq | sed "s/ /\n/g" | grep -vE "(\\_test.go|\\.pb\\.go)")
  356. # shellcheck disable=SC2206
  357. gofiles=($gofiles)
  358. # only check for broken exported godocs
  359. gowordRes=$(goword -use-spell=false "${gofiles[@]}" | grep godoc-export | sort)
  360. if [ ! -z "$gowordRes" ]; then
  361. echo -e "goword checking failed:\\n${gowordRes}"
  362. exit 255
  363. fi
  364. # check some spelling
  365. gowordRes=$(goword -ignore-file=.words clientv3/{*,*/*}.go 2>&1 | grep spell | sort)
  366. if [ ! -z "$gowordRes" ]; then
  367. echo -e "goword checking failed:\\n${gowordRes}"
  368. exit 255
  369. fi
  370. else
  371. echo "Skipping goword..."
  372. fi
  373. }
  374. function gofmt_pass {
  375. fmtRes=$(gofmt -l -s -d "${FMT[@]}")
  376. if [ -n "${fmtRes}" ]; then
  377. echo -e "gofmt checking failed:\\n${fmtRes}"
  378. exit 255
  379. fi
  380. }
  381. function govet_pass {
  382. vetRes=$(go vet "${TEST[@]}")
  383. if [ -n "${vetRes}" ]; then
  384. echo -e "govet checking failed:\\n${vetRes}"
  385. exit 255
  386. fi
  387. }
  388. function govet_shadow_pass {
  389. fmtpkgs=$(for a in "${FMT[@]}"; do dirname "$a"; done | sort | uniq | grep -v "\\.")
  390. # shellcheck disable=SC2206
  391. fmtpkgs=($fmtpkgs)
  392. vetRes=$(go tool vet -all -shadow "${fmtpkgs[@]}" 2>&1 | grep -v '/gw/' || true)
  393. if [ -n "${vetRes}" ]; then
  394. echo -e "govet -all -shadow checking failed:\\n${vetRes}"
  395. exit 255
  396. fi
  397. }
  398. function gosimple_pass {
  399. if which gosimple >/dev/null; then
  400. gosimpleResult=$(gosimple "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  401. if [ -n "${gosimpleResult}" ]; then
  402. echo -e "gosimple checking failed:\\n${gosimpleResult}"
  403. exit 255
  404. fi
  405. else
  406. echo "Skipping gosimple..."
  407. fi
  408. }
  409. function unused_pass {
  410. if which unused >/dev/null; then
  411. unusedResult=$(unused "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  412. if [ -n "${unusedResult}" ]; then
  413. echo -e "unused checking failed:\\n${unusedResult}"
  414. exit 255
  415. fi
  416. else
  417. echo "Skipping unused..."
  418. fi
  419. }
  420. function unparam_pass {
  421. if which unparam >/dev/null; then
  422. unparamResult=$(unparam "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  423. if [ -n "${unparamResult}" ]; then
  424. echo -e "unparam checking failed:\\n${unparamResult}"
  425. exit 255
  426. fi
  427. else
  428. echo "Skipping unparam..."
  429. fi
  430. }
  431. function staticcheck_pass {
  432. if which staticcheck >/dev/null; then
  433. staticcheckResult=$(staticcheck "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  434. if [ -n "${staticcheckResult}" ]; then
  435. # TODO: resolve these after go1.8 migration
  436. # See https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck
  437. STATIC_CHECK_MASK="SA(1012|1019|2002)"
  438. if echo "${staticcheckResult}" | grep -vE "$STATIC_CHECK_MASK"; then
  439. echo -e "staticcheck checking failed:\\n${staticcheckResult}"
  440. exit 255
  441. else
  442. suppressed=$(echo "${staticcheckResult}" | sed 's/ /\n/g' | grep "(SA" | sort | uniq -c)
  443. echo -e "staticcheck suppressed warnings:\\n${suppressed}"
  444. fi
  445. fi
  446. else
  447. echo "Skipping staticcheck..."
  448. fi
  449. }
  450. function revive_pass {
  451. if which revive >/dev/null; then
  452. reviveResult=$(revive -config ./tests/revive.toml -exclude "vendor/..." ./... 2>&1 || true)
  453. if [ -n "${reviveResult}" ]; then
  454. echo -e "revive checking failed:\\n${reviveResult}"
  455. exit 255
  456. fi
  457. else
  458. echo "Skipping revive..."
  459. fi
  460. }
  461. function unconvert_pass {
  462. if which unconvert >/dev/null; then
  463. unconvertResult=$(unconvert -v "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  464. if [ -n "${unconvertResult}" ]; then
  465. echo -e "unconvert checking failed:\\n${unconvertResult}"
  466. exit 255
  467. fi
  468. else
  469. echo "Skipping unconvert..."
  470. fi
  471. }
  472. function ineffassign_pass {
  473. if which ineffassign >/dev/null; then
  474. ineffassignResult=$(ineffassign "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  475. if [ -n "${ineffassignResult}" ]; then
  476. echo -e "ineffassign checking failed:\\n${ineffassignResult}"
  477. exit 255
  478. fi
  479. else
  480. echo "Skipping ineffassign..."
  481. fi
  482. }
  483. function nakedret_pass {
  484. if which nakedret >/dev/null; then
  485. nakedretResult=$(nakedret "${STATIC_ANALYSIS_PATHS[@]}" 2>&1 || true)
  486. if [ -n "${nakedretResult}" ]; then
  487. echo -e "nakedret checking failed:\\n${nakedretResult}"
  488. exit 255
  489. fi
  490. else
  491. echo "Skipping nakedret..."
  492. fi
  493. }
  494. function license_header_pass {
  495. licRes=""
  496. files=$(find . -type f -iname '*.go' ! -path './vendor/*' ! -path './gopath.proto/*')
  497. for file in $files; do
  498. if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
  499. licRes="${licRes}"$(echo -e " ${file}")
  500. fi
  501. done
  502. if [ -n "${licRes}" ]; then
  503. echo -e "license header checking failed:\\n${licRes}"
  504. exit 255
  505. fi
  506. }
  507. function receiver_name_pass {
  508. # shellcheck disable=SC1117
  509. recvs=$(grep 'func ([^*]' {*,*/*,*/*/*}.go | grep -Ev "(generated|pb/)" | tr ':' ' ' | \
  510. awk ' { print $2" "$3" "$4" "$1 }' | sed "s/[a-zA-Z\.]*go//g" | sort | uniq | \
  511. grep -Ev "(Descriptor|Proto|_)" | awk ' { print $3" "$4 } ' | sort | uniq -c | grep -v ' 1 ' | awk ' { print $2 } ')
  512. if [ -n "${recvs}" ]; then
  513. # shellcheck disable=SC2206
  514. recvs=($recvs)
  515. for recv in "${recvs[@]}"; do
  516. echo "Mismatched receiver for $recv..."
  517. grep "$recv" "${FMT[@]}" | grep 'func ('
  518. done
  519. exit 255
  520. fi
  521. }
  522. function commit_title_pass {
  523. git log --oneline "$(git merge-base HEAD master)"...HEAD | while read -r l; do
  524. commitMsg=$(echo "$l" | cut -f2- -d' ')
  525. if [[ "$commitMsg" == Merge* ]]; then
  526. # ignore "Merge pull" commits
  527. continue
  528. fi
  529. if [[ "$commitMsg" == Revert* ]]; then
  530. # ignore revert commits
  531. continue
  532. fi
  533. pkgPrefix=$(echo "$commitMsg" | cut -f1 -d':')
  534. spaceCommas=$(echo "$commitMsg" | sed 's/ /\n/g' | grep -c ',$' || echo 0)
  535. commaSpaces=$(echo "$commitMsg" | sed 's/,/\n/g' | grep -c '^ ' || echo 0)
  536. if [[ $(echo "$commitMsg" | grep -c ":..*") == 0 || "$commitMsg" == "$pkgPrefix" || "$spaceCommas" != "$commaSpaces" ]]; then
  537. echo "$l"...
  538. echo "Expected commit title format '<package>{\", \"<package>}: <description>'"
  539. echo "Got: $l"
  540. exit 255
  541. fi
  542. done
  543. }
  544. function fmt_pass {
  545. toggle_failpoints disable
  546. for p in shellcheck \
  547. markdown_you \
  548. markdown_marker \
  549. goword \
  550. gofmt \
  551. govet \
  552. govet_shadow \
  553. gosimple \
  554. unused \
  555. unparam \
  556. staticcheck \
  557. revive \
  558. unconvert \
  559. ineffassign \
  560. nakedret \
  561. license_header \
  562. receiver_name \
  563. commit_title \
  564. ; do
  565. echo "'$p' started at $(date)"
  566. "${p}"_pass "$@"
  567. echo "'$p' completed at $(date)"
  568. done
  569. }
  570. function bom_pass {
  571. if ! which license-bill-of-materials >/dev/null; then
  572. return
  573. fi
  574. echo "Checking bill of materials..."
  575. license-bill-of-materials \
  576. --override-file bill-of-materials.override.json \
  577. go.etcd.io/etcd go.etcd.io/etcd/etcdctl >bom-now.json || true
  578. if ! diff bill-of-materials.json bom-now.json; then
  579. echo "vendored licenses do not match given bill of materials"
  580. exit 255
  581. fi
  582. rm bom-now.json
  583. }
  584. function dep_pass {
  585. echo "Checking package dependencies..."
  586. # don't pull in etcdserver package
  587. pushd clientv3 >/dev/null
  588. badpkg="(etcdserver$|mvcc$|backend$|grpc-gateway)"
  589. deps=$(go list -f '{{ .Deps }}' | sed 's/ /\n/g' | grep -E "${badpkg}" || echo "")
  590. popd >/dev/null
  591. if [ ! -z "$deps" ]; then
  592. echo -e "clientv3 has masked dependencies:\\n${deps}"
  593. exit 255
  594. fi
  595. }
  596. function build_cov_pass {
  597. out="bin"
  598. if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi
  599. go test -tags cov -c -covermode=set -coverpkg="$PKGS_COMMA" -o "${out}/etcd_test"
  600. go test -tags cov -c -covermode=set -coverpkg="$PKGS_COMMA" -o "${out}/etcdctl_test" "${REPO_PATH}/etcdctl"
  601. }
  602. # fail fast on static tests
  603. function build_pass {
  604. echo "Checking build..."
  605. GO_BUILD_FLAGS="-v" etcd_build
  606. GO_BUILD_FLAGS="-v" tools_build
  607. }
  608. for pass in $PASSES; do
  609. echo "Starting '$pass' pass at $(date)"
  610. "${pass}"_pass "$@"
  611. echo "Finished '$pass' pass at $(date)"
  612. done
  613. echo "Success"