test 21 KB

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