]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
regress: add new diff tool in debug mode
[bacula/bacula] / regress / scripts / functions
1 #
2 # A set of useful functions to be sourced in each test
3 #
4
5
6 check_encoding()
7 {
8    ${bin}/bacula-dir -d50 -t -c ${conf}/bacula-dir.conf 2>&1 | grep 'Wanted SQL_ASCII, got UTF8' >/dev/null
9    if [ $? = 0 ]; then
10        echo "Found database encoding problem, please modify the database encoding (SQL_ASCII)"
11        exit 1
12    fi
13 }
14
15 start_test()
16 {
17    check_encoding
18    # Turn off email
19    outf="${tmp}/sed_tmp"
20    echo "s%  mail =%# mail = %g" >${outf}
21    echo "s%  operator =%# operator =%g" >>${outf}
22    cp ${conf}/bacula-dir.conf ${tmp}/1
23    sed -f ${outf} ${tmp}/1 > ${conf}/bacula-dir.conf
24    echo " "
25    echo " "
26    echo " === Starting $TestName at `date +%R:%S` ==="
27    echo " === Starting $TestName at `date +%R:%S` ===" >> ${working}/log
28    echo " "
29    export TestName
30    export zstat
31    export estat
32    estat=0
33    zstat=0
34    bstat=0
35    rstat=0
36    dstat=0
37 }
38
39 require_root()
40 {
41 MUID=`/usr/bin/id | awk -F= '{print $2}' | awk -F\( '{print $1}'`
42 if [ $MUID != 0 ] ; then
43    echo " "
44    echo "You must be root to run this test."
45    echo "  ===== !!!! $TestName not run at `date +%R:%S` ==="
46    echo "  ===== !!!! $TestName not run at `date +%R:%S` !!!! ===== " >>test.out
47    echo " "
48    exit 1
49 fi
50 }
51
52 require_tape_drive() 
53 {
54 if test x${TAPE_DRIVE} = x/dev/null ; then
55    echo "$TestName test needs a tape drive, but has none."
56    exit 1
57 fi
58 }
59
60 require_second_drive()
61
62 if test x${TAPE_DRIVE1} = x/dev/null ; then
63    echo "$JobName needs second drive, but has none."
64    exit 1
65 fi
66 }
67
68 require_autochanger()
69 {
70 if test x${AUTOCHANGER} = x/dev/null ; then
71    echo "$TestName needs an autochanger, but has none."
72    exit 1
73 fi
74 }
75
76 require_vtape() 
77 {
78 if test x${USE_VTAPE} = x ; then
79    echo "$TestName test needs the vtape driver."
80    exit 0
81 fi
82 }
83
84 require_linux()
85 {
86 os=`uname`
87 if [ $os != 'Linux' ]; then
88    echo "This test $TestName runs only on Linux"
89    exit 0
90 fi
91 }
92
93 skip_if_no_autochanger()
94 {
95 if test x${AUTOCHANGER} = x/dev/null ; then
96    echo "$TestName test skipped. No autochanger."
97    exit
98 fi
99 }
100
101 set_debug()
102 {
103    debug=$1
104    if test "$debug" -eq 1 ; then
105      out="tee"
106    else
107      out="output"
108    fi
109
110
111 print_debug()
112 {
113    echo $* | grep ERROR > /dev/null
114    if test $? -eq 0; then
115      echo $* >> $tmp/err.log
116    fi
117    if test "$debug" -eq 1 ; then
118      echo $*
119    fi
120 }
121
122 check_files_written()
123 {
124     LOG=$1
125     NB=$2
126     FILES=`awk '/FD Files Written:/ { last=$4 } END { print last }' $LOG`
127
128     if [ "$NB" != "$FILES" ]; then
129         print_debug "Expect $NB files, get $FILES"
130         bstat=2
131     fi
132 }
133
134 ################################################################
135 # Get information from logs
136 get_mig_info()
137 {
138     # Prev Backup JobId
139     JOBID=$1
140     LOG=$2
141     RET=`awk -F: "BEGIN { jobid=$JOBID } "'/Prev Backup JobId/ { cjid=$2 } /New Backup JobId/  { if (cjid == jobid) { print $2 } }' $LOG`
142 }
143
144 get_duration()
145 {
146    LOG=$1
147    RET=`awk 'BEGIN {t["secs"]=1;t["sec"]=1;t["min"]=60;t["mins"]=60}; /Elapsed time:/ { last=$3*t[$4] } END { print last }' $LOG`
148 }
149
150 check_duration()
151 {
152    LOG=$1
153    TIME=$2
154    OP=${3-gt}
155
156    get_duration $LOG
157    if [ "$RET" -$OP "$TIME" ]; then
158        print_debug "Expect $OP than $TIME sec, get $RET"
159        bstat=2
160    fi
161 }
162
163 run_bacula()
164 {
165    debug_wait
166    zstat=0
167    estat=0
168    if test "$debug" -eq 1 ; then
169      ${scripts}/bacula-ctl-sd start -m
170      ${scripts}/bacula-ctl-fd start -m $1
171      ${scripts}/bacula-ctl-dir start -m
172      cat ${tmp}/bconcmds | ${bin}/bconsole -c ${conf}/bconsole.conf
173      return $?
174    else
175      ${bin}/bacula start >/dev/null 2>&1
176      cat ${tmp}/bconcmds | ${bin}/bconsole -c ${conf}/bconsole.conf >/dev/null 2>&1
177      return $?
178    fi
179 }
180
181 run_bconsole()
182 {
183    bconsole_file=${1:-${tmp}/bconcmds}
184    if test "$debug" -eq 1 ; then
185      cat $bconsole_file | ${bin}/bconsole -c ${conf}/bconsole.conf
186    else
187      cat $bconsole_file | ${bin}/bconsole -c ${conf}/bconsole.conf  2>&1 >/dev/null
188    fi
189 }
190
191 run_btape()
192 {
193    if test "$debug" -eq 1 ; then
194      cat ${tmp}/bconcmds | ${bin}/btape -c ${conf}/bacula-sd.conf tape | tee ${tmp}/log1.out
195    else
196      cat ${tmp}/bconcmds | ${bin}/btape -c ${conf}/bacula-sd.conf tape >${tmp}/log1.out 2>&1
197    fi
198 }
199
200 run_bscan()
201 {
202    if test "$debug" -eq 1 ; then
203       ${bin}/bscan $* | tee ${tmp}/log.out
204    else
205       ${bin}/bscan $* 2>&1 >/dev/null
206    fi
207 }
208
209 bscan_libdbi()
210 {   
211    B=`echo $LIBDBI | sed 's/;//' | sed 's/;//g'`
212    B_D=`echo $B | awk '{print $3}'`
213    B_t=`echo $B | awk '{print $6}'`
214    B_p=`echo $B | awk '{print $9}'`
215
216    BSCANLIBDBI="${LIBDBI:+1}"
217                 
218    if test "$BSCANLIBDBI" = "1" ; then
219       BSCANLIBDBI="-D $B_D -h $B_t -t $B_p"
220    else
221       BSCANLIBDBI=" "
222    fi
223 }
224
225 stop_bacula()
226 {
227    if test "$debug" -eq 1 ; then
228       ${bin}/bacula stop
229    else
230       ${bin}/bacula stop 2>&1 >/dev/null
231    fi
232 }
233
234 check_for_zombie_jobs()
235 {
236    ${rscripts}/check_for_zombie_jobs $*
237 }
238
239 change_jobname()
240 {
241    if test $# -eq 1; then
242       oldname=NightlySave
243       newname=$1
244    else
245       oldname=$1
246       newname=$2
247    fi
248    rm -f ${bin}/1
249    mv ${conf}/bacula-dir.conf ${bin}/1
250    echo "s%${oldname}%${newname}%g" >${tmp}/1
251    sed -f ${tmp}/1 ${bin}/1 >${conf}/bacula-dir.conf
252 #  echo "Job ${oldname} changed to ${newname}"
253 }
254
255 check_two_logs()
256 {
257    grep "^  Termination: *Backup OK" ${tmp}/log1.out 2>&1 >/dev/null
258    bstat=${bstat:-$?}
259    grep "^  Termination: .*Backup Error" ${tmp}/log1.out 2>&1 >/dev/null
260    if test $? -eq 0; then
261       bstat=2
262    fi
263    grep "^  Termination: *Restore OK" ${tmp}/log2.out 2>&1 >/dev/null
264    rstat=${rstat:-$?}
265    grep "^  Termination: .*Restore Error" ${tmp}/log2.out 2>&1 >/dev/null
266    if test $? -eq 0; then
267       rstat=2
268    fi
269    grep "^  Termination: *Restore OK -- warning file count mismatch" ${tmp}/log2.out 2>&1 >/dev/null
270    if test $? -eq 0; then
271       rstat=3
272    fi
273    grep "^  Termination: .*Verify Differences" ${tmp}/log2.out 2>&1 >/dev/null
274    if test $? -eq 0; then
275       rstat=4
276    fi
277    grep "Encoding error for database" ${tmp}/log1.out > /dev/null
278    if test $? -eq 0; then
279       print_debug "Found database encoding error"
280       bstat=2
281    fi
282 }
283
284 check_restore_diff()
285 {
286    if test "$debug" -eq 1 ; then
287       $rscripts/diff.pl -s ${src} -d ${tmp}/bacula-restores${src}
288       diff -ur ${src} ${tmp}/bacula-restores${src}
289    else 
290       diff -ur ${src} ${tmp}/bacula-restores${src} 2>&1 >/dev/null
291    fi
292    dstat=$?
293 }
294
295 check_restore_bin_diff()
296 {
297    if test "$debug" -eq 1 ; then
298       $rscripts/diff.pl -s ${bin} -d ${tmp}/bacula-restores${bin}
299       diff -ur ${bin} ${tmp}/bacula-restores${bin}
300    else 
301       diff -ur ${bin} ${tmp}/bacula-restores${bin} 2>&1 >/dev/null
302    fi
303    dstat=$?
304 }
305
306
307 check_restore_tmp_build_diff()
308 {
309    if test "$debug" -eq 1 ; then
310       $rscripts/diff.pl -s ${tmpsrc} -d ${tmp}/bacula-restores${tmpsrc}
311       diff -ur ${tmpsrc} ${tmp}/bacula-restores${tmpsrc}
312    else
313       diff -ur ${tmpsrc} ${tmp}/bacula-restores${tmpsrc} 2>&1 >/dev/null
314    fi
315    dstat=$?
316 }
317
318 # bstat is backup error
319 # dstat is diff difference
320 # estat is special error status (shown by print_debug message)
321 # rstat is restore status
322 # zstat is zombie job(s)
323 #
324 end_test()
325 {
326    if [ x$notracedump != xyes ]; then 
327       cat ${working}/bacula.*.traceback 2>/dev/null
328       cp -f  ${working}/bacula.*.traceback ${dumps} 2>/dev/null
329       cat ${working}/*.bactrace 2>/dev/null
330       cp -f ${working}/*.bactrace ${dumps} 2>/dev/null
331    fi
332    if [ -f $tmp/err.log ]; then
333       cat $tmp/err.log
334    fi
335    if [ $estat != 0 ] ; then
336       echo " "
337       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
338       echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
339       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
340       echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
341       echo " "
342       exit 1
343    fi
344    if [ $zstat != 0 ] ; then
345       echo " "
346       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
347       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
348       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
349       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
350       echo " "
351       exit 1
352    fi
353    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
354       echo " "
355       echo " "
356       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
357       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
358       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
359       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
360       if [ $bstat != 0 -o $rstat != 0 ] ; then
361          echo "  !!!!! Bad termination status       !!!!! "
362          echo "  !!!!! Bad termination status       !!!!! " >>test.out
363       else
364          echo "  !!!!! Restored files differ          !!!!! "
365          echo "  !!!!! Restored files differ          !!!!! " >>test.out
366       fi
367       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
368       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
369       echo "   Test owner of $SITE_NAME is $EMAIL"
370       echo "   Test owner of $SITE_NAME is $EMAIL" >>test.out
371       echo " "
372       exit 1
373    else
374       echo "  ===== $TestName OK `date +%R:%S` ===== "
375       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
376       if test "$debug" -eq 0 ; then
377          ${rscripts}/cleanup
378       fi
379    fi
380 }
381
382 copy_tape_confs()
383 {
384    ${rscripts}/copy-tape-confs
385    ${rscripts}/cleanup-tape
386 }
387
388 copy_test_confs()
389 {
390    ${rscripts}/copy-test-confs
391    ${rscripts}/cleanup
392 }
393
394 disable_pluguins()
395 {
396    for i in ${conf}/bacula-fd.conf; do
397       sed 's/Plugin/#Plugin/' $i > $tmp/1
398       cp -f $tmp/1 $i
399    done
400 }
401
402 debug_wait()
403 {
404   if test "x${REGRESS_WAIT}" = "x1"; then
405      echo "Start Bacula under debugger and enter anything when ready ..."
406      read a
407   fi
408 }
409
410 init_slot()
411 {
412    DRIVE=$1
413    SLOT=$2
414    if test -n "$DRIVE" -a -n "$SLOT"; then
415       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
416          dir=`dirname $DRIVE`
417          if [ ! -d "$dir" ]; then
418             mkdir -p "$dir"
419          fi
420          touch $dir/slot$SLOT
421       fi
422    fi
423 }
424
425 init_drive()
426 {
427   if test a$USE_VTAPE = a; then
428     mt -f $1 rewind
429     mt -f $1 weof
430   else
431     cp /dev/null $1
432   fi
433 }
434
435 rewind_drive()
436 {
437   if test a$USE_VTAPE = a; then
438     mt -f $1 rewind
439   fi
440 }
441
442 load_slot1() 
443 {
444 # Get a tape from slot1
445 slot=`${scripts}/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
446 case $slot in
447  0)
448     ${scripts}/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
449     slot=$SLOT1
450     ;;
451  $SLOT1)
452     slot=$SLOT1
453     ;;
454  *)
455     rewind_drive ${TAPE_DRIVE}
456     ${scripts}/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
457     ${scripts}/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
458     slot=$SLOT1
459     ;;
460 esac
461 }
462
463 #
464 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
465 #    if 2, load 1; if 0 load 1
466 #
467 load_other_slot()
468 {
469 rewind_drive ${TAPE_DRIVE}
470 case $1 in
471  0)
472     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
473     slot=1
474     ;;
475  $SLOT1)
476     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
477     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
478     slot=2
479     ;;
480  $SLOT2)
481     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
482     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
483     slot=1
484     ;;
485  *)
486     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
487     exit 1
488     ;;
489 esac
490 }
491
492 # Save current directory
493 cwd=`pwd`
494 if test "x${REGRESS_DEBUG}" = "x1"; then
495    set_debug 1
496 else
497    set_debug 0
498 fi
499
500 # Source the configuration variables
501 . ${cwd}/config
502
503 db_name=${db_name:-"regress"}
504 db_user=${db_user:-"regress"}
505 db_password=${db_password:-""}
506 working=${working:-"$cwd/working"}
507 dumps=${dumps:-"$cwd/dumps"}
508 bin=${bin:-"$cwd/bin"}
509
510 # Bacula scripts
511 scripts=${scripts:-"$bin"}
512
513 # Bacula conf files
514 conf=${conf:-"$bin"}
515
516 # Regress scripts
517 rscripts=${rscripts:-"$cwd/scripts"}
518
519 tmp=${tmp:-"$cwd/tmp"}
520
521 # Bacula source directory when copied here
522 #  also build directory
523 src=${src:-"$cwd/build"}
524
525 # Temp source directory so we don't mess up $src
526 tmpsrc=${tmpsrc:-"$cwd/tmp/build"}
527
528 export bin
529 export conf
530 export working
531 export dumps
532 export scripts
533 export rscripts
534 export tmp
535 export src
536 export tmpsrc
537
538 bperl="perl -Mscripts::functions"
539 export bperl
540
541 mkdir -p ${tmp}
542 touch ${tmp}/dir.out ${tmp}/fd.out ${tmp}/sd.out
543
544 CLIENT=${HOST}-fd
545
546 if [ x$USE_VTAPE = xyes ]; then
547    mkdir -p $working/ach
548    SLOT1=1
549    SLOT2=2
550    TAPE_DRIVE=$working/ach/drive0
551    TAPE_DRIVE1=$working/ach/drive1
552    AUTOCHANGER=$working/ach/config
553    AUTOCHANGER_SCRIPT=disk-changer
554    DRIVE1=0
555    DRIVE2=1
556    cp /dev/null $working/ach/slot$SLOT1
557    cp /dev/null $working/ach/slot$SLOT2
558    cat > $AUTOCHANGER <<EOF
559 maxdrive=8
560 maxslot=80
561 EOF
562 fi
563
564 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
565 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
566 export LD_LIBRARY_PATH