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