generic/317,318: fail gracefully if userns not supported
[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 SUPPORTED_TESTS="[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]"
62 SRC_GROUPS="generic shared"
63 export SRC_DIR="tests"
64
65 usage()
66 {
67     echo "Usage: $0 [options] [testlist]"'
68
69 check options
70     -nfs                test NFS
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;\(^[0-9][0-9][0-9]\).* $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 # This assumes that tests are defined purely by alphanumeric filenames with no
106 # ".xyz" extensions in the name.
107 get_all_tests()
108 {
109         touch $tmp.list
110         for d in $SRC_GROUPS $FSTYP; do
111                 ls $SRC_DIR/$d/* | \
112                         grep -v "\..*" | \
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         -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         -l)     diff="diff" ;;
233         -udiff) diff="$diff -u" ;;
234
235         -n)     showme=true ;;
236         -r)     randomize=true ;;
237
238         -T)     timestamp=true ;;
239
240         --large-fs) export LARGE_SCRATCH_DEV=yes ;;
241         --extra-space=*) export SCRATCH_DEV_EMPTY_SPACE=${r#*=} ;;
242
243         -*)     usage ;;
244         *)      # not an argument, we've got tests now.
245                 have_test_arg=true ;;
246         esac
247
248         # if we've found a test specification, the break out of the processing
249         # loop before we shift the arguments so that this is the first argument
250         # that we process in the test arg loop below.
251         if $have_test_arg; then
252                 break;
253         fi
254
255         shift
256 done
257
258 # Process tests from command line now.
259 if $have_test_arg; then
260         while [ $# -gt 0 ]; do
261                 case "$1" in
262                 -*)     echo "Argments before tests, please!"
263                         status=1
264                         exit $status
265                         ;;
266                 *)      test_dir=`dirname $1`
267                         test_dir=${test_dir#$SRC_DIR/*}
268                         test_name=`basename $1`
269                         group_file=$SRC_DIR/$test_dir/group
270
271                         if egrep "^$test_name" $group_file >/dev/null ; then
272                                 # in group file ... OK
273                                 echo $SRC_DIR/$test_dir/$test_name >>$tmp.arglist
274                         else
275                                 # oops
276                                 echo "$1 - unknown test, ignored"
277                         fi
278                         ;;
279                 esac
280
281                 shift
282         done
283 fi
284
285 # we need common/rc
286 if ! . ./common/rc
287 then
288     echo "check: failed to source common/rc"
289     exit 1
290 fi
291
292 if [ `id -u` -ne 0 ]
293 then
294     echo "check: QA must be run as root"
295     exit 1
296 fi
297
298 _wipe_counters()
299 {
300         n_try="0"
301         n_bad="0"
302         unset try notrun bad
303 }
304
305 _wrapup()
306 {
307     seq="check"
308     check="$RESULT_BASE/check"
309
310     if $showme
311     then
312         :
313     elif $needwrap
314     then
315         if [ -f $check.time -a -f $tmp.time ]
316         then
317             cat $check.time $tmp.time \
318             | $AWK_PROG '
319         { t[$1] = $2 }
320 END     { if (NR > 0) {
321             for (i in t) print i " " t[i]
322           }
323         }' \
324             | sort -n >$tmp.out
325             mv $tmp.out $check.time
326         fi
327
328         echo "" >>$check.log
329         date >>$check.log
330         echo $list | fmt | sed -e 's/^/    /' -e "s;$SRC_DIR/;;g" >>$check.log
331         $interrupt && echo "Interrupted!" >>$check.log
332
333         echo "SECTION       -- $section" >>$tmp.summary
334         echo "=========================" >>$tmp.summary
335         if [ ! -z "$n_try" -a $n_try != 0 ]
336         then
337             echo "Ran:$try"
338             echo "Ran:$try" >>$tmp.summary
339         fi
340
341         if [ ! -z "$notrun" ]
342         then
343             echo "Not run:$notrun"
344             echo "Not run:$notrun" >>$check.log
345             echo "Not run:$notrun" >>$tmp.summary
346         fi
347
348         if [ ! -z "$n_bad" -a $n_bad != 0 ]
349         then
350             echo "Failures:$bad"
351             echo "Failed $n_bad of $n_try tests"
352             echo "Failures:$bad" | fmt >>$check.log
353             echo "Failed $n_bad of $n_try tests" >>$check.log
354             echo "Failures:$bad" >>$tmp.summary
355             echo "Failed $n_bad of $n_try tests" >>$tmp.summary
356         else
357             echo "Passed all $n_try tests"
358             echo "Passed all $n_try tests" >>$check.log
359             echo "Passed all $n_try tests" >>$tmp.summary
360         fi
361         echo "" >>$tmp.summary
362         needwrap=false
363     fi
364
365     sum_bad=`expr $sum_bad + $n_bad`
366     _wipe_counters
367     rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
368     if ! $OPTIONS_HAVE_SECTIONS; then
369         rm -f $tmp.*
370     fi
371 }
372
373 _summary()
374 {
375         _wrapup
376         if $showme; then
377                 :
378         elif $needsum; then
379                 count=`wc -L $tmp.summary | cut -f1 -d" "`
380                 cat $tmp.summary
381                 needsum=false
382         fi
383         rm -f $tmp.*
384 }
385
386 _prepare_test_list
387
388 if $OPTIONS_HAVE_SECTIONS; then
389         trap "_summary; exit \$status" 0 1 2 3 15
390 else
391         trap "_wrapup; exit \$status" 0 1 2 3 15
392 fi
393
394 for section in $HOST_OPTIONS_SECTIONS; do
395         OLD_FSTYP=$FSTYP
396         OLD_MOUNT_OPTIONS=$MOUNT_OPTIONS
397         get_next_config $section
398
399         # Do we need to run only some sections ?
400         if [ ! -z "$RUN_SECTION" ]; then
401                 skip=true
402                 for s in $RUN_SECTION; do
403                         if [ $section == $s ]; then
404                                 skip=false
405                         fi
406                 done
407                 if $skip; then
408                         continue
409                 fi
410         fi
411
412         mkdir -p $RESULT_BASE
413         if [ ! -d $RESULT_BASE ]; then
414                 echo "failed to create results directory $RESULT_BASE"
415                 exit 1;
416         fi
417
418         if $OPTIONS_HAVE_SECTIONS; then
419                 echo "SECTION       -- $section"
420         fi
421
422         if $RECREATE_TEST_DEV || [ "$OLD_FSTYP" != "$FSTYP" ]; then
423                 echo "RECREATING    -- $FSTYP on $TEST_DEV"
424                 umount $TEST_DEV 2> /dev/null
425                 if ! _test_mkfs >$tmp.err 2>&1
426                 then
427                         echo "our local _test_mkfs routine ..."
428                         cat $tmp.err
429                         echo "check: failed to mkfs \$TEST_DEV using specified options"
430                         exit 1
431                 fi
432                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
433                 if [ $? -ne 1 ]; then
434                         echo $out
435                         exit 1
436                 fi
437                 _prepare_test_list
438         elif [ "$OLD_MOUNT_OPTIONS" != "$MOUNT_OPTIONS" ]; then
439                 umount $TEST_DEV 2> /dev/null
440                 out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
441                 if [ $? -ne 1 ]; then
442                         echo $out
443                         exit 1
444                 fi
445         fi
446
447         init_rc
448
449         seq="check"
450         check="$RESULT_BASE/check"
451
452         # don't leave old full output behind on a clean run
453         rm -f $check.full
454
455         [ -f $check.time ] || touch $check.time
456
457         # print out our test configuration
458         echo "FSTYP         -- `_full_fstyp_details`"
459         echo "PLATFORM      -- `_full_platform_details`"
460         if [ ! -z "$SCRATCH_DEV" ]; then
461           echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
462           echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
463         fi
464         echo
465         needwrap=true
466
467         if [ ! -z "$SCRATCH_DEV" ]; then
468           umount $SCRATCH_DEV 2>/dev/null
469           # call the overridden mkfs - make sure the FS is built
470           # the same as we'll create it later.
471
472           if ! _scratch_mkfs $flag >$tmp.err 2>&1
473           then
474               echo "our local _scratch_mkfs routine ..."
475               cat $tmp.err
476               echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
477               exit 1
478           fi
479
480           # call the overridden mount - make sure the FS mounts with
481           # the same options that we'll mount with later.
482           if ! _scratch_mount >$tmp.err 2>&1
483           then
484               echo "our local mount routine ..."
485               cat $tmp.err
486               echo "check: failed to mount \$SCRATCH_DEV using specified options"
487               exit 1
488           fi
489         fi
490
491         seqres="$check"
492         _check_test_fs
493
494         for seq in $list
495         do
496             err=false
497
498             # the filename for the test and the name output are different.
499             # we don't include the tests/ directory in the name output.
500             seqnum=`echo $seq | sed -e "s;$SRC_DIR/;;"`
501
502             # Similarly, the result directory needs to replace the tests/
503             # part of the test location.
504             group=`dirname $seq`
505             if $OPTIONS_HAVE_SECTIONS; then
506                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;${RESULT_BASE}/$section;"`
507                 seqres="$RESULT_BASE/$section/$seqnum"
508             else
509                 export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;$RESULT_BASE;"`
510                 seqres="$RESULT_BASE/$seqnum"
511             fi
512
513             mkdir -p $RESULT_DIR
514
515             echo -n "$seqnum"
516
517             if $showme
518             then
519                 echo
520                 continue
521             elif [ ! -f $seq ]
522             then
523                 echo " - no such test?"
524             else
525                 # really going to try and run this one
526                 #
527                 rm -f $seqres.out.bad
528
529                 # check if we really should run it
530                 if [ -s $tmp.xlist ]; then
531                         if grep $seqnum $tmp.xlist > /dev/null 2>&1 ; then
532                                 echo "       [expunged]"
533                                 continue
534                         fi
535                 fi
536
537                 # slashes now in names, sed barfs on them so use grep
538                 lasttime=`grep -w ^$seqnum $check.time | awk '// {print $2}'`
539                 if [ "X$lasttime" != X ]; then
540                         echo -n " ${lasttime}s ..."
541                 else
542                         echo -n "       "       # prettier output with timestamps.
543                 fi
544                 rm -f core $seqres.notrun
545
546                 start=`_wallclock`
547                 $timestamp && echo -n " ["`date "+%T"`"]"
548                 [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
549                 $LOGGER_PROG "run xfstest $seqnum"
550                 ./$seq >$tmp.rawout 2>&1
551                 sts=$?
552                 $timestamp && _timestamp
553                 stop=`_wallclock`
554
555                 _fix_malloc <$tmp.rawout >$tmp.out
556                 rm -f $tmp.rawout
557
558                 if [ -f core ]
559                 then
560                     echo -n " [dumped core]"
561                     mv core $RESULT_BASE/$seqnum.core
562                     err=true
563                 fi
564
565                 if [ -f $seqres.notrun ]
566                 then
567                     $timestamp || echo -n " [not run] "
568                     $timestamp && echo " [not run]" && echo -n "        $seqnum -- "
569                     cat $seqres.notrun
570                     notrun="$notrun $seqnum"
571                 else
572                     if [ $sts -ne 0 ]
573                     then
574                         echo -n " [failed, exit status $sts]"
575                         err=true
576                     fi
577                     if [ ! -f $seq.out ]
578                     then
579                         echo " - no qualified output"
580                         err=true
581                     else
582
583                         # coreutils 8.16+ changed quote formats in error messages from
584                         # `foo' to 'foo'. Filter old versions to match the new version.
585                         sed -i "s/\`/\'/g" $tmp.out
586                         if diff $seq.out $tmp.out >/dev/null 2>&1
587                         then
588                             if $err
589                             then
590                                 :
591                             else
592                                 echo "$seqnum `expr $stop - $start`" >>$tmp.time
593                                 echo -n " `expr $stop - $start`s"
594                             fi
595                             echo ""
596                         else
597                             echo " - output mismatch (see $seqres.out.bad)"
598                             mv $tmp.out $seqres.out.bad
599                             $diff $seq.out $seqres.out.bad | {
600                                 if test "$DIFF_LENGTH" -le 0; then
601                                         cat
602                                 else
603                                         head -n "$DIFF_LENGTH"
604                                         echo "..."
605                                         echo "(Run '$diff $seq.out $seqres.out.bad'" \
606                                                 " to see the entire diff)"
607                                 fi; } | \
608                                 sed -e 's/^\(.\)/    \1/'
609                             err=true
610                         fi
611                     fi
612                 fi
613
614             fi
615
616             # come here for each test, except when $showme is true
617             #
618             if $err
619             then
620                 bad="$bad $seqnum"
621                 n_bad=`expr $n_bad + 1`
622                 quick=false
623             fi
624             if [ ! -f $seqres.notrun ]
625             then
626                 try="$try $seqnum"
627                 n_try=`expr $n_try + 1`
628                 _check_test_fs
629             fi
630
631             seq="after_$seqnum"
632         done
633         _wrapup
634         echo
635
636         umount $TEST_DEV 2> /dev/null
637         umount $SCRATCH_DEV 2> /dev/null
638 done
639
640 interrupt=false
641 status=`expr $sum_bad`
642 exit