]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
Merge branch 'master' into basejobv3
[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
162      ${scripts}/bacula-ctl-fd start $1
163      ${scripts}/bacula-ctl-dir start
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 DDS-4 | tee ${tmp}/log1.out
187    else
188      cat ${tmp}/bconcmds | ${bin}/btape -c ${conf}/bacula-sd.conf DDS-4 >${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    ${bin}/bacula stop 2>&1 >/dev/null
220 }
221
222 check_for_zombie_jobs()
223 {
224    ${rscripts}/check_for_zombie_jobs $*
225 }
226
227 change_jobname()
228 {
229    if test $# -eq 1; then
230       oldname=NightlySave
231       newname=$1
232    else
233       oldname=$1
234       newname=$2
235    fi
236    rm -f ${bin}/1
237    mv ${conf}/bacula-dir.conf ${bin}/1
238    echo "s%${oldname}%${newname}%g" >${tmp}/1
239    sed -f ${tmp}/1 ${bin}/1 >${conf}/bacula-dir.conf
240 #  echo "Job ${oldname} changed to ${newname}"
241 }
242
243 check_two_logs()
244 {
245    grep "^  Termination: *Backup OK" ${tmp}/log1.out 2>&1 >/dev/null
246    bstat=${bstat:-$?}
247    grep "^  Termination: .*Backup Error" ${tmp}/log1.out 2>&1 >/dev/null
248    if test $? -eq 0; then
249       bstat=2
250    fi
251    grep "^  Termination: *Restore OK" ${tmp}/log2.out 2>&1 >/dev/null
252    rstat=${rstat:-$?}
253    grep "^  Termination: .*Restore Error" ${tmp}/log2.out 2>&1 >/dev/null
254    if test $? -eq 0; then
255       rstat=2
256    fi
257    grep "^  Termination: *Restore OK -- warning file count mismatch" ${tmp}/log2.out 2>&1 >/dev/null
258    if test $? -eq 0; then
259       rstat=3
260    fi
261    grep "^  Termination: .*Verify Differences" ${tmp}/log2.out 2>&1 >/dev/null
262    if test $? -eq 0; then
263       rstat=4
264    fi
265    grep "Encoding error for database" ${tmp}/log1.out > /dev/null
266    if test $? -eq 0; then
267       print_debug "Found database encoding error"
268       bstat=2
269    fi
270 }
271
272 check_restore_diff()
273 {
274    if test "$debug" -eq 1 ; then
275       diff -ur ${src} ${tmp}/bacula-restores${src}
276    else 
277       diff -ur ${src} ${tmp}/bacula-restores${src} 2>&1 >/dev/null
278    fi
279    dstat=$?
280 }
281
282 check_restore_bin_diff()
283 {
284    if test "$debug" -eq 1 ; then
285       diff -ur ${bin} ${tmp}/bacula-restores${bin}
286    else 
287       diff -ur ${bin} ${tmp}/bacula-restores${bin} 2>&1 >/dev/null
288    fi
289    dstat=$?
290 }
291
292
293 check_restore_tmp_build_diff()
294 {
295    if test "$debug" -eq 1 ; then
296       diff -ur ${tmpsrc} ${tmp}/bacula-restores${tmpsrc}
297    else
298       diff -ur ${tmpsrc} ${tmp}/bacula-restores${tmpsrc} 2>&1 >/dev/null
299    fi
300    dstat=$?
301 }
302
303 end_test()
304 {
305    if [ $zstat != 0 ] ; then
306       echo " "
307       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
308       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
309       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
310       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
311       echo " "
312       exit 1
313    fi
314    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
315       echo " "
316       echo " "
317       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
318       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
319       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
320       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
321       if [ $bstat != 0 -o $rstat != 0 ] ; then
322          echo "  !!!!! Bad termination status       !!!!! "
323          echo "  !!!!! Bad termination status       !!!!! " >>test.out
324       else
325          echo "  !!!!! Restored files differ          !!!!! "
326          echo "  !!!!! Restored files differ          !!!!! " >>test.out
327       fi
328       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
329       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
330       echo " "
331       exit 1
332    else
333       echo "  ===== $TestName OK `date +%R:%S` ===== "
334       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
335       if test "$debug" -eq 0 ; then
336          ${rscripts}/cleanup
337       fi
338    fi
339 }
340
341 copy_tape_confs()
342 {
343    ${rscripts}/copy-tape-confs
344    ${rscripts}/cleanup-tape
345 }
346
347 copy_test_confs()
348 {
349    ${rscripts}/copy-test-confs
350    ${rscripts}/cleanup
351 }
352
353 disable_pluguins()
354 {
355    for i in ${conf}/bacula-fd.conf; do
356       sed 's/Plugin/#Plugin/i' $i > $tmp/1
357       cp -f $tmp/1 $i
358    done
359 }
360
361 debug_wait()
362 {
363   if test "x${REGRESS_WAIT}" = "x1"; then
364      echo "Start Bacula under debugger and enter anything when ready ..."
365      read a
366   fi
367 }
368
369 init_slot()
370 {
371    DRIVE=$1
372    SLOT=$2
373    if test -n "$DRIVE" -a -n "$SLOT"; then
374       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
375          dir=`dirname $DRIVE`
376          if [ ! -d "$dir" ]; then
377             mkdir -p "$dir"
378          fi
379          touch $dir/slot$SLOT
380       fi
381    fi
382 }
383
384 init_drive()
385 {
386   if test a$USE_VTAPE = a; then
387     mt -f $1 rewind
388     mt -f $1 weof
389   else
390     cp /dev/null $1
391   fi
392 }
393
394 rewind_drive()
395 {
396   if test a$USE_VTAPE = a; then
397     mt -f $1 rewind
398   fi
399 }
400
401 load_slot1() 
402 {
403 # Get a tape from slot1
404 slot=`${scripts}/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
405 case $slot in
406  0)
407     ${scripts}/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
408     slot=$SLOT1
409     ;;
410  $SLOT1)
411     slot=$SLOT1
412     ;;
413  *)
414     rewind_drive ${TAPE_DRIVE}
415     ${scripts}/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
416     ${scripts}/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
417     slot=$SLOT1
418     ;;
419 esac
420 }
421
422 #
423 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
424 #    if 2, load 1; if 0 load 1
425 #
426 load_other_slot()
427 {
428 rewind_drive ${TAPE_DRIVE}
429 case $1 in
430  0)
431     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
432     slot=1
433     ;;
434  $SLOT1)
435     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
436     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
437     slot=2
438     ;;
439  $SLOT2)
440     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
441     ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
442     slot=1
443     ;;
444  *)
445     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
446     exit 1
447     ;;
448 esac
449 }
450
451 # Save current directory
452 cwd=`pwd`
453 if test "x${REGRESS_DEBUG}" = "x1"; then
454    set_debug 1
455 else
456    set_debug 0
457 fi
458
459 # Source the configuration variables
460 . ${cwd}/config
461
462 db_name=${db_name:-"regress"}
463 db_user=${db_user:-"regress"}
464 db_password=${db_password:-""}
465 working=${working:-"$cwd/working"}
466 bin=${bin:-"$cwd/bin"}
467
468 # Bacula scripts
469 scripts=${scripts:-"$bin"}
470
471 # Bacula conf files
472 conf=${conf:-"$bin"}
473
474 # Regress scripts
475 rscripts=${rscripts:-"$cwd/scripts"}
476
477 tmp=${tmp:-"$cwd/tmp"}
478
479 # Bacula source directory when copied here
480 #  also build directory
481 src=${src:-"$cwd/build"}
482
483 # Temp source directory so we don't mess up $src
484 tmpsrc=${tmpsrc:-"$cwd/tmp/build"}
485
486 export bin
487 export conf
488 export working
489 export scripts
490 export rscripts
491 export tmp
492 export src
493 export tmpsrc
494
495 touch ${tmp}/dir.out ${tmp}/fd.out ${tmp}/sd.out
496
497 CLIENT=${HOST}-fd
498
499 if [ x$USE_VTAPE = xyes ]; then
500    mkdir -p $working/ach
501    SLOT1=1
502    SLOT2=2
503    TAPE_DRIVE=$working/ach/drive0
504    TAPE_DRIVE1=$working/ach/drive1
505    AUTOCHANGER=$working/ach/config
506    AUTOCHANGER_SCRIPT=disk-changer
507    DRIVE1=0
508    DRIVE2=1
509    cp /dev/null $working/ach/slot$SLOT1
510    cp /dev/null $working/ach/slot$SLOT2
511    cat > $AUTOCHANGER <<EOF
512 maxdrive=8
513 maxslot=80
514 EOF
515 fi
516
517 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
518 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
519 export LD_LIBRARY_PATH