added files
[bcm963xx.git] / userapps / opensource / net-snmp / testing / RUNTESTS
1 #!/bin/sh
2 #
3 # RUNTESTS [-h]...
4 #
5 # RETURNS:      Number of failed tests.
6 #
7 # CALLS: eval_oneprogram.sh [-h][-lk] <program>
8 #
9 # SOURCES: TESTCONF.sh
10 #
11 #
12
13                                                                 USAGE_LONG='
14 #
15 # HOW TO ENTER A NEW TEST
16 #
17 # Copy the template file (T2.sh) XXX to the tests directory, and have
18 # the file begin with a 'T'.  All files in the tests directory
19 # begining with a 'T' are assumed to be a test.  See the file for
20 # further instructions
21 #
22 # HOW TESTS ARE RUN AND EVALUATED
23 #
24 # ...
25
26 '
27
28 #
29 # Suggested improvement(s):
30 #       Run a given test against a running agent.
31 #       Display errors
32 #       interactively pick tests to run.
33 #       options arguments to pick tests to run.
34 #
35 #
36 # Variables:  (* = exported)
37 #  *SNMP_BASEDIR:         the source base directory for tests
38 #  *SNMP_UPDIR:           directory above where the test binaries live (-D option)
39 #  *SNMP_PATH   ## yes, PATH is already setup
40 #  *SNMP_VERBOSE        ## 0=silent, 1=warnings, 2=more
41
42 # Usage mess.  (No, it works.)
43 #
44 USAGE="Usage: `basename $0` [-h] [-i] [-v] [-V] [-s] [-T TESTNUMS] [-D bindir] [-S seconds]"
45
46 usage() { 
47     echo; 
48     echo $USAGE;
49     #cat <<BLIK | sed 's/^#//' | sed '1d' | $PAGER
50     #$USAGE_LONG
51     #BLIK
52     exit 0
53 }
54
55 trap "exit 1;" 1 2 3 9 13 15 17
56
57 SNMP_BASEDIR=`dirname $0`
58
59 ### Check for the configuration script.
60 ##if [ ! -s "${SNMP_BASEDIR}/TESTCONF.sh"  ] ; then 
61 ##  echo "No TESTCONF.sh in \"$SNMP_BASEDIR\" ; exiting"
62 ##  exit 0
63 ##fi
64
65 ORIGDIR=`pwd`           ## this script may be invoked with relative path
66 SNMP_UPDIR=..           ## building from the source tree
67 interactive=no
68 SNMP_VERBOSE=0
69 SNMP_SLEEP=${SNMP_SLEEP:=1}     ## default seconds to sleep
70 SH_DEBUG=0
71
72 while [ -n "$1" ]; do
73     case "$1" in
74         -h)
75             usage
76             exit
77             ;;
78
79         -i)
80             interactive="yes"
81             ;;
82         -v)
83             SNMP_VERBOSE=1
84             ;;
85         -V)
86             SNMP_VERBOSE=2
87             ;;
88         -s)
89             SNMP_SAVE_TMPDIR="yes"
90             export SNMP_SAVE_TMPDIR
91             ;;
92         -D)
93             shift
94             SNMP_UPDIR="$1"
95             ;;
96         -T)
97             shift
98             test_nums="$1"
99             ;;
100         -a)
101             test_nums="all"
102             ;;
103
104         -A)
105             shift
106             AGENT_FLAGS="$1"
107             export AGENT_FLAGS
108             ;;
109         -S)
110             shift
111             SNMP_SLEEP="$1"
112             ;;
113
114         -x)
115             SH_DEBUG=1
116             ;;
117
118     esac
119
120     shift
121 done
122
123 export SNMP_SLEEP
124
125 # Find executables in source first, then build, then existing PATH.
126 ## Add to PATH if a binary is found.
127
128 cd $SNMP_UPDIR
129 SNMP_UPDIR=`pwd`
130 bf=snmpget
131 if [ -f "$bf" ] ; then
132    PATH=$SNMP_UPDIR:$PATH
133 else
134    bf=apps/snmpget
135    if [ -f "$bf" ] ; then
136       PATH=$SNMP_UPDIR/apps:$PATH
137    fi
138 fi
139 bf=agent/snmpd
140 if [ -f "$bf" ] ; then
141    PATH=$SNMP_UPDIR/agent:$PATH
142 fi
143
144 bf=include/net-snmp/net-snmp-config.h
145 if [ ! -s "$bf" ] ; then
146    echo "No \"$bf\" in $SNMP_UPDIR . Some tests will be skipped"
147 fi
148 unset bf
149
150 # Run from the test scripts directory.
151 cd $ORIGDIR ; cd ${SNMP_BASEDIR}
152 SNMP_BASEDIR=`pwd`
153
154 # Setup for the next test run.
155 rm -f core tests/core
156
157 PATH=${SNMP_BASEDIR}:$PATH
158 SNMP_PATH=yes
159
160 export PATH
161 export SNMP_BASEDIR
162 export SNMP_PATH
163 export SNMP_UPDIR
164 export SNMP_VERBOSE
165
166 WHICH=which
167 $WHICH $0 > /dev/null 2>&1
168 if [ $? -ne 0 ] ; then
169     WHICH=type
170 fi
171
172 for needed in snmpd snmpget snmpgetnext; do
173     $WHICH $needed > /dev/null 2>&1
174     if [ $? -ne 0  ] ; then 
175         echo "No $needed found. Exiting"
176         exit 1
177     fi
178 done
179
180 #
181 # Distinguished expectations.
182 #
183 if [ $SNMP_VERBOSE -gt 0 ]; then
184     echo ${SNMP_UPDIR}/testing
185     echo path is $PATH
186     echo top of build is $SNMP_UPDIR
187     echo source testing is $SNMP_BASEDIR
188     $WHICH snmpusm
189 fi
190
191 #
192 # Source the testing configuration file
193 #
194
195 . TESTCONF.sh
196
197 # Hack: the above created a directory, now we have to nuke it and
198 # forget about it...  All for the convenience of the test writer.
199 rmdir $SNMP_TMPDIR
200 unset SNMP_TMPDIR
201 export SNMP_TMPDIR
202
203 #
204 # Switch to the testing directory, for ease of the client test packages.
205 #
206 cd ./tests
207
208 #------------------------------------ -o- 
209 # Globals.
210 #
211 PROGRAM=
212 ARGUMENTS="$*"
213
214 TMPFILE=$SNMP_TMPDIR/eval_suite.sh$$
215
216 testname=
217
218 success_count=0
219 failed_count=0
220
221 if [ "x$do_tests" = "x" ]; then
222     #
223     # List the tests in question
224     #
225     num=0
226     for testfile in T*; do
227         case $testfile in
228             # Skip backup files, and the like.
229             *~)     ;;
230             *.bak)  ;;
231             *.orig) ;;
232             *.rej)  ;;
233
234             # Do the rest
235             *)
236                 num=`expr $num + 1`
237                 if [ "x$interactive" != "xyes" -a "x$test_nums" = "x" ]; then
238                     eval_onescript.sh $testfile $num "yes"
239                 fi
240                 all_tests="$all_tests $num"
241                 all_files="$all_files $testfile"
242                 ;;
243         esac
244     done
245
246     #
247     # TODO: allow user to interactively pick the list of tests to run.
248     #
249
250     if [ "x$interactive" != "xyes" ]; then
251         if [ "x$test_nums" = "x" ]; then
252             ECHO "Enter test numbers [all]: "
253             read inp
254         else
255             if [ "x$test_nums" = "xall" ]; then
256                 inp=""
257             else
258                 inp="$test_nums"
259             fi
260         fi
261         if [ "x$inp" = "x" ]; then
262             do_tests="$all_files"
263         else
264             a=1
265             set $all_files
266             while [ $a -le $num ]; do
267                 if echo " $inp " | grep " $a " > /dev/null; then
268                     do_tests="$do_tests $1"
269                     if [ "x$test_nums" = "x" ] ; then
270                         # actually broken.  sets only the first num correctly.
271                         test_nums=$a
272                     fi
273                 fi
274                 shift
275                 a=`expr $a + 1`
276             done
277         fi
278     fi
279
280     #echo Starting: Running tests $do_tests
281 fi
282
283 #
284 # Run the tests
285 #
286 if [ "x$test_nums" = "xall" -o "x$test_nums" = "x" ] ; then
287     num=1
288 else
289     num="$test_nums"
290 fi
291 for testfile in $do_tests; do
292     dothisone="yes";
293     if [ "x$interactive" = "xyes" ]; then
294
295         if [ $SH_DEBUG = 1 ] ; then
296             sh -x eval_onescript.sh $testfile $num "yes"
297         else
298             eval_onescript.sh $testfile $num "yes"
299         fi
300
301         ECHO "  Run test #$num (y/n) [y]? "
302         read inp
303         if [ "x$inp" = "xn" ]; then
304             dothisone=no
305         fi
306     fi
307   
308     if [ "x$dothisone" = "xyes" ]; then
309         if [ $SH_DEBUG = 1 ] ; then
310             sh -x eval_onescript.sh $testfile $num "no"
311         else
312             eval_onescript.sh $testfile $num "no"
313         fi
314         if [ $? = 0 ]; then
315             success_count=`expr $success_count + 1`
316         else
317             failed_count=`expr $failed_count + 1`
318         fi
319     fi
320     num=`expr $num + 1`
321 done
322
323 echo Summary: $success_count / `expr $failed_count + $success_count` succeeded.
324
325 exit $failed_count