test 19 KB

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