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