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