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