8f2a1bbb3fb5b3f8309a00334a7496e668d87190
[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                 *)      test_dir=`dirname $1`
285                         test_dir=${test_dir#$SRC_DIR/*}
286                         test_name=`basename $1`
287                         group_file=$SRC_DIR/$test_dir/group
288
289                         if egrep "^$test_name" $group_file >/dev/null ; then
290                                 # in group file ... OK
291                                 echo $SRC_DIR/$test_dir/$test_name >>$tmp.arglist
292                         else
293                                 # oops
294                                 echo "$1 - unknown test, ignored"
295                         fi
296                         ;;
297                 esac
298
299                 shift
300         done
301 fi
302
303 # we need common/rc
304 if ! . ./common/rc
305 then
306     echo "check: failed to source common/rc"
307     exit 1
308 fi
309
310 if [ `id -u` -ne 0 ]
311 then
312     echo "check: QA must be run as root"
313     exit 1
314 fi
315
316 _wipe_counters()
317 {
318         n_try="0"
319         n_bad="0"
320         unset try notrun bad
321 }
322
323 _wrapup()
324 {
325         seq="check"
326         check="$RESULT_BASE/check"
327
328         if $showme; then
329         :
330         elif $needwrap; then
331                 if [ -f $check.time -a -f $tmp.time ]; then
332                         cat $check.time $tmp.time  \
333                                 | $AWK_PROG '
334                                 { t[$1] = $2 }
335                                 END {
336                                         if (NR > 0) {
337                                                 for (i in t) print i " " t[i]
338                                         }
339                                 }' \
340                                 | sort -n >$tmp.out
341                         mv $tmp.out $check.time
342                 fi
343
344                 echo "" >>$check.log
345                 date >>$check.log
346
347                 echo "SECTION       -- $section" >>$tmp.summary
348                 echo "=========================" >>$tmp.summary
349                 if [ ! -z "$n_try" -a $n_try != 0 ]; then
350                         if [ $brief_test_summary == "false" ]; then
351                                 echo "Ran:$try"
352                                 echo "Ran:$try" >>$tmp.summary
353                         fi
354                         echo "Ran:$try" >>$check.log
355                 fi
356
357                 $interrupt && echo "Interrupted!" >>$check.log
358
359                 if [ ! -z "$notrun" ]; then
360                         if [ $brief_test_summary == "false" ]; then
361                                 echo "Not run:$notrun"
362                                 echo "Not run:$notrun" >>$tmp.summary
363                         fi
364                         echo "Not run:$notrun" >>$check.log
365                 fi
366
367                 if [ ! -z "$n_bad" -a $n_bad != 0 ]; then
368                         echo "Failures:$bad"
369                         echo "Failed $n_bad of $n_try tests"
370                         echo "Failures:$bad" >>$check.log
371                         echo "Failed $n_bad of $n_try tests" >>$check.log
372                         echo "Failures:$bad" >>$tmp.summary
373                         echo "Failed $n_bad of $n_try tests" >>$tmp.summary
374                 else
375                         echo "Passed all $n_try tests"
376                         echo "Passed all $n_try tests" >>$check.log
377                         echo "Passed all $n_try tests" >>$tmp.summary
378                 fi
379                 echo "" >>$tmp.summary
380                 needwrap=false
381         fi
382
383         sum_bad=`expr $sum_bad + $n_bad`
384         _wipe_counters
385         rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
386         if ! $OPTIONS_HAVE_SECTIONS; then
387                 rm -f $tmp.*
388         fi
389 }
390
391 _summary()
392 {
393         _wrapup
394         if $showme; then
395                 :
396         elif $needsum; then
397                 count=`wc -L $tmp.summary | cut -f1 -d" "`
398                 cat $tmp.summary
399                 needsum=false
400         fi
401         rm -f $tmp.*
402 }
403
404 _check_filesystems()
405 {
406         if [ -f ${RESULT_DIR}/require_test ]; then
407                 _check_test_fs || err=true
408                 rm -f ${RESULT_DIR}/require_test
409         fi
410         if [ -f ${RESULT_DIR}/require_scratch ]; then
411                 _check_scratch_fs || err=true
412                 rm -f ${RESULT_DIR}/require_scratch
413         fi
414 }
415
416 _prepare_test_list
417
418 if $OPTIONS_HAVE_SECTIONS; then
419         trap "_summary; exit \$status" 0 1 2 3 15
420 else
421         trap "_wrapup; exit \$status" 0 1 2 3 15
422 fi
423
424 for section in $HOST_OPTIONS_SECTIONS; do
425         OLD_FSTYP=$FSTYP
426         OLD_MOUNT_OPTIONS=$MOUNT_OPTIONS
427         get_next_config $section
428
429         # Do we need to run only some sections ?
430         if [ ! -z "$RUN_SECTION" ]; then
431                 skip=true
432                 for s in $RUN_SECTION; do
433                         if [ $section == $s ]; then
434                                 skip=false
435                                 break;
436                         fi
437                 done
438                 if $skip; then
439                         continue
440                 fi
441         fi
442
443         # Did this section get excluded?
444         if [ ! -z "$EXCLUDE_SECTION" ]; then
445                 skip=false
446                 for s in $EXCLUDE_SECTION; do
447                         if [ $section == $s ]; then
448                                 skip=true
449                                 break;
450                         fi
451                 done
452                 if $skip; then
453                         continue
454                 fi
455         fi
456
457         mkdir -p $RESULT_BASE
458         if [ ! -d $RESULT_BASE ]; then
459                 echo "failed to create results directory $RESULT_BASE"
460                 status=1
461                 exit
462         fi
463
464         if $OPTIONS_HAVE_SECTIONS; then
465                 echo "SECTION       -- $section"
466         fi
467
468         if $RECREATE_TEST_DEV || [ "$OLD_FSTYP" != "$FSTYP" ]; then
469                 echo "RECREATING    -- $FSTYP on $TEST_DEV"
470                 _test_unmount 2> /dev/null
471                 if ! _test_mkfs >$tmp.err 2>&1
472                 then
473                         echo "our local _test_mkfs routine ..."
474                         cat $tmp.err
475                         echo "check: failed to mkfs \$TEST_DEV using specified options"
476                         status=1
477                         exit
478                 fi
479                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
480                 if [ $? -ne 1 ]; then
481                         echo $out
482                         status=1
483                         exit
484                 fi
485                 _prepare_test_list
486         elif [ "$OLD_MOUNT_OPTIONS" != "$MOUNT_OPTIONS" ]; then
487                 _test_unmount 2> /dev/null
488                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
489                 if [ $? -ne 1 ]; then
490                         echo $out
491                         status=1
492                         exit
493                 fi
494         fi
495
496         init_rc
497
498         seq="check"
499         check="$RESULT_BASE/check"
500
501         # don't leave old full output behind on a clean run
502         rm -f $check.full
503
504         [ -f $check.time ] || touch $check.time
505
506         # print out our test configuration
507         echo "FSTYP         -- `_full_fstyp_details`"
508         echo "PLATFORM      -- `_full_platform_details`"
509         if [ ! -z "$SCRATCH_DEV" ]; then
510           echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
511           echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
512         fi
513         echo
514         needwrap=true
515
516         if [ ! -z "$SCRATCH_DEV" ]; then
517           _scratch_unmount 2> /dev/null
518           # call the overridden mkfs - make sure the FS is built
519           # the same as we'll create it later.
520
521           if ! _scratch_mkfs >$tmp.err 2>&1
522           then
523               echo "our local _scratch_mkfs routine ..."
524               cat $tmp.err
525               echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
526               status=1
527               exit
528           fi
529
530           # call the overridden mount - make sure the FS mounts with
531           # the same options that we'll mount with later.
532           if ! _scratch_mount >$tmp.err 2>&1
533           then
534               echo "our local mount routine ..."
535               cat $tmp.err
536               echo "check: failed to mount \$SCRATCH_DEV using specified options"
537               status=1
538               exit
539           fi
540         fi
541
542         seqres="$check"
543         _check_test_fs
544
545         for seq in $list
546         do
547             err=false
548             if [ ! -f $seq ]; then
549                 # Try to get full name in case the user supplied only seq id
550                 # and the test has a name. A bit of hassle to find really
551                 # the test and not its sample output or helping files.
552                 bname=$(basename $seq)
553                 full_seq=$(find $(dirname $seq) -name $bname* -executable |
554                     awk '(NR == 1 || length < length(shortest)) { shortest = $0 }\
555                         END { print shortest }')
556                 if [ -f $full_seq ] \
557                     && [ x$(echo $bname | grep -o "^$VALID_TEST_ID") != x ]; then
558                     seq=$full_seq
559                 fi
560             fi
561
562             # the filename for the test and the name output are different.
563             # we don't include the tests/ directory in the name output.
564             export seqnum=`echo $seq | sed -e "s;$SRC_DIR/;;"`
565
566             # Similarly, the result directory needs to replace the tests/
567             # part of the test location.
568             group=`dirname $seq`
569             if $OPTIONS_HAVE_SECTIONS; then
570                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;${RESULT_BASE}/$section;"`
571                 seqres="$RESULT_BASE/$section/$seqnum"
572             else
573                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;$RESULT_BASE;"`
574                 seqres="$RESULT_BASE/$seqnum"
575             fi
576
577             mkdir -p $RESULT_DIR
578
579             echo -n "$seqnum"
580
581                 if $showme; then
582                         echo
583                         continue
584                 fi
585
586                 if [ ! -f $seq ]; then
587                         echo " - no such test?"
588                 else
589                 # really going to try and run this one
590                 #
591                 rm -f $seqres.out.bad
592
593                 # check if we really should run it
594                 if [ -s $tmp.xlist ]; then
595                         if grep $seqnum $tmp.xlist > /dev/null 2>&1 ; then
596                                 echo "       [expunged]"
597                                 continue
598                         fi
599                 fi
600
601                 # slashes now in names, sed barfs on them so use grep
602                 lasttime=`grep -w ^$seqnum $check.time | awk '// {print $2}'`
603                 if [ "X$lasttime" != X ]; then
604                         echo -n " ${lasttime}s ..."
605                 else
606                         echo -n "       "       # prettier output with timestamps.
607                 fi
608                 rm -f core $seqres.notrun
609
610                 start=`_wallclock`
611                 $timestamp && echo -n " ["`date "+%T"`"]"
612                 [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
613                 $LOGGER_PROG "run xfstest $seqnum"
614                 if [ -w /dev/kmsg ]; then
615                         export date_time=`date +"%F %T"`
616                         echo "run fstests $seqnum at $date_time" > /dev/kmsg
617                         # _check_dmesg depends on this log in dmesg
618                         touch ${RESULT_DIR}/check_dmesg
619                 fi
620                 if [ "$DUMP_OUTPUT" = true ]; then
621                         ./$seq 2>&1 | tee $tmp.rawout
622                         # Because $? would get tee's return code
623                         sts=${PIPESTATUS[0]}
624                 else
625                         ./$seq >$tmp.rawout 2>&1
626                         sts=$?
627                 fi
628                 $timestamp && _timestamp
629                 stop=`_wallclock`
630
631                 _fix_malloc <$tmp.rawout >$tmp.out
632                 rm -f $tmp.rawout
633
634                 if [ -f core ]
635                 then
636                     echo -n " [dumped core]"
637                     mv core $RESULT_BASE/$seqnum.core
638                     err=true
639                 fi
640
641                 if [ -f $seqres.notrun ]
642                 then
643                     $timestamp || echo -n " [not run] "
644                     $timestamp && echo " [not run]" && echo -n "        $seqnum -- "
645                     cat $seqres.notrun
646                     notrun="$notrun $seqnum"
647                 else
648                     if [ $sts -ne 0 ]
649                     then
650                         echo -n " [failed, exit status $sts]"
651                         err=true
652                     fi
653                     if [ ! -f $seq.out ]
654                     then
655                         echo " - no qualified output"
656                         err=true
657                     else
658
659                         # coreutils 8.16+ changed quote formats in error messages from
660                         # `foo' to 'foo'. Filter old versions to match the new version.
661                         sed -i "s/\`/\'/g" $tmp.out
662                         if diff $seq.out $tmp.out >/dev/null 2>&1
663                         then
664                             if $err
665                             then
666                                 :
667                             else
668                                 echo "$seqnum `expr $stop - $start`" >>$tmp.time
669                                 echo -n " `expr $stop - $start`s"
670                             fi
671                             echo ""
672                         else
673                             echo " - output mismatch (see $seqres.out.bad)"
674                             mv $tmp.out $seqres.out.bad
675                             $diff $seq.out $seqres.out.bad | {
676                                 if test "$DIFF_LENGTH" -le 0; then
677                                         cat
678                                 else
679                                         head -n "$DIFF_LENGTH"
680                                         echo "..."
681                                         echo "(Run '$diff $seq.out $seqres.out.bad'" \
682                                                 " to see the entire diff)"
683                                 fi; } | \
684                                 sed -e 's/^\(.\)/    \1/'
685                             err=true
686                         fi
687                     fi
688                     try="$try $seqnum"
689                     n_try=`expr $n_try + 1`
690                     _check_filesystems
691                     _check_dmesg || err=true
692                 fi
693
694             fi
695
696             # come here for each test, except when $showme is true
697             #
698             if $err
699             then
700                 bad="$bad $seqnum"
701                 n_bad=`expr $n_bad + 1`
702                 quick=false
703             fi
704
705             seq="after_$seqnum"
706         done
707         _wrapup
708         echo
709
710         _test_unmount 2> /dev/null
711         _scratch_unmount 2> /dev/null
712 done
713
714 interrupt=false
715 status=`expr $sum_bad`
716 exit