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