blob: f6546d427953c892ae3067a67673cf3787b487a2 [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 +00008parser = optparse.OptionParser()
9parser.add_option("-r", action="store", type="string", dest="start",
10 default='',
11 help="root directory to start running unittests")
12parser.add_option("--full", action="store_true", dest="full", default=False,
13 help="whether to run the shortened version of the test")
mbligh43758df2008-09-04 19:54:45 +000014parser.add_option("--debug", action="store_true", dest="debug", default=False,
15 help="run in debug mode")
mblighf9751332008-04-08 18:25:33 +000016
mbligh671c5922008-07-28 19:34:38 +000017LONG_TESTS = set((
18 'monitor_db_unittest.py',
19 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000020 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000021 'frontend_unittest.py',
showard363cdb52009-05-12 17:21:36 +000022 'client_compilation_unittest.py',
23 'csv_encoder_unittest.py',
showardf8b19042009-05-12 17:22:49 +000024 'rpc_interface_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000025 'logging_manager_test.py',
showarded2afea2009-07-07 20:54:07 +000026 'models_test.py',
mbligh671c5922008-07-28 19:34:38 +000027 ))
28
mbligh780fa7f2009-07-02 19:01:53 +000029ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +000030
mbligheeb13572008-07-30 00:04:01 +000031
mbligh780fa7f2009-07-02 19:01:53 +000032class TestFailure(Exception): pass
mbligh671c5922008-07-28 19:34:38 +000033
34
mbligh780fa7f2009-07-02 19:01:53 +000035def run_test(mod_names, options):
36 """
37 @param mod_names: A list of individual parts of the module name to import
38 and run as a test suite.
39 @param options: optparse options.
40 """
mbligh43758df2008-09-04 19:54:45 +000041 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000042 parallel.redirect_io()
43
mbligh780fa7f2009-07-02 19:01:53 +000044 print "Running %s" % '.'.join(mod_names)
45 mod = common.setup_modules.import_module(mod_names[-1],
46 '.'.join(mod_names[:-1]))
mbligh671c5922008-07-28 19:34:38 +000047 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
48 suite = unittest.TestSuite(test)
49 runner = unittest.TextTestRunner(verbosity=2)
50 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000051 if result.errors or result.failures:
mbligh780fa7f2009-07-02 19:01:53 +000052 raise TestFailure(
53 "%s had %d failures and %d errors." %
54 ('.'.join(mod_names), len(result.failures), len(result.errors)))
mbligh671c5922008-07-28 19:34:38 +000055
56
mbligh780fa7f2009-07-02 19:01:53 +000057def find_and_run_tests(start, options):
58 """
59 Find and run Python unittest suites below the given directory. Only look
60 in subdirectories of start that are actual importable Python modules.
61
62 @param start: The absolute directory to look for tests under.
63 @param options: optparse options.
64 """
65 modules = []
66
67 for dirpath, subdirs, filenames in os.walk(start):
68 # Only look in and below subdirectories that are python modules.
69 if '__init__.py' not in filenames:
mbligha64df1a2009-09-18 16:54:39 +000070 if options.full:
71 for filename in filenames:
72 if filename.endswith('.pyc'):
73 os.unlink(os.path.join(dirpath, filename))
mbligh780fa7f2009-07-02 19:01:53 +000074 # Skip all subdirectories below this one, it is not a module.
75 del subdirs[:]
76 if options.debug:
77 print 'Skipping', dirpath
78 continue # Skip this directory.
79
80 # Look for unittest files.
81 for fname in filenames:
82 if fname.endswith('_unittest.py') or fname.endswith('_test.py'):
83 if not options.full and fname in LONG_TESTS:
84 continue
85 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
86 assert path_no_py.startswith(ROOT)
87 names = path_no_py[len(ROOT)+1:].split('/')
88 modules.append(['autotest_lib'] + names)
89 if options.debug:
90 print 'testing', path_no_py
91
92 if options.debug:
93 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +000094
showardcc85e812008-08-08 20:33:30 +000095 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +000096 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +000097 # Create a function that'll test a particular module. module=module
98 # is a hack to force python to evaluate the params now. We then
99 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000100 run_module = lambda module=module_names: run_test(module, options)
101 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000102 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000103 functions[run_module] = set()
104
mbligheeb13572008-07-30 00:04:01 +0000105 try:
106 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000107 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000108 dargs['max_simultaneous_procs'] = 1
109 pe = parallel.ParallelExecute(functions, **dargs)
110 pe.run_until_completion()
111 except parallel.ParallelError, err:
112 return err.errors
113 return []
mblighf9751332008-04-08 18:25:33 +0000114
115
mbligheeb13572008-07-30 00:04:01 +0000116def main():
117 options, args = parser.parse_args()
118 if args:
119 parser.error('Unexpected argument(s): %s' % args)
120 parser.print_help()
121 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000122
showardcc85e812008-08-08 20:33:30 +0000123 # Strip the arguments off the command line, so that the unit tests do not
124 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000125 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000126
mbligh780fa7f2009-07-02 19:01:53 +0000127 absolute_start = os.path.join(ROOT, options.start)
128 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000129 if errors:
130 print "%d tests resulted in an error/failure:" % len(errors)
131 for error in errors:
132 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000133 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000134 sys.exit(1)
135 else:
136 print "All passed!"
137 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000138
mbligh780fa7f2009-07-02 19:01:53 +0000139
mbligheeb13572008-07-30 00:04:01 +0000140if __name__ == "__main__":
141 main()