generic: add new test for fsync() on directories
[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 n_try=0
26 try=""
27 n_bad=0
28 bad=""
29 notrun=""
30 interrupt=true
31 diff="diff -u"
32 showme=false
33 have_test_arg=false
34 randomize=false
35 here=`pwd`
36 FSTYP=xfs
37 xfile=""
38
39 # start the initialisation work now
40 iam=check
41
42 export MSGVERB="text:action"
43 export QA_CHECK_FS=${QA_CHECK_FS:=true}
44
45 # number of diff lines from a failed test, 0 for whole output
46 export DIFF_LENGTH=${DIFF_LENGTH:=10}
47
48 # by default don't output timestamps
49 timestamp=${TIMESTAMP:=false}
50
51 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.xlist
52
53 # we need common/config
54 if ! . ./common/config
55 then
56     echo "$iam: failed to source common/config"
57     exit 1
58 fi
59
60 # Autodetect fs type based on what's on $TEST_DEV
61 if [ "$HOSTOS" == "Linux" ]; then
62     FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
63 fi
64 export FSTYP
65
66 SUPPORTED_TESTS="[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]"
67 SRC_GROUPS="generic shared"
68 export SRC_DIR="tests"
69
70 usage()
71 {
72     echo "Usage: $0 [options] [testlist]"'
73
74 check options
75     -nfs                test NFS
76     -l                  line mode diff
77     -udiff              show unified diff (default)
78     -n                  show me, do not run tests
79     -T                  output timestamps
80     -r                  randomize test order
81     --large-fs          optimise scratch device for large filesystems
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     [testlist]          include tests matching names in testlist
88 '
89             exit 0
90 }
91
92 get_group_list()
93 {
94         grp=$1
95
96         for d in $SRC_GROUPS $FSTYP; do
97                 l=$(sed -n < $SRC_DIR/$d/group \
98                         -e 's/#.*//' \
99                         -e 's/$/ /' \
100                         -e "s;\(^[0-9][0-9][0-9]\).* $grp .*;$SRC_DIR/$d/\1;p")
101                 grpl="$grpl $l"
102         done
103         echo $grpl
104 }
105
106 # find all tests, excluding files that are test metadata such as group files.
107 # This assumes that tests are defined purely by alphanumeric filenames with no
108 # ".xyz" extensions in the name.
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 -v "group\|Makefile" >> $tmp.list 2>/dev/null
116         done
117 }
118
119 # takes the list of tests to run in $tmp.list, and removes the tests passed to
120 # the function from that list.
121 trim_test_list()
122 {
123         test_list="$*"
124
125         rm -f $tmp.grep
126         numsed=0
127         for t in $test_list
128         do
129             if [ $numsed -gt 100 ]; then
130                 grep -v -f $tmp.grep <$tmp.list >$tmp.tmp
131                 mv $tmp.tmp $tmp.list
132                 numsed=0
133                 rm -f $tmp.grep
134             fi
135             echo "^$t\$" >>$tmp.grep
136             numsed=`expr $numsed + 1`
137         done
138         grep -v -f $tmp.grep <$tmp.list >$tmp.tmp
139         mv $tmp.tmp $tmp.list
140 }
141
142
143 _wallclock()
144 {
145     date "+%H %M %S" | $AWK_PROG '{ print $1*3600 + $2*60 + $3 }'
146 }
147
148 _timestamp()
149 {
150     now=`date "+%T"`
151     echo -n " [$now]"
152 }
153
154 _prepare_test_list()
155 {
156         unset list
157         # Tests specified on the command line
158         if [ -s $tmp.arglist ]; then
159                 cat $tmp.arglist > $tmp.list
160         else
161                 touch $tmp.list
162         fi
163
164         # Specified groups to include
165         for group in $GROUP_LIST; do
166                 list=$(get_group_list $group)
167                 if [ -z "$list" ]; then
168                         echo "Group \"$group\" is empty or not defined?"
169                         exit 1
170                 fi
171
172                 for t in $list; do
173                         grep -s "^$t\$" $tmp.list >/dev/null || \
174                                                         echo "$t" >>$tmp.list
175                 done
176         done
177
178         if ! $have_test_arg && [ -z "$GROUP_LIST" ]; then
179                 # no test numbers, do everything
180                 get_all_tests
181         fi
182
183         # Specified groups to exclude
184         for xgroup in $XGROUP_LIST; do
185                 list=$(get_group_list $xgroup)
186                 if [ -z "$list" ]; then
187                         echo "Group \"$xgroup\" is empty or not defined?"
188                         exit 1
189                 fi
190
191                 trim_test_list $list
192         done
193
194         # sort the list of tests into numeric order
195         list=`sort -n $tmp.list | uniq`
196         rm -f $tmp.list $tmp.tmp $tmp.grep
197
198         if $randomize
199         then
200                 list=`echo $list | awk -f randomize.awk`
201         fi
202 }
203
204 # Process command arguments first.
205 while [ $# -gt 0 ]; do
206         case "$1" in
207         -\? | -h | --help) usage ;;
208
209         -nfs)   FSTYP=nfs ;;
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
228         -l)     diff="diff" ;;
229         -udiff) diff="$diff -u" ;;
230
231         -n)     showme=true ;;
232         -r)     randomize=true ;;
233
234         -T)     timestamp=true ;;
235
236         --large-fs) export LARGE_SCRATCH_DEV=yes ;;
237         --extra-space=*) export SCRATCH_DEV_EMPTY_SPACE=${r#*=} ;;
238
239         -*)     usage ;;
240         *)      # not an argument, we've got tests now.
241                 have_test_arg=true ;;
242         esac
243
244         # if we've found a test specification, the break out of the processing
245         # loop before we shift the arguments so that this is the first argument
246         # that we process in the test arg loop below.
247         if $have_test_arg; then
248                 break;
249         fi
250
251         shift
252 done
253
254 # Process tests from command line now.
255 if $have_test_arg; then
256         while [ $# -gt 0 ]; do
257                 case "$1" in
258                 -*)     echo "Argments before tests, please!"
259                         status=1
260                         exit $status
261                         ;;
262                 *)      test_dir=`dirname $1`
263                         test_name=`basename $1`
264                         group_file=$SRC_DIR/$test_dir/group
265
266                         if egrep "^$test_name" $group_file >/dev/null ; then
267                                 # in group file ... OK
268                                 echo $SRC_DIR/$1 >>$tmp.arglist
269                         else
270                                 # oops
271                                 echo "$1 - unknown test, ignored"
272                         fi
273                         ;;
274                 esac
275
276                 shift
277         done
278 fi
279
280 _prepare_test_list
281
282 # we need common/rc
283 if ! . ./common/rc
284 then
285     echo "check: failed to source common/rc"
286     exit 1
287 fi
288
289 if [ `id -u` -ne 0 ]
290 then
291     echo "check: QA must be run as root"
292     exit 1
293 fi
294
295 # Ok, time to start running...
296
297 _wrapup()
298 {
299     seq="check"
300     check="$RESULT_BASE/check"
301
302     if $showme
303     then
304         :
305     elif $needwrap
306     then
307         if [ -f $check.time -a -f $tmp.time ]
308         then
309             cat $check.time $tmp.time \
310             | $AWK_PROG '
311         { t[$1] = $2 }
312 END     { if (NR > 0) {
313             for (i in t) print i " " t[i]
314           }
315         }' \
316             | sort -n >$tmp.out
317             mv $tmp.out $check.time
318         fi
319
320         echo "" >>$check.log
321         date >>$check.log
322         echo $list | fmt | sed -e 's/^/    /' -e "s;$SRC_DIR/;;g" >>$check.log
323         $interrupt && echo "Interrupted!" >>$check.log
324         
325         if [ ! -z "$n_try" -a $n_try != 0 ]
326         then
327             echo "Ran:$try"
328         fi
329
330         if [ ! -z "$notrun" ]
331         then
332             echo "Not run:$notrun"
333             echo "Not run:$notrun" >>$check.log
334         fi
335
336         if [ ! -z "$n_bad" -a $n_bad != 0 ]
337         then
338             echo "Failures:$bad"
339             echo "Failed $n_bad of $n_try tests"
340             echo "Failures:$bad" | fmt >>$check.log
341             echo "Failed $n_bad of $n_try tests" >>$check.log
342         else
343             echo "Passed all $n_try tests"
344             echo "Passed all $n_try tests" >>$check.log
345         fi
346         needwrap=false
347     fi
348
349     rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
350     rm -f $tmp.*
351 }
352
353 trap "_wrapup; exit \$status" 0 1 2 3 15
354
355 mkdir -p $RESULT_BASE
356 if [ ! -d $RESULT_BASE ]; then
357         echo "failed to create results directory $RESULTS_BASE"
358         exit 1;
359 fi
360
361 seq="check"
362 check="$RESULT_BASE/check"
363
364 # don't leave old full output behind on a clean run
365 rm -f $check.full
366
367 [ -f $check.time ] || touch $check.time
368
369 # print out our test configuration
370 echo "FSTYP         -- `_full_fstyp_details`"
371 echo "PLATFORM      -- `_full_platform_details`"
372 if [ ! -z "$SCRATCH_DEV" ]; then
373   echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
374   echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
375 fi
376 echo
377
378
379 if [ ! -z "$SCRATCH_DEV" ]; then
380   umount $SCRATCH_DEV 2>/dev/null
381   # call the overridden mkfs - make sure the FS is built
382   # the same as we'll create it later.
383
384   if ! _scratch_mkfs $flag >$tmp.err 2>&1
385   then
386       echo "our local _scratch_mkfs routine ..."
387       cat $tmp.err
388       echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
389       exit 1
390   fi
391
392   # call the overridden mount - make sure the FS mounts with
393   # the same options that we'll mount with later.
394   if ! _scratch_mount >$tmp.err 2>&1
395   then
396       echo "our local mount routine ..."
397       cat $tmp.err
398       echo "check: failed to mount \$SCRATCH_DEV using specified options"
399       exit 1
400   fi
401 fi
402
403 seqres="$check"
404 _check_test_fs
405
406 for seq in $list
407 do
408     err=false
409
410     # the filename for the test and the name output are different.
411     # we don't include the tests/ directory in the name output.
412     seqnum=`echo $seq | sed -e "s;$SRC_DIR/;;"`
413
414     # Similarly, the result directory needs to replace the tests/
415     # part of the test location.
416     group=`dirname $seq`
417     export RESULT_DIR=`echo $group | sed -e "s;$SRC_DIR;$RESULT_BASE;"`
418     mkdir -p $RESULT_DIR
419     seqres="$RESULT_BASE/$seqnum"
420
421     echo -n "$seqnum"
422
423     if $showme
424     then
425         echo
426         continue
427     elif [ ! -f $seq ]
428     then
429         echo " - no such test?"
430     else
431         # really going to try and run this one
432         #
433         rm -f $seqres.out.bad
434
435         # check if we really should run it
436         if [ -s $tmp.xlist ]; then
437                 if grep $seqnum $tmp.xlist > /dev/null 2>&1 ; then
438                         echo "       [expunged]"
439                         continue
440                 fi
441         fi
442
443         # slashes now in names, sed barfs on them so use grep
444         lasttime=`grep -w ^$seqnum $check.time | awk '// {print $2}'`
445         if [ "X$lasttime" != X ]; then
446                 echo -n " ${lasttime}s ..."
447         else
448                 echo -n "       "       # prettier output with timestamps.
449         fi
450         rm -f core $seqres.notrun
451
452         start=`_wallclock`
453         $timestamp && echo -n " ["`date "+%T"`"]"
454         [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
455         $LOGGER_PROG "run xfstest $seqnum"
456         ./$seq >$tmp.rawout 2>&1
457         sts=$?
458         $timestamp && _timestamp
459         stop=`_wallclock`
460
461         _fix_malloc <$tmp.rawout >$tmp.out
462         rm -f $tmp.rawout
463
464         if [ -f core ]
465         then
466             echo -n " [dumped core]"
467             mv core $RESULT_BASE/$seqnum.core
468             err=true
469         fi
470
471         if [ -f $seqres.notrun ]
472         then
473             $timestamp || echo -n " [not run] "
474             $timestamp && echo " [not run]" && echo -n "        $seqnum -- "
475             cat $seqres.notrun
476             notrun="$notrun $seqnum"
477         else
478             if [ $sts -ne 0 ]
479             then
480                 echo -n " [failed, exit status $sts]"
481                 err=true
482             fi
483             if [ ! -f $seq.out ]
484             then
485                 echo " - no qualified output"
486                 err=true
487             else
488
489                 # coreutils 8.16+ changed quote formats in error messages from
490                 # `foo' to 'foo'. Filter old versions to match the new version.
491                 sed -i "s/\`/\'/g" $tmp.out
492                 if diff $seq.out $tmp.out >/dev/null 2>&1
493                 then
494                     if $err
495                     then
496                         :
497                     else
498                         echo "$seqnum `expr $stop - $start`" >>$tmp.time
499                         echo -n " `expr $stop - $start`s"
500                     fi
501                     echo ""
502                 else
503                     echo " - output mismatch (see $seqres.out.bad)"
504                     mv $tmp.out $seqres.out.bad
505                     $diff $seq.out $seqres.out.bad | {
506                         if test "$DIFF_LENGTH" -le 0; then
507                                 cat
508                         else
509                                 head -n "$DIFF_LENGTH"
510                         fi; } | \
511                         sed -e 's/^\(.\)/    \1/'
512                     echo "     ..."
513                     echo "     (Run '$diff $seq.out $seqres.out.bad' to see the" \
514                          "entire diff)"
515                     err=true
516                 fi
517             fi
518         fi
519
520     fi
521
522     # come here for each test, except when $showme is true
523     #
524     if $err
525     then
526         bad="$bad $seqnum"
527         n_bad=`expr $n_bad + 1`
528         quick=false
529     fi
530     if [ ! -f $seqres.notrun ]
531     then
532         try="$try $seqnum"
533         n_try=`expr $n_try + 1`
534         _check_test_fs
535     fi
536
537     seq="after_$seqnum"
538 done
539
540 interrupt=false
541 status=`expr $n_bad`
542 exit