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