-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.sh
executable file
·396 lines (357 loc) · 12 KB
/
tests.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
#!/bin/bash
# Error codes
# 200 = Empty Variable
# 201 = Value is not a file
# 202 = Value is not a directory
# 203 = Missing dependency
# 204 = Databases listing failed
# 205 = Views listing failed
# 206 = No databases to backup
# 207 = Structure dump failed
# 208 = Data dump failed
# 209 = Routines dump failed
# 210 = Triggers dump failed
# 211 = Events dump failed
# 212 = Views dump failed
# 213 = Users dump failed
# 214 = Grants dump failed
# 215 = Grants list failed
TEST_MYSQL_HOST="${TEST_MYSQL_HOST:-localhost}"
TEST_MYSQL_USER="${TEST_MYSQL_USER:-root}"
TEST_MYSQL_PASS="${TEST_MYSQL_PASS:-}"
TEST_MYSQL_PORT="${TEST_MYSQL_PORT:-3306}"
SCRIPT_ROOT="$(dirname $0)"
echo "SCRIPT_ROOT=${SCRIPT_ROOT}"
MYSQLCREDS="${MYSQLCREDS:---host ${TEST_MYSQL_HOST} -u${TEST_MYSQL_USER} -p${TEST_MYSQL_PASS} --port ${TEST_MYSQL_PORT}}"
compareFiles() {
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/structure.sql" "${SCRIPT_ROOT}/test/structure.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/database.sql" "${SCRIPT_ROOT}/test/database.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/grants.sql" "${SCRIPT_ROOT}/test/grants.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/users.sql" "${SCRIPT_ROOT}/test/users.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/events.sql" "${SCRIPT_ROOT}/test/events.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/views.sql" "${SCRIPT_ROOT}/test/views.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/triggers.sql" "${SCRIPT_ROOT}/test/triggers.sql"
compareFilesOrExit "${SCRIPT_ROOT}/samples/$1/routines.sql" "${SCRIPT_ROOT}/test/routines.sql"
}
sourceCopy() {
sed -n "/^$1()/,/^}/p" ./backup.sh > func.sh; source func.sh; rm func.sh
}
compareFilesSUM() {
chk1="$(sha1sum $1 | awk -F" " '{print $1}')"
chk2="$(sha1sum $2 | awk -F" " '{print $1}')"
if [ "$chk1" = "$chk2" ]; then
return 0
else
return 1
fi
}
compareFilesOrExit() {
diff -ia "$1" "$2"
compareFilesSUM "$@" || fail "Files are not identical ($1) ($2)"
}
testEMPTY_VAR_Fail() {
./backup.sh 1>/dev/null 2>&1
assertEquals 200 "$?"
}
testBACKUP_CONFIG_ENVFILE_Fail() {
export BACKUP_CONFIG_ENVFILE="/home/myuser/secretbackupconfig.env.txt"
./backup.sh
assertEquals 201 "$?"
unset BACKUP_CONFIG_ENVFILE
}
fillConfigFile() {
echo "MYSQL_HOST=${TEST_MYSQL_HOST}" >> $1
echo "MYSQL_USER=${TEST_MYSQL_USER}" >> $1
echo "MYSQL_PASS=${TEST_MYSQL_PASS}" >> $1
echo "MYSQL_PORT=${TEST_MYSQL_PORT}" >> $1
echo "SKIP_DATABASES=mysql,sys,information_schema,performance_schema" >> $1
}
createTestData() {
mysql ${MYSQLCREDS} < ${SCRIPT_ROOT}/samples/$1/create.sql
}
destroyTestData() {
mysql ${MYSQLCREDS} < ${SCRIPT_ROOT}/samples/$1/destroy.sql
}
preTest() {
mkdir ./test
touch ./test/envfile
echo "BACKUP_DIR=${SCRIPT_ROOT}/test" > ./test/envfile
fillConfigFile ./test/envfile
export BACKUP_CONFIG_ENVFILE="./test/envfile"
}
postTest() {
rm ./test/*
rmdir ./test
unset BACKUP_CONFIG_ENVFILE
}
testBACKUP_CONFIG_ENVFILE_Success() {
preTest
echo "MYSQL_HOST=tests.test" >> "./test/envfile"
./backup.sh
assertEquals 204 "$?"
postTest
}
testBACKUP_EMPTY_Success() {
preTest
./backup.sh
assertEquals 206 "$?"
postTest
}
testBACKUP_Success_NoDiff() {
preTest
createTestData "empty"
./backup.sh
assertEquals 0 "$?"
destroyTestData "empty"
compareFiles "empty"
postTest
}
testBACKUP_Success() {
preTest
createTestData "empty"
./backup.sh
assertEquals 0 "$?"
destroyTestData "empty"
postTest
}
testOnSuccessScript() {
preTest
echo 'ON_SUCCESS="./postTest.sh"' >> "./test/envfile"
createTestData "empty"
./backup.sh
cat ./test/endfile > /dev/null
assertEquals 0 "$?"
destroyTestData "empty"
postTest
}
testCompareFail() {
# Expected fail
compareFilesSUM "${SCRIPT_ROOT}/.gitignore" "${SCRIPT_ROOT}/samples/empty/events.sql"
assertEquals 1 "$?"
}
testBACKUP_Success_NoDiff_Strange_BS_Data0() {
preTest
createTestData "withdata0"
./backup.sh
assertEquals 0 "$?"
destroyTestData "withdata0"
compareFiles "withdata0"
postTest
}
testBACKUP_Success_NoDiff_Strange_BS_Data1() {
preTest
createTestData "withdata1"
./backup.sh
assertEquals 0 "$?"
destroyTestData "withdata1"
compareFiles "withdata1"
postTest
}
testBACKUP_Success_NoDiff_Strange_BS_Data2() {
preTest
createTestData "withdata2"
./backup.sh
assertEquals 0 "$?"
destroyTestData "withdata2"
compareFiles "withdata2"
postTest
}
testBACKUP_Success_NoDatabases() {
preTest
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
./backup.sh
assertEquals 206 "$?"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testEvents_NoData_Fail() {
preTest
createTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT SELECT ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
./backup.sh
assertEquals 211 "$?"
destroyTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testViewsDump_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
echo "SKIP_OP=structure,data,routines,events,triggers,users,grants" >> "./test/envfile"
./backup.sh
assertEquals 212 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testUsers_Fail() {
preTest
createTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToTestBench.sql"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
./backup.sh
assertEquals 213 "$?"
destroyTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testEvents_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT SELECT, TRIGGER, SHOW VIEW ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
./backup.sh
assertEquals 211 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testStructure_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT, TRIGGER, SHOW VIEW ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
./backup.sh
assertEquals 207 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testSkip_HasNot() {
export SKIP_OP=()
sourceCopy "hasSkip"
hasSkip "t"
assertEquals 0 "$?"
unset SKIP_OP
}
testSkip_Has() {
export SKIP_OP=("skip1")
sourceCopy "hasSkip"
hasSkip "skip1"
assertEquals 1 "$?"
unset SKIP_OP
}
testData_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
echo "SKIP_OP=structure,routines,triggers,events,views,users,grants" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
./backup.sh
assertEquals 208 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testRoutines_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'grantfail'@'%';"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/function1.sql"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
echo "SKIP_OP=structure,data,triggers,events,views,users,grants" >> "./test/envfile"
./backup.sh
assertEquals 209 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testTriggers_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'grantfail'@'%';"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/trigger1.sql"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
echo "SKIP_OP=structure,data,routines,events,views,users,grants" >> "./test/envfile"
./backup.sh
assertEquals 210 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testGrantsList_Fail() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'userwd2gf'@'%';"
echo "MYSQL_USER=userwd2gf" >> "./test/envfile"
echo "SKIP_OP=structure,data,routines,events,triggers,users,views" >> "./test/envfile"
./backup.sh
assertEquals 215 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/deleteuser.sql"
postTest
}
#testGrants_Fail() {
# preTest
# createTestData "withdata2"
# mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/createuser.sql"
# mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'userwd2gf'@'%';"
# echo "MYSQL_USER=userwd2gf" >> "./test/envfile"
# mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/grantToUsers.sql"
# echo "SKIP_OP=structure,data,routines,events,triggers,users,views" >> "./test/envfile"
# ./backup.sh
# assertEquals 214 "$?"
# destroyTestData "withdata2"
# mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/withdata2/deleteuser.sql"
# postTest
#}
testBACKUP_manualgrant_Success() {
preTest
createTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToTestBench.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
mysql ${MYSQLCREDS} -ANe "GRANT ALL PRIVILEGES ON mysql.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
./backup.sh
assertEquals 0 "$?"
destroyTestData "withdata0"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
testNoSkipDatabases() {
preTest
createTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/createuser.sql"
mysql ${MYSQLCREDS} -ANe "GRANT INSERT ON testbench.* TO 'grantfail'@'%';"
echo "MYSQL_USER=grantfail" >> "./test/envfile"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToUsers.sql"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/grantToDB.sql"
echo "SKIP_OP=structure,data,routines,events,triggers,users,grants" >> "./test/envfile"
echo "SKIP_DATABASES=" >> "./test/envfile"
./backup.sh
assertEquals 0 "$?"
destroyTestData "withdata2"
mysql ${MYSQLCREDS} < "${SCRIPT_ROOT}/samples/empty/deleteuser.sql"
postTest
}
export SHUNIT_COLOR="none" # GitHub actions
. ${SHUNIT_SCRIPT:-shunit2}