forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity-test.sh
executable file
·116 lines (100 loc) · 3.05 KB
/
sanity-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
#!/bin/bash
BINARY_PATH=$GOPATH/bin
TINODE_BINARY=$BINARY_PATH/server
# Kills and removes any running containers.
cleanup() {
./run-cluster.sh stop
if [ -f "./server" ]; then
rm ./server
fi
docker stop mysql && docker rm mysql
}
# Reports failure.
fail() {
cleanup
echo "**************************************************"
printf "Tests Failed: ${@}\n"
echo "**************************************************"
exit 1
}
# Reports success.
pass() {
cleanup
echo "**************************************************"
echo "* OK *"
echo "**************************************************"
exit 0
}
# Brings up a mysql docker container.
setup() {
docker info 1>/dev/null 2>&1 || (echo "docker not running" && return 1)
docker run -p 3306:3306 --name mysql --env MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7 || return 1
# This fails to detect when the mysql is actually ready.
# TODO: figure out why.
#until nc -z -v -w30 localhost 3306; do
# echo "Waiting for database connection..."
# sleep 1
#done
echo -n "Waiting for mysql to come up."
while ! mysqladmin ping -u root -h 127.0.0.1 --silent; do
echo -n "."
sleep 1
done
# Make sure there's no Tinode server binary in the current directory.
if [ -f "./server" ]; then
rm ./server
fi
}
# Compiles Tinode binaries.
build() {
go install -tags mysql -ldflags "-X main.buildstamp=`date -u '+%Y%m%dT%H:%M:%SZ'`" \
github.com/tinode/chat/tinode-db \
github.com/tinode/chat/server && \
ln -s $TINODE_BINARY
}
# Initializes Tinode database.
init-db() {
$GOPATH/bin/tinode-db -config=./tinode.conf -data=../tinode-db/data.json
}
wait-for() {
local port=$1
while ! nc -z localhost $port; do
sleep 1
done
}
# Brings up a three-node Tinode cluster.
run-server() {
./run-cluster.sh -s "" start && wait-for 16060
}
send-requests() {
local port=$1
local id=$2
local outfile=$(mktemp /tmp/tinode-${id}.txt)
pushd .
cd ../tn-cli
python3 tn-cli.py --host=localhost:${port} --no-login < sample-script.txt > $outfile || fail "Test script failed (instance port ${port})"
popd
num_positive_responses=`grep -c '<= 20[0-9]' $outfile`
if [ $num_positive_responses -ne 10 ]; then fail "Instance ${port}: unexpected number of 20* responses: ${num_positive_responses}. Log file ${outfile}"; fi
rm $outfile
}
# Catch unexpected failures, do cleanup and output an error message
trap 'cleanup ; fail "For Unexpected Reasons"'\
HUP INT QUIT PIPE TERM
# Normal script termination.
#trap 'cleanup'\
# EXIT
run_id=`date +%s`
echo "+----------------------------------------------------+"
echo "| Tinode sanity test. |"
echo "+----------------------------------------------------+"
echo "Timestamp = ${run_id}"
setup || fail "Test setup failed."
build || fail "Could not build Tinode binaries"
init-db || fail "Could not initialize Tinode database"
run-server || fail "Could not start tinode"
# Test requests.
send-requests 16060 $run_id
send-requests 16061 $run_id
send-requests 16062 $run_id
pass