-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.sh
More file actions
executable file
·366 lines (291 loc) · 7.45 KB
/
test_runner.sh
File metadata and controls
executable file
·366 lines (291 loc) · 7.45 KB
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
#! /bin/sh
# Run tests for all platform combinations.
Results=results.md
Img="https://raw.githubusercontent.com/mbucc/shmig_test/master"
__ok() {
echo "$1 [PASS]"
}
__fail() {
echo "$1 [FAIL]" >&2
return 1
}
_date_u() {
date -u +"%a, %d %b %Y %T %Z"
}
_info() {
if [ -z "$2" ] ; then
echo "[$(date)] $1"
else
echo "[$(date)] $1"="'$2'"
fi
}
_err() {
_info "$@" >&2
return 1
}
_debug() {
if [ -z "$DEBUG" ] ; then
return
fi
_err "$@"
return 0
}
#alpine:3.8-bash-mysql:8
_normalizeFilename() {
_nplat="$1"
printf "%s" "$_nplat" | tr ':/ \\' '----'
}
#alpine:3.8-bash-mysql:8
_getOutfile() {
_gnplat="$1"
statusfile="$(_normalizeFilename "$_gnplat")"
mkdir -p logs
printf "%s" "logs/$statusfile.out"
}
# alpine:3.8|apk update|apk add|bash,sqlite|/bin/bash|sqlite
update_results() {
code="$1"
platname="$2"
shell="$3"
db="$4"
dockerimg="$5"
if [ "$code" = "0" ] ; then
__ok "$platname"
else
__fail "$platname"
fi
if [ "$CI" = "1" ] ; then
if ! git pull >/dev/null 2>&1 ; then
_err "git pull error"
fi
badge="badges/$platname.png"
if [ "$code" = "0" ] ; then
cat "badges/ok.png" > "$badge"
else
cat "badges/ng.png" > "$badge"
fi
git add "$badge" >/dev/null 2>&1
badgeurl="$Img/${badge}?$(date +%s)"
logurl="$Img/logs/$platname.out?$(date +%s)"
echo "| $dockerimg | $shell | $db |  | $(date) ([log]($logurl)) |" >> "$Results"
fi
}
_writeShmigConfig() {
db="$1"
echo MIGRATIONS=./sql > shmig.conf
case "$db" in
sqlite3*)
echo TYPE=sqlite3 >> shmig.conf
echo DATABASE=./sql/test.db >> shmig.conf
;;
mysql*)
echo TYPE=mysql >> shmig.conf
echo DATABASE=mysql >> shmig.conf
echo LOGIN=root >> shmig.conf
echo HOST=db >> shmig.conf
;;
psql*)
echo TYPE=postgresql >> shmig.conf
echo DATABASE=postgres >> shmig.conf
echo LOGIN=postgres >> shmig.conf
echo PASSWORD=postgres >> shmig.conf
echo HOST=db >> shmig.conf
;;
*)
_err "invalid database $db"
return 1
;;
esac
return 0
}
_startdb() {
db="$1"
logfile="$2"
_debug "_startdb($db)"
case "$db" in
sqlite*)
_debug "no need to turn Docker for sqlite3"
rm -f ./sql/test.db
mkdir -p ./sql
;;
mysql*)
_debug "starting Docker instance $db"
docker run --rm \
-l info \
-d \
--name db \
-e MYSQL_ALLOW_EMPTY_PASSWORD=True \
"$db" \
> "$logfile" 2>&1
if [ $? -ne 0 ]
then
_err "Docker failed to start $db"
return 1
fi
;;
psql*)
_debug "starting Docker instance $db"
docker run --rm \
-d \
-l info \
--name db \
-e POSTGRES_PASSWORD=postgres \
"$db" \
> "$logfile" 2>&1
if [ $? -ne 0 ]
then
_err "Docker failed to start $db"
return 1
fi
;;
*)
_err "Invalid Docker image $db"
return 1
;;
esac
return 0
}
# alpine:3.8-bash-mysql:8|apk update_cmd -f|apk --no-cache add -f|bash,mysql-client|/bin/bash
_writeDockerFile() {
platname="$1"
dockerimg="$2"
update_cmd="$3"
install_cmd="$4"
pkgs="$5"
buildq="2>&1"
if [ "$DEBUG" ] || [ "$DEBUGING" ] ; then
buildq=""
fi
echo "FROM $dockerimg" > "$platname/ClientDockerfile"
if [ "$install_cmd" ] ; then
echo "RUN ${update_cmd:+$update_cmd $buildq &&} $install_cmd \\" \
>> "$platname/ClientDockerfile"
if [ "$pkgs" ] ; then
pkgsline=$(echo "$pkgs" | tr ',' ' ' )
if [ "$pkgsline" ] ; then
echo "$pkgsline $buildq" >> "$platname/ClientDockerfile"
fi
fi
fi
if [ "$DEBUG" ] ; then
cat "$platname/ClientDockerfile"
fi
}
# alpine:3.8|apk update|apk add|bash,sqlite|/bin/bash|sqlite
clientimg() {
echo "$1" | cut -d '|' -f 1
}
update() {
echo "$1" | cut -d '|' -f 2
}
install() {
echo "$1" | cut -d '|' -f 3
}
pkgs() {
echo "$1" | cut -d '|' -f 4
}
shell() {
echo "$1" | cut -d '|' -f 5
}
shellbasename() {
basename "$(echo "$1" | cut -d '|' -f 5)"
}
db() {
echo "$1" | cut -d '|' -f 6
}
platname() {
echo "$(clientimg "$1")-$(shellbasename "$1")-$(db "$1")"
}
# alpine:3.8|apk update|apk add|bash,sqlite|/bin/bash|sqlite
testplat() {
platline="$1"
_debug "platline" "$platline"
platname="$(_normalizeFilename "$(platname "$platline")")"
dockerimg="$(clientimg "$platline")"
update_cmd="$(update "$platline")"
install_cmd="$(install "$platline")"
pkgs="$(pkgs "$platline")"
db="$(db "$platline")"
shell="$(shell "$platline")"
_debug "platname" "$platname"
_debug "dockerimg" "$dockerimg"
_debug "update_cmd" "$update_cmd"
_debug "install_cmd" "$install_cmd"
_debug "pkgs" "$pkgs"
_debug "db" "$db"
_debug "shell=" "$shell"
_info "Running $platname, this may take a few minutes, please wait."
mkdir -p "$platname"
_writeDockerFile "$platname" "$dockerimg" "$update_cmd" "$install_cmd" "$pkgs"
Log_Out="$(_getOutfile "$platname")"
_debug "Log_Out" "$Log_Out"
if ! _startdb "$db" "$Log_Out" ; then
update_results "$code" "$platname" "$shell" "$db" "$dockerimg"
return "$code"
fi
_writeShmigConfig "$db"
if docker build -t "$platname" -f "$platname/ClientDockerfile" "$platname" > "$Log_Out" 2>&1 ; then
docker run --net=shmig-net --rm \
-e DEBUG="$DEBUG" \
-e DB="$db" \
-v "$(pwd)":/shmigtest \
"$platname" "$shell" -c "cd /shmigtest && ./test_shmig.sh" >> "$Log_Out" 2>&1
code="$?"
_debug "docker run returned" "$code"
else
code="$?"
_debug "docker build returned" "$code"
cat "$Log_Out"
fi
update_results "$code" "$platname" "$shell" "$db" "$dockerimg"
return $code
}
testall() {
code=0
docker network create shmig-net
grep -v '^#' test_runner.conf | while read -r plat
do
if [ "$plat" ] ; then
testplat "$plat"
code=$(( code + $? ))
fi
done
docker network rm shmig-net
test "$code" = "0"
return $?
}
if [ "$CI" = "1" ] ; then
rm -f "$Results"
echo "| Client | Shell | DB | Result | Test Date |" > "$Results"
echo "| ------ | ----- | --- | ------ | --------- |" >> "$Results"
fi
if [ "$DEBUG" ] ; then
cat test_runner.conf
grep -v '^#' test_runner.conf
fi
testall
code="$?"
if [ "$CI" = "1" ] ; then
cat head.md "$Results" > README.md
git add README.md >/dev/null 2>&1
git add logs/* >/dev/null 2>&1
git commit -m "CI test run" >/dev/null 2>&1
# Decode private deploy SSH key
openssl aes-256-cbc -k "$travis_key_password" -md sha256 -d -a -in travis_key.enc -out ./travis_key
chmod 400 ./travis_key
echo "Host github.com" > ~/.ssh/config
echo " IdentityFile $(pwd)/travis_key" >> ~/.ssh/config
git remote set-url origin git@github.com:mbucc/shmig_test.git
if [ "$DEBUG" ] ; then
cat ~/.ssh/config
echo "$(pwd)"
ls -l $(pwd)/travis_key
git remote -v
ssh -T git@github.com
fi
echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" > ~/.ssh/known_hosts
if ! git push -v ; then
_err "git push error"
fi
fi
exit "$code"