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