-
Notifications
You must be signed in to change notification settings - Fork 15
/
libtest.sh
executable file
·453 lines (409 loc) · 10.1 KB
/
libtest.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env bash
# highest number of servers and clients
NBR=${NBR:-3}
# Per default, have $NBR servers
NBR_SERVERS=${NBR_SERVERS:-$NBR}
# Per default, keep one inactive server
NBR_SERVERS_GROUP=${NBR_SERVERS_GROUP:-$(( NBR_SERVERS - 1))}
# Show the output of the commands: 0=none, 1=test-names, 2=all
DBG_TEST=${DBG_TEST:-0}
# DBG-level for server
DBG_SRV=${DBG_SRV:-0}
# APPDIR is usually where the test.sh-script is located
APPDIR=${APPDIR:-$(pwd)}
# The app is the name of the builddir
APP=${APP:-$(basename $APPDIR)}
# Name of conode-log
COLOG=conode
# Base port when generating the configurations
BASE_PORT=2000
RUNOUT=$( mktemp )
for i in . .. ../.. ../../.. ../../../.. $( go env GOPATH )/src/go.dedis.ch/cothority
do
if [ -f $i/conode/conode.go ]; then
root=$( cd -P $i; pwd )
break
fi
done
if [ -z "$root" ]; then
echo "Cannot find conode/connode.go"
exit 1
fi
# cleans the test-directory and builds the CLI binary
# Globals:
# CLEANBUILD
# APPDIR
# APP
startTest(){
set +m
if [ "$CLEANBUILD" ]; then
rm -f conode $APP
fi
build $APPDIR
}
# Prints `Name`, cleans up build-directory, deletes all databases from services
# in previous run, and calls `testName`.
# Arguments:
# `Name` - name of the testing function to run
run(){
cleanup
echo -e "\n* Testing $1"
sleep .5
$1
}
# Asserts that the exit-code of running `$@` using `dbgRun` is `0`.
# Arguments:
# $@ - used to run the command
testOK(){
testOut "Assert OK for '$@'"
if ! dbgRun "$@"; then
fail "starting $@ failed"
fi
}
# Asserts that the exit-code of running `$@` using `dbgRun` is NOT `0`.
# Arguments:
# $@ - used to run the command
testFail(){
testOut "Assert FAIL for '$@'"
if dbgRun "$@"; then
fail "starting $@ should've failed, but succeeded"
fi
}
# Asserts `File` exists and is a file.
# Arguments:
# `File` - path to the file to test
testFile(){
testOut "Assert file $1 exists"
if [ ! -f $1 ]; then
fail "file $1 is not here"
fi
}
# Asserts `File` DOES NOT exist.
# Arguments:
# `File` - path to the file to test
testNFile(){
testOut "Assert file $1 DOESN'T exist"
if [ -f $1 ]; then
fail "file $1 IS here"
fi
}
# Asserts that `String` exists in `File`.
# Arguments:
# `String` - what to search for
# `File` - in which file to search
testFileGrep(){
local G="$1" F="$2"
testFile "$F"
testOut "Assert file $F contains --$G--"
if ! pcregrep -M -q "$G" $F; then
fail "Didn't find '$G' in file '$F': $(cat $F)"
fi
}
# Asserts that `String` is in the output of the command being run by `dbgRun`
# and all but the first input argument. Ignores the exit-code of the command.
# Arguments:
# `String` - what to search for
# `$@[1..]` - command to run
testGrep(){
S="$1"
shift
testOut "Assert grepping '$S' in '$@'"
runOutFile "$@"
doGrep "$S"
if [ ! "$EGREP" ]; then
fail "Didn't find '$S' in output of '$@': $GREP"
fi
}
# Asserts the output of the command being run by `dbgRun` and all but the first
# input argument is N lines long. Ignores the exit-code of the command.
# Arguments:
# `N` - how many lines should be output
# `$@[1..]` - command to run
testCountLines(){
N="$1"
shift
testOut "Assert wc -l is $N lines in '$@'"
runOutFile "$@"
lines=`wc -l < $RUNOUT`
if [ $lines != $N ]; then
fail "Found $lines lines in output of '$@'"
fi
}
# Asserts that `String` is NOT in the output of the command being run by `dbgRun`
# and all but the first input argument. Ignores the exit-code of the command.
# Arguments:
# `String` - what to search for
# `$@[1..]` - command to run
testNGrep(){
G="$1"
shift
testOut "Assert NOT grepping '$G' in '$@'"
runOutFile "$@"
doGrep "$G"
if [ "$EGREP" ]; then
fail "DID find '$G' in output of '$@': $(cat $RUNOUT)"
fi
}
# Asserts `String` is part of the last command being run by `testGrep` or
# `testNGrep`.
# Arguments:
# `String` - what to search for
testReGrep(){
G="$1"
testOut "Assert grepping again '$G' in same output as before"
doGrep "$G"
if [ ! "$EGREP" ]; then
fail "Didn't find '$G' in last output: $(cat $RUNOUT)"
fi
}
# Asserts `String` is NOT part of the last command being run by `testGrep` or
# `testNGrep`.
# Arguments:
# `String` - what to search for
testReNGrep(){
G="$1"
testOut "Assert grepping again NOT '$G' in same output as before"
doGrep "$G"
if [ "$EGREP" ]; then
fail "DID find '$G' in last output: $(cat $RUNOUT)"
fi
}
# used in test*Grep methods.
doGrep(){
# echo "grepping in $RUNOUT"
# cat $RUNOUT
WC=$( cat $RUNOUT | egrep "$1" | wc -l )
EGREP=$( cat $RUNOUT | egrep "$1" )
}
# Asserts that `String` exists exactly `Count` times in the output of the
# command being run by `dbgRun` and all but the first two arguments.
# Arguments:
# `Count` - number of occurences
# `String` - what to search for
# `$@[2..]` - command to run
testCount(){
C="$1"
G="$2"
shift 2
testOut "Assert counting '$C' of '$G' in '$@'"
runOutFile "$@"
doGrep "$G"
if [ $WC -ne $C ]; then
fail "Didn't find '$C' (but '$WC') of '$G' in output of '$@': $(cat $RUNOUT)"
fi
}
# Outputs all arguments if `DBT_TEST -ge 1`
# Globals:
# DBG_TEST - determines debug-level
testOut(){
if [ "$DBG_TEST" -ge 1 ]; then
echo -e "$@"
fi
}
# Outputs all arguments if `DBT_TEST -ge 2`
# Globals:
# DBG_TEST - determines debug-level
dbgOut(){
if [ "$DBG_TEST" -ge 2 ]; then
echo -e "$@"
fi
}
# Runs `$@` and outputs the result of `$@` if `DBG_TEST -ge 2`. Redirects the
# output in all cases if `OUTFILE` is set.
# Globals:
# DBG_TEST - determines debug-level
# OUTFILE - if set, used to write output
dbgRun(){
if [ "$DBG_TEST" -ge 2 ]; then
OUT=/dev/stdout
else
OUT=/dev/null
fi
if [ "$OUTFILE" ]; then
"$@" 2>&1 | tee $OUTFILE > $OUT
else
"$@" 2>&1 > $OUT
fi
}
runGrepSed(){
GREP="$1"
SED="$2"
shift 2
runOutFile "$@"
doGrep "$GREP"
SED=$( echo $EGREP | sed -e "$SED" )
}
runOutFile(){
OLDOUTFILE=$OUTFILE
OUTFILE=$RUNOUT
dbgRun "$@"
OUTFILE=$OLDOUTFILE
}
fail(){
echo
echo -e "\tFAILED: $@"
cleanup
exit 1
}
backg(){
( "$@" 2>&1 & )
}
# Builds the app stored in the directory given in the first argument.
# Globals:
# CLEANBUILD - if set, forces build of app, even if it exists.
# TAGS - what tags to use when calling go build
# Arguments:
# builddir - where to search for the app to build
build(){
local builddir=$1
local app=$( basename $builddir )
local out=$( pwd )/$app
if [ ! -e $app -o "$CLEANBUILD" ]; then
testOut "Building $app"
(
cd $builddir
if ! go build -o $out $TAGS *.go; then
fail "Couldn't build $builddir"
fi
)
else
dbgOut "Not building $app because it's here"
fi
}
buildDir(){
BUILDDIR=./build
mkdir -p $BUILDDIR
testOut "Working in $BUILDDIR"
cd $BUILDDIR
}
# If a directory is given as an argument, the service will be taken from that
# directory.
# Globals:
# APPDIR - where the app is stored
# Arguments:
# [serviceDir, ...] - if given, used as directory to be included. At least one
# argument must be given.
buildConode(){
local incl="$@"
if [ -z "$incl" ]; then
echo "buildConode: No import paths provided."
exit 1
fi
echo "Building conode"
mkdir -p conode_
( echo -e "package main\nimport ("
for i in $incl; do
echo -e "\t_ \"$i\""
done
echo ")" ) > conode_/import.go
cp "$root/conode/conode.go" conode_/conode.go
go build -o conode ./conode_
setupConode
}
setupConode(){
# Don't show any setup messages
DBG_OLD=$DBG_TEST
DBG_TEST=0
rm -f public.toml
for n in $( seq $NBR_SERVERS ); do
co=co$n
rm -f $co/*
mkdir -p $co
local port="$(( $BASE_PORT + 2 * $n ))"
echo -e "localhost:$port\nCot-$n\n$co\n" | dbgRun runCo $n setup
if [ ! -f $co/public.toml ]; then
echo "Setup failed: file $co/public.toml is missing."
exit
fi
if [ $n -le $NBR_SERVERS_GROUP ]; then
cat $co/public.toml >> public.toml
fi
done
DBG_TEST=$DBG_OLD
}
runCoBG(){
for nb in "$@"; do
dbgOut "starting conode-server #$nb"
(
# Always redirect output of server in log-file, but
# only output to stdout if DBG_TEST > 1.
rm -f "$COLOG$nb.log.dead"
if [ $DBG_TEST -ge 2 ]; then
./conode -d $DBG_SRV -c co$nb/private.toml server 2>&1 | tee "$COLOG$nb.log"
else
./conode -d $DBG_SRV -c co$nb/private.toml server >& "$COLOG$nb.log"
fi
touch "$COLOG$nb.log.dead"
) &
# This makes `pkill conode` not outputting errors here
disown
done
local allStarted=0
# wait for conodes to start but maximum 10 seconds
for (( k=0; k < 20 && allStarted == 0; k++ )) do
sleep .5
allStarted=1
for nb in "$@"; do
local port="$(( $BASE_PORT + $nb * 2 + 1 ))"
if ! (echo >"/dev/tcp/localhost/$port") &>/dev/null; then
allStarted=0
fi
done
done
if [ "$allStarted" -ne "1" ]; then
echo "Servers failed to start"
cat *.log
cat *.log.dead
exit 1
fi
dbgOut "All conodes have started"
}
runCo(){
local nb=$1
shift
dbgOut "starting conode-server #$nb"
dbgRun ./conode -d $DBG_SRV -c co$nb/private.toml "$@"
}
cleanup(){
pkill -9 conode 2> /dev/null
pkill -9 ^${APP}$ 2> /dev/null
sleep .5
rm -f co*/*bin
rm -f cl*/*bin
if [ -z "$KEEP_DB" ]; then
rm -rf $CONODE_SERVICE_PATH
fi
}
stopTest(){
cleanup
echo "Success"
}
if ! which pcregrep > /dev/null; then
echo "*** WARNING ***"
echo "Most probably you're missing pcregrep which might be used here..."
echo "On mac you can install it with"
echo -e "\n brew install pcre\n"
echo "Not aborting because it might work anyway."
echo
fi
if ! which realpath > /dev/null; then
echo "*** WARNING ***"
echo "Most probably you're missing realpath which might be used here..."
echo "On mac you can install it with"
echo -e "\n brew install coreutils\n"
echo "Not aborting because it might work anyway."
echo
realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
fi
for i in "$@"; do
case $i in
-b|--build)
CLEANBUILD=yes
shift # past argument=value
;;
esac
done
buildDir
export CONODE_SERVICE_PATH=build/service_storage