More tests.
[monikop.git] / test / test.sh
blob681d154033c85ab74b85f0c1f21571990aea23de
1 #! /bin/bash
3 # Caveats: kills all killable rsyncs
4 # don't disturb test timing by putting too much (extra) load on the machine
6 TESTDIR=/tmp/monikop-test
7 DEV=$TESTDIR/dev
8 MNT=$TESTDIR/mnt
9 LOG=$TESTDIR/log
10 RSYNC=$TESTDIR/rsync
11 MONIKOP_1="../monikop ../test/monikop.config.test.1"
12 MONIKOP_2="../monikop ../test/monikop.config.test.2"
13 MONIKOP_3="../monikop ../test/monikop.config.test.3"
14 POKINOM="../pokinom ../test/pokinom.config.test"
15 TEST_COUNT=0
16 FAIL_COUNT=0
17 FAILED_TESTS=""
19 function kill_rsyncd {
20 kill `cat $TESTDIR/rsync/rsyncd.pid`
23 function start_rsyncd {
24 kill_rsyncd 2> /dev/null
25 rm -f $RSYNC/rsyncd.pid 2> /dev/null
26 chmod o-rwx ../test/rsyncd.secrets.test
27 rsync --daemon --config=../test/rsyncd.conf.test
30 # make_test_drive <name> <size>
31 function make_test_drive {
32 mkdir -p $MNT/$1
33 dd if=/dev/zero of=$DEV/$1 bs=1024 count=$2 2> /dev/null
34 /sbin/mkfs.ext3 -m 0 -Fq $DEV/$1
35 if ! mount $MNT/$1 2> /dev/null; then
36 echo "# Can't mount $DEV/$1 to $MNT/$1."
37 echo "# Redo from start after adding the following line to your /etc/fstab:"
38 echo
39 echo " $DEV/$1 $MNT/$1 ext3 loop,user,noauto 0 0"
40 echo
41 return 1
45 # make_test_file <name> <size> <date>
46 function make_test_file {
47 mkdir -p `dirname "$1"`
48 dd if=/dev/zero of="$1" bs=1024 count=$2 2> /dev/null
49 echo "++++++++++++++++++++++++++$RANDOM***$3---$1" >> "$1"
50 touch -t $3 $1
53 # find_and_compare <origin_dir> <origin_dir> ... :: <copy_dir> <copy_dir> ...
54 function find_and_compare {
55 ORIGIN_DIRS=$1; shift;
56 until [[ $1 == "::" ]]; do
57 ORIGIN_DIRS="$ORIGIN_DIRS $1"; shift;
58 done
59 shift
60 COPY_DIRS=$@
61 MISSING=""
62 DIVERGING=""
63 DIVERGING_MTIME=""
64 RETURN_VALUE=0
65 for ORIGIN_DIR in $ORIGIN_DIRS; do
66 for ORIGIN_FILE in `find $ORIGIN_DIR -type f 2> /dev/null`; do
67 for COPY_DIR in $COPY_DIRS; do
68 FOUND=`find $COPY_DIR -path "$COPY_DIR/${ORIGIN_FILE#$ORIGIN_DIR/}" 2> /dev/null`
69 if [[ $FOUND != "" ]] ; then
70 break
72 done
73 if [[ $FOUND == "" ]] ; then
74 MISSING="$MISSING $ORIGIN_FILE";
75 elif ! cmp --quiet $ORIGIN_FILE $FOUND; then
76 DIVERGING="$DIVERGING $ORIGIN_FILE"
77 elif [[ `stat --printf="%Y" $ORIGIN_FILE` != `stat --printf="%Y" $FOUND` ]]; then
78 DIVERGING_MTIME="$DIVERGING_MTIME $ORIGIN_FILE"
80 done
81 done
82 if [[ $MISSING != "" ]]; then
83 RETURN_VALUE=1
84 echo "MISSING: $MISSING"
86 if [[ $DIVERGING != "" ]]; then
87 RETURN_VALUE=$((return_value + 2))
88 echo "DIVERGING: $DIVERGING"
90 if [[ $DIVERGING_MTIME != "" ]]; then
91 RETURN_VALUE=$((return_value + 4))
92 echo "DIVERGING MTIME: $DIVERGING_MTIME"
94 return $RETURN_VALUE
97 # run_test <expected_return> <test-command> <comment>
98 function run_test {
99 sleep 2
100 echo "RUNNING $2 [$3]"
101 TEST_COUNT=$(( TEST_COUNT + 1 ))
103 RETURN_VALUE=$?
104 if [[ $RETURN_VALUE -ne $1 ]]; then
105 FAIL_COUNT=$(( FAIL_COUNT + 1 ))
106 FAILED_TESTS="$FAILED_TESTS$2($1? $RETURN_VALUE!) [$3]\n"
107 echo "$2 should have returned $1 but returned $RETURN_VALUE instead."
109 sleep 2
112 umount $MNT/* #2> /dev/null
113 rm -rf $DEV $MNT $LOG
114 mkdir -p $DEV $MNT $RSYNC
116 # Create and mount test drives:
117 for i in 01 02 03 04; do
118 make_test_drive $i 102400
119 if [[ $? == 1 ]]; then
120 MOUNTING_PROBLEM=1
122 done
123 make_test_drive 05 307200
124 if [[ $? == 1 ]]; then
125 MOUNTING_PROBLEM=1
127 if [[ $MOUNTING_PROBLEM == 1 ]]; then exit; fi
129 function fill_sources_with_big_files {
130 for i in f1 f2 f3; do
131 make_test_file $MNT/01/data/$i 25000 200703250845.33
132 done
133 for i in f10 f11 f12; do
134 make_test_file $MNT/02/data/$i 25000 200703250845.33
135 done
136 for i in f4 f5 f6; do
137 make_test_file $MNT/01/data/d1/$i 2000 200703250845.33
138 make_test_file $MNT/01/data/d1/d2/$i 2000 200703250845.33
139 done
140 for i in f7 f8 f9; do
141 make_test_file $MNT/02/data/d1/$i 2000 200703250845.33
142 make_test_file $MNT/02/data/d1/d2/$i 2000 200703250845.33
143 done
146 function fill_sources_with_hidden_files {
147 for i in 01 02; do
148 make_test_file $MNT/$i/data/.hidden_dir_$i/.hidden_file 20 200804250955.10
149 done
152 function fill_destinations_with_few_small_files {
153 for i in 03 04; do
154 for j in file_one file_two file_three; do
155 make_test_file $MNT/$i/measuring_data/$i/$j 20 200004250955.10
156 done
157 done
160 # Prepare data sources:
161 fill_sources_with_big_files
163 # Check how fast we are:
165 T1=`/usr/bin/time --format="%e" rsync --recursive --times $MNT/01/data/ $MNT/03/ 2>&1 &`
166 T2=`/usr/bin/time --format="%e" rsync --recursive --times $MNT/02/data/ $MNT/04/ 2>&1 &`
167 INTERRUPTION_TIME_0=`echo "($T1 + $T2) * 3" | bc`
168 INTERRUPTION_TIME_1=`echo "($T1 + $T2) * .08" | bc`
169 INTERRUPTION_TIME_2=`echo "($T1 + $T2) * .82" | bc`
170 echo $INTERRUPTION_TIME_0
171 rm -rf $MNT/0{1,2,3,4}/*
173 function test_monikop_simple {
174 $MONIKOP_1 & sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
175 sleep 2
176 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4}/measuring_data
179 function test_monikop_simple_late_sources {
180 kill_rsyncd
181 $MONIKOP_1 & sleep $INTERRUPTION_TIME_2; start_rsyncd; sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
182 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4}/measuring_data
185 function test_monikop_short {
186 $MONIKOP_1 & sleep $INTERRUPTION_TIME_1; /bin/kill -TERM $!
187 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4}/measuring_data
190 function test_monikop_short_2 {
191 $MONIKOP_2 & sleep $INTERRUPTION_TIME_1; /bin/kill -TERM $!
192 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4,5}/measuring_data
195 function test_monikop_short_kill_rsync_first {
196 $MONIKOP_2 & sleep $INTERRUPTION_TIME_1; /usr/bin/killall -KILL rsync; sleep 1; /bin/kill -TERM $!
197 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4,5}/measuring_data
198 RETURN=$?
199 start_rsyncd
200 sleep 2
201 return $RETURN
204 function test_monikop_short_cut_sources {
205 $MONIKOP_2 & sleep $INTERRUPTION_TIME_1; kill_rsyncd; sleep 1; /bin/kill -TERM $!
206 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4,5}/measuring_data
207 RETURN=$?
208 start_rsyncd
209 sleep 2
210 return $RETURN
213 function test_monikop_simple_2 {
214 $MONIKOP_2 & sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
215 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4,5}/measuring_data
218 function test_monikop_simple_3 {
219 $MONIKOP_3 & sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
220 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4}/measuring_data/dir_0{1,2}
223 function test_monikop_overflow {
224 # Stuff one of the destinations a bit:
225 make_test_file $MNT/03/stuffing 25000 199903250845
226 $MONIKOP_1 & sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
227 find_and_compare $MNT/0{1,2}/data :: $MNT/0{3,4}/measuring_data
230 function test_monikop_no_destination {
231 # We test basically if there is something to kill.
232 umount $MNT/{03,04}
233 $MONIKOP_1 & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
234 RETURN=$?
235 mount $MNT/03
236 mount $MNT/04
237 return $RETURN
240 function test_monikop_no_source {
241 # We test basically if there is something to kill.
242 kill_rsyncd
243 $MONIKOP_1 & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
244 RETURN=$?
245 start_rsyncd
246 return $RETURN
249 function test_pokinom_clean_finish {
250 $POKINOM & sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
251 sleep 2
252 find_and_compare $MNT/0{1,2}/data :: $MNT/05/NEW_DATA
255 function test_pokinom_short {
256 $POKINOM & sleep $INTERRUPTION_TIME_1; /bin/kill -TERM $!
257 sleep 2
258 find_and_compare $MNT/0{1,2}/data :: $MNT/05/NEW_DATA
261 function test_pokinom_late_destination {
262 kill_rsyncd
263 $POKINOM & sleep $INTERRUPTION_TIME_2; start_rsyncd; sleep $INTERRUPTION_TIME_0; /bin/kill -TERM $!
264 find_and_compare $MNT/0{1,2}/data :: $MNT/05/NEW_DATA
267 function test_dirs_backed_up {
268 test -d $MNT/03/backed_up && test -d $MNT/04/backed_up
271 function test_monikop_deletes_being_deleted_dir {
272 mkdir -p $MNT/0{3,4}/{being_deleted,backed_up}
273 touch $MNT/0{3,4}/{being_deleted,backed_up}/some_file
274 touch $MNT/0{3,4}/{being_deleted,backed_up}/.some_hidden_file
275 $MONIKOP_1 & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
276 test -d $MNT/03/being_deleted || test -d $MNT/04/being_deleted
279 function test_pokinom_deletes_being_deleted_dir {
280 mkdir -p $MNT/0{3,4}/being_deleted
281 touch $MNT/0{3,4}/being_deleted/some_file
282 touch $MNT/0{3,4}/being_deleted/.some_hidden_file
283 $POKINOM & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
284 test -d $MNT/03/being_deleted || test -d $MNT/04/being_deleted
287 function test_pokinom_newer_files_win {
288 fill_destinations_with_few_small_files
289 $POKINOM & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
290 for i in 03 04; do
291 mv $MNT/$i/backed_up $MNT/$i/measuring_data
292 touch $MNT/$i/measuring_data/$i/*
293 done
294 $POKINOM & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
295 sleep 2
296 find_and_compare $MNT/0{3,4}/backed_up :: $MNT/05/NEW_DATA
299 function test_pokinom_older_files_lose {
300 fill_destinations_with_few_small_files
301 $POKINOM & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
302 for i in 03 04; do
303 mv $MNT/$i/backed_up $MNT/$i/measuring_data
304 done
305 touch -t 198001011200.00 $MNT/03/measuring_data/03/file_one
306 $POKINOM & sleep $INTERRUPTION_TIME_2; /bin/kill -TERM $!
307 sleep 2
308 find_and_compare $MNT/0{3,4}/backed_up :: $MNT/05/NEW_DATA
311 start_rsyncd
313 ##########################
314 ### Run tests: Monikop
315 ##########################
317 run_test 1 test_monikop_deletes_being_deleted_dir "Monikop deletes left-over directory named being_deleted."
319 rm -rf $MNT/0{3,4}/* $LOG
321 chmod a-w,a-x $MNT/0{3,4}
322 run_test 1 test_monikop_simple "Unwritable destination"
323 chmod a+w,a+x $MNT/0{3,4}
324 run_test 0 test_monikop_simple "Unwritable destination"
326 rm -rf $MNT/0{3,4}/* $LOG
328 run_test 0 test_monikop_simple_3 "Source-specific directories on disks"
330 run_test 0 test_monikop_simple_late_sources "Simple run, sources coming up late."
332 mv $MNT/03/measuring_data $MNT/03/backed_up
333 mv $MNT/04/measuring_data $MNT/04/backed_up
334 rm -rf $LOG
336 run_test 0 test_monikop_simple "Simple run, deletion."
338 rm -rf $MNT/0{3,4}/* $LOG
340 run_test 1 test_monikop_short "Interruption, finished.* or finished.*.bak deleted."
341 rm -f $LOG/finished.rsync___localhost_2000_test_01_data $LOG/finished.rsync___localhost_2000_test_02_data.bak
342 run_test 0 test_monikop_simple "Recovery after interruption, finished.* or finished.*.bak deleted."
344 rm -rf $MNT/0{3,4}/* $LOG
346 run_test 1 test_monikop_short "Interruption, finished.* and/or log.* deleted."
347 rm -f $LOG/finished.rsync___localhost_2000_test_01_data $LOG/log.rsync___localhost_2000_test_01_data
348 rm -f $LOG/rm log.rsync___localhost_2000_test_02_data
349 run_test 0 test_monikop_simple "Recovery after interruption, finished.* and/or log.* deleted."
351 rm -rf $MNT/0{3,4}/* $LOG
353 run_test 1 test_monikop_short_2 "Repeated interruption."
354 run_test 1 test_monikop_short_2 "Repeated interruption (may pass unexpectedly due to test timing)."
355 run_test 0 test_monikop_simple_2 "Repeated interruption."
357 mv $MNT/03/measuring_data $MNT/03/backed_up
358 mv $MNT/04/measuring_data $MNT/04/backed_up
359 mv $MNT/05/measuring_data $MNT/05/backed_up
360 rm -rf $LOG
362 run_test 1 test_monikop_short_2 "Repeated interruption, deletion."
363 run_test 1 test_monikop_short_2 "Repeated interruption, deletion (may pass unexpectedly due to test timing)."
364 run_test 0 test_monikop_simple_2 "Repeated interruption, deletion."
366 rm -rf $MNT/0{3,4,5}/* $LOG
368 run_test 0 test_monikop_no_destination "No destination available."
369 run_test 0 test_monikop_no_source "No destination available."
371 rm -rf $MNT/0{3,4}/* $LOG
373 run_test 1 test_monikop_short_kill_rsync_first "Rsync killed."
374 ps aux | grep rsync
375 run_test 0 test_monikop_simple_2 "Rsync killed."
377 rm -rf $MNT/0{3,4,5}/* $LOG
379 run_test 1 test_monikop_short_cut_sources "Connection to source destroyed."
380 run_test 0 test_monikop_simple_2 "Connection to source destroyed."
382 rm -rf $MNT/0{3,4,5}/* $LOG
384 ##############################
385 # Pokinom
386 ##############################
388 run_test 1 test_pokinom_deletes_being_deleted_dir "Pokinom deletes left-over directory named being_deleted."
390 rm -rf $MNT/0{3,4,5}/*
392 run_test 0 test_pokinom_newer_files_win "Pokinom overwrites older files in Destination."
394 run_test 4 test_pokinom_older_files_lose "Pokinom discards older files on removable disk."
396 ##############################
397 # Monikop and Pokinom together
398 ##############################
400 fill_sources_with_hidden_files
402 run_test 0 test_monikop_simple "Preparation for simple Pokinom test, hidden files."
403 run_test 0 test_pokinom_clean_finish "Simple Pokinom test, hidden files."
404 run_test 0 test_dirs_backed_up "Simple Pokinom test, hidden files."
405 run_test 1 test_monikop_short "After test with hidden files, this one should do nothing but delete backed_up."
406 run_test 1 test_dirs_backed_up "Deletion of backed_up with hidden files."
408 rm -rf $MNT/0{3,4,5}/*
410 fill_sources_with_big_files
412 run_test 0 test_monikop_simple "Simple run in preparation for simple Pokinom test."
413 run_test 0 test_pokinom_clean_finish "Simple Pokinom test."
414 run_test 0 test_dirs_backed_up "Simple Pokinom test."
416 rm -rf $MNT/05/* $LOG
418 run_test 0 test_monikop_simple "Preparation for Pokinom's destination overfull."
419 # Stuff destination:
420 make_test_file $MNT/05/stuffing 200000 199903250845
421 run_test 1 test_pokinom_clean_finish "Pokinom's destination overfull."
422 rm $MNT/05/stuffing
423 run_test 0 test_pokinom_clean_finish "Pokinom's destination no longer overfull: recovering."
425 rm -rf $MNT/05/* $LOG
427 run_test 0 test_monikop_simple "Simple run in preparation for Pokinom, late destination."
428 run_test 0 test_pokinom_late_destination "Pokinom, late destination."
430 rm -rf $MNT/05/* $LOG
432 run_test 0 test_monikop_simple "Simple run in preparation for Pokinom stopped early."
433 run_test 1 test_pokinom_short "Pokinom stopped early."
434 run_test 0 test_monikop_simple "Simple run after Pokinom having been stopped early."
435 run_test 0 test_pokinom_clean_finish "Simple run after Pokinom having been stopped early."
437 rm -rf $MNT/05/* $LOG
439 run_test 0 test_monikop_simple "Simple run in preparation for \"file grown too large\""
440 rm $MNT/01/data/f3
441 cat $MNT/01/data/f1 >> $MNT/01/data/f2
442 run_test 2 test_monikop_simple "Repeated run, file grown too large."
443 run_test 2 test_pokinom_clean_finish "Repeated run, file grown too large."
444 run_test 1 test_monikop_simple "Repeated run, file grown too large."
445 run_test 0 test_pokinom_clean_finish "Repeated run, file grown too large."
447 rm -rf $MNT/05/* $LOG
449 run_test 1 test_monikop_overflow "Initially, too little room on disks."
450 run_test 1 test_pokinom_clean_finish "Initially, too little room on disks."
451 run_test 1 test_monikop_overflow "Previously, too little room on disks."
452 run_test 0 test_pokinom_clean_finish "Previously, too little room on disks."
454 rm -rf $MNT/0{3,4,5}/* $LOG
456 run_test 1 test_monikop_short "Unfinished by Monikop, then another full cycle."
457 run_test 1 test_pokinom_clean_finish "Unfinished by Monikop, then another full cycle (Outcome unpredictable)."
458 run_test 1 test_monikop_simple "Previously unfinished by Monikop, now another full cycle (Outcome unpredictable)."
459 run_test 0 test_pokinom_clean_finish "Previously unfinished by Monikop, now another full cycle."
461 # TODO: the following fails at least in part due to bugs in the tests themselves.
462 rm -rf $MNT/0{1,2,3,4,5}/* $LOG
463 make_test_file $MNT/01/data/d1/f1 10 200703250845.33
464 make_test_file $MNT/01/data/d2/f3 10 200703250845.33
465 make_test_file $MNT/01/data/d3/d4/f4 10 200703250845.33
466 make_test_file $MNT/01/data/f2 10 200703250845.33
467 make_test_file $MNT/01/data/f5 10 200703250845.33
468 mv $MNT/01/data/d1/f1 "$MNT/01/data/d1/Große Datei"
469 mv $MNT/01/data/d1 "$MNT/01/data/Schönes Verzeichnis"
470 mv $MNT/01/data/f2 "$MNT/01/data/{[cool]} file\ n\\a\\m\\e."
471 # Not sure if this will ever work:
472 mv $MNT/01/data/d2 $MNT/01/data/.rsync_partial
473 mv $MNT/01/data/d3/d4 $MNT/01/data/d3/.rsync_partial
474 run_test 0 test_monikop_simple "Weird file names."
475 run_test 0 test_pokinom_clean_finish "Weird file names."
476 run_test 1 test_monikop_short "Weird file names, second run: nothing to do."
478 kill_rsyncd
480 echo "TOTAL NUMBER OF TESTS: $TEST_COUNT"
481 echo "NUMBER OF FAILED TESTS: $FAIL_COUNT"
482 echo "FAILED TESTS:"
483 echo -e "$FAILED_TESTS"
485 exit $FAIL_COUNT