tests: Add command line argument -f for specifying a test file
[mech_eap.git] / tests / hwsim / run-tests.py
1 #!/usr/bin/python
2 #
3 # AP tests
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import os
10 import re
11 import sys
12 import time
13
14 import logging
15
16 from wpasupplicant import WpaSupplicant
17 from hostapd import HostapdGlobal
18
19 def reset_devs(dev, apdev):
20     for d in dev:
21         d.reset()
22     hapd = HostapdGlobal()
23     for ap in apdev:
24         hapd.remove(ap['ifname'])
25
26 def main():
27     test_file = None
28     idx = 1
29     if len(sys.argv) > 1 and sys.argv[1] == '-d':
30         logging.basicConfig(level=logging.DEBUG)
31         idx = idx + 1
32     elif len(sys.argv) > 1 and sys.argv[1] == '-q':
33         logging.basicConfig(level=logging.WARNING)
34         idx = idx + 1
35     else:
36         logging.basicConfig(level=logging.INFO)
37
38     if len(sys.argv) > idx + 1 and sys.argv[idx] == '-f':
39         test_file = sys.argv[idx + 1]
40         idx = idx + 2
41
42     if len(sys.argv) > idx:
43         test_filter = sys.argv[idx]
44     else:
45         test_filter = None
46
47     dev0 = WpaSupplicant('wlan0')
48     dev1 = WpaSupplicant('wlan1')
49     dev2 = WpaSupplicant('wlan2')
50     dev = [ dev0, dev1, dev2 ]
51     apdev = [ ]
52     apdev.append({"ifname": 'wlan3', "bssid": "02:00:00:00:03:00"})
53     apdev.append({"ifname": 'wlan4', "bssid": "02:00:00:00:04:00"})
54
55     for d in dev:
56         if not d.ping():
57             print d.ifname + ": No response from wpa_supplicant"
58             return
59         d.reset()
60         print "DEV: " + d.ifname + ": " + d.p2p_dev_addr()
61     for ap in apdev:
62         print "APDEV: " + ap['ifname']
63
64     tests = []
65     for t in os.listdir("."):
66         m = re.match(r'(test_.*)\.py$', t)
67         if m:
68             if test_file and test_file not in t:
69                 continue
70             print "Import test cases from " + t
71             mod = __import__(m.group(1))
72             for s in dir(mod):
73                 if s.startswith("test_"):
74                     func = mod.__dict__.get(s)
75                     tests.append(func)
76
77     passed = []
78     failed = []
79
80     for t in tests:
81         if test_filter:
82             if test_filter != t.__name__:
83                 continue
84         reset_devs(dev, apdev)
85         print "START " + t.__name__
86         if t.__doc__:
87             print "Test: " + t.__doc__
88         for d in dev:
89             d.request("NOTE TEST-START " + t.__name__)
90         try:
91             if t.func_code.co_argcount > 1:
92                 t(dev, apdev)
93             else:
94                 t(dev)
95             passed.append(t.__name__)
96             print "PASS " + t.__name__
97         except Exception, e:
98             print e
99             failed.append(t.__name__)
100             print "FAIL " + t.__name__
101         for d in dev:
102             d.request("NOTE TEST-STOP " + t.__name__)
103
104     if not test_filter:
105         reset_devs(dev, apdev)
106
107     print "passed tests: " + str(passed)
108     print "failed tests: " + str(failed)
109     if len(failed):
110         sys.exit(1)
111
112 if __name__ == "__main__":
113     main()