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