]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
Update next-vol-test
[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 }
207
208 check_for_zombie_jobs()
209 {
210    scripts/check_for_zombie_jobs $*
211 }
212
213 change_jobname()
214 {
215    if test $# -eq 1; then
216       oldname=NightlySave
217       newname=$1
218    else
219       oldname=$1
220       newname=$2
221    fi
222    rm -f bin/1
223    mv bin/bacula-dir.conf bin/1
224    echo "s%${oldname}%${newname}%g" >tmp/1
225    sed -f tmp/1 bin/1 >bin/bacula-dir.conf
226 #  echo "Job ${oldname} changed to ${newname}"
227 }
228
229 check_two_logs()
230 {
231    grep "^  Termination: *Backup OK" tmp/log1.out 2>&1 >/dev/null
232    bstat=${bstat:-$?}
233    grep "^  Termination: .*Backup Error" tmp/log1.out 2>&1 >/dev/null
234    if test $? -eq 0; then
235       bstat=2
236    fi
237    grep "^  Termination: *Restore OK" tmp/log2.out 2>&1 >/dev/null
238    rstat=${rstat:-$?}
239    grep "^  Termination: .*Restore Error" tmp/log2.out 2>&1 >/dev/null
240    if test $? -eq 0; then
241       rstat=2
242    fi
243    grep "^  Termination: *Restore OK -- warning file count mismatch" tmp/log2.out 2>&1 >/dev/null
244    if test $? -eq 0; then
245       rstat=3
246    fi
247    grep "^  Termination: .*Verify Differences" tmp/log2.out 2>&1 >/dev/null
248    if test $? -eq 0; then
249       rstat=4
250    fi
251 }
252
253 check_restore_diff()
254 {
255    if test "$debug" -eq 1 ; then
256       diff -ur build tmp/bacula-restores${cwd}/build
257    else 
258       diff -ur build tmp/bacula-restores${cwd}/build 2>&1 >/dev/null
259    fi
260    dstat=$?
261 }
262
263 check_restore_tmp_build_diff()
264 {
265    if test "$debug" -eq 1 ; then
266       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build
267    else
268       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build 2>&1 >/dev/null
269    fi
270    dstat=$?
271 }
272
273 end_test()
274 {
275    if [ $zstat != 0 ] ; then
276       echo " "
277       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
278       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
279       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
280       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
281       echo " "
282       exit 1
283    fi
284    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
285       echo " "
286       echo " "
287       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
288       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
289       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
290       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
291       if [ $bstat != 0 -o $rstat != 0 ] ; then
292          echo "  !!!!! Bad termination status       !!!!! "
293          echo "  !!!!! Bad termination status       !!!!! " >>test.out
294       else
295          echo "  !!!!! Restored files differ          !!!!! "
296          echo "  !!!!! Restored files differ          !!!!! " >>test.out
297       fi
298       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
299       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
300       echo " "
301       exit 1
302    else
303       echo "  ===== $TestName OK `date +%R:%S` ===== "
304       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
305       if test "$debug" -eq 0 ; then
306          scripts/cleanup
307       fi
308    fi
309 }
310
311 copy_tape_confs()
312 {
313    scripts/copy-tape-confs
314    scripts/cleanup-tape
315 }
316
317 copy_test_confs()
318 {
319    scripts/copy-test-confs
320    scripts/cleanup
321 }
322
323 debug_wait()
324 {
325   if test "x${REGRESS_WAIT}" = "x1"; then
326      echo "Start Bacula under debugger and enter anything when ready ..."
327      read a
328   fi
329 }
330
331 init_slot()
332 {
333    DRIVE=$1
334    SLOT=$2
335    if test -n "$DRIVE" -a -n "$SLOT"; then
336       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
337          dir=`dirname $DRIVE`
338          touch $dir/slot$SLOT
339       fi
340    fi
341 }
342
343 init_drive()
344 {
345   if test a$USE_VTAPE = a; then
346     mt -f $1 rewind
347     mt -f $1 weof
348   else
349     cp /dev/null $1
350   fi
351 }
352
353 rewind_drive()
354 {
355   if test a$USE_VTAPE = a; then
356     mt -f $1 rewind
357   fi
358 }
359
360 load_slot1() 
361 {
362 # Get a tape from slot1
363 slot=`$scripts/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
364 case $slot in
365  0)
366     $scripts/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
367     slot=$SLOT1
368     ;;
369  $SLOT1)
370     slot=$SLOT1
371     ;;
372  *)
373     rewind_drive ${TAPE_DRIVE}
374     $scripts/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
375     $scripts/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
376     slot=$SLOT1
377     ;;
378 esac
379 }
380
381 #
382 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
383 #    if 2, load 1; if 0 load 1
384 #
385 load_other_slot()
386 {
387 rewind_drive ${TAPE_DRIVE}
388 case $1 in
389  0)
390     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
391     slot=1
392     ;;
393  $SLOT1)
394     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
395     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
396     slot=2
397     ;;
398  $SLOT2)
399     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
400     $scripts/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
401     slot=1
402     ;;
403  *)
404     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
405     exit 1
406     ;;
407 esac
408 }
409
410 # Save current directory
411 cwd=`pwd`
412 if test "x${REGRESS_DEBUG}" = "x1"; then
413    set_debug 1
414 else
415    set_debug 0
416 fi
417
418 # Source the configuration variables
419 . ${cwd}/config
420
421 db_name=${db_name:-"regress"}
422 db_user=${db_user:-"regress"}
423 db_password=${db_password:-""}
424 working=${working:-"$cwd/working"}
425 bin=${bin:-"$cwd/bin"}
426 scripts=${scripts:-"$bin"}
427
428 export bin
429 export working
430 export scripts
431
432 CLIENT=${HOST}-fd
433
434 if [ x$USE_VTAPE = xyes ]; then
435    mkdir -p $working/ach
436    SLOT1=1
437    SLOT2=2
438    TAPE_DRIVE=$working/ach/drive1
439    TAPE_DRIVE1=$working/ach/drive1
440    AUTOCHANGER=$working/ach/config
441    AUTOCHANGER_SCRIPT=disk-changer
442    DRIVE1=0
443    DRIVE2=1
444    cat > $AUTOCHANGER <<EOF
445 maxdrive=2
446 maxslot=10
447 EOF
448 fi
449
450 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
451 LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
452 export LD_LIBRARY_PATH