dbutil_test.sh.in 14.8 KB
Newer Older
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
#!/bin/sh
# Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# Checks that the logger will limit the output of messages less severe than
# the severity/debug setting.

testname="Database Upgrade Test"
echo $testname

failcount=0
tempfile=@abs_builddir@/dbutil_test_tempfile_$$
backupfile=${tempfile}.backup
testdata=@abs_srcdir@/testdata
verfile=@abs_builddir@/dbutil_test_verfile_$$

# @brief Record a success
succeed() {
    echo "--- PASS"
}


# @brief Record a fail
#
# @param $1 Optional additional reason to output
fail() {
Stephen Morris's avatar
Stephen Morris committed
38
    if [ "$1" != "" ]
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
    then
        echo "ERROR: $1"
    fi
    echo "*** FAIL"
    failcount=`expr $failcount + 1`
}


# @brief Record a pass if the argument is zero
#
# @param $1 Value to test
passzero() {
    if [ $1 -eq 0 ]; then
        succeed
    else
        fail
    fi
}


# @brief Record a fail if the argument is non-zero
#
# @param $1 Value to test
failzero() {
    if [ $1 -ne 0 ]; then
        succeed
    else
        fail
    fi
}


Stephen Morris's avatar
Stephen Morris committed
71
72
73
74
75
76
77
78
79
80
81
82
83
# @brief Copy File
#
# Executes a "cp" operation followed by a "chmod" to make the target writeable.
#
# @param $1 Source file
# @param $2 Target file
copy_file () {
    cp $1 $2
    chmod a+w $2
}



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
# @brief Check backup file
#
# Record a failure if the backup file does not exist or if it is different
# to the data file. (N.B. No success is recorded if they are the same.)
#
# @param $1 Source database file
# @param $2 Backup file
check_backup() {
    if [ ! -e $1 ]
    then
        fail "database file $1 not found"

    elif [ ! -e $2 ]
    then
        fail "backup file $2 not found"

    else
        diff $1 $2 > /dev/null
        if [ $? -ne 0 ]
        then
            fail "database file $1 different to backup file $2"
        fi
    fi
}


# @brief Check No Backup File
#
# Record a failure if the backup file exists.  (N.B. No success is recorded if
# it does not.)
#
# @param $1 Source database file (unused, present for symmetry)
# @param $2 Backup file
check_no_backup() {
    if [ -e $2 ]
    then
        fail "backup of database $2 exists when it should not"
    fi
}


# @brief Get Database Schema
#
# As the schema stored in the database is format-dependent - how it is printed
# depends on how the commands were entered (on one line, split across two
# lines etc.) - comparing schema is awkward.
#
# The function sets the local variable db_schema to the output of the
# .schema command, with spaces removed and upper converted to lowercase.
#
# The database is copied before the schema is taken (and removed after)
135
# as SQLite3 assumes a writeable database, which may not be the case if
136
137
138
139
140
# getting the schema from a reference copy.
#
# @param $1 Database for which the schema is required
get_schema() {
    db1=@abs_builddir@/dbutil_test_schema_$$
Stephen Morris's avatar
Stephen Morris committed
141
    copy_file $1 $db1
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    # The purpose of the following sed command is to join multi-line SQL
    # statements to form single-line SQL statements.
    #
    # The sed command is explained as follows:
    # ':a' creates a new label "a"
    # 'N' appends the next line to the pattern space
    # '$!ba' if it's not the last line, branch to "a"
    #
    # The above makes sed loop over the entire sqlite3 output. At this
    # point, the pattern space contain all lines in the sqlite3 output.
    #
    # 's/,[\ ]*\n/, /g' then substitutes lines trailing with comma
    # followed by zero or more spaces and the newline character, with
    # just a comma and a single space.

158
    db_schema=`sqlite3 $db1 '.schema' | \
159
160
               sed -e ':a' -e 'N' -e '$!ba' -e 's/,[\ ]*\n/, /g' | \
               sort | \
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
               awk '{line = line $0} END {print line}' | \
               sed -e 's/ //g' | \
               tr [:upper:] [:lower:]`
    rm -f $db1
}


# @brief Successful Schema Upgrade Test
#
# This test is done where the upgrade is expected to be successful - when
# the end result of the test is that the test database is upgraded to a
# database of the expected schema.
#
# Note: the caller must ensure that $tempfile and $backupfile do not exist
#       on entry, and is responsible for removing them afterwards.
#
# @param $1 Database to upgrade
Stephen Morris's avatar
Stephen Morris committed
178
# @param $2 Expected backup file
179
upgrade_ok_test() {
Stephen Morris's avatar
Stephen Morris committed
180
    copy_file $1 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
181
    @SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
182
183
184
    if [ $? -eq 0 ]
    then
        # Compare schema with the reference
185
        get_schema $testdata/v2_2.sqlite3
186
187
188
        expected_schema=$db_schema
        get_schema $tempfile
        actual_schema=$db_schema
Stephen Morris's avatar
Stephen Morris committed
189
        if [ "$expected_schema" = "$actual_schema" ]
190
191
192
193
194
195
        then
            succeed
        else
            fail "upgraded schema not as expected"
        fi

Stephen Morris's avatar
Stephen Morris committed
196
        # Check the version is set correctly
197
        check_version $tempfile "V2.2"
Stephen Morris's avatar
Stephen Morris committed
198
199
200

        # Check that a backup was made
        check_backup $1 $2
201
202
203
204
205
206
207
    else
        # Error should have been output already
        fail
    fi
}


Stephen Morris's avatar
Stephen Morris committed
208
209
210
211
212
213
214
215
216
217
218
# @brief Unsuccessful Upgrade Test
#
# Checks that an upgrade of the specified database fails.
#
# Note: the caller must ensure that $tempfile and $backupfile do not exist
#       on entry, and is responsible for removing them afterwards.
#
# @param $1 Database to upgrade
# @param $2 Expected backup file
upgrade_fail_test() {
    copy_file $1 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
219
    @SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
Stephen Morris's avatar
Stephen Morris committed
220
221
222
223
224
    failzero $?
    check_backup $1 $backupfile
}


225
226
227
228
229
230
231
232
233
234
# @brief Record Count Test
#
# Checks that the count of records in each table is preserved in the upgrade.
#
# Note 1: This test assumes that the "diffs" table is present.
# Note 2: The caller must ensure that $tempfile and $backupfile do not exist
#         on entry, and is responsible for removing them afterwards.
#
# @brief $1 Database to upgrade
record_count_test() {
Stephen Morris's avatar
Stephen Morris committed
235
    copy_file $1 $tempfile
236
237
238
239
240
241

    diffs_count=`sqlite3 $tempfile 'select count(*) from diffs'`
    nsec3_count=`sqlite3 $tempfile 'select count(*) from nsec3'`
    records_count=`sqlite3 $tempfile 'select count(*) from records'`
    zones_count=`sqlite3 $tempfile 'select count(*) from zones'`

Mukund Sivaraman's avatar
Mukund Sivaraman committed
242
    @SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
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
    if [ $? -ne 0 ]
    then
        # Reason for failure should already have been output
        fail
    else
        new_diffs_count=`sqlite3 $tempfile 'select count(*) from diffs'`
        new_nsec3_count=`sqlite3 $tempfile 'select count(*) from nsec3'`
        new_records_count=`sqlite3 $tempfile 'select count(*) from records'`
        new_zones_count=`sqlite3 $tempfile 'select count(*) from zones'`

        if [ $diffs_count -ne $new_diffs_count ]
        then
            fail "diffs table was not completely copied"
        fi

        if [ $nsec3_count -ne $new_nsec3_count ]
        then
            fail "nsec3 table was not completely copied"
        fi

        if [ $records_count -ne $new_records_count ]
        then
            fail "records table was not completely copied"
        fi

        if [ $zones_count -ne $new_zones_count ]
        then
            fail "zones table was not completely copied"
        fi

        # As an extra check, test that the backup was successful
        check_backup $1 $backupfile
    fi
}


# @brief Version Check
#
# Checks that the database is at the specified version (and so checks the
# --check function).  On success, a pass is recorded.
#
# @param $1 Database to check
# @param $2 Expected version string
check_version() {
Stephen Morris's avatar
Stephen Morris committed
287
    copy_file $1 $verfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
288
    @SHELL@ ../run_dbutil.sh --check $verfile
Jelte Jansen's avatar
Jelte Jansen committed
289
    if [ $? -gt 2 ]
290
    then
Jelte Jansen's avatar
Jelte Jansen committed
291
        fail "version check failed on database $1; return code $?"
292
    else
Mukund Sivaraman's avatar
Mukund Sivaraman committed
293
        @SHELL@ ../run_dbutil.sh --check $verfile 2>&1 | grep "$2" > /dev/null
294
295
        if [ $? -ne 0 ]
        then
296
            fail "database $1 not at expected version $2 (output: $?)"
297
298
299
300
301
302
303
304
        else
            succeed
        fi
    fi
    rm -f $verfile
}


Stephen Morris's avatar
Stephen Morris committed
305
306
307
308
309
310
311
312
# @brief Version Check Fail
#
# Does a version check but expected the check to fail
#
# @param $1 Database to check
# @param $2 Backup file
check_version_fail() {
    copy_file $1 $verfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
313
    @SHELL@ ../run_dbutil.sh --check $verfile
Stephen Morris's avatar
Stephen Morris committed
314
315
316
317
318
    failzero $?
    check_no_backup $tempfile $backupfile
}


319
320
321
322
# Main test sequence

rm -f $tempfile $backupfile

323
324
325
326
327
328
329
# This is the section number that is echoed during tests. It is
# incremented when each section is run.
sec=0

# Test: check that the utility fails if the database does not exist
sec=`expr $sec + 1`
echo $sec".1. Non-existent database - check"
Mukund Sivaraman's avatar
Mukund Sivaraman committed
330
@SHELL@ ../run_dbutil.sh --check $tempfile
331
332
333
failzero $?
check_no_backup $tempfile $backupfile

334
echo $sec".2. Non-existent database - upgrade"
Mukund Sivaraman's avatar
Mukund Sivaraman committed
335
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
336
337
338
339
340
failzero $?
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile


341
342
343
# Test: should fail to check an empty file and fail to upgrade it
sec=`expr $sec + 1`
echo $sec".1. Database is an empty file - check"
344
touch $tempfile
Stephen Morris's avatar
Stephen Morris committed
345
check_version_fail $tempfile $backupfile
346
347
rm -f $tempfile $backupfile

348
echo $sec".2. Database is an empty file - upgrade"
349
touch $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
350
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
351
352
353
354
355
failzero $?
# A backup is performed before anything else, so the backup should exist.
check_backup $tempfile $backupfile
rm -f $tempfile $backupfile

356
357
sec=`expr $sec + 1`
echo $sec".1. Database is not an SQLite file - check"
358
echo "This is not an sqlite3 database" > $tempfile
Stephen Morris's avatar
Stephen Morris committed
359
360
check_version_fail $tempfile $backupfile
rm -f $tempfile $backupfile
361

362
echo $sec".2. Database is not an SQLite file - upgrade"
Stephen Morris's avatar
Stephen Morris committed
363
echo "This is not an sqlite3 database" > $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
364
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile
365
366
367
368
369
370
failzero $?
# ...and as before, a backup should have been created
check_backup $tempfile $backupfile
rm -f $tempfile $backupfile


371
372
sec=`expr $sec + 1`
echo $sec".1. Database is an SQLite3 file without the schema table - check"
Stephen Morris's avatar
Stephen Morris committed
373
check_version_fail $testdata/no_schema.sqlite3 $backupfile
374
375
rm -f $tempfile $backupfile

376
echo $sec".1. Database is an SQLite3 file without the schema table - upgrade"
Stephen Morris's avatar
Stephen Morris committed
377
upgrade_fail_test $testdata/no_schema.sqlite3 $backupfile
378
379
380
rm -f $tempfile $backupfile


381
382
sec=`expr $sec + 1`
echo $sec".1. Database is an old V1 database - check"
383
384
385
386
check_version $testdata/old_v1.sqlite3 "V1.0"
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile

387
echo $sec".2. Database is an old V1 database - upgrade"
388
389
upgrade_ok_test $testdata/old_v1.sqlite3 $backupfile
rm -f $tempfile $backupfile
390
391


392
393
sec=`expr $sec + 1`
echo $sec".1. Database is new V1 database - check"
394
395
396
check_version $testdata/new_v1.sqlite3 "V1.0"
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile
397

398
echo $sec".2. Database is a new V1 database - upgrade"
399
400
upgrade_ok_test $testdata/new_v1.sqlite3 $backupfile
rm -f $tempfile $backupfile
401
402


403
404
sec=`expr $sec + 1`
echo $sec".1. Database is V2.0 database - check"
405
406
407
408
check_version $testdata/v2_0.sqlite3 "V2.0"
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile

409
echo $sec".2. Database is a V2.0 database - upgrade"
Stephen Morris's avatar
Stephen Morris committed
410
upgrade_ok_test $testdata/v2_0.sqlite3 $backupfile
411
412
413
rm -f $tempfile $backupfile


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
sec=`expr $sec + 1`
echo $sec".1. Database is V2.1 database - check"
check_version $testdata/v2_1.sqlite3 "V2.1"
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile

echo $sec".2. Database is a V2.1 database - upgrade"
upgrade_ok_test $testdata/v2_1.sqlite3 $backupfile
rm -f $tempfile $backupfile


sec=`expr $sec + 1`
echo $sec".1. Database is V2.2 database - check"
check_version $testdata/v2_2.sqlite3 "V2.2"
check_no_backup $tempfile $backupfile
rm -f $tempfile $backupfile

echo $sec".2. Database is a V2.2 database - upgrade"
upgrade_ok_test $testdata/v2_2.sqlite3 $backupfile
rm -f $tempfile $backupfile


436
437
sec=`expr $sec + 1`
echo $sec".1. Database is V2.0 database with empty schema table - check"
Stephen Morris's avatar
Stephen Morris committed
438
439
440
check_version_fail $testdata/empty_version.sqlite3 $backupfile
rm -f $tempfile $backupfile

441
echo $sec".2. Database is V2.0 database with empty schema table - upgrade"
Stephen Morris's avatar
Stephen Morris committed
442
upgrade_fail_test $testdata/empty_version.sqlite3 $backupfile
443
444
445
rm -f $tempfile $backupfile


446
447
sec=`expr $sec + 1`
echo $sec".1. Database is V2.0 database with over-full schema table - check"
Stephen Morris's avatar
Stephen Morris committed
448
449
450
check_version_fail $testdata/too_many_version.sqlite3 $backupfile
rm -f $tempfile $backupfile

451
echo $sec".2. Database is V2.0 database with over-full schema table - upgrade"
Stephen Morris's avatar
Stephen Morris committed
452
453
454
455
upgrade_fail_test $testdata/too_many_version.sqlite3 $backupfile
rm -f $tempfile $backupfile


456
457
sec=`expr $sec + 1`
echo $sec". Upgrade corrupt database"
458
459
upgrade_fail_test $testdata/corrupt.sqlite3 $backupfile
rm -f $tempfile $backupfile
Stephen Morris's avatar
Stephen Morris committed
460
461


462
463
sec=`expr $sec + 1`
echo $sec". Record count test"
Stephen Morris's avatar
Stephen Morris committed
464
465
466
467
record_count_test $testdata/new_v1.sqlite3
rm -f $tempfile $backupfile


468
469
sec=`expr $sec + 1`
echo $sec". Backup file already exists"
470
471
touch $backupfile
touch ${backupfile}-1
Stephen Morris's avatar
Stephen Morris committed
472
upgrade_ok_test $testdata/v2_0.sqlite3 ${backupfile}-2
473
474
475
rm -f $tempfile $backupfile ${backupfile}-1 ${backupfile}-2


476
477
sec=`expr $sec + 1`
echo $sec".1 Command-line errors"
Stephen Morris's avatar
Stephen Morris committed
478
copy_file $testdata/old_v1.sqlite3 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
479
@SHELL@ ../run_dbutil.sh $tempfile
480
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
481
@SHELL@ ../run_dbutil.sh --upgrade --check $tempfile
482
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
483
@SHELL@ ../run_dbutil.sh --noconfirm --check $tempfile
Stephen Morris's avatar
Stephen Morris committed
484
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
485
@SHELL@ ../run_dbutil.sh --check
Stephen Morris's avatar
Stephen Morris committed
486
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
487
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm
Stephen Morris's avatar
Stephen Morris committed
488
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
489
@SHELL@ ../run_dbutil.sh --check $tempfile $backupfile
490
failzero $?
Mukund Sivaraman's avatar
Mukund Sivaraman committed
491
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm $tempfile $backupfile
Stephen Morris's avatar
Stephen Morris committed
492
failzero $?
493
494
rm -f $tempfile $backupfile

495
echo $sec".2 verbose flag"
Stephen Morris's avatar
Stephen Morris committed
496
copy_file $testdata/old_v1.sqlite3 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
497
@SHELL@ ../run_dbutil.sh --upgrade --noconfirm --verbose $tempfile
498
499
500
passzero $?
rm -f $tempfile $backupfile

501
echo $sec".3 Interactive prompt - yes"
502
copy_file $testdata/old_v1.sqlite3 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
503
@SHELL@ ../run_dbutil.sh --upgrade $tempfile << .
504
505
506
Yes
.
passzero $?
507
check_version $tempfile "V2.2"
508
rm -f $tempfile $backupfile
509

510
echo $sec".4 Interactive prompt - no"
Stephen Morris's avatar
Stephen Morris committed
511
copy_file $testdata/old_v1.sqlite3 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
512
@SHELL@ ../run_dbutil.sh --upgrade $tempfile << .
513
514
515
516
517
518
519
no
.
passzero $?
diff $testdata/old_v1.sqlite3 $tempfile > /dev/null
passzero $?
rm -f $tempfile $backupfile

520
echo $sec".5 quiet flag"
521
copy_file $testdata/old_v1.sqlite3 $tempfile
Mukund Sivaraman's avatar
Mukund Sivaraman committed
522
@SHELL@ ../run_dbutil.sh --check --quiet $tempfile 2>&1 | grep .
523
524
failzero $?
rm -f $tempfile $backupfile
525
526
527
528
529
530
531
532
533
534
535
536

# Report the result
if [ $failcount -eq 0 ]; then
    echo "PASS: $testname"
elif [ $failcount -eq 1 ]; then
    echo "FAIL: $testname - 1 test failed"
else
    echo "FAIL: $testname - $failcount tests failed"
fi

# Exit with appropriate error status
exit $failcount