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