blob: e4f0f8cc8133b692bfec4356b3cbe706e9b324dd [file] [log] [blame]
mbligh1ee9ad72008-08-01 16:15:08 +00001#!/usr/bin/python -u
mblighf9751332008-04-08 18:25:33 +00002
mbligheeb13572008-07-30 00:04:01 +00003import os, sys, unittest, optparse
mblighdc906012008-06-27 19:29:11 +00004import common
mbligheeb13572008-07-30 00:04:01 +00005from autotest_lib.utils import parallel
6
7
mbligheeb13572008-07-30 00:04:01 +00008root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
9
10parser = optparse.OptionParser()
11parser.add_option("-r", action="store", type="string", dest="start",
12 default='',
13 help="root directory to start running unittests")
14parser.add_option("--full", action="store_true", dest="full", default=False,
15 help="whether to run the shortened version of the test")
mbligh43758df2008-09-04 19:54:45 +000016parser.add_option("--debug", action="store_true", dest="debug", default=False,
17 help="run in debug mode")
mblighf9751332008-04-08 18:25:33 +000018
mbligh671c5922008-07-28 19:34:38 +000019
20LONG_TESTS = set((
21 'monitor_db_unittest.py',
22 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000023 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000024 'frontend_unittest.py',
showard3de2a762008-09-12 16:33:18 +000025 'client_compilation_unittest.py'
mbligh671c5922008-07-28 19:34:38 +000026 ))
27
mbligh671c5922008-07-28 19:34:38 +000028modules = []
29
mbligheeb13572008-07-30 00:04:01 +000030
31def lister(full, dirname, files):
jadmanski80718c72009-02-26 00:04:07 +000032 if not os.path.exists(os.path.join(dirname, '__init__.py')):
33 return
jadmanski0afbb632008-06-06 21:10:57 +000034 for f in files:
35 if f.endswith('_unittest.py'):
mbligheeb13572008-07-30 00:04:01 +000036 if not full and f in LONG_TESTS:
mbligh671c5922008-07-28 19:34:38 +000037 continue
mbligh4205d892008-07-14 16:23:20 +000038 temp = os.path.join(dirname, f).strip('.py')
39 mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/')
mbligh671c5922008-07-28 19:34:38 +000040 modules.append(mod_name)
41
42
43def run_test(mod_name):
mbligh43758df2008-09-04 19:54:45 +000044 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000045 parallel.redirect_io()
46
47 print "Running %s" % '.'.join(mod_name)
mbligh671c5922008-07-28 19:34:38 +000048 mod = common.setup_modules.import_module(mod_name[-1],
49 '.'.join(mod_name[:-1]))
50 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
51 suite = unittest.TestSuite(test)
52 runner = unittest.TextTestRunner(verbosity=2)
53 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000054 if result.errors or result.failures:
55 raise Exception("%s failed" % '.'.join(mod_name))
mbligh671c5922008-07-28 19:34:38 +000056
57
mbligheeb13572008-07-30 00:04:01 +000058def run_tests(start, full=False):
59 os.path.walk(start, lister, full)
mbligh671c5922008-07-28 19:34:38 +000060
showardcc85e812008-08-08 20:33:30 +000061 functions = {}
mbligh671c5922008-07-28 19:34:38 +000062 for module in modules:
mbligheeb13572008-07-30 00:04:01 +000063 # Create a function that'll test a particular module. module=module
64 # is a hack to force python to evaluate the params now. We then
65 # rename the function to make error reporting nicer.
66 run_module = lambda module=module: run_test(module)
showardcc85e812008-08-08 20:33:30 +000067 name = '.'.join(module)
68 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +000069 functions[run_module] = set()
70
mbligheeb13572008-07-30 00:04:01 +000071 try:
72 dargs = {}
mbligh43758df2008-09-04 19:54:45 +000073 if options.debug:
mbligheeb13572008-07-30 00:04:01 +000074 dargs['max_simultaneous_procs'] = 1
75 pe = parallel.ParallelExecute(functions, **dargs)
76 pe.run_until_completion()
77 except parallel.ParallelError, err:
78 return err.errors
79 return []
mblighf9751332008-04-08 18:25:33 +000080
81
mbligheeb13572008-07-30 00:04:01 +000082def main():
mbligh43758df2008-09-04 19:54:45 +000083 global options, args
mbligheeb13572008-07-30 00:04:01 +000084 options, args = parser.parse_args()
85 if args:
86 parser.error('Unexpected argument(s): %s' % args)
87 parser.print_help()
88 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +000089
showardcc85e812008-08-08 20:33:30 +000090 # Strip the arguments off the command line, so that the unit tests do not
91 # see them.
92 sys.argv = [sys.argv[0]]
93
mbligheeb13572008-07-30 00:04:01 +000094 errors = run_tests(os.path.join(root, options.start), options.full)
mbligh671c5922008-07-28 19:34:38 +000095 if errors:
96 print "%d tests resulted in an error/failure:" % len(errors)
97 for error in errors:
98 print "\t%s" % error
99 sys.exit(1)
100 else:
101 print "All passed!"
102 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000103
104if __name__ == "__main__":
105 main()