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