b93316a7237b498d526d3f08e39032cda0485836
[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
38 SUPPORTED_TESTS="[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]"
39
40 # generic initialization
41 iam=check
42
43 export QA_CHECK_FS=${QA_CHECK_FS:=true}
44
45 # by default don't output timestamps
46 timestamp=${TIMESTAMP:=false}
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 usage()
55 {
56     echo "Usage: $0 [options] [testlist]"'
57
58 check options
59     -xfs                test XFS (default)
60     -udf                test UDF
61     -nfs                test NFS
62     -l                  line mode diff
63     -udiff              show unified diff (default)
64     -n                  show me, do not run tests
65     -T                  output timestamps
66     -r                  randomize test order
67     --large-fs          optimise scratch device for large filesystems
68
69 testlist options
70     -g group[,group...] include tests from these groups
71     -x group[,group...] exclude tests from these groups
72     NNN                 include test NNN
73     NNN-NNN             include test range (eg. 012-021)
74 '
75             exit 0
76 }
77
78 _setenvironment()
79 {
80     MSGVERB="text:action"
81     export MSGVERB
82 }
83
84 get_group_list()
85 {
86         grp=$1
87
88         grpl=$(sed -n < group \
89                        -e 's/#.*//' \
90                        -e 's/$/ /' \
91                        -e "/^[0-9][0-9][0-9].* $grp /"'{ s/ .*//p }')
92         echo $grpl
93 }
94
95 expand_test_numbers()
96 {
97         # strip leading zeros, could be considered octal.
98         start=`echo $1 | sed 's/^0*//'`
99         end=`echo $2 | sed 's/^0*//'`
100
101         $AWK_PROG </dev/null '
102 BEGIN   { for (t='$start'; t<='$end'; t++) printf "%03d\n",t }' \
103         | while read id
104         do
105                 if grep -s "^$id " group >/dev/null ; then
106                         # in group file ... OK
107                         echo $id >>$tmp.list
108                 else
109                         # oops
110                         echo "$id - unknown test, ignored"
111                 fi
112         done
113 }
114
115 _wallclock()
116 {
117     date "+%H %M %S" | $AWK_PROG '{ print $1*3600 + $2*60 + $3 }'
118 }
119
120 _timestamp()
121 {
122     now=`date "+%T"`
123     echo -n " [$now]"
124 }
125
126 # start the initialisation work now
127 _setenvironment
128
129 rm -f $tmp.list $tmp.tmp $tmp.sed $here/$iam.out
130
131 # Autodetect fs type based on what's on $TEST_DEV
132 if [ "$HOSTOS" == "Linux" ]; then
133     FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
134 fi
135 export FSTYP
136
137 # we need common.config
138 if ! . ./common.config
139 then
140     echo "$iam: failed to source common.config"
141     exit 1
142 fi
143
144 while [ $# -gt 0 ]; do
145         case "$1" in
146         -\? | -h | --help) usage ;;
147
148         -udf)   FSTYP=udf ;;
149         -xfs)   FSTYP=xfs ;;
150         -nfs)   FSTYP=nfs ;;
151
152         -g)     group=$2 ; shift ;
153                 group_list=$(get_group_list $group)
154                 if [ -z "$group_list" ]; then
155                     echo "Group \"$group\" is empty or not defined?"
156                     exit 1
157                 fi
158
159                 [ ! -s $tmp.list ] && touch $tmp.list
160                 for t in $group_list; do
161                         grep -s "^$t\$" $tmp.list >/dev/null || \
162                                                         echo "$t" >>$tmp.list
163                 done
164
165                 ;;
166
167         -x)     xgroup=$2 ; shift ;
168                 [ ! -s $tmp.list ] &&  ls $SUPPORTED_TESTS >$tmp.list 2>/dev/null
169                 group_list=$(get_group_list $xgroup)
170                 if [ -z "$group_list" ]; then
171                     echo "Group \"$xgroup\" is empty or not defined?"
172                     exit 1
173                 fi
174
175                 rm -f $tmp.sed
176                 numsed=0
177                 for t in $group_list
178                 do
179                     if [ $numsed -gt 100 ]; then
180                         sed -f $tmp.sed <$tmp.list >$tmp.tmp
181                         mv $tmp.tmp $tmp.list
182                         numsed=0
183                         rm -f $tmp.sed
184                     fi
185                     echo "/^$t\$/d" >>$tmp.sed
186                     numsed=`expr $numsed + 1`
187                 done
188                 sed -f $tmp.sed <$tmp.list >$tmp.tmp
189                 mv $tmp.tmp $tmp.list
190                 ;;
191
192         -l)     diff="diff" ;;
193         -udiff) diff="$diff -u" ;;
194
195         -n)     showme=true ;;
196         -r)     randomize=true ;;
197
198         -T)     timestamp=true ;;
199
200         "$SUPPORTED_TESTS")
201                 echo "No tests?"
202                 status=1
203                 exit $status
204                 ;;
205
206         [0-9]*-[0-9]*)
207                 eval `echo $1 | sed -e 's/^/start=/' -e 's/-/ end=/'`
208                 expand_test_numbers $start $end
209                 have_test_arg=true
210                 ;;
211
212         [0-9]*-)
213                 eval `echo $1 | sed -e 's/^/start=/' -e 's/-//'`
214                 end=`echo $SUPPORTED_TESTS | sed -e 's/\[0-9]//g' -e 's/  *$//' -e 's/.* //'`
215                 if [ -z "$end" ]; then
216                         echo "No tests in range \"$1\"?"
217                         status=1
218                         exit $status
219                 fi
220                 expand_test_numbers $start $end
221                 have_test_arg=true
222                 ;;
223
224         --large-fs) export LARGE_SCRATCH_DEV=yes ;;
225         --extra-space=*) export SCRATCH_DEV_EMPTY_SPACE=${r#*=} ;;
226
227         -*)     usage ;;
228         *)      expand_test_numbers $1 $1 ;
229                 have_test_arg=true
230                 ;;
231         esac
232
233         shift
234 done
235
236 if [ -s $tmp.list ]; then
237     # found some valid test numbers ... this is good
238     :
239 elif $have_test_arg; then
240         # had test numbers, but none in group file ... do nothing
241         touch $tmp.list
242 else
243         # no test numbers, do everything from group file
244         sed -n -e '/^[0-9][0-9][0-9]*/s/[       ].*//p' <group >$tmp.list
245 fi
246
247 # sort the list of tests into numeric order
248 list=`sort -n $tmp.list`
249 rm -f $tmp.list $tmp.tmp $tmp.sed
250
251 if $randomize
252 then
253     list=`echo $list | awk -f randomize.awk`
254 fi
255
256 # we need common.rc
257 if ! . ./common.rc
258 then
259     echo "check: failed to source common.rc"
260     exit 1
261 fi
262
263 if [ `id -u` -ne 0 ]
264 then
265     echo "check: QA must be run as root"
266     exit 1
267 fi
268
269 # Ok, time to start running...
270
271 _wrapup()
272 {
273     if $showme
274     then
275         :
276     elif $needwrap
277     then
278         if [ -f check.time -a -f $tmp.time ]
279         then
280             cat check.time $tmp.time \
281             | $AWK_PROG '
282         { t[$1] = $2 }
283 END     { if (NR > 0) {
284             for (i in t) print i " " t[i]
285           }
286         }' \
287             | sort -n >$tmp.out
288             mv $tmp.out check.time
289         fi
290
291         echo "" >>check.log
292         date >>check.log
293         echo $list | fmt | sed -e 's/^/    /' >>check.log
294         $interrupt && echo "Interrupted!" >>check.log
295         
296         if [ ! -z "$n_try" -a $n_try != 0 ]
297         then
298             echo "Ran:$try"
299         fi
300
301         if [ ! -z "$notrun" ]
302         then
303             echo "Not run:$notrun"
304             echo "Not run:$notrun" >>check.log
305         fi
306
307         if [ ! -z "$n_bad" -a $n_bad != 0 ]
308         then
309             echo "Failures:$bad"
310             echo "Failed $n_bad of $n_try tests"
311             echo "Failures:$bad" | fmt >>check.log
312             echo "Failed $n_bad of $n_try tests" >>check.log
313         else
314             echo "Passed all $n_try tests"
315             echo "Passed all $n_try tests" >>check.log
316         fi
317         needwrap=false
318     fi
319
320     rm -f /tmp/*.rawout /tmp/*.out /tmp/*.err /tmp/*.time
321     rm -f $tmp.*
322 }
323
324 trap "_wrapup; exit \$status" 0 1 2 3 15
325
326 # don't leave old full output behind on a clean run
327 rm -f check.full
328
329 [ -f check.time ] || touch check.time
330
331 # print out our test configuration
332 echo "FSTYP         -- `_full_fstyp_details`"
333 echo "PLATFORM      -- `_full_platform_details`"
334 if [ ! -z "$SCRATCH_DEV" ]; then
335   echo "MKFS_OPTIONS  -- `_scratch_mkfs_options`"
336   echo "MOUNT_OPTIONS -- `_scratch_mount_options`"
337 fi
338 echo
339
340
341 if [ ! -z "$SCRATCH_DEV" ]; then
342   umount $SCRATCH_DEV 2>/dev/null
343   # call the overridden mkfs - make sure the FS is built
344   # the same as we'll create it later.
345
346   if ! _scratch_mkfs $flag >$tmp.err 2>&1
347   then
348       echo "our local _scratch_mkfs routine ..."
349       cat $tmp.err
350       echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
351       exit 1
352   fi
353
354   # call the overridden mount - make sure the FS mounts with
355   # the same options that we'll mount with later.
356   if ! _scratch_mount >$tmp.err 2>&1
357   then
358       echo "our local mount routine ..."
359       cat $tmp.err
360       echo "check: failed to mount \$SCRATCH_DEV using specified options"
361       exit 1
362   fi
363 fi
364
365 seq="check"
366 _check_test_fs
367
368 for seq in $list
369 do
370     err=false
371     echo -n "$seq"
372
373     if $showme
374     then
375         echo
376         continue
377     elif [ ! -f $seq ]
378     then
379         echo " - no such test?"
380     else
381         # really going to try and run this one
382         #
383         rm -f $seq.out.bad
384         lasttime=`sed -n -e "/^$seq /s/.* //p" <check.time`
385         if [ "X$lasttime" != X ]; then
386                 echo -n " ${lasttime}s ..."
387         else
388                 echo -n "       "       # prettier output with timestamps.
389         fi
390         rm -f core $seq.notrun
391
392         start=`_wallclock`
393         $timestamp && echo -n " ["`date "+%T"`"]"
394         [ ! -x $seq ] && chmod u+x $seq # ensure we can run it
395         $LOGGER_PROG "run xfstest $seq"
396         ./$seq >$tmp.rawout 2>&1
397         sts=$?
398         $timestamp && _timestamp
399         stop=`_wallclock`
400
401         _fix_malloc <$tmp.rawout >$tmp.out
402         rm -f $tmp.rawout
403
404         if [ -f core ]
405         then
406             echo -n " [dumped core]"
407             mv core $seq.core
408             err=true
409         fi
410
411         if [ -f $seq.notrun ]
412         then
413             $timestamp || echo -n " [not run] "
414             $timestamp && echo " [not run]" && echo -n "        $seq -- "
415             cat $seq.notrun
416             notrun="$notrun $seq"
417         else
418             if [ $sts -ne 0 ]
419             then
420                 echo -n " [failed, exit status $sts]"
421                 err=true
422             fi
423             if [ ! -f $seq.out ]
424             then
425                 echo " - no qualified output"
426                 err=true
427             else
428                 if diff $seq.out $tmp.out >/dev/null 2>&1
429                 then
430                     if $err
431                     then
432                         :
433                     else
434                         echo "$seq `expr $stop - $start`" >>$tmp.time
435                         echo -n " `expr $stop - $start`s"
436                     fi
437                     echo ""
438                 else
439                     echo " - output mismatch (see $seq.out.bad)"
440                     mv $tmp.out $seq.out.bad
441                     $diff $seq.out $seq.out.bad | {
442                         if test "$DIFF_LENGTH" -le 0; then
443                                 cat
444                         else
445                                 head -n "$DIFF_LENGTH"
446                         fi; } | \
447                         sed -e 's/^\(.\)/    \1/'
448                     echo "     ..."
449                     echo "     (Run '$diff $seq.out $seq.out.bad' to see the" \
450                          "entire diff)"
451                     err=true
452                 fi
453             fi
454         fi
455
456     fi
457
458     # come here for each test, except when $showme is true
459     #
460     if $err
461     then
462         bad="$bad $seq"
463         n_bad=`expr $n_bad + 1`
464         quick=false
465     fi
466     if [ ! -f $seq.notrun ]
467     then
468         try="$try $seq"
469         n_try=`expr $n_try + 1`
470         _check_test_fs
471     fi
472     
473     seq="after_$seq"
474 done
475
476 interrupt=false
477 status=`expr $n_bad`
478 exit