blob: d28cbf356844ea657e4efec773dd0bb0a0198934 [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',
25 ))
26
showardcc85e812008-08-08 20:33:30 +000027DEPENDENCIES = {
28 # Annotate dependencies here. The format is
29 # module: [list of modules on which it is dependent]
30 # (All modules in the list must run before this module can)
31
32 # Note: Do not make a short test dependent on a long one. This will cause
33 # the suite to fail if it is run without the --full flag, since the module
34 # that the short test depends on will not be run.
35
36
37 # The next two dependencies are not really dependencies. This is actually a
38 # hack to keep these three modules from running at the same time, since they
39 # all create and destroy a database with the same name.
40 'autotest_lib.frontend.frontend_unittest':
41 ['autotest_lib.migrate.migrate_unittest'],
42
43 'autotest_lib.scheduler.monitor_db_unittest':
44 ['autotest_lib.frontend.frontend_unittest',
45 'autotest_lib.migrate.migrate_unittest'],
46}
47
mbligh671c5922008-07-28 19:34:38 +000048modules = []
49
mbligheeb13572008-07-30 00:04:01 +000050
51def lister(full, dirname, files):
jadmanski0afbb632008-06-06 21:10:57 +000052 for f in files:
53 if f.endswith('_unittest.py'):
mbligheeb13572008-07-30 00:04:01 +000054 if not full and f in LONG_TESTS:
mbligh671c5922008-07-28 19:34:38 +000055 continue
mbligh4205d892008-07-14 16:23:20 +000056 temp = os.path.join(dirname, f).strip('.py')
57 mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/')
mbligh671c5922008-07-28 19:34:38 +000058 modules.append(mod_name)
59
60
61def run_test(mod_name):
mbligh43758df2008-09-04 19:54:45 +000062 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000063 parallel.redirect_io()
64
65 print "Running %s" % '.'.join(mod_name)
mbligh671c5922008-07-28 19:34:38 +000066 mod = common.setup_modules.import_module(mod_name[-1],
67 '.'.join(mod_name[:-1]))
68 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
69 suite = unittest.TestSuite(test)
70 runner = unittest.TextTestRunner(verbosity=2)
71 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000072 if result.errors or result.failures:
73 raise Exception("%s failed" % '.'.join(mod_name))
mbligh671c5922008-07-28 19:34:38 +000074
75
mbligheeb13572008-07-30 00:04:01 +000076def run_tests(start, full=False):
77 os.path.walk(start, lister, full)
mbligh671c5922008-07-28 19:34:38 +000078
showardcc85e812008-08-08 20:33:30 +000079 functions = {}
80 names_to_functions = {}
mbligh671c5922008-07-28 19:34:38 +000081 for module in modules:
mbligheeb13572008-07-30 00:04:01 +000082 # Create a function that'll test a particular module. module=module
83 # is a hack to force python to evaluate the params now. We then
84 # rename the function to make error reporting nicer.
85 run_module = lambda module=module: run_test(module)
showardcc85e812008-08-08 20:33:30 +000086 name = '.'.join(module)
87 run_module.__name__ = name
88 names_to_functions[name] = run_module
89 functions[run_module] = set()
90
91 for fn, deps in DEPENDENCIES.iteritems():
92 if fn in names_to_functions:
93 functions[names_to_functions[fn]] = set(
94 names_to_functions[dep] for dep in deps)
mbligh671c5922008-07-28 19:34:38 +000095
mbligheeb13572008-07-30 00:04:01 +000096 try:
97 dargs = {}
mbligh43758df2008-09-04 19:54:45 +000098 if options.debug:
mbligheeb13572008-07-30 00:04:01 +000099 dargs['max_simultaneous_procs'] = 1
100 pe = parallel.ParallelExecute(functions, **dargs)
101 pe.run_until_completion()
102 except parallel.ParallelError, err:
103 return err.errors
104 return []
mblighf9751332008-04-08 18:25:33 +0000105
106
mbligheeb13572008-07-30 00:04:01 +0000107def main():
mbligh43758df2008-09-04 19:54:45 +0000108 global options, args
mbligheeb13572008-07-30 00:04:01 +0000109 options, args = parser.parse_args()
110 if args:
111 parser.error('Unexpected argument(s): %s' % args)
112 parser.print_help()
113 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000114
showardcc85e812008-08-08 20:33:30 +0000115 # Strip the arguments off the command line, so that the unit tests do not
116 # see them.
117 sys.argv = [sys.argv[0]]
118
mbligheeb13572008-07-30 00:04:01 +0000119 errors = run_tests(os.path.join(root, options.start), options.full)
mbligh671c5922008-07-28 19:34:38 +0000120 if errors:
121 print "%d tests resulted in an error/failure:" % len(errors)
122 for error in errors:
123 print "\t%s" % error
124 sys.exit(1)
125 else:
126 print "All passed!"
127 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000128
129if __name__ == "__main__":
130 main()