]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
ebl use scripts/bacula-dir.conf instead of bin/bacula-dir.conf
[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 $cwd/scripts/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 bin/bacula-dir.conf tmp/1
23    sed -f ${outf} tmp/1 > bin/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      bin/bacula-ctl-sd start
158      bin/bacula-ctl-fd start $1
159      bin/bacula-ctl-dir start
160      cat tmp/bconcmds | $bin/bconsole -c bin/bconsole.conf
161      return $?
162    else
163      bin/bacula start >/dev/null 2>&1
164      cat tmp/bconcmds | $bin/bconsole -c bin/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 bin/bconsole.conf
174    else
175      cat $bconsole_file | $bin/bconsole -c bin/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 bin/bacula-sd.conf DDS-4 | tee tmp/log1.out
183    else
184      cat tmp/bconcmds | $bin/btape -c bin/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    scripts/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 bin/bacula-dir.conf bin/1
238    echo "s%${oldname}%${newname}%g" >tmp/1
239    sed -f tmp/1 bin/1 >bin/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 build tmp/bacula-restores${cwd}/build
276    else 
277       diff -ur build tmp/bacula-restores${cwd}/build 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 tmp/build tmp/bacula-restores${cwd}/tmp/build
286    else
287       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build 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          scripts/cleanup
326       fi
327    fi
328 }
329
330 copy_tape_confs()
331 {
332    scripts/copy-tape-confs
333    scripts/cleanup-tape
334 }
335
336 copy_test_confs()
337 {
338    scripts/copy-test-confs
339    scripts/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          touch $dir/slot$SLOT
358       fi
359    fi
360 }
361
362 init_drive()
363 {
364   if test a$USE_VTAPE = a; then
365     mt -f $1 rewind
366     mt -f $1 weof
367   else
368     cp /dev/null $1
369   fi
370 }
371
372 rewind_drive()
373 {
374   if test a$USE_VTAPE = a; then
375     mt -f $1 rewind
376   fi
377 }
378
379 load_slot1() 
380 {
381 # Get a tape from slot1
382 slot=`$scripts/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
383 case $slot in
384  0)
385     $scripts/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
386     slot=$SLOT1
387     ;;
388  $SLOT1)
389     slot=$SLOT1
390     ;;
391  *)
392     rewind_drive ${TAPE_DRIVE}
393     $scripts/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
394     $scripts/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
395     slot=$SLOT1
396     ;;
397 esac
398 }
399
400 #
401 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
402 #    if 2, load 1; if 0 load 1
403 #
404 load_other_slot()
405 {
406 rewind_drive ${TAPE_DRIVE}
407 case $1 in
408  0)
409     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
410     slot=1
411     ;;
412  $SLOT1)
413     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
414     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
415     slot=2
416     ;;
417  $SLOT2)
418     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
419     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
420     slot=1
421     ;;
422  *)
423     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
424     exit 1
425     ;;
426 esac
427 }
428
429 # Save current directory
430 cwd=`pwd`
431 if test "x${REGRESS_DEBUG}" = "x1"; then
432    set_debug 1
433 else
434    set_debug 0
435 fi
436
437 # Source the configuration variables
438 . ${cwd}/config
439
440 db_name=${db_name:-"regress"}
441 db_user=${db_user:-"regress"}
442 db_password=${db_password:-""}
443 working=${working:-"$cwd/working"}
444 bin=${bin:-"$cwd/bin"}
445 scripts=${scripts:-"$bin"}
446
447 export bin
448 export working
449 export scripts
450
451 CLIENT=${HOST}-fd
452
453 if [ x$USE_VTAPE = xyes ]; then
454    mkdir -p $working/ach
455    SLOT1=1
456    SLOT2=2
457    TAPE_DRIVE=$working/ach/drive1
458    TAPE_DRIVE1=$working/ach/drive1
459    AUTOCHANGER=$working/ach/config
460    AUTOCHANGER_SCRIPT=disk-changer
461    DRIVE1=0
462    DRIVE2=1
463    cat > $AUTOCHANGER <<EOF
464 maxdrive=2
465 maxslot=10
466 EOF
467 fi
468
469 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
470 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
471 export LD_LIBRARY_PATH