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