-
Notifications
You must be signed in to change notification settings - Fork 92
/
test.sh
executable file
·223 lines (181 loc) · 4.15 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env bash
# this script is specified as the 'tests' task in .circleci/config.yml
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" # get dir containing this script
cd $DIR # always from from script dir
# set error on return if any part of a pipe command fails
set -o pipefail
# base go test command
GO_TEST="go test -v -timeout=10m"
# list modules for CI testing
function listModules() {
go list ./... | grep -Ev 'Utilities|elections|longTest|peerTest|simTest|activations|netTest'
}
# formatted list of simTest/<testname>
function listSimTest() {
go test --list=Test ./simTest/... | awk '/^Test/ { print "simTest/"$1 }'
}
# list of A/B Peer tests that need to be run concurrently
function listPeer() {
ls peerTest/*A_test.go
}
# read a list of tests from a file called .ci_tests
function hardcodedList() {
cat .circleci/ci_tests
}
# load a list of tests to execute
function loadTestList() {
case $1 in
unittest) # run unit test in batches - manually triggered
TESTS=$({
listModules
})
;;
peertest) # run only peer tests - manually triggered
TESTS=$({
ls peerTest/*A_test.go
})
;;
simtest) # run only simulation tests - manually triggered
TESTS=$({
listSimTest
})
;;
short) # triggered on every commit
# run on circle
if [[ "${CI}x" != "x" ]]; then
TESTS=$({
listModules
echo "simTest/TestAnElection"
} | circleci tests split) # circleci helper spreads tests across containers
else # run locally
TESTS=$({
listModules
echo "simTest/TestAnElection"
})
fi
;;
full) # run on circle triggered on develop branch nightly
if [[ "${CI}x" != "x" ]]; then
# Just run failing tests if .circleci/ci_tests file is present
if [[ -f .circleci/ci_tests ]]; then
TESTS=$({
hardcodedList # limit the tests to a hardcoded list
} | circleci tests split)
else
TESTS=$({
listModules
listSimTest
listPeer
} | circleci tests split) # circleci helper spreads tests across containers
fi
else # run locally
TESTS=$({
listModules
listSimTest
listPeer
})
fi
;;
*)
echo "Unknown option '$1'"
echo "usage: ./test.sh [unittest|peertest|simtest|short|full]"
exit -1
;;
esac
}
function testGoFmt() {
FILES=$(find . -name '*.go')
for FILE in ${FILES[*]}; do
if [[ $(gofmt -l $FILE) != "" ]]; then
FAIL=1
FAILURES+=($FILE)
fi
done
}
function runTests() {
loadTestList $1
echo '---------------'
echo "${TESTS}"
echo '---------------'
for TST in ${TESTS[*]}; do
case $(dirname $TST) in
simTest)
testSim $TST
;;
peerTest)
testPeer $TST
;;
*) # package name provided instead
unitTest $TST
;;
esac
if [[ $? != 0 ]]; then
FAIL=1
FAILURES+=($TST)
fi
done
}
# run A/B peer coodinated tests
# $1 should be a path to a test file
function testPeer() {
A=${1/B_/A_}
B=${1/A_/B_}
# run part A in background
nohup $GO_TEST $A &>a_testout.txt &
# run part B in foreground
$GO_TEST $B &>b_testout.txt
}
# run unit tests per module this ignores all simtests
function unitTest() {
$GO_TEST $1 | egrep "PASS|FAIL|panic|bind|Timeout|no test files"
}
# run a simtest
# $1 matches simTest/<TestSomeTestName>
function testSim() {
$GO_TEST -run=${1/simTest\//} ./simTest/... | egrep "PASS|FAIL|panic|bind|Timeout"
}
function writeTestList() {
> .circleci/ci_tests
for TST in ${TESTS[*]}; do
echo $TST >> .circleci/ci_tests
done
echo "Wrote .circleci/ci_tests"
}
function main() {
FAILURES=()
FAIL=""
case $1 in
mklist)
# writelist of all tests to a file
# so full test run can be customized for a given build
loadTestList full
writeTestList
;;
gofmt)
# check all go files pass gofmt
testGoFmt
;;
*)
if [[ $CIRCLE_BRANCH =~ _ci$ ]]; then
# if branch name ends in _ci
# force 'full' test run
runTests full
else
# otherwise run tests as specified
runTests $1
fi
;;
esac
if [[ "${FAIL}x" != "x" ]]; then
echo "TESTS FAIL"
echo '---------------'
for F in ${FAILURES[*]}; do
echo $F
done
exit 1
else
echo "ALL TESTS PASS"
exit 0
fi
}
main $1