r21700: Run mktestsetup as an external program. This guarantees that it can be used...
[ira/wip.git] / source / script / tests / selftest.sh
1 #!/bin/sh
2 # Bootstrap Samba and run a number of tests against it.
3
4 if [ $# -lt 1 ]
5 then
6         echo "$0 PREFIX TESTS"
7         exit
8 fi
9
10 ARG0=$0
11 ARG1=$1
12 ARG2=$2
13 ARG3=$3
14
15 if [ -z "$TORTURE_MAXTIME" ]; then
16     TORTURE_MAXTIME=1200
17 fi
18
19 # disable rpc validation when using valgrind - its way too slow
20 if [ -z "$VALGRIND" ]; then
21     VALIDATE="validate";
22 else
23     VALIDATE="";
24 fi
25
26 OLD_PWD=`pwd`
27 PREFIX=$ARG1
28 PREFIX=`echo $PREFIX | sed s+//+/+`
29 export PREFIX
30
31 # allow selection of the test lists
32 TESTS=$ARG2
33
34 if [ $TESTS = "all" ]; then
35     TLS_ENABLED="yes"
36 else
37     TLS_ENABLED="no"
38 fi
39 export TLS_ENABLED
40
41 LD_LDB_MODULE_PATH=$OLD_PWD/bin/modules/ldb
42 export LD_LDB_MODULE_PATH
43
44 LD_SAMBA_MODULE_PATH=$OLD_PWD/bin/modules
45 export LD_SAMBA_MODULE_PATH
46
47 LD_LIBRARY_PATH=$OLD_PWD/bin/shared:$LD_LIBRARY_PATH
48 export LD_LIBRARY_PATH
49
50 PKG_CONFIG_PATH=$OLD_PWD/bin/pkgconfig:$PKG_CONFIG_PATH
51 export PKG_CONFIG_PATH
52
53 incdir=`dirname $ARG0`
54 echo -n "PROVISIONING..."
55 eval `$incdir/mktestsetup.sh $PREFIX || exit 1`
56 echo "DONE"
57
58 export KRB5_CONFIG
59 export PREFIX_ABS
60 export TEST_DATA_PREFIX
61 export CONFIGURATION
62 export CONFFILE
63 export PIDDIR
64 export AUTH
65 export SERVER
66 export NETBIOSNAME
67
68 PATH=bin:$PATH
69 export PATH
70
71 DO_SOCKET_WRAPPER=$ARG3
72 if [ x"$DO_SOCKET_WRAPPER" = x"SOCKET_WRAPPER" ];then
73         SOCKET_WRAPPER_DIR="$PREFIX/w"
74         export SOCKET_WRAPPER_DIR
75         echo "SOCKET_WRAPPER_DIR=$SOCKET_WRAPPER_DIR"
76 else
77         echo "NOT USING SOCKET_WRAPPER"
78 fi
79
80 incdir=`dirname $ARG0`
81 . $incdir/test_functions.sh
82
83 #Start slapd before smbd
84 if [ x"$TEST_LDAP" = x"yes" ]; then
85     if test -z "$FEDORA_DS_PREFIX"; then
86         slapd_start || exit 1;
87     else
88         fedora_ds_start || exit 1;
89     fi
90     echo -n "LDAP PROVISIONING..."
91     $srcdir/bin/smbscript $srcdir/setup/provision $PROVISION_OPTIONS "$PROVISION_ACI" --ldap-backend=$LDAP_URI || {
92         echo "LDAP PROVISIONING failed: $srcdir/bin/smbscript $srcdir/setup/provision $PROVISION_OPTIONS $PROVISION_ACI --ldap-backend=$LDAP_URI"
93         exit 1;
94     }
95     #LDAP is slow
96     TORTURE_MAXTIME=`expr $TORTURE_MAXTIME '*' 2`
97 fi
98
99 SMBD_TEST_FIFO="$PREFIX/smbd_test.fifo"
100 export SMBD_TEST_FIFO
101 SMBD_TEST_LOG="$PREFIX/smbd_test.log"
102 export SMBD_TEST_LOG
103
104 SOCKET_WRAPPER_DEFAULT_IFACE=1
105 export SOCKET_WRAPPER_DEFAULT_IFACE
106 smbd_check_or_start
107
108 SOCKET_WRAPPER_DEFAULT_IFACE=6
109 export SOCKET_WRAPPER_DEFAULT_IFACE
110 TORTURE_INTERFACES='127.0.0.6/8,127.0.0.7/8,127.0.0.8/8,127.0.0.9/8,127.0.0.10/8,127.0.0.11/8'
111 TORTURE_OPTIONS="--option=interfaces=$TORTURE_INTERFACES $CONFIGURATION"
112 # ensure any one smbtorture call doesn't run too long
113 TORTURE_OPTIONS="$TORTURE_OPTIONS --maximum-runtime=$TORTURE_MAXTIME"
114 TORTURE_OPTIONS="$TORTURE_OPTIONS --target=samba4"
115 export TORTURE_OPTIONS
116
117 if [ x"$RUN_FROM_BUILD_FARM" = x"yes" ];then
118         TORTURE_OPTIONS="$TORTURE_OPTIONS --option=torture:progress=no"
119 fi
120
121  runtest() {
122         if [ -z "$PREFIX" ]; then
123             PREFIX=test_prefix
124             mkdir -p $PREFIX
125         fi
126         name=$1
127         shift 1
128         cmdline="$*"
129
130         SMBD_IS_UP="no"
131
132         shname=`echo $name | \
133         sed -e 's%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g'`
134
135         UNIQUE_PID=`/bin/sh -c 'echo $$'`
136         TEST_LOG="$PREFIX/test_log.${UNIQUE_PID}"
137         TEST_PCAP="$PREFIX/test_${shname}_${UNIQUE_PID}.pcap"
138         trap "rm -f $TEST_LOG $TEST_PCAP" EXIT
139
140         if [ -n "$SMBD_TEST_LOG" -a -z "$smbd_log_size" ]; then
141             smbd_log_size=`wc -l < $SMBD_TEST_LOG`;
142         fi
143
144         if [ x"$RUN_FROM_BUILD_FARM" = x"yes" ];then
145                 echo "--==--==--==--==--==--==--==--==--==--==--"
146                 echo "Running test $name (level 0 stdout)"
147                 echo "--==--==--==--==--==--==--==--==--==--==--"
148                 date
149                 echo "Testing $name"
150         else
151                 nf="`expr $failed + $totalfailed`";
152                 if [ "$nf" = "0" ]; then 
153                     echo "[$current/$total] Testing $name"
154                 else 
155                     echo "[$current/$total, $nf failures] Testing $name"
156                 fi
157         fi
158
159         smbd_check_only && SMBD_IS_UP="yes"
160         if [ x"$SMBD_IS_UP" != x"yes" ];then
161                 if [ x"$RUN_FROM_BUILD_FARM" = x"yes" ];then
162                         echo "SMBD is down! Skipping: $cmdline"
163                         echo "=========================================="
164                         echo "TEST SKIPPED: $name (reason SMBD is down)"
165                         echo "=========================================="
166                 else
167                         echo "TEST SKIPPED: $name (reason SMBD is down)"
168                 fi
169                 return 1
170         fi
171
172         if [ x"$MAKE_TEST_ENABLE_PCAP" = x"yes" ];then
173                 SOCKET_WRAPPER_PCAP_FILE=$TEST_PCAP
174                 export SOCKET_WRAPPER_PCAP_FILE
175         fi
176
177         ( $cmdline > $TEST_LOG 2>&1 )
178         status=$?
179         # show any additional output from smbd that has happened in this test
180         smbd_have_test_log && {             
181             new_log_size=`wc -l < $SMBD_TEST_LOG`;
182             test "$new_log_size" = "$smbd_log_size" || {
183                 echo "SMBD OUTPUT:";
184                 incr_log_size=`expr $new_log_size - $smbd_log_size`;
185                 tail -$incr_log_size $SMBD_TEST_LOG;
186                 smbd_log_size=$new_log_size;
187             }
188         }
189         if [ x"$status" != x"0" ]; then
190                 echo "TEST OUTPUT:"
191                 cat $TEST_LOG;
192                 rm -f $TEST_LOG;
193                 if [ x"$MAKE_TEST_ENABLE_PCAP" = x"yes" ];then
194                         echo "TEST PCAP: $TEST_PCAP"
195                 fi
196                 if [ x"$RUN_FROM_BUILD_FARM" = x"yes" ];then
197                         echo "=========================================="
198                         echo "TEST FAILED: $name (status $status)"
199                         echo "=========================================="
200                 else
201                         echo "TEST FAILED: $cmdline (status $status)"
202                 fi
203                 trap "" EXIT
204                 return 1;
205         fi
206         rm -f $TEST_LOG;
207         if [ x"$MAKE_TEST_KEEP_PCAP" = x"yes" ];then
208                 echo "TEST PCAP: $TEST_PCAP"
209         else
210                 rm -f $TEST_PCAP;
211         fi
212         if [ x"$RUN_FROM_BUILD_FARM" = x"yes" ];then
213                 echo "ALL OK: $cmdline"
214                 echo "=========================================="
215                 echo "TEST PASSED: $name"
216                 echo "=========================================="
217         fi
218         trap "" EXIT
219         return 0;
220 }
221
222 START=`date`
223 (
224  # give time for nbt server to register its names
225  echo delaying for nbt name registration
226  sleep 4
227  # This will return quickly when things are up, but be slow if we need to wait for (eg) SSL init 
228  bin/nmblookup $CONFIGURATION $SERVER
229  bin/nmblookup $CONFIGURATION -U $SERVER $SERVER
230  bin/nmblookup $CONFIGURATION $SERVER
231  bin/nmblookup $CONFIGURATION -U $SERVER $NETBIOSNAME
232  bin/nmblookup $CONFIGURATION $NETBIOSNAME
233  bin/nmblookup $CONFIGURATION -U $SERVER $NETBIOSNAME
234
235 # start off with 0 failures
236 totalfailed=0
237 export totalfailed
238
239 . script/tests/tests_$TESTS.sh > $PREFIX/recipe
240 total=`grep "TEST --" $PREFIX/recipe | wc -l`
241 current=0
242 cat $PREFIX/recipe | (
243         while read LINE
244         do
245                 if [ "$LINE" = "-- TEST --" ]; then
246                         read NAME
247                         read CMDLINE
248                         current=`expr $current + 1`
249                         runtest "$NAME" "$CMDLINE" || totalfailed=`expr $totalfailed + $?`
250                 else
251                         echo "$LINE"
252                 fi
253         done
254         exit $totalfailed
255 )
256 exit $?
257 ) 9>$SMBD_TEST_FIFO
258 totalfailed=$?
259
260 smbd_PID=`cat $PIDDIR/smbd.pid`
261 waitforpid $smbd_PID 20 || {
262     echo "smbd process $1 took more than 20 seconds to exit, killing"
263     kill -9 $smbd_PID
264 }
265
266 if [ "$TEST_LDAP"x = "yesx" ]; then
267     if test -z "$FEDORA_DS_PREFIX"; then
268         kill `cat $PIDDIR/slapd.pid`
269     else
270         $LDAPDIR/slapd-samba4/stop-slapd
271     fi
272 fi
273
274 END=`date`
275 echo "START: $START ($ARG0)";
276 echo "END:   $END ($ARG0)";
277
278 # if there were any valgrind failures, show them
279 count=`find $PREFIX -name 'valgrind.log*' | wc -l`
280 if [ "$count" != 0 ]; then
281     for f in $PREFIX/valgrind.log*; do
282         if [ -s $f ] && grep -v DWARF2.CFI.reader $f > /dev/null; then
283             echo "VALGRIND FAILURE";
284             totalfailed=`expr $totalfailed + 1`
285             cat $f
286         fi
287     done
288 fi
289
290 teststatus $ARG0 $totalfailed