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