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