tests: Use argparse module with hwsim

Instead of re-implementing a command-line parser, use the
argparse module.

The only real change (I hope) is that the test module must
now be given to the -f option without the .py suffix.

Also, --help now works, and if a test module/test name is
given that doesn't exist, the valid list is printed.

Signed-hostap: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
Johannes Berg 2013-10-30 19:28:22 +02:00 committed by Jouni Malinen
parent 2f3a3edd5d
commit 0b1faa8bf1
3 changed files with 87 additions and 75 deletions

View file

@ -151,10 +151,10 @@ For manual testing, ./start.sh can be used to initialize interfaces and
programs and run-tests.py to execute one or more test programs and run-tests.py to execute one or more test
cases. run-tests.py output verbosity can be controlled with -d (more cases. run-tests.py output verbosity can be controlled with -d (more
verbose debug output) and -q (less verbose output) on the command verbose debug output) and -q (less verbose output) on the command
line. "-f <test_*.py>" can be used to specify that all test cases from a line. "-f <test_filename(without.py)>" can be used to specify that all
single file are to be run. Test name as the last command line argument test cases from a single file are to be run. Test name as the last
can be specified that a single test case is to be run (e.g., command line argument can be specified that a single test case is to be
"./run-tests.py test_ap_pmf_required"). run (e.g., "./run-tests.py test_ap_pmf_required").
Adding/modifying test cases Adding/modifying test cases

View file

@ -24,7 +24,7 @@ if [ "x$1" = "xconcurrent-valgrind" ]; then
DATE=`ls -1tr $LOGDIR | tail -1 | cut -f1 -d-` DATE=`ls -1tr $LOGDIR | tail -1 | cut -f1 -d-`
rm $LOGDIR/last-debug rm $LOGDIR/last-debug
for i in autogo discovery grpform; do for i in autogo discovery grpform; do
./run-tests.py -l $LOGDIR/$DATE-run-$i $DB -e $LOGDIR/$DATE-failed-$i -r $LOGDIR/results.txt -f test_p2p_$i.py || errors=1 ./run-tests.py -l $LOGDIR/$DATE-run-$i $DB -e $LOGDIR/$DATE-failed-$i -r $LOGDIR/results.txt -f test_p2p_$i || errors=1
cat $LOGDIR/$DATE-run-$i >> $LOGDIR/last-debug cat $LOGDIR/$DATE-run-$i >> $LOGDIR/last-debug
done done
./stop-wifi.sh ./stop-wifi.sh
@ -45,7 +45,7 @@ elif [ "x$1" = "xconcurrent" ]; then
DATE=`ls -1tr $LOGDIR | tail -1 | cut -f1 -d-` DATE=`ls -1tr $LOGDIR | tail -1 | cut -f1 -d-`
rm $LOGDIR/last-debug rm $LOGDIR/last-debug
for i in autogo discovery grpform; do for i in autogo discovery grpform; do
./run-tests.py -l $LOGDIR/$DATE-run-$i $DB -e $LOGDIR/$DATE-failed-$i -r $LOGDIR/results.txt -f test_p2p_$i.py || errors=1 ./run-tests.py -l $LOGDIR/$DATE-run-$i $DB -e $LOGDIR/$DATE-failed-$i -r $LOGDIR/results.txt -f test_p2p_$i || errors=1
cat $LOGDIR/$DATE-run-$i >> $LOGDIR/last-debug cat $LOGDIR/$DATE-run-$i >> $LOGDIR/last-debug
done done
./stop-wifi.sh ./stop-wifi.sh

View file

@ -11,6 +11,7 @@ import re
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
import argparse
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -47,49 +48,74 @@ def report(conn, build, commit, run, test, result, diff):
print "sql: %r" % (params, ) print "sql: %r" % (params, )
def main(): def main():
test_file = None tests = []
error_file = None test_modules = []
log_file = None for t in os.listdir("."):
results_file = None m = re.match(r'(test_.*)\.py$', t)
conn = None if m:
run = None logger.debug("Import test cases from " + t)
build = None mod = __import__(m.group(1))
commit = None test_modules.append(mod.__name__)
idx = 1 for s in dir(mod):
print_res = False if s.startswith("test_"):
if len(sys.argv) > 1 and sys.argv[1] == '-d': func = mod.__dict__.get(s)
logging.basicConfig(level=logging.DEBUG) tests.append(func)
idx = idx + 1 test_names = list(set([t.__name__ for t in tests]))
elif len(sys.argv) > 1 and sys.argv[1] == '-q':
logging.basicConfig(level=logging.WARNING)
print_res = True
idx = idx + 1
elif len(sys.argv) > 2 and sys.argv[1] == '-l':
log_file = sys.argv[2]
logging.basicConfig(filename=log_file,level=logging.DEBUG)
idx = idx + 2
else:
logging.basicConfig(level=logging.INFO)
while len(sys.argv) > idx: run = None
if len(sys.argv) > idx + 1 and sys.argv[idx] == '-e': commit = None
error_file = sys.argv[idx + 1] print_res = False
idx = idx + 2
elif len(sys.argv) > idx + 1 and sys.argv[idx] == '-r': parser = argparse.ArgumentParser(description='hwsim test runner')
results_file = sys.argv[idx + 1] group = parser.add_mutually_exclusive_group()
idx = idx + 2 group.add_argument('-d', const=logging.DEBUG, action='store_const',
elif len(sys.argv) > idx + 1 and sys.argv[idx] == '-f': dest='loglevel', default=logging.INFO,
test_file = sys.argv[idx + 1] help="verbose debug output")
idx = idx + 2 group.add_argument('-q', const=logging.WARNING, action='store_const',
elif len(sys.argv) > idx + 1 and sys.argv[idx] == '-S': dest='loglevel', help="be quiet")
import sqlite3 group.add_argument('-l', metavar='<filename>', dest='logfile',
conn = sqlite3.connect(sys.argv[idx + 1]) help='debug log filename')
idx = idx + 2
elif len(sys.argv) > idx + 1 and sys.argv[idx] == '-b': parser.add_argument('-e', metavar="<filename>", dest='errorfile',
build = sys.argv[idx + 1] help='error filename')
idx = idx + 2 parser.add_argument('-r', metavar="<filename>", dest='resultsfile',
help='results filename')
parser.add_argument('-S', metavar='<sqlite3 db>', dest='database',
help='database to write results to')
parser.add_argument('-b', metavar='<build>', dest='build', help='build ID')
parser.add_argument('-L', action='store_true', dest='update_tests_db',
help='List tests (and update descriptions in DB)')
parser.add_argument('-f', dest='testmodule', metavar='<test module>',
help='execute only tests from this test module',
type=str, choices=[[]] + test_modules)
parser.add_argument('tests', metavar='<test>', nargs='*', type=str,
help='tests to run (only valid without -f)',
choices=[[]] + test_names)
args = parser.parse_args()
if args.tests and args.testmodule:
print 'Invalid arguments - both test module and tests given'
sys.exit(2)
if args.logfile:
logging.basicConfig(filename=args.logfile,
level=logging.DEBUG)
log_to_file = True
else: else:
break logging.basicConfig(level=args.loglevel)
log_to_file = False
if args.loglevel == logging.WARNING:
print_res = True
error_file = args.errorfile
results_file = args.resultsfile
if args.database:
import sqlite3
conn = sqlite3.connect(args.database)
else:
conn = None
if conn: if conn:
run = str(int(time.time())) run = str(int(time.time()))
@ -101,20 +127,7 @@ def main():
except IOError: except IOError:
pass pass
tests = [] if args.update_tests_db:
for t in os.listdir("."):
m = re.match(r'(test_.*)\.py$', t)
if m:
if test_file and test_file not in t:
continue
logger.debug("Import test cases from " + t)
mod = __import__(m.group(1))
for s in dir(mod):
if s.startswith("test_"):
func = mod.__dict__.get(s)
tests.append(func)
if len(sys.argv) > idx and sys.argv[idx] == '-L':
for t in tests: for t in tests:
print t.__name__ + " - " + t.__doc__ print t.__name__ + " - " + t.__doc__
if conn: if conn:
@ -130,10 +143,6 @@ def main():
conn.close() conn.close()
sys.exit(0) sys.exit(0)
if len(sys.argv) > idx:
test_filter = sys.argv[idx]
else:
test_filter = None
dev0 = WpaSupplicant('wlan0', '/tmp/wpas-wlan0') dev0 = WpaSupplicant('wlan0', '/tmp/wpas-wlan0')
dev1 = WpaSupplicant('wlan1', '/tmp/wpas-wlan1') dev1 = WpaSupplicant('wlan1', '/tmp/wpas-wlan1')
@ -156,12 +165,15 @@ def main():
failed = [] failed = []
for t in tests: for t in tests:
if test_filter: if args.tests:
if test_filter != t.__name__: if not t.__name__ in args.tests:
continue
if args.testmodule:
if not t.__module__ == args.testmodule:
continue continue
reset_devs(dev, apdev) reset_devs(dev, apdev)
logger.info("START " + t.__name__) logger.info("START " + t.__name__)
if log_file: if log_to_file:
print "START " + t.__name__ print "START " + t.__name__
sys.stdout.flush() sys.stdout.flush()
if t.__doc__: if t.__doc__:
@ -191,11 +203,11 @@ def main():
else: else:
passed.append(t.__name__) passed.append(t.__name__)
result = "PASS" result = "PASS"
report(conn, build, commit, run, t.__name__, result, diff) report(conn, args.build, commit, run, t.__name__, result, diff)
result = result + " " + t.__name__ + " " result = result + " " + t.__name__ + " "
result = result + str(diff.total_seconds()) + " " + str(end) result = result + str(diff.total_seconds()) + " " + str(end)
logger.info(result) logger.info(result)
if log_file or print_res: if log_to_file or print_res:
print result print result
sys.stdout.flush() sys.stdout.flush()
if results_file: if results_file:
@ -207,10 +219,10 @@ def main():
diff = end - start diff = end - start
logger.info(e) logger.info(e)
failed.append(t.__name__) failed.append(t.__name__)
report(conn, build, commit, run, t.__name__, "FAIL", diff) report(conn, args.build, commit, run, t.__name__, "FAIL", diff)
result = "FAIL " + t.__name__ + " " + str(diff.total_seconds()) + " " + str(end) result = "FAIL " + t.__name__ + " " + str(diff.total_seconds()) + " " + str(end)
logger.info(result) logger.info(result)
if log_file: if log_to_file:
print result print result
sys.stdout.flush() sys.stdout.flush()
if results_file: if results_file:
@ -224,7 +236,7 @@ def main():
logger.info("Failed to issue TEST-STOP after " + t.__name__ + " for " + d.ifname) logger.info("Failed to issue TEST-STOP after " + t.__name__ + " for " + d.ifname)
logger.info(e) logger.info(e)
if not test_filter: if not args.tests:
reset_devs(dev, apdev) reset_devs(dev, apdev)
if conn: if conn:
@ -242,7 +254,7 @@ def main():
logger.info("passed all " + str(len(passed)) + " test case(s)") logger.info("passed all " + str(len(passed)) + " test case(s)")
if len(skipped): if len(skipped):
logger.info("skipped " + str(len(skipped)) + " test case(s)") logger.info("skipped " + str(len(skipped)) + " test case(s)")
if log_file: if log_to_file:
print "passed all " + str(len(passed)) + " test case(s)" print "passed all " + str(len(passed)) + " test case(s)"
if len(skipped): if len(skipped):
print "skipped " + str(len(skipped)) + " test case(s)" print "skipped " + str(len(skipped)) + " test case(s)"