forked from autotest/autotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest_suite.py
executable file
·337 lines (276 loc) · 9.29 KB
/
unittest_suite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python -u
import os, sys, unittest, optparse, fcntl
try:
import autotest.common as common
except ImportError:
import common
from autotest.utils import parallel
from autotest.client.shared.test_utils import unittest as custom_unittest
class StreamProxy(object):
"""
Mechanism to supress stdout output, while keeping the original stdout.
"""
def __init__(self, filename='/dev/null', stream=sys.stdout):
"""
Keep 2 streams to write to, and eventually switch.
"""
self.terminal = stream
self.log = open(filename, "a")
self.stream = self.log
def write(self, message):
"""
Write to the current stream.
"""
self.stream.write(message)
def flush(self):
"""
Flush the current stream.
"""
self.stream.flush()
def switch(self):
"""
Switch between the 2 currently available streams.
"""
if self.stream == self.log:
self.stream = self.terminal
else:
self.stream = self.log
def print_stdout(sr, end=True):
try:
sys.stdout.switch()
except AttributeError:
pass
if end:
print(sr)
else:
print(sr),
try:
sys.stdout.switch()
except AttributeError:
pass
class Bcolors(object):
"""
Very simple class with color support.
"""
def __init__(self):
self.HEADER = '\033[94m'
self.PASS = '\033[92m'
self.SKIP = '\033[93m'
self.FAIL = '\033[91m'
self.ENDC = '\033[0m'
allowed_terms = ['linux', 'xterm', 'xterm-256color', 'vt100']
term = os.environ.get("TERM")
if (not os.isatty(1)) or (not term in allowed_terms):
self.disable()
def disable(self):
self.HEADER = ''
self.PASS = ''
self.SKIP = ''
self.FAIL = ''
self.ENDC = ''
# Instantiate bcolors to be used in the functions below.
bcolors = Bcolors()
def print_header(sr):
"""
Print a string to stdout with HEADER (blue) color.
"""
print_stdout(bcolors.HEADER + sr + bcolors.ENDC)
def print_skip():
"""
Print SKIP to stdout with SKIP (yellow) color.
"""
print_stdout(bcolors.SKIP + "SKIP" + bcolors.ENDC)
def print_pass(end=True):
"""
Print PASS to stdout with PASS (green) color.
"""
print_stdout(bcolors.PASS + "PASS" + bcolors.ENDC, end=end)
def print_fail(end=True):
"""
Print FAIL to stdout with FAIL (red) color.
"""
print_stdout(bcolors.FAIL + "FAIL" + bcolors.ENDC, end=end)
def silence_stderr():
out_fd = os.open('/dev/null', os.O_WRONLY | os.O_CREAT)
try:
os.dup2(out_fd, 2)
finally:
os.close(out_fd)
sys.stderr = os.fdopen(2, 'w')
parser = optparse.OptionParser()
parser.add_option("-r", action="store", type="string", dest="start",
default='',
help="root directory to start running unittests")
parser.add_option("--full", action="store_true", dest="full", default=False,
help="whether to run the shortened version of the test")
parser.add_option("--debug", action="store_true", dest="debug", default=False,
help="run in debug mode")
parser.add_option("--skip-tests", dest="skip_tests", default=[],
help="A space separated list of tests to skip")
parser.set_defaults(module_list=None)
REQUIRES_DJANGO = set((
'monitor_db_unittest.py',
'monitor_db_functional_unittest.py',
'monitor_db_cleanup_unittest.py',
'frontend_unittest.py',
'csv_encoder_unittest.py',
'rpc_interface_unittest.py',
'models_unittest.py',
'scheduler_models_unittest.py',
'metahost_scheduler_unittest.py',
'site_metahost_scheduler_unittest.py',
'rpc_utils_unittest.py',
'site_rpc_utils_unittest.py',
'execution_engine_unittest.py',
'service_proxy_lib_unittest.py',
))
REQUIRES_MYSQLDB = set((
'migrate_unittest.py',
'db_utils_unittest.py',
))
REQUIRES_GWT = set((
'client_compilation_unittest.py',
))
REQUIRES_SIMPLEJSON = set((
'resources_unittest.py',
'serviceHandler_unittest.py',
))
REQUIRES_AUTH = set ((
'trigger_unittest.py',
))
REQUIRES_PROTOBUFS = set((
'job_serializer_unittest.py',
))
LONG_RUNTIME = set((
'base_barrier_unittest.py',
'logging_manager_unittest.py',
'base_syncdata_unittest.py'
))
LONG_TESTS = (REQUIRES_DJANGO |
REQUIRES_MYSQLDB |
REQUIRES_GWT |
REQUIRES_SIMPLEJSON |
REQUIRES_AUTH |
REQUIRES_PROTOBUFS |
LONG_RUNTIME)
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
class TestFailure(Exception): pass
def run_test(mod_names, options):
"""
@param mod_names: A list of individual parts of the module name to import
and run as a test suite.
@param options: optparse options.
"""
if not options.debug:
sys.stdout = StreamProxy(stream=sys.stdout)
silence_stderr()
else:
sys.stdout = StreamProxy(stream=sys.stdout)
test_name = '.'.join(mod_names)
fail = False
try:
mod = common.setup_modules.import_module(mod_names[-1],
'.'.join(mod_names[:-1]))
for ut_module in [unittest, custom_unittest]:
test = ut_module.defaultTestLoader.loadTestsFromModule(mod)
suite = ut_module.TestSuite(test)
runner = ut_module.TextTestRunner(verbosity=2)
result = runner.run(suite)
if result.errors or result.failures:
fail = True
except:
fail = True
lockfile = open('/var/tmp/unittest.lock', 'w')
fcntl.flock(lockfile, fcntl.LOCK_EX)
print_stdout(test_name + ":", end=False)
if fail:
print_fail()
else:
print_pass()
fcntl.flock(lockfile, fcntl.LOCK_UN)
lockfile.close()
if fail:
raise TestFailure("Test %s failed" % test_name)
def scan_for_modules(start, options):
modules = []
skip_tests = []
if options.skip_tests:
skip_tests = options.skip_tests.split()
for dirpath, subdirs, filenames in os.walk(start):
# Only look in and below subdirectories that are python modules.
if '__init__.py' not in filenames:
if options.full:
for filename in filenames:
if filename.endswith('.pyc'):
os.unlink(os.path.join(dirpath, filename))
# Skip all subdirectories below this one, it is not a module.
del subdirs[:]
if options.debug:
print 'Skipping', dirpath
continue # Skip this directory.
# Look for unittest files.
for fname in filenames:
if fname.endswith('_unittest.py'):
if not options.full and fname in LONG_TESTS:
continue
if fname[:-3] in skip_tests:
continue
path_no_py = os.path.join(dirpath, fname).rstrip('.py')
assert path_no_py.startswith(ROOT)
names = path_no_py[len(ROOT)+1:].split('/')
modules.append(['autotest'] + names)
if options.debug:
print 'testing', path_no_py
return modules
def find_and_run_tests(start, options):
"""
Find and run Python unittest suites below the given directory. Only look
in subdirectories of start that are actual importable Python modules.
@param start: The absolute directory to look for tests under.
@param options: optparse options.
"""
if options.module_list:
modules = []
for m in options.module_list:
modules.append(m.split('.'))
else:
modules = scan_for_modules(start, options)
print_header('Number of test modules found: %d' % len(modules))
functions = {}
for module_names in modules:
# Create a function that'll test a particular module. module=module
# is a hack to force python to evaluate the params now. We then
# rename the function to make error reporting nicer.
run_module = lambda module=module_names: run_test(module, options)
name = '.'.join(module_names)
run_module.__name__ = name
functions[run_module] = set()
try:
dargs = {}
if options.debug:
dargs['max_simultaneous_procs'] = 1
pe = parallel.ParallelExecute(functions, **dargs)
pe.run_until_completion()
except parallel.ParallelError, err:
return err.errors
return []
def main():
options, args = parser.parse_args()
if args:
options.module_list = args
# Strip the arguments off the command line, so that the unit tests do not
# see them.
del sys.argv[1:]
absolute_start = os.path.join(ROOT, options.start)
errors = find_and_run_tests(absolute_start, options)
if errors:
print "%d tests resulted in an error/failure:" % len(errors)
for error in errors:
print "\t%s" % error
print "Rerun", sys.argv[0], "--debug to see the failure details."
sys.exit(1)
else:
print "All passed!"
sys.exit(0)
if __name__ == "__main__":
main()