test 19 KB

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