-
Notifications
You must be signed in to change notification settings - Fork 0
/
shunit2_test.sh
executable file
·816 lines (678 loc) · 23.5 KB
/
shunit2_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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#!/bin/bash
#
# Copyright (C) 2013 Dan Fruehauf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
######################
# CORE FUNCTIONALITY #
######################
# test logging via log::logfile
test_module_log_logfile() {
# build a tmp model
local backup_name="$RANDOM"
local tmp_model=`mktemp`
local tmp_log_file1=`mktemp -u`
local tmp_log_file2=`mktemp -u`
local tmp_log_file3=`mktemp -u`
cat > $tmp_model <<EOF
log() {
logfile $tmp_log_file1
logfile $tmp_log_file2
logfile $tmp_log_file3
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'log file created and used' "[ -f $tmp_log_file1 ]"
assertTrue 'log file created and used' "[ -f $tmp_log_file2 ]"
assertTrue 'log file created and used' "[ -f $tmp_log_file3 ]"
rm -f $tmp_log_file1 $tmp_log_file2 $tmp_log_file3 $tmp_model
}
# test a bogus model file passed
test_invalid_model() {
# build a tmp model
local backup_name="$RANDOM"
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
p[INCORRECT BASH SYNTAX((
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertFalse 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
}
###########
# MODULES #
###########
###########
# EXECUTE #
###########
# test backup::execute backup and restore
test_module_backup_execute() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
local tmp_file_to_touch=`mktemp -u`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
execute "touch a temporary file" touch $tmp_file_to_touch
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'execute plugin failed' "test -f $tmp_file_to_touch"
# remove tmp_file_to_touch before testing restore
rm -f $tmp_file_to_touch
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# make sure file exists again
assertTrue 'execute plugin failed' "test -f $tmp_file_to_touch"
# cleanup
rm -f $tmp_model $tmp_file_to_touch
}
#######
# TAR #
#######
# test backup::tar backup and restore
test_module_backup_tar() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
# test backup::tar backup and exclude
test_module_backup_tar_excludes() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local file_to_exclude=$BACKUP_SOURCE/$directory_to_backup/EXCLUDED
echo "this file is excluded" > $file_to_exclude
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup --exclude=EXCLUDED
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
assertFalse 'excluded file found after restore' "[ -f $file_to_exclude ]"
}
# test backup::tar backup and sudo
test_module_backup_tar_sudo() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name --sudo $BACKUP_SOURCE/$directory_to_backup
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
#########
# RSYNC #
#########
# test backup::rsync backup
test_module_backup_rsync() {
# build a tmp model
local backup_name="$RANDOM"
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
rsync $backup_name $BACKUP_SOURCE/backup
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
local backup_timestamp=`ls -1 $BACKUP_DEST`
local -i diff_lines=`diff -urN $BACKUP_SOURCE $BACKUP_DEST/$backup_timestamp/$backup_name | wc -l`
assertTrue 'rsync not identical to source' "[ $diff_lines -eq 0 ]"
}
#########
# MYSQL #
#########
# test backup::mysql backup and restore
test_module_backup_mysql() {
# build a tmp model
local test_db="test_db_$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
local test_user=test
local test_password="${RANDOM}${RANDOM}${RANDOM}"
cat > $tmp_model <<EOF
backup() {
mysql $test_db localhost::$test_db:$test_user:$test_password
}
store() {
cp $BACKUP_DEST
}
EOF
# before running this section, you'll probably have to execute something
# like:
# echo "GRANT ALL PRIVILEGES ON *.* to $USER@'localhost' WITH GRANT OPTION;" | mysql
# echo "FLUSH PRIVILEGES;" | mysql
# another option is to change $mysql_executable run with admin credentials
local mysql_admin_privs='mysql'
local mysql_backup_privs="mysql -h localhost -u $test_user -p$test_password $test_db"
# assume we have full local mysql access
echo "CREATE DATABASE $test_db" | $mysql_admin_privs
echo "CREATE USER $test_user@'localhost' IDENTIFIED BY '$test_password'" | $mysql_admin_privs
echo "GRANT ALL ON $test_db.* TO $test_user@'localhost'" | $mysql_admin_privs
assertTrue 'privileges granting' "[ $? -eq 0 ]"
# build the database
echo "CREATE TABLE $test_db.$test_db (c1 INT, c2 INT);" | $mysql_backup_privs
echo "INSERT INTO $test_db.$test_db (c1, c2) VALUES (1,2)" | $mysql_backup_privs
echo "INSERT INTO $test_db.$test_db (c1, c2) VALUES (3,4)" | $mysql_backup_privs
assertTrue 'table creation' "[ $? -eq 0 ]"
# save the table in a file
local tmp_output1=`mktemp`
echo "SELECT * FROM $test_db.$test_db" | $mysql_backup_privs > $tmp_output1
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# backup succeeded?
assertTrue 'mysql backup failed' "test -f ${BACKUP_DEST}/*/$test_db.sql"
# remove database and recreate it
echo "DROP DATABASE $test_db" | $mysql_admin_privs
echo "GRANT ALL ON $test_db.* TO $test_user@'localhost'" | $mysql_admin_privs
echo "CREATE DATABASE $test_db" | $mysql_admin_privs
echo "GRANT ALL ON $test_db.* TO $test_user@'localhost'" | $mysql_admin_privs
# make sure the database was dropped
echo "SELECT * FROM $test_db.$test_db" | $mysql_backup_privs >& /dev/null
assertFalse 'database dropped' "[ $? -eq 0 ]"
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# see what came out after the restore
local tmp_output2=`mktemp`
echo "SELECT * FROM $test_db.$test_db" | $mysql_backup_privs > $tmp_output2
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $tmp_output1 $tmp_output2 | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
# teardown all the DB stuff
echo "DROP USER $test_user@'localhost'" | $mysql_admin_privs
echo "DROP DATABASE $test_db" | $mysql_admin_privs
# cleanup
rm -f $tmp_model $tmp_output1 $tmp_output2
}
#########
# PGSQL #
#########
# test backup::pgsql backup and restore
test_module_backup_pgsql() {
# build a tmp model
local test_db="test_db_$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
local test_user=test
local test_password="${RANDOM}${RANDOM}${RANDOM}"
cat > $tmp_model <<EOF
backup() {
pgsql $test_db localhost::$test_db:$test_user:$test_password
}
store() {
cp $BACKUP_DEST
}
EOF
# before running this section, you'll probably have to execute something
# like:
# echo "CREATE ROLE $USER WITH SUPERUSER LOGIN PASSWORD 'some_password';" | psql
# another option is to change $pgsql_executable run with admin credentials like:
# local pgsql_admin_privs="sudo su - postgres -c 'psql'"
local pgpass_file_admin=`mktemp`
chmod 600 $pgpass_file_admin
echo "localhost:5432:template1:$USER:some_password" > $pgpass_file_admin
local pgsql_admin_privs='PGPASSFILE=$pgpass_file_admin psql -h localhost -U $USER -d template1'
local pgpass_file_backup=`mktemp`
chmod 600 $pgpass_file_backup
echo "localhost:5432:$test_db:$test_user:$test_password" > $pgpass_file_backup
local pgsql_backup_privs='PGPASSFILE=$pgpass_file_backup psql -h localhost -U $test_user -d $test_db'
# create role and database
echo "DROP ROLE $test_user" | eval $pgsql_admin_privs >& /dev/null
echo "CREATE ROLE $test_user LOGIN PASSWORD '$test_password'" | eval $pgsql_admin_privs >& /dev/null
echo "CREATE DATABASE $test_db owner=$test_user" | eval $pgsql_admin_privs >& /dev/null
assertTrue 'privileges granting' "[ $? -eq 0 ]"
# build the database
echo "CREATE TABLE $test_db (c1 INTEGER, c2 INTEGER);" | eval $pgsql_backup_privs >& /dev/null
echo "INSERT INTO $test_db (c1, c2) VALUES (1,2);" | eval $pgsql_backup_privs >& /dev/null
echo "INSERT INTO $test_db (c1, c2) VALUES (3,4);" | eval $pgsql_backup_privs >& /dev/null
assertTrue 'table creation' "[ $? -eq 0 ]"
# save the table in a file
local tmp_output1=`mktemp`
echo "SELECT * FROM $test_db" | eval $pgsql_backup_privs > $tmp_output1
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# backup succeeded?
assertTrue 'pgsql backup failed' "test -f ${BACKUP_DEST}/*/$test_db.sql"
# remove database and recreate it
echo "DROP DATABASE $test_db" | eval $pgsql_admin_privs >& /dev/null
echo "CREATE DATABASE $test_db owner=$test_user" | eval $pgsql_admin_privs >& /dev/null
# make sure the database was dropped
local tmp_query_output=`mktemp`
echo "SELECT * FROM $test_db" | eval $pgsql_backup_privs > $tmp_query_output 2> /dev/null
assertTrue 'database dropped' "[ `wc -c $tmp_query_output | cut -d' ' -f1` -eq 0 ]"
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# see what came out after the restore
local tmp_output2=`mktemp`
echo "SELECT * FROM $test_db" | eval $pgsql_backup_privs > $tmp_output2
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $tmp_output1 $tmp_output2 | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
# teardown all the DB stuff
echo "DROP DATABASE $test_db" | eval $pgsql_admin_privs >& /dev/null
echo "DROP ROLE $test_user" | eval $pgsql_admin_privs >& /dev/null
rm -f $tmp_model $pgpass_file_admin $pgpass_file_backup \
$tmp_query_output $tmp_output1 $tmp_output2
}
########
# GZIP #
########
# test process::bzip backup and restore
test_module_process_gzip() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
process() {
gzip '.*'
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar.gz backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar.gz"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
#########
# BZIP2 #
#########
# test process::bzip2 backup and restore
test_module_process_bzip2() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
process() {
bzip2 '.*'
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar.bz2 backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar.bz2"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
######
# XZ #
######
# test process::xz backup and restore
test_module_process_xz() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
process() {
xz '.*'
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
assertTrue 'tar.xz backup failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar.xz"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
#########
# SPLIT #
#########
# test process::split backup and restore
test_module_process_split() {
# build a tmp model
# have at least 2 files here as we should test reconstruction of more than
# one file in the backup directory when performing a restore
local backup_name1="$RANDOM"
local backup_name2="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
# max size in bytes to split files
local -i split_max_size=30000
cat > $tmp_model <<EOF
backup() {
tar $backup_name1 $BACKUP_SOURCE/$directory_to_backup
tar $backup_name2 $BACKUP_SOURCE/$directory_to_backup
}
process() {
split '.*' $split_max_size
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
local -i files_larger_than_max_size=`find ${BACKUP_DEST} -size +${split_max_size}c | wc -l`
assertTrue "files larger than $split_max_size exist" "[ $files_larger_than_max_size -eq 0 ]"
# remove source directory (it'll come back from backup)
mv $BACKUP_SOURCE/$directory_to_backup $BACKUP_SOURCE/$directory_to_backup.orig
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
rm -f $tmp_model
# take a diff between directories after restore, they should be identical
local -i diff_lines=`diff -urN $BACKUP_SOURCE/$directory_to_backup.orig $BACKUP_SOURCE/$directory_to_backup | wc -l`
assertTrue 'restore not identical to backup' "[ $diff_lines -eq 0 ]"
}
#######
# GPG #
#######
# test process::gpg backup (no restore)
test_module_process_gpg() {
local gpg_test_key=`mktemp`
local gpg_test_key_default=gpg_test_key.asc
if [ -f $gpg_test_key_default ]; then
cp -a $gpg_test_key_default $gpg_test_key
else
# try to export key
# skip test if key doesn't exist
gpg --export --armor > $gpg_test_key
[ $? -ne 0 ] && echo "Skipping test 'test_module_process_gpg'" && return
fi
assertTrue "gpg key not found" "test -f $gpg_test_key"
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
process() {
gpg '.*\.tar' $gpg_test_key
}
store() {
cp $BACKUP_DEST
}
EOF
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# encryption succeded?
assertTrue 'gpg encryption failed' "test -f ${BACKUP_DEST}/*/$backup_name.tar.gpg"
# make sure all the files are just encrypted data and not tar
local num_files_that_are_not_data=`find $BACKUP_DEST -type f -exec file {} \; | grep -v ': data$' | wc -l`
assertTrue 'non encrypted files found' "[ $num_files_that_are_not_data -eq 0 ]"
# cleanup
rm -f $tmp_model $gpg_test_key
}
#########
# CYCLE #
#########
# test store::cycle backup
test_module_store_cycle() {
# build a tmp model
local -i cycle_backups=2
local backup_name="$RANDOM"
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
rsync $backup_name $BACKUP_SOURCE/backup
}
store() {
cp $BACKUP_DEST
cycle $BACKUP_DEST $cycle_backups
}
EOF
# have at least $cycle_backups+3 in directory
local -i backups_to_procude=`expr $cycle_backups + 3`
for i in `seq 1 $backups_to_procude`; do
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
done
local -i backups_nr=`ls -1 $BACKUP_DEST | wc -l`
assertTrue "cycling broken in directory, have $backups_nr, expected: $cycle_backups" \
"[ $backups_nr -eq $cycle_backups ]"
rm -f $tmp_model
}
############
# S3_CYCLE #
############
# test store::cycle backup
test_module_store_s3_cycle() {
# build a tmp model
local -i cycle_backups=2
local backup_name="$RANDOM"
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
store() {
s3 $backup_name --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY
s3_cycle $backup_name $cycle_backups --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY
}
EOF
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY mb s3://$backup_name >& /dev/null
# have at least $cycle_backups+3 in directory
local -i backups_to_procude=`expr $cycle_backups + 3`
for i in `seq 1 $backups_to_procude`; do
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
done
local -i backups_nr=`s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY ls s3://$backup_name | wc -l`
assertTrue "cycling broken in bucket, have $backups_nr, expected: $cycle_backups" \
"[ $backups_nr -eq $cycle_backups ]"
rm -f $tmp_model
# cleanup s3 bucket
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY --recursive --force del s3://$backup_name >& /dev/null
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY rb s3://$backup_name >& /dev/null
}
######
# S3 #
######
# test store::s3 backup
test_module_store_s3() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
store() {
s3 $backup_name --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY
}
EOF
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY mb s3://$backup_name >& /dev/null
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# restore!
$BACKUP_EXEC -r -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# cleanup
rm -f $tmp_model
# cleanup s3 bucket
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY --recursive --force del s3://$backup_name >& /dev/null
s3cmd --access_key=$AWS_ACCESS_KEY --secret_key=$AWS_SECRET_KEY rb s3://$backup_name >& /dev/null
}
#################
# NAGIOS_STATUS #
#################
# test notify::nagios_status backup
test_module_notify_nagios_status() {
# build a tmp model
local backup_name="$RANDOM"
local directory_to_backup=`ls -1 $BACKUP_SOURCE | head -1`
local tmp_model=`mktemp`
local model_name=`basename $tmp_model`
local tmp_status_dir=`mktemp -d`
cat > $tmp_model <<EOF
backup() {
tar $backup_name $BACKUP_SOURCE/$directory_to_backup
}
notify() {
nagios_status $tmp_status_dir
}
EOF
# backup!
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertTrue 'exit status of backup' "[ $? -eq 0 ]"
# compare success message
local status_message=`cat $tmp_status_dir/$model_name`
assertTrue 'nagios_status message' \
"[ '$status_message' = 'OK: Backup successful' ]"
# create a backup model that'll fail
cat > $tmp_model <<EOF
backup() {
tar $backup_name /non-existing-directory
}
notify() {
nagios_status $tmp_status_dir
}
EOF
# backup (should fail here!)
$BACKUP_EXEC -m $tmp_model >& /dev/null
assertFalse 'exit status of backup' "[ $? -eq 0 ]"
local status_message=`cat $tmp_status_dir/$model_name`
assertTrue 'nagios_status message' \
"[ '$status_message' = 'Critical: Backup failed for: backup::tar' ]"
# cleanup
rm -f $tmp_model
rm -f $tmp_status_dir/$model_name
rmdir $tmp_status_dir
}
##################
# SETUP/TEARDOWN #
##################
oneTimeSetUp() {
# load include to test
BACKUP_EXEC=`dirname $0`/backup.sh
BACKUP_SOURCE_SETUP=`mktemp -d`
mkdir -p $BACKUP_SOURCE_SETUP
# TODO
#(cd $BACKUP_SOURCE_SETUP && git clone [email protected]:danfruehauf/backup.git)
(mkdir -p $BACKUP_SOURCE_SETUP/backup && cp -a ./* $BACKUP_SOURCE_SETUP/backup/)
# AWS settings (for S3)
source aws-credentials.sh
}
oneTimeTearDown() {
rm -rf --preserve-root $BACKUP_SOURCE_SETUP
}
setUp() {
BACKUP_DEST=`mktemp -d`
BACKUP_SOURCE=`mktemp -d`
cp -a $BACKUP_SOURCE_SETUP/* $BACKUP_SOURCE/
}
tearDown() {
rm -rf --preserve-root $BACKUP_DEST $BACKUP_SOURCE
}
# load and run shUnit2
. /usr/share/shunit2/shunit2