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