e8d39438a33d06366c19dfab8af96162efea6fa8
[xfstests-dev.git] / check
1 #!/bin/bash
2 #
3 # Control script for QA
4 #
5 # Copyright (c) 2000-2002,2006 Silicon Graphics, Inc.  All Rights Reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it would be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write the Free Software Foundation,
18 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 #
21
22 tmp=/tmp/$$
23 status=0
24 needwrap=true
25 n_try=0
26 try=""
27 n_bad=0
28 bad=""
29 notrun=""
30 interrupt=true
31 diff="diff -u"
32 showme=false
33 have_test_arg=false
34 randomize=false
35 here=`pwd`
36 FSTYP=xfs
37 xfile=""
38
39 # start the initialisation work now
40 iam=check
41
42 export MSGVERB="text:action"
43 export QA_CHECK_FS=${QA_CHECK_FS:=true}
44
45 # number of diff lines from a failed test, 0 for whole output
46 export DIFF_LENGTH=${DIFF_LENGTH:=10}
47
48 # by default don't output timestamps
49 timestamp=${TIMESTAMP:=false}
50
51 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.xlist
52
53 # we need common/config
54 if ! . ./common/config
55 then
56     echo "$iam: failed to source common/config"
57     exit 1
58 fi
59
60 # Autodetect fs type based on what's on $TEST_DEV
61 if [ "$HOSTOS" == "Linux" ]; then
62     FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
63 fi
64 export FSTYP
65
66 SUPPORTED_TESTS="[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]"
67 SRC_GROUPS="generic shared"
68 export SRC_DIR="tests"
69
70 usage()
71 {
72     echo "Usage: $0 [options] [testlist]"'
73
74 check options
75     -nfs                test NFS
76     -tmpfs              test TMPFS
77     -l                  line mode diff
78     -udiff              show unified diff (default)
79     -n                  show me, do not run tests
80     -T                  output timestamps
81     -r                  randomize test order
82     --large-fs          optimise scratch device for large filesystems
83
84 testlist options
85     -g group[,group...] include tests from these groups
86     -x group[,group...] exclude tests from these groups
87     -X file             exclude individual tests
88     [testlist]          include tests matching names in testlist
89 '
90             exit 0
91 }
92
93 get_group_list()
94 {
95         grp=$1
96
97         for d in $SRC_GROUPS $FSTYP; do
98                 l=$(sed -n < $SRC_DIR/$d/group \
99                         -e 's/#.*//' \
100                         -e 's/$/ /' \
101                         -e "s;\(^[0-9][0-9][0-9]\).* $grp .*;$SRC_DIR/$d/\1;p")
102                 grpl="$grpl $l"
103         done
104         echo $grpl
105 }
106
107 # find all tests, excluding files that are test metadata such as group files.
108 # This assumes that tests are defined purely by alphanumeric filenames with no
109 # ".xyz" extensions in the name.
110 get_all_tests()
111 {
112         touch $tmp.list
113         for d in $SRC_GROUPS $FSTYP; do
114                 ls $SRC_DIR/$d/* | \
115                         grep -v "\..*" | \
116                         grep -v "group\|Makefile" >> $tmp.list 2>/dev/null
117         done
118 }
119
120 # takes the list of tests to run in $tmp.list, and removes the tests passed to
121 # the function from that list.
122 trim_test_list()
123 {
124         test_list="$*"
125
126         rm -f $tmp.grep
127         numsed=0
128         for t in $test_list
129         do
130             if [ $numsed -gt 100 ]; then
131                 grep -v -f $tmp.grep <$tmp.list >$tmp.tmp
132                 mv $tmp.tmp $tmp.list
133                 numsed=0
134                 rm -f $tmp.grep
135             fi
136             echo "^$t\$" >>$tmp.grep
137             numsed=`expr $numsed + 1`
138         done
139         grep -v -f $tmp.grep <$tmp.list >$tmp.tmp
140         mv $tmp.tmp $tmp.list
141 }
142
143
144 _wallclock()
145 {
146     date "+%H %M %S" | $AWK_PROG '{ print $1*3600 + $2*60 + $3 }'
147 }
148
149 _timestamp()
150 {
151     now=`date "+%T"`
152     echo -n " [$now]"
153 }
154
155 _prepare_test_list()
156 {
157         unset list
158         # Tests specified on the command line
159         if [ -s $tmp.arglist ]; then
160                 cat $tmp.arglist > $tmp.list
161         else
162                 touch $tmp.list
163         fi
164
165         # Specified groups to include
166         for group in $GROUP_LIST; do
167                 list=$(get_group_list $group)
168                 if [ -z "$list" ]; then
169                         echo "Group \"$group\" is empty or not defined?"
170                         exit 1
171                 fi
172
173                 for t in $list; do
174                         grep -s "^$t\$" $tmp.list >/dev/null || \
175                                                         echo "$t" >>$tmp.list
176                 done
177         done
178
179         if ! $have_test_arg && [ -z "$GROUP_LIST" ]; then
180                 # no test numbers, do everything
181                 get_all_tests
182         fi
183
184         # Specified groups to exclude
185         for xgroup in $XGROUP_LIST; do
186                 list=$(get_group_list $xgroup)
187                 if [ -z "$list" ]; then
188                         echo "Group \"$xgroup\" is empty or not defined?"
189                         exit 1
190                 fi
191
192                 trim_test_list $list
193         done
194
195         # sort the list of tests into numeric order
196         list=`sort -n $tmp.list | uniq`
197         rm -f $tmp.list $tmp.tmp $tmp.grep
198
199         if $randomize
200         then
201                 list=`echo $list | awk -f randomize.awk`
202         fi
203 }
204
205 # Process command arguments first.
206 while [ $# -gt 0 ]; do
207         case "$1" in
208         -\? | -h | --help) usage ;;
209
210         -nfs)   FSTYP=nfs ;;
211         -tmpfs) FSTYP=tmpfs ;;
212
213         -g)     group=$2 ; shift ;
214                 GROUP_LIST="$GROUP_LIST $group"
215                 ;;
216
217         -x)     xgroup=$2 ; shift ;
218                 XGROUP_LIST="$XGROUP_LIST $xgroup"
219                 ;;
220
221         -X)     xfile=$2; shift ;
222                 for d in $SRC_GROUPS $FSTYP; do
223                         [ -f $SRC_DIR/$d/$xfile ] || continue
224                         for f in `cat $SRC_DIR/$d/$xfile`; do
225                                 echo $d/$f >> $tmp.xlist
226                         done
227                 done
228                 ;;
229
230         -l)     diff="diff" ;;
231         -udiff) diff="$diff -u" ;;
232
233         -n)     showme=true ;;
234         -r)     randomize=true ;;
235
236         -T)     timestamp=true ;;
237
238         --large-fs) export LARGE_SCRATCH_DEV=yes ;;
239         --extra-space=*) export SCRATCH_DEV_EMPTY_SPACE=${r#*=} ;;
240
241         -*)     usage ;;
242         *)      # not an argument, we've got tests now.
243                 have_test_arg=true ;;
244         esac
245
246         # if we've found a test specification, the break out of the processing
247         # loop before we shift the arguments so that this is the first argument
248         # that we process in the test arg loop below.
249         if $have_test_arg; then
250                 break;
251         fi
252
253         shift
254 done
255
256 # Process tests from command line now.
257 if $have_test_arg; then
258         while [ $# -gt 0 ]; do
259                 case "$1" in
260                 -*)     echo "Argments before tests, please!"
261                         status=1
262                         exit $status
263                         ;;
264                 *)      test_dir=`dirname $1`
265                         test_dir=${test_dir#$SRC_DIR/*}
266                         test_name=`basename $1`
267                         group_file=$SRC_DIR/$test_dir/group
268
269                         if egrep "^$test_name" $group_file >/dev/null ; then
270                                 # in group file ... OK
271                                 echo $SRC_DIR/$test_dir/$test_name >>$tmp.arglist
272                         else
273                                 # oops
274                                 echo "$1 - unknown test, ignored"
275                         fi
276                         ;;
277                 esac
278
279                 shift
280         done
281 fi
282
283 _prepare_test_list
284
285 # we need common/rc
286 if ! . ./common/rc
287 then
288     echo "check: failed to source common/rc"
289     exit 1
290 fi
291
292 if [ `id -u` -ne 0 ]
293 then
294     echo "check: QA must be run as root"
295     exit 1
296 fi
297
298 # Ok, time to start running...
299
300 _wrapup()
301 {
302     seq="check"
303     check="$RESULT_BASE/check"
304
305     if $showme
306     then
307         :
308     elif $needwrap
309     then
310         if [ -f $check.time -a -f $tmp.time ]
311         then
312             cat $check.time $tmp.time \
313             | $AWK_PROG '
314         { t[$1] = $2 }
315 END     { if (NR > 0) {
316             for (i in t) print i " " t[i]
317           }
318         }' \
319             | sort -n >$tmp.out
320             mv $tmp.out $check.time
321         fi
322
323         echo "" >>$check.log
324         date >>$check.log
325         echo $list | fmt | sed -e 's/^/    /' -e "s;$SRC_DIR/;;g" >>$check.log
326         $interrupt && echo "Interrupted!" >>$check.log
327         
328         if [ ! -z "$n_try" -a $n_try != 0 ]
329         then
330             echo "Ran:$try"
331         fi
332
333         if [ ! -z "$notrun" ]
334         then
335             echo "Not run:$notrun"
336             echo "Not run:$notrun" >>$check.log
337         fi
338
339         if [ ! -z "$n_bad" -a $n_bad != 0 ]
340         then
341             echo "Failures:$bad"
342             echo "Failed $n_bad of $n_try tests"
343             echo "Failures:$bad" | fmt >>$check.log
344             echo "Failed $n_bad of $n_try tests" >>$check.log
345         else
346             echo "Passed all $n_try tests"
347             echo "Passed all $n_try tests" >>$check.log
348         fi
349         needwrap=false
350     fi
351
352     rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
353     rm -f $tmp.*
354 }
355
356 trap "_wrapup; exit \$status" 0 1 2 3 15
357
358 mkdir -p $RESULT_BASE
359 if [ ! -d $RESULT_BASE ]; then
360         echo "failed to create results directory $RESULTS_BASE"
361         exit 1;
362 fi
363
364 seq="check"
365 check="$RESULT_BASE/check"
366
367 # don't leave old full output behind on a clean run
368 rm -f $check.full
369
370 [ -f $check.time ] || touch $check.time
371
372 # print out our test configuration
373 echo "FSTYP         -- `_full_fstyp_details`"
374 echo "PLATFORM      -- `_full_platform_details`"
375 if [ ! -z "$SCRATCH_DEV" ]; then
376   echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
377   echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
378 fi
379 echo
380
381
382 if [ ! -z "$SCRATCH_DEV" ]; then
383   umount $SCRATCH_DEV 2>/dev/null
384   # call the overridden mkfs - make sure the FS is built
385   # the same as we'll create it later.
386
387   if ! _scratch_mkfs $flag >$tmp.err 2>&1
388   then
389       echo "our local _scratch_mkfs routine ..."
390       cat $tmp.err
391       echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
392       exit 1
393   fi
394
395   # call the overridden mount - make sure the FS mounts with
396   # the same options that we'll mount with later.
397   if ! _scratch_mount >$tmp.err 2>&1
398   then
399       echo "our local mount routine ..."
400       cat $tmp.err
401       echo "check: failed to mount \$SCRATCH_DEV using specified options"
402       exit 1
403   fi
404 fi
405
406 seqres="$check"
407 _check_test_fs
408
409 for seq in $list
410 do
411     err=false
412
413     # the filename for the test and the name output are different.
414     # we don't include the tests/ directory in the name output.
415     seqnum=`echo $seq | sed -e "s;$SRC_DIR/;;"`
416
417     # Similarly, the result directory needs to replace the tests/
418     # part of the test location.
419     group=`dirname $seq`
420     export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;$RESULT_BASE;"`
421     mkdir -p $RESULT_DIR
422     seqres="$RESULT_BASE/$seqnum"
423
424     echo -n "$seqnum"
425
426     if $showme
427     then
428         echo
429         continue
430     elif [ ! -f $seq ]
431     then
432         echo " - no such test?"
433     else
434         # really going to try and run this one
435         #
436         rm -f $seqres.out.bad
437
438         # check if we really should run it
439         if [ -s $tmp.xlist ]; then
440                 if grep $seqnum $tmp.xlist > /dev/null 2>&1 ; then
441                         echo "       [expunged]"
442                         continue
443                 fi
444         fi
445
446         # slashes now in names, sed barfs on them so use grep
447         lasttime=`grep -w ^$seqnum $check.time | awk '// {print $2}'`
448         if [ "X$lasttime" != X ]; then
449                 echo -n " ${lasttime}s ..."
450         else
451                 echo -n "       "       # prettier output with timestamps.
452         fi
453         rm -f core $seqres.notrun
454
455         start=`_wallclock`
456         $timestamp && echo -n " ["`date "+%T"`"]"
457         [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
458         $LOGGER_PROG "run xfstest $seqnum"
459         ./$seq >$tmp.rawout 2>&1
460         sts=$?
461         $timestamp && _timestamp
462         stop=`_wallclock`
463
464         _fix_malloc <$tmp.rawout >$tmp.out
465         rm -f $tmp.rawout
466
467         if [ -f core ]
468         then
469             echo -n " [dumped core]"
470             mv core $RESULT_BASE/$seqnum.core
471             err=true
472         fi
473
474         if [ -f $seqres.notrun ]
475         then
476             $timestamp || echo -n " [not run] "
477             $timestamp && echo " [not run]" && echo -n "        $seqnum -- "
478             cat $seqres.notrun
479             notrun="$notrun $seqnum"
480         else
481             if [ $sts -ne 0 ]
482             then
483                 echo -n " [failed, exit status $sts]"
484                 err=true
485             fi
486             if [ ! -f $seq.out ]
487             then
488                 echo " - no qualified output"
489                 err=true
490             else
491
492                 # coreutils 8.16+ changed quote formats in error messages from
493                 # `foo' to 'foo'. Filter old versions to match the new version.
494                 sed -i "s/\`/\'/g" $tmp.out
495                 if diff $seq.out $tmp.out >/dev/null 2>&1
496                 then
497                     if $err
498                     then
499                         :
500                     else
501                         echo "$seqnum `expr $stop - $start`" >>$tmp.time
502                         echo -n " `expr $stop - $start`s"
503                     fi
504                     echo ""
505                 else
506                     echo " - output mismatch (see $seqres.out.bad)"
507                     mv $tmp.out $seqres.out.bad
508                     $diff $seq.out $seqres.out.bad | {
509                         if test "$DIFF_LENGTH" -le 0; then
510                                 cat
511                         else
512                                 head -n "$DIFF_LENGTH"
513                                 echo "..."
514                                 echo "(Run '$diff $seq.out $seqres.out.bad'" \
515                                         " to see the entire diff)"
516                         fi; } | \
517                         sed -e 's/^\(.\)/    \1/'
518                     err=true
519                 fi
520             fi
521         fi
522
523     fi
524
525     # come here for each test, except when $showme is true
526     #
527     if $err
528     then
529         bad="$bad $seqnum"
530         n_bad=`expr $n_bad + 1`
531         quick=false
532     fi
533     if [ ! -f $seqres.notrun ]
534     then
535         try="$try $seqnum"
536         n_try=`expr $n_try + 1`
537         _check_test_fs
538     fi
539
540     seq="after_$seqnum"
541 done
542
543 interrupt=false
544 status=`expr $n_bad`
545 exit