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