blob: 5ff0fb1e97cdd3afbacf3605f751a959704f8bc5 [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
8debug = False
9root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
10
11parser = optparse.OptionParser()
12parser.add_option("-r", action="store", type="string", dest="start",
13 default='',
14 help="root directory to start running unittests")
15parser.add_option("--full", action="store_true", dest="full", default=False,
16 help="whether to run the shortened version of the test")
mblighf9751332008-04-08 18:25:33 +000017
mbligh671c5922008-07-28 19:34:38 +000018
19LONG_TESTS = set((
20 'monitor_db_unittest.py',
21 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000022 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000023 'frontend_unittest.py',
24 ))
25
mbligh671c5922008-07-28 19:34:38 +000026modules = []
27
mbligheeb13572008-07-30 00:04:01 +000028
29def lister(full, dirname, files):
jadmanski0afbb632008-06-06 21:10:57 +000030 for f in files:
31 if f.endswith('_unittest.py'):
mbligheeb13572008-07-30 00:04:01 +000032 if not full and f in LONG_TESTS:
mbligh671c5922008-07-28 19:34:38 +000033 continue
mbligh4205d892008-07-14 16:23:20 +000034 temp = os.path.join(dirname, f).strip('.py')
35 mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/')
mbligh671c5922008-07-28 19:34:38 +000036 modules.append(mod_name)
37
38
39def run_test(mod_name):
mbligheeb13572008-07-30 00:04:01 +000040 if not debug:
41 parallel.redirect_io()
42
43 print "Running %s" % '.'.join(mod_name)
mbligh671c5922008-07-28 19:34:38 +000044 mod = common.setup_modules.import_module(mod_name[-1],
45 '.'.join(mod_name[:-1]))
46 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
47 suite = unittest.TestSuite(test)
48 runner = unittest.TextTestRunner(verbosity=2)
49 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000050 if result.errors or result.failures:
51 raise Exception("%s failed" % '.'.join(mod_name))
mbligh671c5922008-07-28 19:34:38 +000052
53
mbligheeb13572008-07-30 00:04:01 +000054def run_tests(start, full=False):
55 os.path.walk(start, lister, full)
mbligh671c5922008-07-28 19:34:38 +000056
mbligheeb13572008-07-30 00:04:01 +000057 functions = []
mbligh671c5922008-07-28 19:34:38 +000058 for module in modules:
mbligheeb13572008-07-30 00:04:01 +000059 # Create a function that'll test a particular module. module=module
60 # is a hack to force python to evaluate the params now. We then
61 # rename the function to make error reporting nicer.
62 run_module = lambda module=module: run_test(module)
63 run_module.__name__ = '.'.join(module)
64 functions.append(run_module)
mbligh671c5922008-07-28 19:34:38 +000065
mbligheeb13572008-07-30 00:04:01 +000066 try:
67 dargs = {}
68 if debug:
69 dargs['max_simultaneous_procs'] = 1
70 pe = parallel.ParallelExecute(functions, **dargs)
71 pe.run_until_completion()
72 except parallel.ParallelError, err:
73 return err.errors
74 return []
mblighf9751332008-04-08 18:25:33 +000075
76
mbligheeb13572008-07-30 00:04:01 +000077def main():
78 options, args = parser.parse_args()
79 if args:
80 parser.error('Unexpected argument(s): %s' % args)
81 parser.print_help()
82 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +000083
mbligheeb13572008-07-30 00:04:01 +000084 errors = run_tests(os.path.join(root, options.start), options.full)
mbligh671c5922008-07-28 19:34:38 +000085 if errors:
86 print "%d tests resulted in an error/failure:" % len(errors)
87 for error in errors:
88 print "\t%s" % error
89 sys.exit(1)
90 else:
91 print "All passed!"
92 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +000093
94if __name__ == "__main__":
95 main()