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