blob: 6364df8c5eedf5debeeeeb9291163eed72d911a3 [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
mbligh5f554842009-12-21 21:50:18 +00006from autotest_lib.client.common_lib.test_utils import unittest as custom_unittest
mbligheeb13572008-07-30 00:04:01 +00007
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")
jamesren74e77fc2010-02-19 21:56:39 +000016parser.add_option("--skip-tests", dest="skip_tests", default=[],
17 help="A space separated list of tests to skip")
18
mblighf9751332008-04-08 18:25:33 +000019
jamesren92c18592010-02-25 19:10:32 +000020REQUIRES_DJANGO = set((
21 'monitor_db_unittest.py',
22 'monitor_db_functional_test.py',
23 'monitor_db_cleanup_test.py',
24 'frontend_unittest.py',
25 'csv_encoder_unittest.py',
26 'rpc_interface_unittest.py',
27 'models_test.py',
28 'scheduler_models_unittest.py',
29 'metahost_scheduler_unittest.py',
30 'site_metahost_scheduler_unittest.py',
31 'rpc_utils_unittest.py',
32 ))
33
34REQUIRES_MYSQLDB = set((
35 'migrate_unittest.py',
36 'db_utils_unittest.py',
37 ))
38
39REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000040 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000041 ))
42
43REQUIRES_SIMPLEJSON = set((
44 'resources_test.py',
45 'serviceHandler_unittest.py',
46 ))
47
jamesren6461fff2010-04-06 23:33:05 +000048REQUIRES_AUTH = set ((
49 'trigger_unittest.py',
50 ))
51
jamesren92c18592010-02-25 19:10:32 +000052LONG_RUNTIME = set((
mbligh671c5922008-07-28 19:34:38 +000053 'barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000054 'logging_manager_test.py',
mbligh671c5922008-07-28 19:34:38 +000055 ))
56
jamesren92c18592010-02-25 19:10:32 +000057LONG_TESTS = (REQUIRES_DJANGO |
58 REQUIRES_MYSQLDB |
59 REQUIRES_GWT |
60 REQUIRES_SIMPLEJSON |
jamesren6461fff2010-04-06 23:33:05 +000061 REQUIRES_AUTH |
jamesren92c18592010-02-25 19:10:32 +000062 LONG_RUNTIME)
63
jamesren74e77fc2010-02-19 21:56:39 +000064
mbligh780fa7f2009-07-02 19:01:53 +000065ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +000066
mbligheeb13572008-07-30 00:04:01 +000067
mbligh780fa7f2009-07-02 19:01:53 +000068class TestFailure(Exception): pass
mbligh671c5922008-07-28 19:34:38 +000069
70
mbligh780fa7f2009-07-02 19:01:53 +000071def run_test(mod_names, options):
72 """
73 @param mod_names: A list of individual parts of the module name to import
74 and run as a test suite.
75 @param options: optparse options.
76 """
mbligh43758df2008-09-04 19:54:45 +000077 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000078 parallel.redirect_io()
79
mbligh780fa7f2009-07-02 19:01:53 +000080 print "Running %s" % '.'.join(mod_names)
81 mod = common.setup_modules.import_module(mod_names[-1],
82 '.'.join(mod_names[:-1]))
mbligh5f554842009-12-21 21:50:18 +000083 for ut_module in [unittest, custom_unittest]:
84 test = ut_module.defaultTestLoader.loadTestsFromModule(mod)
85 suite = ut_module.TestSuite(test)
86 runner = ut_module.TextTestRunner(verbosity=2)
87 result = runner.run(suite)
88 if result.errors or result.failures:
89 msg = '%s had %d failures and %d errors.'
90 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
91 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +000092
93
mbligh780fa7f2009-07-02 19:01:53 +000094def find_and_run_tests(start, options):
95 """
96 Find and run Python unittest suites below the given directory. Only look
97 in subdirectories of start that are actual importable Python modules.
98
99 @param start: The absolute directory to look for tests under.
100 @param options: optparse options.
101 """
102 modules = []
jamesren74e77fc2010-02-19 21:56:39 +0000103 skip_tests = set()
104 if options.skip_tests:
105 skip_tests.update(options.skip_tests.split())
mbligh780fa7f2009-07-02 19:01:53 +0000106
107 for dirpath, subdirs, filenames in os.walk(start):
108 # Only look in and below subdirectories that are python modules.
109 if '__init__.py' not in filenames:
mbligha64df1a2009-09-18 16:54:39 +0000110 if options.full:
111 for filename in filenames:
112 if filename.endswith('.pyc'):
113 os.unlink(os.path.join(dirpath, filename))
mbligh780fa7f2009-07-02 19:01:53 +0000114 # Skip all subdirectories below this one, it is not a module.
115 del subdirs[:]
116 if options.debug:
117 print 'Skipping', dirpath
118 continue # Skip this directory.
119
120 # Look for unittest files.
121 for fname in filenames:
122 if fname.endswith('_unittest.py') or fname.endswith('_test.py'):
123 if not options.full and fname in LONG_TESTS:
124 continue
jamesren74e77fc2010-02-19 21:56:39 +0000125 if fname in skip_tests:
126 continue
mbligh780fa7f2009-07-02 19:01:53 +0000127 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
128 assert path_no_py.startswith(ROOT)
129 names = path_no_py[len(ROOT)+1:].split('/')
130 modules.append(['autotest_lib'] + names)
131 if options.debug:
132 print 'testing', path_no_py
133
134 if options.debug:
135 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000136
showardcc85e812008-08-08 20:33:30 +0000137 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000138 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +0000139 # Create a function that'll test a particular module. module=module
140 # is a hack to force python to evaluate the params now. We then
141 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000142 run_module = lambda module=module_names: run_test(module, options)
143 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000144 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000145 functions[run_module] = set()
146
mbligheeb13572008-07-30 00:04:01 +0000147 try:
148 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000149 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000150 dargs['max_simultaneous_procs'] = 1
151 pe = parallel.ParallelExecute(functions, **dargs)
152 pe.run_until_completion()
153 except parallel.ParallelError, err:
154 return err.errors
155 return []
mblighf9751332008-04-08 18:25:33 +0000156
157
mbligheeb13572008-07-30 00:04:01 +0000158def main():
159 options, args = parser.parse_args()
160 if args:
161 parser.error('Unexpected argument(s): %s' % args)
162 parser.print_help()
163 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000164
showardcc85e812008-08-08 20:33:30 +0000165 # Strip the arguments off the command line, so that the unit tests do not
166 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000167 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000168
mbligh780fa7f2009-07-02 19:01:53 +0000169 absolute_start = os.path.join(ROOT, options.start)
170 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000171 if errors:
172 print "%d tests resulted in an error/failure:" % len(errors)
173 for error in errors:
174 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000175 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000176 sys.exit(1)
177 else:
178 print "All passed!"
179 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000180
mbligh780fa7f2009-07-02 19:01:53 +0000181
mbligheeb13572008-07-30 00:04:01 +0000182if __name__ == "__main__":
183 main()