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