check: fix excluding test groups
[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 brief_test_summary=false
40
41 DUMP_OUTPUT=false
42
43 # start the initialisation work now
44 iam=check
45
46 export MSGVERB="text:action"
47 export QA_CHECK_FS=${QA_CHECK_FS:=true}
48
49 # number of diff lines from a failed test, 0 for whole output
50 export DIFF_LENGTH=${DIFF_LENGTH:=10}
51
52 # by default don't output timestamps
53 timestamp=${TIMESTAMP:=false}
54
55 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.xlist
56
57 SRC_GROUPS="generic shared"
58 export SRC_DIR="tests"
59
60 usage()
61 {
62     echo "Usage: $0 [options] [testlist]"'
63
64 check options
65     -nfs                test NFS
66     -cifs               test CIFS
67     -overlay            test overlay
68     -tmpfs              test TMPFS
69     -l                  line mode diff
70     -udiff              show unified diff (default)
71     -n                  show me, do not run tests
72     -T                  output timestamps
73     -r                  randomize test order
74     -d                  dump test output to stdout
75     -b                  brief test summary
76     --large-fs          optimise scratch device for large filesystems
77     -s section          run only specified section from config file
78     -S section          exclude the specified section from the config file
79
80 testlist options
81     -g group[,group...] include tests from these groups
82     -x group[,group...] exclude tests from these groups
83     -X file             exclude individual tests
84     -E external_file    exclude individual tests
85     [testlist]          include tests matching names in testlist
86 '
87             exit 0
88 }
89
90 get_group_list()
91 {
92         grp=$1
93
94         for d in $SRC_GROUPS $FSTYP; do
95                 if ! test -d "$SRC_DIR/$d" ; then
96                         continue
97                 fi
98                 l=$(sed -n < $SRC_DIR/$d/group \
99                         -e 's/#.*//' \
100                         -e 's/$/ /' \
101                         -e "s;^\($VALID_TEST_NAME\).* $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 # It matches test names against $VALID_TEST_NAME defined in common/rc
109 get_all_tests()
110 {
111         touch $tmp.list
112         for d in $SRC_GROUPS $FSTYP; do
113                 if ! test -d "$SRC_DIR/$d" ; then
114                         continue
115                 fi
116                 ls $SRC_DIR/$d/* | \
117                         grep -v "\..*" | \
118                         grep "^$SRC_DIR/$d/$VALID_TEST_NAME"| \
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 "+%s"
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         -cifs)          FSTYP=cifs ;;
215         -overlay)       FSTYP=overlay ;;
216         -tmpfs)         FSTYP=tmpfs ;;
217
218         -g)     group=$2 ; shift ;
219                 GROUP_LIST="$GROUP_LIST ${group//,/ }"
220                 ;;
221
222         -x)     xgroup=$2 ; shift ;
223                 XGROUP_LIST="$XGROUP_LIST ${xgroup//,/ }"
224                 ;;
225
226         -X)     xfile=$2; shift ;
227                 for d in $SRC_GROUPS $FSTYP; do
228                         [ -f $SRC_DIR/$d/$xfile ] || continue
229                         for f in `sed "s/#.*$//" $SRC_DIR/$d/$xfile`; do
230                                 echo $d/$f >> $tmp.xlist
231                         done
232                 done
233                 ;;
234         -E)     xfile=$2; shift ;
235                 if [ -f $xfile ]; then
236                         sed "s/#.*$//" "$xfile" >> $tmp.xlist
237                 fi
238                 ;;
239         -s)     RUN_SECTION="$RUN_SECTION $2"; shift ;;
240         -S)     EXCLUDE_SECTION="$EXCLUDE_SECTION $2"; shift ;;
241         -l)     diff="diff" ;;
242         -udiff) diff="$diff -u" ;;
243
244         -n)     showme=true ;;
245         -r)     randomize=true ;;
246
247         -T)     timestamp=true ;;
248         -d)     DUMP_OUTPUT=true ;;
249         -b)     brief_test_summary=true;;
250
251         --large-fs) export LARGE_SCRATCH_DEV=yes ;;
252         --extra-space=*) export SCRATCH_DEV_EMPTY_SPACE=${r#*=} ;;
253
254         -*)     usage ;;
255         *)      # not an argument, we've got tests now.
256                 have_test_arg=true ;;
257         esac
258
259         # if we've found a test specification, the break out of the processing
260         # loop before we shift the arguments so that this is the first argument
261         # that we process in the test arg loop below.
262         if $have_test_arg; then
263                 break;
264         fi
265
266         shift
267 done
268
269 # we need common/config, source it after processing args, overlay needs FSTYP
270 # set before sourcing common/config
271 if ! . ./common/config; then
272         echo "$iam: failed to source common/config"
273         exit 1
274 fi
275
276 # Process tests from command line now.
277 if $have_test_arg; then
278         while [ $# -gt 0 ]; do
279                 case "$1" in
280                 -*)     echo "Arguments before tests, please!"
281                         status=1
282                         exit $status
283                         ;;
284                 *)      # Expand test pattern (e.g. xfs/???, *fs/001)
285                         list=$(cd $SRC_DIR; echo $1)
286                         for t in $list; do
287                                 test_dir=`dirname $t`
288                                 test_dir=${test_dir#$SRC_DIR/*}
289                                 test_name=`basename $t`
290                                 group_file=$SRC_DIR/$test_dir/group
291
292                                 if egrep -q "^$test_name" $group_file; then
293                                         # in group file ... OK
294                                         echo $SRC_DIR/$test_dir/$test_name \
295                                                 >>$tmp.arglist
296                                 else
297                                         # oops
298                                         echo "$t - unknown test, ignored"
299                                 fi
300                         done
301                         ;;
302                 esac
303
304                 shift
305         done
306 fi
307
308 # we need common/rc
309 if ! . ./common/rc
310 then
311     echo "check: failed to source common/rc"
312     exit 1
313 fi
314
315 if [ `id -u` -ne 0 ]
316 then
317     echo "check: QA must be run as root"
318     exit 1
319 fi
320
321 _wipe_counters()
322 {
323         n_try="0"
324         n_bad="0"
325         unset try notrun bad
326 }
327
328 _wrapup()
329 {
330         seq="check"
331         check="$RESULT_BASE/check"
332
333         if $showme; then
334         :
335         elif $needwrap; then
336                 if [ -f $check.time -a -f $tmp.time ]; then
337                         cat $check.time $tmp.time  \
338                                 | $AWK_PROG '
339                                 { t[$1] = $2 }
340                                 END {
341                                         if (NR > 0) {
342                                                 for (i in t) print i " " t[i]
343                                         }
344                                 }' \
345                                 | sort -n >$tmp.out
346                         mv $tmp.out $check.time
347                 fi
348
349                 echo "" >>$check.log
350                 date >>$check.log
351
352                 echo "SECTION       -- $section" >>$tmp.summary
353                 echo "=========================" >>$tmp.summary
354                 if [ ! -z "$n_try" -a $n_try != 0 ]; then
355                         if [ $brief_test_summary == "false" ]; then
356                                 echo "Ran:$try"
357                                 echo "Ran:$try" >>$tmp.summary
358                         fi
359                         echo "Ran:$try" >>$check.log
360                 fi
361
362                 $interrupt && echo "Interrupted!" >>$check.log
363
364                 if [ ! -z "$notrun" ]; then
365                         if [ $brief_test_summary == "false" ]; then
366                                 echo "Not run:$notrun"
367                                 echo "Not run:$notrun" >>$tmp.summary
368                         fi
369                         echo "Not run:$notrun" >>$check.log
370                 fi
371
372                 if [ ! -z "$n_bad" -a $n_bad != 0 ]; then
373                         echo "Failures:$bad"
374                         echo "Failed $n_bad of $n_try tests"
375                         echo "Failures:$bad" >>$check.log
376                         echo "Failed $n_bad of $n_try tests" >>$check.log
377                         echo "Failures:$bad" >>$tmp.summary
378                         echo "Failed $n_bad of $n_try tests" >>$tmp.summary
379                 else
380                         echo "Passed all $n_try tests"
381                         echo "Passed all $n_try tests" >>$check.log
382                         echo "Passed all $n_try tests" >>$tmp.summary
383                 fi
384                 echo "" >>$tmp.summary
385                 needwrap=false
386         fi
387
388         sum_bad=`expr $sum_bad + $n_bad`
389         _wipe_counters
390         rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
391         if ! $OPTIONS_HAVE_SECTIONS; then
392                 rm -f $tmp.*
393         fi
394 }
395
396 _summary()
397 {
398         _wrapup
399         if $showme; then
400                 :
401         elif $needsum; then
402                 count=`wc -L $tmp.summary | cut -f1 -d" "`
403                 cat $tmp.summary
404                 needsum=false
405         fi
406         rm -f $tmp.*
407 }
408
409 _check_filesystems()
410 {
411         if [ -f ${RESULT_DIR}/require_test ]; then
412                 _check_test_fs || err=true
413                 rm -f ${RESULT_DIR}/require_test*
414         fi
415         if [ -f ${RESULT_DIR}/require_scratch ]; then
416                 _check_scratch_fs || err=true
417                 rm -f ${RESULT_DIR}/require_scratch*
418         fi
419 }
420
421 _prepare_test_list
422
423 if $OPTIONS_HAVE_SECTIONS; then
424         trap "_summary; exit \$status" 0 1 2 3 15
425 else
426         trap "_wrapup; exit \$status" 0 1 2 3 15
427 fi
428
429 for section in $HOST_OPTIONS_SECTIONS; do
430         OLD_FSTYP=$FSTYP
431         OLD_MOUNT_OPTIONS=$MOUNT_OPTIONS
432         get_next_config $section
433
434         # Do we need to run only some sections ?
435         if [ ! -z "$RUN_SECTION" ]; then
436                 skip=true
437                 for s in $RUN_SECTION; do
438                         if [ $section == $s ]; then
439                                 skip=false
440                                 break;
441                         fi
442                 done
443                 if $skip; then
444                         continue
445                 fi
446         fi
447
448         # Did this section get excluded?
449         if [ ! -z "$EXCLUDE_SECTION" ]; then
450                 skip=false
451                 for s in $EXCLUDE_SECTION; do
452                         if [ $section == $s ]; then
453                                 skip=true
454                                 break;
455                         fi
456                 done
457                 if $skip; then
458                         continue
459                 fi
460         fi
461
462         mkdir -p $RESULT_BASE
463         if [ ! -d $RESULT_BASE ]; then
464                 echo "failed to create results directory $RESULT_BASE"
465                 status=1
466                 exit
467         fi
468
469         if $OPTIONS_HAVE_SECTIONS; then
470                 echo "SECTION       -- $section"
471         fi
472
473         if $RECREATE_TEST_DEV || [ "$OLD_FSTYP" != "$FSTYP" ]; then
474                 echo "RECREATING    -- $FSTYP on $TEST_DEV"
475                 _test_unmount 2> /dev/null
476                 if ! _test_mkfs >$tmp.err 2>&1
477                 then
478                         echo "our local _test_mkfs routine ..."
479                         cat $tmp.err
480                         echo "check: failed to mkfs \$TEST_DEV using specified options"
481                         status=1
482                         exit
483                 fi
484                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
485                 if [ $? -ne 1 ]; then
486                         echo $out
487                         status=1
488                         exit
489                 fi
490                 _prepare_test_list
491         elif [ "$OLD_MOUNT_OPTIONS" != "$MOUNT_OPTIONS" ]; then
492                 _test_unmount 2> /dev/null
493                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
494                 if [ $? -ne 1 ]; then
495                         echo $out
496                         status=1
497                         exit
498                 fi
499         fi
500
501         init_rc
502
503         seq="check"
504         check="$RESULT_BASE/check"
505
506         # don't leave old full output behind on a clean run
507         rm -f $check.full
508
509         [ -f $check.time ] || touch $check.time
510
511         # print out our test configuration
512         echo "FSTYP         -- `_full_fstyp_details`"
513         echo "PLATFORM      -- `_full_platform_details`"
514         if [ ! -z "$SCRATCH_DEV" ]; then
515           echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
516           echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
517         fi
518         echo
519         needwrap=true
520
521         if [ ! -z "$SCRATCH_DEV" ]; then
522           _scratch_unmount 2> /dev/null
523           # call the overridden mkfs - make sure the FS is built
524           # the same as we'll create it later.
525
526           if ! _scratch_mkfs >$tmp.err 2>&1
527           then
528               echo "our local _scratch_mkfs routine ..."
529               cat $tmp.err
530               echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
531               status=1
532               exit
533           fi
534
535           # call the overridden mount - make sure the FS mounts with
536           # the same options that we'll mount with later.
537           if ! _scratch_mount >$tmp.err 2>&1
538           then
539               echo "our local mount routine ..."
540               cat $tmp.err
541               echo "check: failed to mount \$SCRATCH_DEV using specified options"
542               status=1
543               exit
544           fi
545         fi
546
547         seqres="$check"
548         _check_test_fs
549
550         for seq in $list
551         do
552             err=false
553             if [ ! -f $seq ]; then
554                 # Try to get full name in case the user supplied only seq id
555                 # and the test has a name. A bit of hassle to find really
556                 # the test and not its sample output or helping files.
557                 bname=$(basename $seq)
558                 full_seq=$(find $(dirname $seq) -name $bname* -executable |
559                     awk '(NR == 1 || length < length(shortest)) { shortest = $0 }\
560                         END { print shortest }')
561                 if [ -f $full_seq ] \
562                     && [ x$(echo $bname | grep -o "^$VALID_TEST_ID") != x ]; then
563                     seq=$full_seq
564                 fi
565             fi
566
567             # the filename for the test and the name output are different.
568             # we don't include the tests/ directory in the name output.
569             export seqnum=`echo $seq | sed -e "s;$SRC_DIR/;;"`
570
571             # Similarly, the result directory needs to replace the tests/
572             # part of the test location.
573             group=`dirname $seq`
574             if $OPTIONS_HAVE_SECTIONS; then
575                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;${RESULT_BASE}/$section;"`
576                 seqres="$RESULT_BASE/$section/$seqnum"
577             else
578                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;$RESULT_BASE;"`
579                 seqres="$RESULT_BASE/$seqnum"
580             fi
581
582             mkdir -p $RESULT_DIR
583
584             echo -n "$seqnum"
585
586                 if $showme; then
587                         echo
588                         continue
589                 fi
590
591                 if [ ! -f $seq ]; then
592                         echo " - no such test?"
593                 else
594                 # really going to try and run this one
595                 #
596                 rm -f $seqres.out.bad
597
598                 # check if we really should run it
599                 if [ -s $tmp.xlist ]; then
600                         if grep $seqnum $tmp.xlist > /dev/null 2>&1 ; then
601                                 echo "       [expunged]"
602                                 continue
603                         fi
604                 fi
605
606                 # slashes now in names, sed barfs on them so use grep
607                 lasttime=`grep -w ^$seqnum $check.time | awk '// {print $2}'`
608                 if [ "X$lasttime" != X ]; then
609                         echo -n " ${lasttime}s ..."
610                 else
611                         echo -n "       "       # prettier output with timestamps.
612                 fi
613                 rm -f core $seqres.notrun
614
615                 start=`_wallclock`
616                 $timestamp && echo -n " ["`date "+%T"`"]"
617                 [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
618                 $LOGGER_PROG "run xfstest $seqnum"
619                 if [ -w /dev/kmsg ]; then
620                         export date_time=`date +"%F %T"`
621                         echo "run fstests $seqnum at $date_time" > /dev/kmsg
622                         # _check_dmesg depends on this log in dmesg
623                         touch ${RESULT_DIR}/check_dmesg
624                 fi
625                 if [ "$DUMP_OUTPUT" = true ]; then
626                         ./$seq 2>&1 | tee $tmp.rawout
627                         # Because $? would get tee's return code
628                         sts=${PIPESTATUS[0]}
629                 else
630                         ./$seq >$tmp.rawout 2>&1
631                         sts=$?
632                 fi
633                 $timestamp && _timestamp
634                 stop=`_wallclock`
635
636                 _fix_malloc <$tmp.rawout >$tmp.out
637                 rm -f $tmp.rawout
638
639                 if [ -f core ]
640                 then
641                     echo -n " [dumped core]"
642                     mv core $RESULT_BASE/$seqnum.core
643                     err=true
644                 fi
645
646                 if [ -f $seqres.notrun ]
647                 then
648                     $timestamp || echo -n " [not run] "
649                     $timestamp && echo " [not run]" && echo -n "        $seqnum -- "
650                     cat $seqres.notrun
651                     notrun="$notrun $seqnum"
652                 else
653                     if [ $sts -ne 0 ]
654                     then
655                         echo -n " [failed, exit status $sts]"
656                         err=true
657                     fi
658                     if [ ! -f $seq.out ]
659                     then
660                         echo " - no qualified output"
661                         err=true
662                     else
663
664                         # coreutils 8.16+ changed quote formats in error messages from
665                         # `foo' to 'foo'. Filter old versions to match the new version.
666                         sed -i "s/\`/\'/g" $tmp.out
667                         if diff $seq.out $tmp.out >/dev/null 2>&1
668                         then
669                             if $err
670                             then
671                                 :
672                             else
673                                 echo "$seqnum `expr $stop - $start`" >>$tmp.time
674                                 echo -n " `expr $stop - $start`s"
675                             fi
676                             echo ""
677                         else
678                             echo " - output mismatch (see $seqres.out.bad)"
679                             mv $tmp.out $seqres.out.bad
680                             $diff $seq.out $seqres.out.bad | {
681                                 if test "$DIFF_LENGTH" -le 0; then
682                                         cat
683                                 else
684                                         head -n "$DIFF_LENGTH"
685                                         echo "..."
686                                         echo "(Run '$diff $seq.out $seqres.out.bad'" \
687                                                 " to see the entire diff)"
688                                 fi; } | \
689                                 sed -e 's/^\(.\)/    \1/'
690                             err=true
691                         fi
692                     fi
693                     try="$try $seqnum"
694                     n_try=`expr $n_try + 1`
695                     _check_filesystems
696                     _check_dmesg || err=true
697                 fi
698
699             fi
700
701             # come here for each test, except when $showme is true
702             #
703             if $err
704             then
705                 bad="$bad $seqnum"
706                 n_bad=`expr $n_bad + 1`
707                 quick=false
708             fi
709
710             seq="after_$seqnum"
711         done
712         _wrapup
713         echo
714
715         _test_unmount 2> /dev/null
716         _scratch_unmount 2> /dev/null
717 done
718
719 interrupt=false
720 status=`expr $sum_bad`
721 exit