]> git.sur5r.net Git - bacula/bacula/blob - regress/scripts/functions
ebl Add check_duration to functions
[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 check_duration()
114 {
115     LOG=$1
116     TIME=$2
117     T=`awk 'BEGIN {t["secs"]=1;t["sec"]=1;t["min"]=60;t["mins"]=60}; /Elapsed time:/ { last=$3*t[$4] } END { print last }' $LOG`
118
119     if [ "$T" -gt $TIME ]; then
120         print_debug "Expect less than $TIME sec, get $T"
121         bstat=2
122     fi
123 }
124
125 run_bacula()
126 {
127    debug_wait
128    zstat=0
129    if test "$debug" -eq 1 ; then
130      bin/bacula-ctl-sd start
131      bin/bacula-ctl-fd start $1
132      bin/bacula-ctl-dir start
133      cat tmp/bconcmds | bin/bconsole -c bin/bconsole.conf
134      return $?
135    else
136      bin/bacula start 2>&1 >/dev/null
137      cat tmp/bconcmds | bin/bconsole -c bin/bconsole.conf  2>&1 >/dev/null
138      return $?
139    fi
140 }
141
142 run_bconsole()
143 {
144    if test "$debug" -eq 1 ; then
145      cat tmp/bconcmds | bin/bconsole -c bin/bconsole.conf
146    else
147      cat tmp/bconcmds | bin/bconsole -c bin/bconsole.conf  2>&1 >/dev/null
148    fi
149 }
150
151 run_btape()
152 {
153    if test "$debug" -eq 1 ; then
154      cat tmp/bconcmds | bin/btape -c bin/bacula-sd.conf DDS-4 | tee tmp/log1.out
155    else
156      cat tmp/bconcmds | bin/btape -c bin/bacula-sd.conf DDS-4 2>&1 >tmp/log1.out
157    fi
158 }
159
160 run_bscan()
161 {
162    if test "$debug" -eq 1 ; then
163       bin/bscan $* | tee tmp/log.out
164    else
165       bin/bscan $* 2>&1 >/dev/null
166    fi
167 }
168
169 bscan_libdbi()
170 {   
171    B=`echo $LIBDBI | sed 's/;//' | sed 's/;//g'`
172    B_D=`echo $B | awk '{print $3}'`
173    B_t=`echo $B | awk '{print $6}'`
174    B_p=`echo $B | awk '{print $9}'`
175
176    BSCANLIBDBI="${LIBDBI:+1}"
177                 
178    if test "$BSCANLIBDBI" = "1" ; then
179       BSCANLIBDBI="-D $B_D -h $B_t -t $B_p"
180    else
181       BSCANLIBDBI=" "
182    fi
183 }
184
185 stop_bacula()
186 {
187    bin/bacula stop 2>&1 >/dev/null
188 }
189
190 check_for_zombie_jobs()
191 {
192    scripts/check_for_zombie_jobs $*
193 }
194
195 change_jobname()
196 {
197    if test $# -eq 1; then
198       oldname=NightlySave
199       newname=$1
200    else
201       oldname=$1
202       newname=$2
203    fi
204    rm -f bin/1
205    mv bin/bacula-dir.conf bin/1
206    echo "s%${oldname}%${newname}%g" >tmp/1
207    sed -f tmp/1 bin/1 >bin/bacula-dir.conf
208 #  echo "Job ${oldname} changed to ${newname}"
209 }
210
211 check_two_logs()
212 {
213    grep "^  Termination: *Backup OK" tmp/log1.out 2>&1 >/dev/null
214    bstat=${bstat-$?}
215    grep "^  Termination: .*Backup Error" tmp/log1.out 2>&1 >/dev/null
216    if test $? -eq 0; then
217       bstat=2
218    fi
219    grep "^  Termination: *Restore OK" tmp/log2.out 2>&1 >/dev/null
220    rstat=${rstat-$?}
221    grep "^  Termination: .*Restore Error" tmp/log2.out 2>&1 >/dev/null
222    if test $? -eq 0; then
223       rstat=2
224    fi
225    grep "^  Termination: *Restore OK -- warning file count mismatch" tmp/log2.out 2>&1 >/dev/null
226    if test $? -eq 0; then
227       rstat=3
228    fi
229    grep "^  Termination: .*Verify Differences" tmp/log2.out 2>&1 >/dev/null
230    if test $? -eq 0; then
231       rstat=4
232    fi
233 }
234
235 check_restore_diff()
236 {
237    if test "$debug" -eq 1 ; then
238       diff -ur build tmp/bacula-restores${cwd}/build
239    else 
240       diff -ur build tmp/bacula-restores${cwd}/build 2>&1 >/dev/null
241    fi
242    dstat=$?
243 }
244
245 check_restore_tmp_build_diff()
246 {
247    if test "$debug" -eq 1 ; then
248       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build
249    else
250       diff -ur tmp/build tmp/bacula-restores${cwd}/tmp/build 2>&1 >/dev/null
251    fi
252    dstat=$?
253 }
254
255 end_test()
256 {
257    if [ $zstat != 0 ] ; then
258       echo " "
259       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
260       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
261       echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
262       echo " "
263       exit 1
264    fi
265    if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
266       echo " "
267       echo " "
268       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! "
269       echo "  !!!!! $TestName failed!!! `date +%R:%S` !!!!! " >>test.out
270       if [ $bstat != 0 -o $rstat != 0 ] ; then
271          echo "  !!!!! Bad termination status       !!!!! "
272          echo "  !!!!! Bad termination status       !!!!! " >>test.out
273       else
274          echo "  !!!!! Restored files differ          !!!!! "
275          echo "  !!!!! Restored files differ          !!!!! " >>test.out
276       fi
277       echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
278       echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
279       echo " "
280       exit 1
281    else
282       echo "  ===== $TestName OK `date +%R:%S` ===== "
283       echo "  ===== $TestName OK `date +%R:%S` ===== " >>test.out
284       if test "$debug" -eq 0 ; then
285          scripts/cleanup
286       fi
287    fi
288 }
289
290 copy_tape_confs()
291 {
292    scripts/copy-tape-confs
293    scripts/cleanup-tape
294 }
295
296 copy_test_confs()
297 {
298    scripts/copy-test-confs
299    scripts/cleanup
300 }
301
302 debug_wait()
303 {
304   if test "x${REGRESS_WAIT}" = "x1"; then
305      echo "Start Bacula under debugger and enter anything when ready ..."
306      read a
307   fi
308 }
309
310 init_slot()
311 {
312    DRIVE=$1
313    SLOT=$2
314    if test -n "$DRIVE" -a -n "$SLOT"; then
315       if test ! -c $DRIVE -a ! -b $DRIVE -a x$USE_VTAPE != x ; then
316          dir=`dirname $DRIVE`
317          touch $dir/slot$SLOT
318       fi
319    fi
320 }
321
322 init_drive()
323 {
324   if test a$USE_VTAPE = a; then
325     mt -f $1 rewind
326     mt -f $1 weof
327   else
328     cp /dev/null $1
329   fi
330 }
331
332 rewind_drive()
333 {
334   if test a$USE_VTAPE = a; then
335     mt -f $1 rewind
336   fi
337 }
338
339 load_slot1() 
340 {
341 # Get a tape from slot1
342 slot=`bin/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
343 case $slot in
344  0)
345     bin/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
346     slot=$SLOT1
347     ;;
348  $SLOT1)
349     slot=$SLOT1
350     ;;
351  *)
352     rewind_drive ${TAPE_DRIVE}
353     bin/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
354     bin/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
355     slot=$SLOT1
356     ;;
357 esac
358 }
359
360 #
361 # $1 has currently loaded slot, load the other one i.e. if 1, load 2;
362 #    if 2, load 1; if 0 load 1
363 #
364 load_other_slot()
365 {
366 rewind_drive ${TAPE_DRIVE}
367 case $1 in
368  0)
369     bin/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
370     slot=1
371     ;;
372  $SLOT1)
373     bin/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
374     bin/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
375     slot=2
376     ;;
377  $SLOT2)
378     bin/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
379     bin/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
380     slot=1
381     ;;
382  *)
383     echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
384     exit 1
385     ;;
386 esac
387 }
388
389
390 # Save current directory
391 cwd=`pwd`
392 if test "x${REGRESS_DEBUG}" = "x1"; then
393    set_debug 1
394 else
395    set_debug 0
396 fi
397
398 # Source the configuration variables
399 . ${cwd}/config
400
401 db_name=${db_name:-"regress"}
402 db_user=${db_user:-"regress"}
403 db_password=${db_password:-""}
404
405 CLIENT=${HOST}-fd
406
407 if [ x$USE_VTAPE = xyes ]; then
408    mkdir -p $cwd/working/ach
409    SLOT1=1
410    SLOT2=2
411    TAPE_DRIVE=$cwd/working/ach/drive1
412    TAPE_DRIVE1=$cwd/working/ach/drive1
413    AUTOCHANGER=$cwd/working/ach/config
414    AUTOCHANGER_SCRIPT=disk-changer
415    DRIVE1=0
416    DRIVE2=1
417    cat > $AUTOCHANGER <<EOF
418 maxdrive=2
419 maxslot=10
420 EOF
421 fi
422
423 AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}