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