]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
Save any dumps during regression to dumps directory
[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       cp -f  ${working}/bacula.*.traceback ${dumps} 2>/dev/null
322       cat ${working}/*.bactrace 2>/dev/null
323       cp -f ${working}/*.bactrace ${dumps} 2>/dev/null
324    fi
325    if [ $estat != 0 ] ; then
326       echo " "
327       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
328       echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
329       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
330       echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
331       echo " "
332       exit 1
333    fi
334    if [ $zstat != 0 ] ; then
335       echo " "
336       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
337       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
338       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
339       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
340       echo " "
341       exit 1
342    fi
343    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
344       echo " "
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       if [ $bstat != 0 -o $rstat != 0 ] ; then
351          echo "  !!!!! Bad termination status       !!!!! "
352          echo "  !!!!! Bad termination status       !!!!! " >>test.out
353       else
354          echo "  !!!!! Restored files differ          !!!!! "
355          echo "  !!!!! Restored files differ          !!!!! " >>test.out
356       fi
357       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
358       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
359       echo "   Test owner of $SITE_NAME is $EMAIL"
360       echo "   Test owner of $SITE_NAME is $EMAIL" >>test.out
361       echo " "
362       exit 1
363    else
364       echo "  ===== $TestName OK `date +%R:%S` ===== "
365       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
366       if test "$debug" -eq 0 ; then
367          ${rscripts}/cleanup
368       fi
369    fi
370 }
371
372 copy_tape_confs()
373 {
374    ${rscripts}/copy-tape-confs
375    ${rscripts}/cleanup-tape
376 }
377
378 copy_test_confs()
379 {
380    ${rscripts}/copy-test-confs
381    ${rscripts}/cleanup
382 }
383
384 disable_pluguins()
385 {
386    for i in ${conf}/bacula-fd.conf; do
387       sed 's/Plugin/#Plugin/' $i > $tmp/1
388       cp -f $tmp/1 $i
389    done
390 }
391
392 debug_wait()
393 {
394   if test "x${REGRESS_WAIT}" = "x1"; then
395      echo "Start Bacula under debugger and enter anything when ready ..."
396      read a
397   fi
398 }
399
400 init_slot()
401 {
402    DRIVE=$1
403    SLOT=$2
404    if test -n "$DRIVE" -a -n "$SLOT"; then
405       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
406          dir=`dirname $DRIVE`
407          if [ ! -d "$dir" ]; then
408             mkdir -p "$dir"
409          fi
410          touch $dir/slot$SLOT
411       fi
412    fi
413 }
414
415 init_drive()
416 {
417   if test a$USE_VTAPE = a; then
418     mt -f $1 rewind
419     mt -f $1 weof
420   else
421     cp /dev/null $1
422   fi
423 }
424
425 rewind_drive()
426 {
427   if test a$USE_VTAPE = a; then
428     mt -f $1 rewind
429   fi
430 }
431
432 load_slot1() 
433 {
434 # Get a tape from slot1
435 slot=`${scripts}/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
436 case $slot in
437  0)
438     ${scripts}/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
439     slot=$SLOT1
440     ;;
441  $SLOT1)
442     slot=$SLOT1
443     ;;
444  *)
445     rewind_drive ${TAPE_DRIVE}
446     ${scripts}/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
447     ${scripts}/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
448     slot=$SLOT1
449     ;;
450 esac
451 }
452
453 #
454 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
455 #    if 2, load 1; if 0 load 1
456 #
457 load_other_slot()
458 {
459 rewind_drive ${TAPE_DRIVE}
460 case $1 in
461  0)
462     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
463     slot=1
464     ;;
465  $SLOT1)
466     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
467     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
468     slot=2
469     ;;
470  $SLOT2)
471     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
472     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
473     slot=1
474     ;;
475  *)
476     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
477     exit 1
478     ;;
479 esac
480 }
481
482 # Save current directory
483 cwd=`pwd`
484 if test "x${REGRESS_DEBUG}" = "x1"; then
485    set_debug 1
486 else
487    set_debug 0
488 fi
489
490 # Source the configuration variables
491 . ${cwd}/config
492
493 db_name=${db_name:-"regress"}
494 db_user=${db_user:-"regress"}
495 db_password=${db_password:-""}
496 working=${working:-"$cwd/working"}
497 dumps=${dumps:-"$cwd/dumps"}
498 bin=${bin:-"$cwd/bin"}
499
500 # Bacula scripts
501 scripts=${scripts:-"$bin"}
502
503 # Bacula conf files
504 conf=${conf:-"$bin"}
505
506 # Regress scripts
507 rscripts=${rscripts:-"$cwd/scripts"}
508
509 tmp=${tmp:-"$cwd/tmp"}
510
511 # Bacula source directory when copied here
512 #  also build directory
513 src=${src:-"$cwd/build"}
514
515 # Temp source directory so we don't mess up $src
516 tmpsrc=${tmpsrc:-"$cwd/tmp/build"}
517
518 export bin
519 export conf
520 export working
521 export dumps
522 export scripts
523 export rscripts
524 export tmp
525 export src
526 export tmpsrc
527
528 bperl="perl -Mscripts::functions"
529 export bperl
530
531 mkdir -p ${tmp}
532 touch ${tmp}/dir.out ${tmp}/fd.out ${tmp}/sd.out
533
534 CLIENT=${HOST}-fd
535
536 if [ x$USE_VTAPE = xyes ]; then
537    mkdir -p $working/ach
538    SLOT1=1
539    SLOT2=2
540    TAPE_DRIVE=$working/ach/drive0
541    TAPE_DRIVE1=$working/ach/drive1
542    AUTOCHANGER=$working/ach/config
543    AUTOCHANGER_SCRIPT=disk-changer
544    DRIVE1=0
545    DRIVE2=1
546    cp /dev/null $working/ach/slot$SLOT1
547    cp /dev/null $working/ach/slot$SLOT2
548    cat > $AUTOCHANGER <<EOF
549 maxdrive=8
550 maxslot=80
551 EOF
552 fi
553
554 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
555 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
556 export LD_LIBRARY_PATH