1#!/bin/bash
2################################################################################
3##                                                                            ##
4## Copyright ©  International Business Machines  Corp., 2007, 2008            ##
5##                                                                            ##
6## This program is free software;  you can redistribute it and#or modify      ##
7## it under the terms of the GNU General Public License as published by       ##
8## the Free Software Foundation; either version 2 of the License, or          ##
9## (at your option) any later version.                                        ##
10##                                                                            ##
11## This program is distributed in the hope that it will be useful, but        ##
12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
14## 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 to the Free Software               ##
18## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
19##                                                                            ##
20################################################################################
21
22
23#
24# Script to run the tests in testcases/realtime
25#
26# Usage: $0 test_argument
27#
28# where test-argument = func | stress | perf | all | list | clean | test_name
29#
30# test_name is the name of a subdirectory in func/, stress/ or perf/
31#
32echo "Real-time tests run"
33
34export LTPROOT=${PWD}
35echo $LTPROOT | grep testscripts > /dev/null 2>&1
36if [ $? -eq 0 ]; then
37        cd ..
38        export LTPROOT=${PWD}
39fi
40
41
42function usage()
43{
44	echo -e "\nUsage: test_realtime.sh  -t test-argument [-l loop num_of_iterations] [-t test-argument1 [-l loop ...]] ..."
45	echo -e "\nWhere test-argument = func | stress | perf | all | list | clean | test_name"
46	echo -e "\nand:\n"
47	echo -e " func = 	all functional tests will be run "
48	echo -e " stress = 	all stress tests will be run "
49	echo -e " perf = 	all perf tests will be run "
50	echo -e " all =		all tests will be run "
51	echo -e " list = 	all available tests will be listed "
52	echo -e " clean = 	all logs deleted, make clean performed "
53	echo -e " test_name = 	only test_name subdir will be run (e.g: func/pi-tests) "
54	echo -e "\n"
55	exit 1;
56}
57
58function check_error()
59{
60        if [ $? -gt 0 ]; then
61        echo -e "\n $1 Failed\n"
62        exit 1
63        fi
64}
65
66list_tests()
67{
68	echo -e "\nAvailable tests are:\n"
69
70	cd $TESTS_DIR
71	for file in `find -name run_auto.sh`
72	do
73		echo -e " `dirname  $file `"
74	done
75		echo -e " \n"
76}
77
78function run_test()
79{
80        iter=0
81        if [ -z "$2" ]; then
82            LOOPS=1
83        else
84         LOOPS=$2
85        fi
86        #Test if $LOOPS is a integer
87        if [[ ! $LOOPS =~ ^[0-9]+$ ]]; then
88            echo "\"$LOOPS\" doesn't appear to be a number"
89            usage
90            exit
91        fi
92        if [ -d "$test" ]; then
93            pushd $test >/dev/null
94            if [ -f "run_auto.sh" ]; then
95                echo " Running $LOOPS runs of $subdir "
96                for((iter=0; $iter < $LOOPS; iter++)); do
97                ./run_auto.sh
98                done
99            else
100                echo -e "\n Failed to find run script in $test \n"
101            fi
102            pushd $TESTS_DIR >/dev/null
103        else
104                echo -e "\n $test is not a valid test subdirectory "
105                usage
106                exit 1
107        fi
108}
109
110function make_clean()
111{
112        pushd $TESTS_DIR >/dev/null
113        rm -rf logs
114        make clean
115}
116
117find_test()
118{
119    case $1 in
120        func)
121            TESTLIST="func"
122            ;;
123        stress)
124            TESTLIST="stress"
125            ;;
126        perf)
127            TESTLIST="perf"
128            ;;
129        all)
130        # Run all tests which have run_auto.sh
131            TESTLIST="func stress perf"
132            ;;
133        list)
134        # This will only display subdirs which have run_auto.sh
135            list_tests
136            exit
137            ;;
138        clean)
139        # This will clobber logs, out files, .o's etc
140            make_clean
141            exit
142            ;;
143
144        *)
145        # run the tests in the individual subdirectory if it exists
146            TESTLIST="$1"
147            ;;
148    esac
149    for subdir in $TESTLIST; do
150        if [ -d $subdir ]; then
151            pushd $subdir >/dev/null
152            for name in `find -name "run_auto.sh"`; do
153                test="`dirname $name`"
154                run_test "$test" "$2"
155                pushd $subdir > /dev/null
156            done
157                pushd $TESTS_DIR >/dev/null
158        else
159            echo -e "\n $subdir not found; check name/path with run.sh list "
160        fi
161    done
162
163}
164
165source $LTPROOT/testcases/realtime/scripts/setenv.sh
166
167if [ $# -lt 1 ]; then
168	usage
169fi
170pushd $TESTS_DIR >/dev/null
171
172cd $TESTS_DIR
173if [ ! -e "logs" ]; then
174        mkdir logs
175        echo " creating logs directory as $TESTS_DIR/logs "
176        chmod -R 775 logs
177fi
178
179# if INSTALL_DIR != top_srcdir assume the individual tests are built and installed.
180# So no need to build lib
181if [[ -d lib ]]; then
182    #Only build the library, most of the tests depend upon.
183    #The Individual tests will be built, just before they run.
184    pushd lib
185    make
186    check_error make
187    popd
188fi
189
190ISLOOP=0
191index=0
192while getopts ":t:l:h" option
193do
194    case "$option" in
195
196        t )
197            if [ $ISLOOP -eq 1 ]; then
198                LOOP=1
199                tests[$index]=$LOOP
200                index=$((index+1))
201            fi
202
203            tests[$index]="$OPTARG"
204            index=$((index+1))
205            TESTCASE="$OPTARG"
206            ISLOOP=1
207            ;;
208
209        l )
210            ISLOOP=0
211            tests[$index]="$OPTARG"
212            LOOP="$OPTARG"
213            index=$((index+1))
214            ;;
215        h )
216            usage
217            ;;
218        * ) echo "Unrecognized option specified"
219            usage
220            ;;
221    esac
222done
223for(( i=0; $i < $index ; $((i+=2)) )); do
224    find_test ${tests[$i]} ${tests[$((i+1))]}
225done
226
227