1#!/bin/bash
2
3# Main pounder control script.
4
5# Copyright (C) 2003-2006 IBM
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20# 02111-1307, USA.
21
22source libpounder.sh
23
24# This function kills off pounder and related subprocesses.
25function dokill() {
26	echo "Killing off pounder(s)..."
27	kill -INT `cat $POUNDER_PIDFILE`;
28	rm -rf $POUNDER_PIDFILE
29	#killall -9 randasys 2> /dev/null	# These run as user 'daemon' and have to be killed manually.
30}
31
32# Main function follows
33
34function help() {
35	cat << ENDL
36Usage: ./pounder [-g logdir] [-x] [-d duration] [-n ipaddr] [-m max_failures] [-f] [-h|-u|-r|-k|-l|-e subtests|-i subtests|-c scheduler] [-s]
37
38-h      	Brings up this menu
39-c scheduler	Creates a new test scheduler called scheduler-tests.tar.gz in the pounder/schedulers folder.
40		All subtests to be packaged with this scheduler must first be placed in the pounder/tests folder.
41-x      	Enable X stress tests.
42-d duration	Run pounder for duration seconds.
43-n ipaddr	Use ipaddr for NFS tests.
44-f		Remove pounder pid file before running.
45-u      	Unmount NFS log storage.
46-r      	Remount NFS log storage.
47-g logdir      	Use logdir as the log directory. (You probably want -s too.)
48-s      	Store logs locally.
49-l      	List (both included and excluded) subtests that came with the test scheduler
50-e subtests     Exclude subtests from next pounder run
51-i subtests     Include previously excluded subtests in the next pounder run
52-k      	Kill pounder.
53
54run "./pounder" to run all subtests
55run "./pounder subtest" to run just one particular subtest
56        (example: ./pounder tests/T90ramp/D02build_kernel)
57ENDL
58}
59
60function exclude() {
61	echo "excluding from tests $@..."
62
63	POUNDER_TESTS=$POUNDER_HOME/tests
64	SUBTESTS_INC=`find $POUNDER_TESTS -name excluded -prune -o -type f -print -o -type l -print`
65
66	for i in $@
67	do
68		BASENAME=`basename "$i"`
69
70		ORIG_DIR=`pwd`
71		cd `dirname $i`
72		FULL_PATH=`pwd`/"$BASENAME"
73		cd $ORIG_DIR
74
75		FOUND=`echo "$SUBTESTS_INC" | grep -x "$FULL_PATH"`
76
77		if [ -n "$FOUND" -a -x "$i" ]; then
78
79			ALREADY_EXCLUDED=`grep -w "$BASENAME" $POUNDER_TESTS/excluded/testlist | cut -d " " -f1`
80			if [ $ALREADY_EXCLUDED ]; then
81				echo $ALREADY_EXCLUDED
82				echo "$BASENAME has already been excluded from the test scheduler. Make sure the test names in the test scheduler are unique."
83				exit 1
84			fi
85
86			echo "$BASENAME" "$FULL_PATH" >> "$POUNDER_TESTS"/excluded/testlist
87			mv "$i" "$POUNDER_TESTS"/excluded/"$BASENAME"
88
89			if [ $? -eq 0 ]; then
90				echo "Successfully removed $i from test scheduler."
91			else
92				echo "Removal of $i from tests UNSUCCESSFUL."
93			fi
94		else
95			echo "$i is not a subtest included in the current scheduler. Use ./pounder -l for list of valid, excludable subtests."
96		fi
97	done
98}
99
100function include() {
101	echo "including in tests $@..."
102
103        POUNDER_EXCLUDED=$POUNDER_HOME/tests/excluded
104
105        for i in $@
106        do
107		ORIG_FILE=`grep -w "$i" $POUNDER_EXCLUDED/testlist | cut -d " " -f2`
108
109		if [ -n "$ORIG_FILE" ]; then
110			mv $POUNDER_EXCLUDED/$i $ORIG_FILE
111			if [ $? -eq 0 ]; then
112				sed "/$i /d" $POUNDER_EXCLUDED/testlist > $POUNDER_EXCLUDED/tempfile
113				mv $POUNDER_EXCLUDED/tempfile $POUNDER_EXCLUDED/testlist
114				echo "Successfully added $i back to test scheduler."
115			else
116				echo "Addition of $i to tests UNSUCCESSFUL."
117			fi
118		else
119			echo "$i is not an excluded subtest. It may already be included in the current scheduler. Use ./pounder -l for list of valid, includable subtests."
120		fi
121        done
122}
123
124function list_tests() {
125
126	if [ ! -d $POUNDER_HOME/tests ]; then
127		echo "Error: No test schedule found. Did you run make install?"
128		exit 1
129	fi
130
131	echo "Included subtests:"
132	INCLUDED_TESTS=`find $POUNDER_HOME/tests -name excluded -prune -o -type f -print -o -type l -print`
133	if [ -z "$INCLUDED_TESTS" ]; then
134		echo "[NONE]"
135	else
136		echo "$INCLUDED_TESTS"
137	fi
138	echo
139
140	echo "Excluded subtests:"
141
142	if [ ! -d $POUNDER_HOME/tests/excluded ]; then
143		echo "Error: $POUNDER_HOME/tests/excluded folder does not exist. Make sure your test scheduler was created or unpackaged correctly. Did you run make install?"
144		exit 1
145	fi
146
147	EXCLUDED_TESTS=`grep -v "#" $POUNDER_HOME/tests/excluded/testlist`
148	if [ -z "$EXCLUDED_TESTS" ]; then
149		echo "[NONE]"
150	else
151		echo "$EXCLUDED_TESTS" | cut -d " " -f1
152	fi
153
154}
155
156function create_scheduler() {
157	TESTS_EXCLUDED=$POUNDER_HOME/tests/excluded
158	REPO_EXCLUDED=$POUNDER_HOME/test_repo/excluded
159
160	if [ ! -d $TESTS_EXCLUDED ]; then
161		cp -r $REPO_EXCLUDED $TESTS_EXCLUDED
162	elif [ ! -e $TESTS_EXCLUDED/testlist ]; then
163		cp $REPO_EXCLUDED/testlist $TESTS_EXCLUDED/testlist
164	fi
165
166	tar -czf $POUNDER_HOME/schedulers/$@-tests.tar.gz tests
167}
168# process options: -x to run in xterms, -k to kill tests, -d to set duration,
169# -f to remove bogus pounder.pid files.
170while getopts d:kn:g:xure:i:m:c:shl? o
171do
172	case "$o" in
173	   d) export DURATION="$OPTARG";;
174	   k) dokill; exit;;
175	   n) export NFS_SERVER="$OPTARG";;
176	   g) export POUNDER_LOGDIR="$OPTARG";;
177	   x) export DO_X_TESTS=1;;
178	   f) rm -rf "$POUNDER_PIDFILE";;
179	   u) umount $POUNDER_HOME/log 2> /dev/null; exit 0;;
180	   s) export NFS_LOGGING=0;;
181	   r) ./nfs_logging; exit;;
182	   e) exclude $OPTARG; exit;;
183	   i) include $OPTARG; exit;;
184	   l) list_tests; exit;;
185	   m) export MAX_FAILURES="$OPTARG";;
186	   c) create_scheduler $OPTARG; exit;;
187	   h) help $0; exit;;
188	   '?') help $0; exit 1;;
189	esac
190done
191
192# Did user ask for X tests when DISPLAY is set?
193# Probably _not_ what the user wants.
194if [ $DO_X_TESTS -eq 1 ] && [ ! -z "$DISPLAY" ]; then
195	echo "WARNING: X tests are enabled and DISPLAY is set.  xterms will appear on this desktop!"
196	echo "WARNING: Kill pounder NOW if this is not what you intended."
197fi
198
199# Get set up to read arguments, even though there aren't any...
200shift `expr $OPTIND - 1`
201
202# Are we already running?
203if [ -f "$POUNDER_PIDFILE" ]; then
204	echo "File $POUNDER_PIDFILE exists; pounder may already be running."
205	echo "Either run 'pounder -k' to stop all tests, or remove it."
206	exit 1
207fi
208
209# Did the user run Install?
210if [ ! -x run-helper -o ! -x timed_loop -o ! -x infinite_loop ]; then
211	echo "Could not find run-helper, infinite_loop or timed_loop.  Did you run Install?"
212	exit -1
213fi
214
215# Try to mount NFS log store.
216if [ -z "$NFS_LOGSERVER" -o -z "$NFS_LOGDIR" ]; then
217	echo "NFS logging disabled because NFS_LOGSERVER or NFS_LOGDIR are not set."
218	export NFS_LOGGING=0
219else
220	if [ $NFS_LOGGING -gt 0 ]; then
221		./nfs_logging
222	fi
223fi
224
225# Set up log directory
226mkdir -p "$POUNDER_LOGDIR" 2> /dev/null
227if [ ! -d "$POUNDER_LOGDIR" ]; then
228	echo "Could not create $POUNDER_LOGDIR; aborting."
229	exit 1
230fi
231
232# Set up dir for optional components
233mkdir -p "$POUNDER_OPTDIR"
234if [ ! -d "$POUNDER_OPTDIR" ]; then
235	echo "Could not create $POUNDER_OPTDIR; aborting."
236	exit 1
237fi
238
239# Set up tmpdir
240mkdir -p "$POUNDER_TMPDIR"
241if [ ! -d "$POUNDER_TMPDIR" ]; then
242	echo "Could not create $POUNDER_TMPDIR; aborting."
243	exit 1
244fi
245
246TEST_HEAD="$1"
247if [ -z $TEST_HEAD ]; then
248	TEST_HEAD=tests
249fi
250
251# needs to be a separate check in case there is no tests/ dir...
252if [ ! -d "$TEST_HEAD" -a ! -f "$TEST_HEAD" ]; then
253	echo "$TEST_HEAD does not exist!"
254	exit 1
255fi
256
257
258# Dump environment variables...
259export > "$POUNDER_LOGDIR/environment"
260
261echo "Starting $POUNDER_VERSION"
262echo "STARTING TESTS."
263echo "To kill all tests, run 'pounder -k' or press ^C."
264
265# Handle the duration thing...
266function kill_after {
267	sleep $DURATION
268	./pounder -k
269}
270if [ ! -z "$DURATION" -a "$DURATION" -gt 0 ]; then
271	kill_after &
272fi
273
274# Now run the tests
275./run-helper $TEST_HEAD --leader
276