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