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