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