tests: Check for exceptions during TEST-START/STOP
[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     hapd = HostapdGlobal()
21     for d in dev:
22         d.reset()
23     for ap in apdev:
24         hapd.remove(ap['ifname'])
25
26 def main():
27     test_file = None
28     error_file = None
29     idx = 1
30     if len(sys.argv) > 1 and sys.argv[1] == '-d':
31         logging.basicConfig(level=logging.DEBUG)
32         idx = idx + 1
33     elif len(sys.argv) > 1 and sys.argv[1] == '-q':
34         logging.basicConfig(level=logging.WARNING)
35         idx = idx + 1
36     else:
37         logging.basicConfig(level=logging.INFO)
38
39     if len(sys.argv) > idx + 1 and sys.argv[idx] == '-e':
40         error_file = sys.argv[idx + 1]
41         idx = idx + 2
42
43     if len(sys.argv) > idx + 1 and sys.argv[idx] == '-f':
44         test_file = sys.argv[idx + 1]
45         idx = idx + 2
46
47     if len(sys.argv) > idx:
48         test_filter = sys.argv[idx]
49     else:
50         test_filter = None
51
52     dev0 = WpaSupplicant('wlan0', '/tmp/wpas-wlan0')
53     dev1 = WpaSupplicant('wlan1', '/tmp/wpas-wlan1')
54     dev2 = WpaSupplicant('wlan2', '/tmp/wpas-wlan2')
55     dev = [ dev0, dev1, dev2 ]
56     apdev = [ ]
57     apdev.append({"ifname": 'wlan3', "bssid": "02:00:00:00:03:00"})
58     apdev.append({"ifname": 'wlan4', "bssid": "02:00:00:00:04:00"})
59
60     for d in dev:
61         if not d.ping():
62             print d.ifname + ": No response from wpa_supplicant"
63             return
64         print "DEV: " + d.ifname + ": " + d.p2p_dev_addr()
65     for ap in apdev:
66         print "APDEV: " + ap['ifname']
67
68     tests = []
69     for t in os.listdir("."):
70         m = re.match(r'(test_.*)\.py$', t)
71         if m:
72             if test_file and test_file not in t:
73                 continue
74             print "Import test cases from " + t
75             mod = __import__(m.group(1))
76             for s in dir(mod):
77                 if s.startswith("test_"):
78                     func = mod.__dict__.get(s)
79                     tests.append(func)
80
81     passed = []
82     failed = []
83
84     for t in tests:
85         if test_filter:
86             if test_filter != t.__name__:
87                 continue
88         reset_devs(dev, apdev)
89         print "START " + t.__name__
90         if t.__doc__:
91             print "Test: " + t.__doc__
92         for d in dev:
93             try:
94                 d.request("NOTE TEST-START " + t.__name__)
95             except Exception, e:
96                 print "Failed to issue TEST-START before " + t.__name__ + " for " + d.ifname
97                 print e
98         try:
99             if t.func_code.co_argcount > 1:
100                 t(dev, apdev)
101             else:
102                 t(dev)
103             passed.append(t.__name__)
104             print "PASS " + t.__name__
105         except Exception, e:
106             print e
107             failed.append(t.__name__)
108             print "FAIL " + t.__name__
109         for d in dev:
110             try:
111                 d.request("NOTE TEST-STOP " + t.__name__)
112             except Exception, e:
113                 print "Failed to issue TEST-STOP after " + t.__name__ + " for " + d.ifname
114                 print e
115
116     if not test_filter:
117         reset_devs(dev, apdev)
118
119     print
120     if len(failed):
121         print "passed " + str(len(passed)) + " test case(s)"
122         print "failed tests: " + str(failed)
123         if error_file:
124             f = open(error_file, 'w')
125             f.write(str(failed) + '\n')
126             f.close()
127         sys.exit(1)
128     print "passed all " + str(len(passed)) + " test case(s)"
129
130 if __name__ == "__main__":
131     main()