]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
Tweaks
[bacula/bacula] / regress / scripts / functions
1 #
2 # A set of useful functions to be sourced in each test
3 #
4
5
6 start_test()
7 {
8    # Turn off email
9    outf="tmp/sed_tmp"
10    echo "s%  mail =%# mail = %g" >${outf}
11    echo "s%  operator =%# operator =%g" >>${outf}
12    cp bin/bacula-dir.conf tmp/1
13    sed -f ${outf} tmp/1 > bin/bacula-dir.conf
14    echo " "
15    echo " "
16    echo " === Starting $TestName at `date +%R:%S` ==="
17    echo " === Starting $TestName at `date +%R:%S` ===" >> working/log
18    echo " "
19    export zstat
20 }
21
22 require_root()
23 {
24 MUID=`/usr/bin/id | awk -F= '{print $2}' | awk -F\( '{print $1}'`
25 if [ $MUID != 0 ] ; then
26    echo " "
27    echo "You must be root to run this test."
28    echo "  ===== !!!! $TestName not run at `date +%R:%S` ==="
29    echo "  ===== !!!! $TestName not run at `date +%R:%S` !!!! ===== " >>test.out
30    echo " "
31    exit 1
32 fi
33 }
34
35 require_tape_drive() 
36 {
37 if test x${TAPE_DRIVE} = x/dev/null ; then
38    echo "$TestName test needs a tape drive, but has none."
39    exit 1
40 fi
41 }
42
43 require_second_drive()
44
45 if test x${TAPE_DRIVE1} = x/dev/null ; then
46    echo "$JobName needs second drive, but has none."
47    exit 1
48 fi
49 }
50
51 require_autochanger()
52 {
53 if test x${AUTOCHANGER} = x/dev/null ; then
54    echo "$TestName needs an autochanger, but has none."
55    exit 1
56 fi
57 }
58
59 require_vtape() 
60 {
61 if test x${USE_VTAPE} = x ; then
62    echo "$TestName test needs the vtape driver."
63    exit 0
64 fi
65 }
66
67 require_linux()
68 {
69 os=`uname`
70 if [ $os != 'Linux' ]; then
71    echo "This test $TestName runs only on Linux"
72    exit 0
73 fi
74 }
75
76 skip_if_no_autochanger()
77 {
78 if test x${AUTOCHANGER} = x/dev/null ; then
79    echo "$TestName test skipped. No autochanger."
80    exit
81 fi
82 }
83
84 set_debug()
85 {
86    debug=$1
87    if test "$debug" -eq 1 ; then
88      out="tee"
89    else
90      out="output"
91    fi
92
93
94 print_debug()
95 {
96    if test "$debug" -eq 1 ; then
97      echo $*
98    fi
99 }
100
101 check_files_written()
102 {
103     LOG=$1
104     NB=$2
105     FILES=`awk '/FD Files Written:/ { last=$4 } END { print last }' $LOG`
106
107     if [ "$NB" != "$FILES" ]; then
108         print_debug "Expect $NB files, get $FILES"
109         bstat=2
110     fi
111 }
112
113 ################################################################
114 # Get information from logs
115 get_mig_info()
116 {
117     # Prev Backup JobId
118     JOBID=$1
119     LOG=$2
120     RET=`awk -F: "BEGIN { jobid=$JOBID } "'/Prev Backup JobId/ { cjid=$2 } /New Backup JobId/  { if (cjid == jobid) { print $2 } }' $LOG`
121 }
122
123 get_duration()
124 {
125    LOG=$1
126    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`
127 }
128
129 check_duration()
130 {
131    LOG=$1
132    TIME=$2
133    OP=${3-gt}
134
135    get_duration $LOG
136    if [ "$RET" -$OP "$TIME" ]; then
137        print_debug "Expect $OP than $TIME sec, get $RET"
138        bstat=2
139    fi
140 }
141
142 run_bacula()
143 {
144    debug_wait
145    zstat=0
146    if test "$debug" -eq 1 ; then
147      bin/bacula-ctl-sd start
148      bin/bacula-ctl-fd start $1
149      bin/bacula-ctl-dir start
150      cat tmp/bconcmds | $bin/bconsole -c bin/bconsole.conf
151      return $?
152    else
153      bin/bacula start >/dev/null 2>&1
154      cat tmp/bconcmds | $bin/bconsole -c bin/bconsole.conf >/dev/null 2>&1
155      return $?
156    fi
157 }
158
159 run_bconsole()
160 {
161    bconsole_file=${1:-tmp/bconcmds}
162    if test "$debug" -eq 1 ; then
163      cat $bconsole_file | $bin/bconsole -c bin/bconsole.conf
164    else
165      cat $bconsole_file | $bin/bconsole -c bin/bconsole.conf  2>&1 >/dev/null
166    fi
167 }
168
169 run_btape()
170 {
171    if test "$debug" -eq 1 ; then
172      cat tmp/bconcmds | $bin/btape -c bin/bacula-sd.conf DDS-4 | tee tmp/log1.out
173    else
174      cat tmp/bconcmds | $bin/btape -c bin/bacula-sd.conf DDS-4 >tmp/log1.out 2>&1
175    fi
176 }
177
178 run_bscan()
179 {
180    if test "$debug" -eq 1 ; then
181       $bin/bscan $* | tee tmp/log.out
182    else
183       $bin/bscan $* 2>&1 >/dev/null
184    fi
185 }
186
187 bscan_libdbi()
188 {   
189    B=`echo $LIBDBI | sed 's/;//' | sed 's/;//g'`
190    B_D=`echo $B | awk '{print $3}'`
191    B_t=`echo $B | awk '{print $6}'`
192    B_p=`echo $B | awk '{print $9}'`
193
194    BSCANLIBDBI="${LIBDBI:+1}"
195                 
196    if test "$BSCANLIBDBI" = "1" ; then
197       BSCANLIBDBI="-D $B_D -h $B_t -t $B_p"
198    else
199       BSCANLIBDBI=" "
200    fi
201 }
202
203 stop_bacula()
204 {
205    bin/bacula stop 2>&1 >/dev/null
206    zstat=0
207    bstat=0
208    rstat=0
209    dstat=0
210 }
211
212 check_for_zombie_jobs()
213 {
214    scripts/check_for_zombie_jobs $*
215 }
216
217 change_jobname()
218 {
219    if test $# -eq 1; then
220       oldname=NightlySave
221       newname=$1
222    else
223       oldname=$1
224       newname=$2
225    fi
226    rm -f bin/1
227    mv bin/bacula-dir.conf bin/1
228    echo "s%${oldname}%${newname}%g" >tmp/1
229    sed -f tmp/1 bin/1 >bin/bacula-dir.conf
230 #  echo "Job ${oldname} changed to ${newname}"
231 }
232
233 check_two_logs()
234 {
235    grep "^  Termination: *Backup OK" tmp/log1.out 2>&1 >/dev/null
236    bstat=${bstat:-$?}
237    grep "^  Termination: .*Backup Error" tmp/log1.out 2>&1 >/dev/null
238    if test $? -eq 0; then
239       bstat=2
240    fi
241    grep "^  Termination: *Restore OK" tmp/log2.out 2>&1 >/dev/null
242    rstat=${rstat:-$?}
243    grep "^  Termination: .*Restore Error" tmp/log2.out 2>&1 >/dev/null
244    if test $? -eq 0; then
245       rstat=2
246    fi
247    grep "^  Termination: *Restore OK -- warning file count mismatch" tmp/log2.out 2>&1 >/dev/null
248    if test $? -eq 0; then
249       rstat=3
250    fi
251    grep "^  Termination: .*Verify Differences" tmp/log2.out 2>&1 >/dev/null
252    if test $? -eq 0; then
253       rstat=4
254    fi
255    grep "Encoding error for database" tmp/log1.out > /dev/null
256    if test $? -eq 0; then
257       print_debug "Found database encoding error"
258       bstat=2
259    fi
260 }
261
262 check_restore_diff()
263 {
264    if test "$debug" -eq 1 ; then
265       diff -ur build tmp/bacula-restores${cwd}/build
266    else 
267       diff -ur build tmp/bacula-restores${cwd}/build 2>&1 >/dev/null
268    fi
269    dstat=$?
270 }
271
272 check_restore_tmp_build_diff()
273 {
274    if test "$debug" -eq 1 ; then
275       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build
276    else
277       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build 2>&1 >/dev/null
278    fi
279    dstat=$?
280 }
281
282 end_test()
283 {
284    if [ $zstat != 0 ] ; then
285       echo " "
286       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
287       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
288       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
289       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
290       echo " "
291       exit 1
292    fi
293    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
294       echo " "
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       if [ $bstat != 0 -o $rstat != 0 ] ; then
301          echo "  !!!!! Bad termination status       !!!!! "
302          echo "  !!!!! Bad termination status       !!!!! " >>test.out
303       else
304          echo "  !!!!! Restored files differ          !!!!! "
305          echo "  !!!!! Restored files differ          !!!!! " >>test.out
306       fi
307       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
308       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
309       echo " "
310       exit 1
311    else
312       echo "  ===== $TestName OK `date +%R:%S` ===== "
313       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
314       if test "$debug" -eq 0 ; then
315          scripts/cleanup
316       fi
317    fi
318 }
319
320 copy_tape_confs()
321 {
322    scripts/copy-tape-confs
323    scripts/cleanup-tape
324 }
325
326 copy_test_confs()
327 {
328    scripts/copy-test-confs
329    scripts/cleanup
330 }
331
332 debug_wait()
333 {
334   if test "x${REGRESS_WAIT}" = "x1"; then
335      echo "Start Bacula under debugger and enter anything when ready ..."
336      read a
337   fi
338 }
339
340 init_slot()
341 {
342    DRIVE=$1
343    SLOT=$2
344    if test -n "$DRIVE" -a -n "$SLOT"; then
345       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
346          dir=`dirname $DRIVE`
347          touch $dir/slot$SLOT
348       fi
349    fi
350 }
351
352 init_drive()
353 {
354   if test a$USE_VTAPE = a; then
355     mt -f $1 rewind
356     mt -f $1 weof
357   else
358     cp /dev/null $1
359   fi
360 }
361
362 rewind_drive()
363 {
364   if test a$USE_VTAPE = a; then
365     mt -f $1 rewind
366   fi
367 }
368
369 load_slot1() 
370 {
371 # Get a tape from slot1
372 slot=`$scripts/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
373 case $slot in
374  0)
375     $scripts/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
376     slot=$SLOT1
377     ;;
378  $SLOT1)
379     slot=$SLOT1
380     ;;
381  *)
382     rewind_drive ${TAPE_DRIVE}
383     $scripts/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
384     $scripts/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
385     slot=$SLOT1
386     ;;
387 esac
388 }
389
390 #
391 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
392 #    if 2, load 1; if 0 load 1
393 #
394 load_other_slot()
395 {
396 rewind_drive ${TAPE_DRIVE}
397 case $1 in
398  0)
399     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
400     slot=1
401     ;;
402  $SLOT1)
403     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
404     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
405     slot=2
406     ;;
407  $SLOT2)
408     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
409     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
410     slot=1
411     ;;
412  *)
413     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
414     exit 1
415     ;;
416 esac
417 }
418
419 # Save current directory
420 cwd=`pwd`
421 if test "x${REGRESS_DEBUG}" = "x1"; then
422    set_debug 1
423 else
424    set_debug 0
425 fi
426
427 # Source the configuration variables
428 . ${cwd}/config
429
430 db_name=${db_name:-"regress"}
431 db_user=${db_user:-"regress"}
432 db_password=${db_password:-""}
433 working=${working:-"$cwd/working"}
434 bin=${bin:-"$cwd/bin"}
435 scripts=${scripts:-"$bin"}
436
437 export bin
438 export working
439 export scripts
440
441 CLIENT=${HOST}-fd
442
443 if [ x$USE_VTAPE = xyes ]; then
444    mkdir -p $working/ach
445    SLOT1=1
446    SLOT2=2
447    TAPE_DRIVE=$working/ach/drive1
448    TAPE_DRIVE1=$working/ach/drive1
449    AUTOCHANGER=$working/ach/config
450    AUTOCHANGER_SCRIPT=disk-changer
451    DRIVE1=0
452    DRIVE2=1
453    cat > $AUTOCHANGER <<EOF
454 maxdrive=2
455 maxslot=10
456 EOF
457 fi
458
459 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
460 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
461 export LD_LIBRARY_PATH