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