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