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