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