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