2013-03-27 08:40:10 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2013-03-29 12:37:03 -04:00
|
|
|
# AP tests
|
2013-03-27 08:40:10 -04:00
|
|
|
# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
|
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import time
|
2013-09-28 11:20:32 -04:00
|
|
|
from datetime import datetime
|
2013-10-30 13:28:22 -04:00
|
|
|
import argparse
|
2013-10-30 16:05:24 -04:00
|
|
|
import subprocess
|
2013-03-27 08:40:10 -04:00
|
|
|
|
|
|
|
import logging
|
2013-10-31 06:46:42 -04:00
|
|
|
logger = logging.getLogger()
|
2013-03-27 08:40:10 -04:00
|
|
|
|
2013-10-27 12:28:28 -04:00
|
|
|
sys.path.append('../../wpaspy')
|
|
|
|
|
2013-03-27 08:40:10 -04:00
|
|
|
from wpasupplicant import WpaSupplicant
|
2013-03-29 12:37:03 -04:00
|
|
|
from hostapd import HostapdGlobal
|
|
|
|
|
2013-03-31 09:16:37 -04:00
|
|
|
def reset_devs(dev, apdev):
|
|
|
|
hapd = HostapdGlobal()
|
2013-03-31 13:32:33 -04:00
|
|
|
for d in dev:
|
2013-08-24 14:16:04 -04:00
|
|
|
try:
|
|
|
|
d.reset()
|
|
|
|
except Exception, e:
|
|
|
|
logger.info("Failed to reset device " + d.ifname)
|
2013-09-28 11:20:32 -04:00
|
|
|
print str(e)
|
2013-10-31 11:28:43 -04:00
|
|
|
hapd.remove('wlan3-3')
|
|
|
|
hapd.remove('wlan3-2')
|
2013-03-31 17:59:50 -04:00
|
|
|
for ap in apdev:
|
|
|
|
hapd.remove(ap['ifname'])
|
2013-03-27 08:40:10 -04:00
|
|
|
|
2013-10-26 18:05:45 -04:00
|
|
|
def report(conn, build, commit, run, test, result, diff):
|
|
|
|
if conn:
|
|
|
|
if not build:
|
|
|
|
build = ''
|
|
|
|
if not commit:
|
|
|
|
commit = ''
|
2013-10-30 13:19:07 -04:00
|
|
|
sql = "INSERT INTO results(test,result,run,time,duration,build,commitid) VALUES(?, ?, ?, ?, ?, ?, ?)"
|
2013-10-31 05:43:45 -04:00
|
|
|
params = (test, result, run, time.time(), diff.total_seconds(), build, commit)
|
2013-10-26 18:05:45 -04:00
|
|
|
try:
|
2013-10-30 13:19:07 -04:00
|
|
|
conn.execute(sql, params)
|
2013-10-26 18:05:45 -04:00
|
|
|
conn.commit()
|
|
|
|
except Exception, e:
|
|
|
|
print "sqlite: " + str(e)
|
2013-10-30 13:19:07 -04:00
|
|
|
print "sql: %r" % (params, )
|
2013-10-26 18:05:45 -04:00
|
|
|
|
2013-10-30 17:50:56 -04:00
|
|
|
class DataCollector(object):
|
|
|
|
def __init__(self, logdir, testname, tracing, dmesg):
|
|
|
|
self._logdir = logdir
|
2013-10-30 16:05:24 -04:00
|
|
|
self._testname = testname
|
2013-10-30 17:50:56 -04:00
|
|
|
self._tracing = tracing
|
|
|
|
self._dmesg = dmesg
|
2013-10-30 16:05:24 -04:00
|
|
|
def __enter__(self):
|
2013-10-30 17:50:56 -04:00
|
|
|
if self._tracing:
|
|
|
|
output = os.path.join(self._logdir, '%s.dat' % (self._testname, ))
|
|
|
|
self._trace_cmd = subprocess.Popen(['sudo', 'trace-cmd', 'record', '-o', output, '-e', 'mac80211', '-e', 'cfg80211', 'sh', '-c', 'echo STARTED ; read l'],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=open('/dev/null', 'w'),
|
|
|
|
cwd=self._logdir)
|
|
|
|
l = self._trace_cmd.stdout.read(7)
|
|
|
|
while not 'STARTED' in l:
|
|
|
|
l += self._trace_cmd.stdout.read(1)
|
2013-10-30 16:05:24 -04:00
|
|
|
def __exit__(self, type, value, traceback):
|
2013-10-30 17:50:56 -04:00
|
|
|
if self._tracing:
|
|
|
|
self._trace_cmd.stdin.write('DONE\n')
|
|
|
|
self._trace_cmd.wait()
|
|
|
|
if self._dmesg:
|
|
|
|
output = os.path.join(self._logdir, '%s.dmesg' % (self._testname, ))
|
|
|
|
subprocess.call(['sudo', 'dmesg', '-c'], stdout=open(output, 'w'))
|
2013-10-30 16:05:24 -04:00
|
|
|
|
2013-03-27 08:40:10 -04:00
|
|
|
def main():
|
2013-10-30 13:28:22 -04:00
|
|
|
tests = []
|
|
|
|
test_modules = []
|
|
|
|
for t in os.listdir("."):
|
|
|
|
m = re.match(r'(test_.*)\.py$', t)
|
|
|
|
if m:
|
|
|
|
logger.debug("Import test cases from " + t)
|
|
|
|
mod = __import__(m.group(1))
|
2013-10-31 05:47:43 -04:00
|
|
|
test_modules.append(mod.__name__.replace('test_', '', 1))
|
2013-10-30 13:28:22 -04:00
|
|
|
for s in dir(mod):
|
|
|
|
if s.startswith("test_"):
|
|
|
|
func = mod.__dict__.get(s)
|
|
|
|
tests.append(func)
|
2013-10-31 05:43:45 -04:00
|
|
|
test_names = list(set([t.__name__.replace('test_', '', 1) for t in tests]))
|
2013-10-30 13:28:22 -04:00
|
|
|
|
2013-10-26 18:05:45 -04:00
|
|
|
run = None
|
2013-09-29 09:07:01 -04:00
|
|
|
print_res = False
|
2013-10-30 13:28:22 -04:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='hwsim test runner')
|
2013-10-31 08:27:57 -04:00
|
|
|
parser.add_argument('--logdir', metavar='<directory>',
|
2013-10-30 17:37:15 -04:00
|
|
|
help='log output directory for all other options, ' +
|
|
|
|
'must be given if other log options are used')
|
2013-10-30 13:28:22 -04:00
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument('-d', const=logging.DEBUG, action='store_const',
|
|
|
|
dest='loglevel', default=logging.INFO,
|
|
|
|
help="verbose debug output")
|
|
|
|
group.add_argument('-q', const=logging.WARNING, action='store_const',
|
|
|
|
dest='loglevel', help="be quiet")
|
2013-10-31 06:46:42 -04:00
|
|
|
group.add_argument('-l', action='store_true', dest='logfile',
|
|
|
|
help='store debug log to a file (in log directory)')
|
2013-10-30 13:28:22 -04:00
|
|
|
|
|
|
|
parser.add_argument('-S', metavar='<sqlite3 db>', dest='database',
|
|
|
|
help='database to write results to')
|
2013-10-30 15:01:11 -04:00
|
|
|
parser.add_argument('--commit', metavar='<commit id>',
|
|
|
|
help='commit ID, only for database')
|
2013-10-30 13:28:22 -04:00
|
|
|
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)')
|
2013-10-30 17:37:15 -04:00
|
|
|
parser.add_argument('-T', action='store_true', dest='tracing',
|
|
|
|
help='collect tracing per test case (in log directory)')
|
2013-10-30 17:50:56 -04:00
|
|
|
parser.add_argument('-D', action='store_true', dest='dmesg',
|
|
|
|
help='collect dmesg per test case (in log directory)')
|
2013-10-30 13:49:12 -04:00
|
|
|
parser.add_argument('-f', dest='testmodules', metavar='<test module>',
|
|
|
|
help='execute only tests from these test modules',
|
|
|
|
type=str, choices=[[]] + test_modules, nargs='+')
|
2013-10-30 13:28:22 -04:00
|
|
|
parser.add_argument('tests', metavar='<test>', nargs='*', type=str,
|
|
|
|
help='tests to run (only valid without -f)',
|
|
|
|
choices=[[]] + test_names)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2013-10-30 13:49:12 -04:00
|
|
|
if args.tests and args.testmodules:
|
2013-10-30 13:28:22 -04:00
|
|
|
print 'Invalid arguments - both test module and tests given'
|
|
|
|
sys.exit(2)
|
|
|
|
|
2013-10-31 08:27:57 -04:00
|
|
|
if not args.logdir:
|
|
|
|
if os.path.exists('logs/current'):
|
|
|
|
args.logdir = 'logs/current'
|
|
|
|
else:
|
|
|
|
args.logdir = 'logs'
|
2013-10-30 17:37:15 -04:00
|
|
|
|
2013-10-30 13:28:22 -04:00
|
|
|
if args.logfile:
|
2013-10-31 06:46:42 -04:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
file_name = os.path.join(args.logdir, 'run-tests.log')
|
|
|
|
log_handler = logging.FileHandler(file_name)
|
|
|
|
fmt = "%(asctime)s %(levelname)s %(message)s"
|
|
|
|
log_formatter = logging.Formatter(fmt)
|
|
|
|
log_handler.setFormatter(log_formatter)
|
|
|
|
logger.addHandler(log_handler)
|
2013-10-30 13:28:22 -04:00
|
|
|
log_to_file = True
|
2013-03-27 08:40:10 -04:00
|
|
|
else:
|
2013-10-30 13:28:22 -04:00
|
|
|
logging.basicConfig(level=args.loglevel)
|
2013-10-31 06:46:42 -04:00
|
|
|
log_handler = None
|
2013-10-30 13:28:22 -04:00
|
|
|
log_to_file = False
|
|
|
|
if args.loglevel == logging.WARNING:
|
|
|
|
print_res = True
|
|
|
|
|
|
|
|
if args.database:
|
|
|
|
import sqlite3
|
|
|
|
conn = sqlite3.connect(args.database)
|
|
|
|
else:
|
|
|
|
conn = None
|
2013-03-31 09:29:16 -04:00
|
|
|
|
2013-10-26 18:05:45 -04:00
|
|
|
if conn:
|
2013-10-31 04:43:02 -04:00
|
|
|
run = int(time.time())
|
2013-10-26 18:05:45 -04:00
|
|
|
|
2013-10-30 13:28:22 -04:00
|
|
|
if args.update_tests_db:
|
2013-09-29 09:11:48 -04:00
|
|
|
for t in tests:
|
2013-10-31 05:43:45 -04:00
|
|
|
name = t.__name__.replace('test_', '', 1)
|
|
|
|
print name + " - " + t.__doc__
|
2013-10-27 06:16:36 -04:00
|
|
|
if conn:
|
2013-10-30 13:19:07 -04:00
|
|
|
sql = 'INSERT OR REPLACE INTO tests(test,description) VALUES (?, ?)'
|
2013-10-31 05:43:45 -04:00
|
|
|
params = (name, t.__doc__)
|
2013-10-27 06:16:36 -04:00
|
|
|
try:
|
2013-10-30 13:19:07 -04:00
|
|
|
conn.execute(sql, params)
|
2013-10-27 06:16:36 -04:00
|
|
|
except Exception, e:
|
|
|
|
print "sqlite: " + str(e)
|
2013-10-30 13:19:07 -04:00
|
|
|
print "sql: %r" % (params,)
|
2013-10-27 06:16:36 -04:00
|
|
|
if conn:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
2013-09-29 09:11:48 -04:00
|
|
|
sys.exit(0)
|
|
|
|
|
2013-03-27 08:40:10 -04:00
|
|
|
|
2013-06-30 17:13:11 -04:00
|
|
|
dev0 = WpaSupplicant('wlan0', '/tmp/wpas-wlan0')
|
|
|
|
dev1 = WpaSupplicant('wlan1', '/tmp/wpas-wlan1')
|
|
|
|
dev2 = WpaSupplicant('wlan2', '/tmp/wpas-wlan2')
|
2013-03-31 09:19:12 -04:00
|
|
|
dev = [ dev0, dev1, dev2 ]
|
2013-03-31 09:16:37 -04:00
|
|
|
apdev = [ ]
|
|
|
|
apdev.append({"ifname": 'wlan3', "bssid": "02:00:00:00:03:00"})
|
2013-03-31 09:19:12 -04:00
|
|
|
apdev.append({"ifname": 'wlan4', "bssid": "02:00:00:00:04:00"})
|
2013-03-27 08:40:10 -04:00
|
|
|
|
|
|
|
for d in dev:
|
|
|
|
if not d.ping():
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info(d.ifname + ": No response from wpa_supplicant")
|
2013-03-27 08:40:10 -04:00
|
|
|
return
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info("DEV: " + d.ifname + ": " + d.p2p_dev_addr())
|
2013-03-31 09:16:37 -04:00
|
|
|
for ap in apdev:
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info("APDEV: " + ap['ifname'])
|
2013-03-27 08:40:10 -04:00
|
|
|
|
|
|
|
passed = []
|
2013-09-29 09:21:42 -04:00
|
|
|
skipped = []
|
2013-03-27 08:40:10 -04:00
|
|
|
failed = []
|
|
|
|
|
2013-10-30 17:53:29 -04:00
|
|
|
# make sure nothing is left over from previous runs
|
|
|
|
# (if there were any other manual runs or we crashed)
|
|
|
|
reset_devs(dev, apdev)
|
|
|
|
|
2013-10-30 17:50:56 -04:00
|
|
|
if args.dmesg:
|
|
|
|
subprocess.call(['sudo', 'dmesg', '-c'], stdout=open('/dev/null', 'w'))
|
|
|
|
|
2013-03-27 08:40:10 -04:00
|
|
|
for t in tests:
|
2013-10-31 05:43:45 -04:00
|
|
|
name = t.__name__.replace('test_', '', 1)
|
2013-10-30 13:28:22 -04:00
|
|
|
if args.tests:
|
2013-10-31 05:43:45 -04:00
|
|
|
if not name in args.tests:
|
2013-10-30 13:28:22 -04:00
|
|
|
continue
|
2013-10-30 13:49:12 -04:00
|
|
|
if args.testmodules:
|
2013-10-31 05:47:43 -04:00
|
|
|
if not t.__module__.replace('test_', '', 1) in args.testmodules:
|
2013-03-27 08:40:10 -04:00
|
|
|
continue
|
2013-10-31 06:46:42 -04:00
|
|
|
|
|
|
|
if log_handler:
|
|
|
|
log_handler.stream.close()
|
|
|
|
logger.removeHandler(log_handler)
|
|
|
|
file_name = os.path.join(args.logdir, name + '.log')
|
|
|
|
log_handler = logging.FileHandler(file_name)
|
|
|
|
log_handler.setFormatter(log_formatter)
|
|
|
|
logger.addHandler(log_handler)
|
|
|
|
|
2013-10-31 05:43:45 -04:00
|
|
|
with DataCollector(args.logdir, name, args.tracing, args.dmesg):
|
|
|
|
logger.info("START " + name)
|
2013-10-30 13:28:22 -04:00
|
|
|
if log_to_file:
|
2013-10-31 05:43:45 -04:00
|
|
|
print "START " + name
|
2013-09-29 13:42:37 -04:00
|
|
|
sys.stdout.flush()
|
2013-10-30 16:05:24 -04:00
|
|
|
if t.__doc__:
|
|
|
|
logger.info("Test: " + t.__doc__)
|
|
|
|
start = datetime.now()
|
|
|
|
for d in dev:
|
|
|
|
try:
|
2013-10-31 05:43:45 -04:00
|
|
|
d.request("NOTE TEST-START " + name)
|
2013-10-30 16:05:24 -04:00
|
|
|
except Exception, e:
|
2013-10-31 05:43:45 -04:00
|
|
|
logger.info("Failed to issue TEST-START before " + name + " for " + d.ifname)
|
2013-10-30 16:05:24 -04:00
|
|
|
logger.info(e)
|
2013-10-31 05:43:45 -04:00
|
|
|
print "FAIL " + name + " - could not start test"
|
2013-10-30 16:05:24 -04:00
|
|
|
if conn:
|
|
|
|
conn.close()
|
|
|
|
conn = None
|
|
|
|
sys.exit(1)
|
2013-08-24 12:41:08 -04:00
|
|
|
try:
|
2013-10-30 16:05:24 -04:00
|
|
|
if t.func_code.co_argcount > 1:
|
|
|
|
res = t(dev, apdev)
|
|
|
|
else:
|
|
|
|
res = t(dev)
|
|
|
|
end = datetime.now()
|
|
|
|
diff = end - start
|
|
|
|
if res == "skip":
|
2013-10-31 05:43:45 -04:00
|
|
|
skipped.append(name)
|
2013-10-30 16:05:24 -04:00
|
|
|
result = "SKIP"
|
|
|
|
else:
|
2013-10-31 05:43:45 -04:00
|
|
|
passed.append(name)
|
2013-10-30 16:05:24 -04:00
|
|
|
result = "PASS"
|
2013-10-31 05:43:45 -04:00
|
|
|
report(conn, args.build, args.commit, run, name, result, diff)
|
|
|
|
result = result + " " + name + " "
|
2013-10-30 16:05:24 -04:00
|
|
|
result = result + str(diff.total_seconds()) + " " + str(end)
|
|
|
|
logger.info(result)
|
|
|
|
if log_to_file or print_res:
|
|
|
|
print result
|
|
|
|
sys.stdout.flush()
|
2013-08-24 12:41:08 -04:00
|
|
|
except Exception, e:
|
2013-10-30 16:05:24 -04:00
|
|
|
end = datetime.now()
|
|
|
|
diff = end - start
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info(e)
|
2013-10-31 05:43:45 -04:00
|
|
|
failed.append(name)
|
|
|
|
report(conn, args.build, args.commit, run, name, "FAIL", diff)
|
|
|
|
result = "FAIL " + name + " " + str(diff.total_seconds()) + " " + str(end)
|
2013-10-30 16:05:24 -04:00
|
|
|
logger.info(result)
|
|
|
|
if log_to_file:
|
|
|
|
print result
|
|
|
|
sys.stdout.flush()
|
|
|
|
for d in dev:
|
|
|
|
try:
|
2013-10-31 05:43:45 -04:00
|
|
|
d.request("NOTE TEST-STOP " + name)
|
2013-10-30 16:05:24 -04:00
|
|
|
except Exception, e:
|
2013-10-31 05:43:45 -04:00
|
|
|
logger.info("Failed to issue TEST-STOP after " + name + " for " + d.ifname)
|
2013-10-30 16:05:24 -04:00
|
|
|
logger.info(e)
|
2013-10-30 17:53:29 -04:00
|
|
|
reset_devs(dev, apdev)
|
2013-03-27 08:40:10 -04:00
|
|
|
|
2013-10-31 07:09:14 -04:00
|
|
|
for i in range(0, 3):
|
|
|
|
try:
|
|
|
|
import getpass
|
|
|
|
srcname = os.path.join(args.logdir, 'log' + str(i))
|
|
|
|
dstname = os.path.join(args.logdir, name + '.log' + str(i))
|
|
|
|
num = 0
|
|
|
|
while os.path.exists(dstname):
|
|
|
|
dstname = os.path.join(args.logdir, name + '.log' + str(i) + '-' + str(num))
|
|
|
|
num = num + 1
|
|
|
|
os.rename(srcname, dstname)
|
|
|
|
dev[i].request("RELOG")
|
|
|
|
subprocess.call(['sudo', 'chown', '-f', getpass.getuser(),
|
|
|
|
srcname])
|
|
|
|
except Exception, e:
|
|
|
|
logger.info("Failed to rename log files")
|
|
|
|
logger.info(e)
|
|
|
|
|
2013-10-31 06:46:42 -04:00
|
|
|
if log_handler:
|
|
|
|
log_handler.stream.close()
|
|
|
|
logger.removeHandler(log_handler)
|
|
|
|
file_name = os.path.join(args.logdir, 'run-tests.log')
|
|
|
|
log_handler = logging.FileHandler(file_name)
|
|
|
|
log_handler.setFormatter(log_formatter)
|
|
|
|
logger.addHandler(log_handler)
|
|
|
|
|
2013-10-26 18:05:45 -04:00
|
|
|
if conn:
|
|
|
|
conn.close()
|
|
|
|
|
2013-03-27 08:40:10 -04:00
|
|
|
if len(failed):
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info("passed " + str(len(passed)) + " test case(s)")
|
2013-09-29 09:21:42 -04:00
|
|
|
logger.info("skipped " + str(len(skipped)) + " test case(s)")
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info("failed tests: " + str(failed))
|
2013-03-27 08:40:10 -04:00
|
|
|
sys.exit(1)
|
2013-08-24 12:48:04 -04:00
|
|
|
logger.info("passed all " + str(len(passed)) + " test case(s)")
|
2013-09-29 09:21:42 -04:00
|
|
|
if len(skipped):
|
|
|
|
logger.info("skipped " + str(len(skipped)) + " test case(s)")
|
2013-10-30 13:28:22 -04:00
|
|
|
if log_to_file:
|
2013-08-25 13:15:54 -04:00
|
|
|
print "passed all " + str(len(passed)) + " test case(s)"
|
2013-09-29 09:21:42 -04:00
|
|
|
if len(skipped):
|
|
|
|
print "skipped " + str(len(skipped)) + " test case(s)"
|
2013-03-27 08:40:10 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|