A few improvements to the "test" commands to neaten them up and
[rsync/rsync.git] / runtests.sh
1 #! /bin/sh
2
3 # Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version
7 # 2 as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18
19 # rsync top-level test script -- this invokes all the other more
20 # detailed tests in order.  This script can either be called by `make
21 # check' or `make installcheck'.  `check' runs against the copies of
22 # the program and other files in the build directory, and
23 # `installcheck' against the installed copy of the program.  
24
25 # In either case we need to also be able to find the source directory,
26 # since we read test scripts and possibly other information from
27 # there.
28
29 # Whenever possible, informational messages are written to stdout and
30 # error messages to stderr.  They're separated out by the build farm
31 # display scripts.
32
33 # According to the GNU autoconf manual, the only valid place to set up
34 # directory locations is through Make, since users are allowed to (try
35 # to) change their mind on the Make command line.  So, Make has to
36 # pass in all the values we need.
37
38 # For other configured settings we read ./config.sh, which tells us
39 # about shell commands on this machine and similar things.
40
41 # rsync_bin gives the location of the rsync binary.  This is either
42 # builddir/rsync if we're testing an uninstalled copy, or
43 # install_prefix/bin/rsync if we're testing an installed copy.  On the
44 # build farm rsync will be installed, but into a scratch /usr.
45
46 # srcdir gives the location of the source tree, which lets us find the
47 # build scripts.  At the moment we assume we are invoked from the
48 # source directory.
49
50 # This script must be invoked from the build directory.  
51
52 # A scratch directory, 'testtmp', is created in the build directory to
53 # hold working files.
54
55 # This script also uses the $loglevel environment variable.  1 is the
56 # default value, and 10 the most verbose.  You can set this from the
57 # Make command line.  It's also set by the build farm to give more
58 # detail for failing builds.
59
60
61 # NOTES FOR TEST CASES:
62
63 # Each test case runs in its own shell. 
64
65 # Exit codes from tests:
66
67 #    1  tests failed
68 #    2  error in starting tests
69 #   77  this test skipped (random value unlikely to happen by chance, same as
70 #       automake)
71
72 # HOWEVER, the overall exit code to the farm is different: we return
73 # the *number of tests that failed*, so that it will show up nicely in
74 # the overall summary.
75
76 # rsync.fns contains some general setup functions and definitions.
77
78
79 # NOTES ON PORTABILITY:
80
81 # Both this script and the Makefile have to be pretty conservative
82 # about which Unix features they use.
83
84 # We cannot count on Make exporting variables to commands, unless
85 # they're explicitly given on the command line.
86
87 # Also, we can't count on 'cp -a' or 'mkdir -p', although they're
88 # pretty handy.
89
90 # I think some of the GNU documentation suggests that we shouldn't
91 # rely on shell functions.  However, the Bash manual seems to say that
92 # they're in POSIX 1003.2, and since the build farm relies on them
93 # they're probably working on most machines we really care about.
94
95 # You cannot use "function foo {" syntax, but must instead say "foo()
96 # {", or it breaks on FreeBSD.
97
98 # BSD machines tend not to have "head" or "seq".
99
100 # You cannot do "export VAR=VALUE" all on one line; the export must be
101 # separate from the assignment.  (SCO SysV)
102
103
104
105 # STILL TO DO:
106
107 # We need a good protection against tests that hang indefinitely.
108 # Perhaps some combination of starting them in the background, wait,
109 # and kill?
110
111 # Perhaps we need a common way to cleanup tests.  At the moment just
112 # clobbering the directory when we're done should be enough.
113
114 # If any of the targets fail, then (GNU?) Make returns 2, instead of
115 # the return code from the failing command.  This is fine, but it
116 # means that the build farm just shows "2" for failed tests, not the
117 # number of tests that actually failed.  For more details we might
118 # need to grovel through the log files to find a line saying how many
119 # failed.
120
121
122 set -e
123
124 . "./shconfig"
125
126 RUNSHFLAGS='-e'
127
128 # for Solaris
129 [ -d /usr/xpg4/bin ] && PATH="/usr/xpg4/bin/:$PATH"
130
131 if [ "x$loglevel" != x ] && [ "$loglevel" -gt 8 ]; then
132     if set -x; then
133         # If it doesn't work the first time, don't keep trying.
134         RUNSHFLAGS="$RUNSHFLAGS -x"
135     fi
136 fi
137
138 echo "============================================================"
139 echo "$0 running in `pwd`"
140 echo "    rsync_bin=$rsync_bin"
141 echo "    srcdir=$srcdir"
142
143 if [ -f /usr/bin/whoami ]; then
144     testuser=`/usr/bin/whoami`
145 elif [ -f /usr/ucb/whoami ]; then
146     testuser=`/usr/ucb/whoami`
147 else
148     testuser=`id -un || whoami || echo ${LOGNAME:-${USERNAME:-${USER:-'UNKNOWN'}}}`
149 fi
150
151 echo "    testuser=$testuser"
152 echo "    os=`uname -a`"
153
154 # It must be "yes", not just nonnull
155 if [ "x$preserve_scratch" = xyes ]; then
156     echo "    preserve_scratch=yes"
157 else
158     echo "    preserve_scratch=no"
159 fi    
160
161
162 if [ ! -f "$rsync_bin" ]; then
163     echo "rsync_bin $rsync_bin is not a file" >&2
164     exit 2
165 fi
166
167 if [ ! -d "$srcdir" ]; then
168     echo "srcdir $srcdir is not a directory" >&2
169     exit 2
170 fi
171
172 RSYNC="$rsync_bin"
173 #RSYNC="valgrind --tool=addrcheck $rsync_bin"
174
175 export rsync_bin RSYNC
176
177 skipped=0
178 missing=0
179 passed=0
180 failed=0
181
182 # Prefix for scratch directory.  We create separate directories for
183 # each test case, so that they can be left behind in case of failure
184 # to aid investigation.
185 scratchbase="`pwd`"/testtmp
186 echo "    scratchbase=$scratchbase"
187
188 suitedir="$srcdir/testsuite"
189
190 export scratchdir suitedir
191
192 prep_scratch() {
193     [ -d "$scratchdir" ] && rm -rf "$scratchdir"
194     mkdir "$scratchdir"
195     return 0
196 }
197
198 maybe_discard_scratch() {
199     [ x"$preserve_scratch" != xyes ] && [ -d "$scratchdir" ] && rm -rf "$scratchdir"
200     return 0
201 }
202
203 if [ "x$whichtests" = x ]; then
204     whichtests="*.test"
205 fi
206
207 for testscript in $suitedir/$whichtests
208 do
209     testbase=`echo $testscript | sed -e 's!.*/!!' -e 's/.test\$//'`
210     scratchdir="$scratchbase.$testbase"
211
212     prep_scratch
213
214     set +e
215     sh $RUNSHFLAGS "$testscript" >"$scratchdir/test.log" 2>&1
216     result=$?
217     set -e
218
219     if [ "x$always_log" = xyes -o \( $result != 0 -a $result != 77 -a $result != 78 \) ]
220     then
221         echo "----- $testbase log follows"
222         cat "$scratchdir/test.log"
223         echo "----- $testbase log ends"
224         if [ -f "$scratchdir/rsyncd.log" ]; then
225             echo "----- $testbase rsyncd.log follows"
226             cat "$scratchdir/rsyncd.log"
227             echo "----- $testbase rsyncd.log ends"
228         fi
229     fi
230
231     case $result in
232     0)
233         echo "PASS    $testbase"
234         passed=`expr $passed + 1`
235         maybe_discard_scratch
236         ;;
237     77)
238         # backticks will fill the whole file onto one line, which is a feature
239         whyskipped=`cat "$scratchdir/whyskipped"`
240         echo "SKIP    $testbase ($whyskipped)"
241         skipped=`expr $skipped + 1`
242         maybe_discard_scratch
243         ;;
244     78)
245         # It failed, but we expected that.  don't dump out error logs, 
246         # because most users won't want to see them.  But do leave
247         # the working directory around.
248         echo "XFAIL   $testbase"
249         failed=`expr $failed + 1`
250         ;;
251     *)
252         echo "FAIL    $testbase"
253         failed=`expr $failed + 1`
254         if [ "x$nopersist" = xyes ]; then
255             exit 1
256         fi
257     esac
258 done
259
260 echo '------------------------------------------------------------'
261 echo "----- overall results:"
262 echo "      $passed passed"
263 [ "$failed" -gt 0 ]  && echo "      $failed failed"
264 [ "$skipped" -gt 0 ] && echo "      $skipped skipped"
265 [ "$missing" -gt 0 ] && echo "      $missing missing"
266 echo '------------------------------------------------------------'
267
268 # OK, so expr exits with 0 if the result is neither null nor zero; and
269 # 1 if the expression is null or zero.  This is the opposite of what
270 # we want, and if we just call expr then this script will always fail,
271 # because -e is set.
272
273 result=`expr $failed + $missing || true`
274 echo "overall result is $result"
275 exit $result