blob: e020545fa51bfcd76717f98a810bc1650932973a [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
showardcc85e812008-08-08 20:33:30 +000026DEPENDENCIES = {
27 # Annotate dependencies here. The format is
28 # module: [list of modules on which it is dependent]
29 # (All modules in the list must run before this module can)
30
31 # Note: Do not make a short test dependent on a long one. This will cause
32 # the suite to fail if it is run without the --full flag, since the module
33 # that the short test depends on will not be run.
34
35
36 # The next two dependencies are not really dependencies. This is actually a
37 # hack to keep these three modules from running at the same time, since they
38 # all create and destroy a database with the same name.
39 'autotest_lib.frontend.frontend_unittest':
40 ['autotest_lib.migrate.migrate_unittest'],
41
42 'autotest_lib.scheduler.monitor_db_unittest':
43 ['autotest_lib.frontend.frontend_unittest',
44 'autotest_lib.migrate.migrate_unittest'],
45}
46
mbligh671c5922008-07-28 19:34:38 +000047modules = []
48
mbligheeb13572008-07-30 00:04:01 +000049
50def lister(full, dirname, files):
jadmanski0afbb632008-06-06 21:10:57 +000051 for f in files:
52 if f.endswith('_unittest.py'):
mbligheeb13572008-07-30 00:04:01 +000053 if not full and f in LONG_TESTS:
mbligh671c5922008-07-28 19:34:38 +000054 continue
mbligh4205d892008-07-14 16:23:20 +000055 temp = os.path.join(dirname, f).strip('.py')
56 mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/')
mbligh671c5922008-07-28 19:34:38 +000057 modules.append(mod_name)
58
59
60def run_test(mod_name):
mbligheeb13572008-07-30 00:04:01 +000061 if not debug:
62 parallel.redirect_io()
63
64 print "Running %s" % '.'.join(mod_name)
mbligh671c5922008-07-28 19:34:38 +000065 mod = common.setup_modules.import_module(mod_name[-1],
66 '.'.join(mod_name[:-1]))
67 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
68 suite = unittest.TestSuite(test)
69 runner = unittest.TextTestRunner(verbosity=2)
70 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000071 if result.errors or result.failures:
72 raise Exception("%s failed" % '.'.join(mod_name))
mbligh671c5922008-07-28 19:34:38 +000073
74
mbligheeb13572008-07-30 00:04:01 +000075def run_tests(start, full=False):
76 os.path.walk(start, lister, full)
mbligh671c5922008-07-28 19:34:38 +000077
showardcc85e812008-08-08 20:33:30 +000078 functions = {}
79 names_to_functions = {}
mbligh671c5922008-07-28 19:34:38 +000080 for module in modules:
mbligheeb13572008-07-30 00:04:01 +000081 # Create a function that'll test a particular module. module=module
82 # is a hack to force python to evaluate the params now. We then
83 # rename the function to make error reporting nicer.
84 run_module = lambda module=module: run_test(module)
showardcc85e812008-08-08 20:33:30 +000085 name = '.'.join(module)
86 run_module.__name__ = name
87 names_to_functions[name] = run_module
88 functions[run_module] = set()
89
90 for fn, deps in DEPENDENCIES.iteritems():
91 if fn in names_to_functions:
92 functions[names_to_functions[fn]] = set(
93 names_to_functions[dep] for dep in deps)
mbligh671c5922008-07-28 19:34:38 +000094
mbligheeb13572008-07-30 00:04:01 +000095 try:
96 dargs = {}
97 if debug:
98 dargs['max_simultaneous_procs'] = 1
99 pe = parallel.ParallelExecute(functions, **dargs)
100 pe.run_until_completion()
101 except parallel.ParallelError, err:
102 return err.errors
103 return []
mblighf9751332008-04-08 18:25:33 +0000104
105
mbligheeb13572008-07-30 00:04:01 +0000106def main():
107 options, args = parser.parse_args()
108 if args:
109 parser.error('Unexpected argument(s): %s' % args)
110 parser.print_help()
111 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000112
showardcc85e812008-08-08 20:33:30 +0000113 # Strip the arguments off the command line, so that the unit tests do not
114 # see them.
115 sys.argv = [sys.argv[0]]
116
mbligheeb13572008-07-30 00:04:01 +0000117 errors = run_tests(os.path.join(root, options.start), options.full)
mbligh671c5922008-07-28 19:34:38 +0000118 if errors:
119 print "%d tests resulted in an error/failure:" % len(errors)
120 for error in errors:
121 print "\t%s" % error
122 sys.exit(1)
123 else:
124 print "All passed!"
125 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000126
127if __name__ == "__main__":
128 main()