blob: 97f4612b0135690c2ac5a7f248e28c208194e639 [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
Eric Li861b2d52011-02-04 14:50:35 -080019parser.set_defaults(module_list=None)
20
Dan Shi8af6fbe2013-02-13 14:20:16 -080021# Following sets are used to define a collection of modules that are optional
22# tests and do not need to be executed in unittest suite for various reasons.
23# Each entry can be file name or relative path that's relative to the parent
24# folder of the folder containing this file (unittest_suite.py). The list
25# will be used to filter any test file with matching name or matching full
26# path. If a file's name is too general and has a chance to collide with files
27# in other folder, it is recommended to specify its relative path here, e.g.,
28# using 'mirror/trigger_unittest.py', instead of 'trigger_unittest.py' only.
mblighf9751332008-04-08 18:25:33 +000029
jamesren92c18592010-02-25 19:10:32 +000030REQUIRES_DJANGO = set((
31 'monitor_db_unittest.py',
32 'monitor_db_functional_test.py',
33 'monitor_db_cleanup_test.py',
34 'frontend_unittest.py',
35 'csv_encoder_unittest.py',
36 'rpc_interface_unittest.py',
37 'models_test.py',
38 'scheduler_models_unittest.py',
39 'metahost_scheduler_unittest.py',
40 'site_metahost_scheduler_unittest.py',
41 'rpc_utils_unittest.py',
jamesren37dfa9d2010-04-12 18:24:31 +000042 'site_rpc_utils_unittest.py',
jamesren14681352010-04-09 20:46:09 +000043 'execution_engine_unittest.py',
jamesrencd7a81a2010-04-21 20:39:08 +000044 'service_proxy_lib_test.py',
jamesren92c18592010-02-25 19:10:32 +000045 ))
46
47REQUIRES_MYSQLDB = set((
48 'migrate_unittest.py',
49 'db_utils_unittest.py',
50 ))
51
52REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000053 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000054 ))
55
56REQUIRES_SIMPLEJSON = set((
57 'resources_test.py',
58 'serviceHandler_unittest.py',
59 ))
60
jamesren6461fff2010-04-06 23:33:05 +000061REQUIRES_AUTH = set ((
62 'trigger_unittest.py',
63 ))
64
jamesren37dfa9d2010-04-12 18:24:31 +000065REQUIRES_HTTPLIB2 = set((
66 ))
67
jamesren34a0f3b2010-06-08 20:38:27 +000068REQUIRES_PROTOBUFS = set((
69 'job_serializer_unittest.py',
70 ))
71
Aviv Keshet78f446d2013-06-18 10:28:57 -070072REQUIRES_SELENIUM = set((
73 'ap_configurator_factory_unittest.py',
Aviv Keshet78f446d2013-06-18 10:28:57 -070074 'ap_batch_locker_unittest.py'
75 ))
76
jamesren92c18592010-02-25 19:10:32 +000077LONG_RUNTIME = set((
Alex Miller13b83c52013-02-14 16:07:34 -080078 'auth_server_unittest.py',
mbligh999fb132010-04-23 17:22:03 +000079 'base_barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000080 'logging_manager_test.py',
Prathmesh Prabhuaf048572013-06-25 12:06:00 -070081 'task_loop_unittest.py' # crbug.com/254030
mbligh671c5922008-07-28 19:34:38 +000082 ))
83
Aviv Keshet78b05952013-02-13 14:37:37 -080084
Eric Li861b2d52011-02-04 14:50:35 -080085SKIP = set((
Aviv Keshet78b05952013-02-13 14:37:37 -080086 # This particular KVM autotest test is not a unittest
Eric Li861b2d52011-02-04 14:50:35 -080087 'guest_test.py',
Aviv Keshet3c5a1f82013-05-10 13:42:18 -070088 'ap_configurator_test.py',
89 'chaos_base_test.py',
Aviv Keshet1f23b692013-05-14 11:13:55 -070090 'chaos_interop_test.py',
beepsd7958542013-06-13 10:04:34 -070091 'monitor_db_functional_test.py',
beeps7d8273b2013-11-06 09:44:34 -080092 'atomic_group_unittests.py',
Aviv Keshetc0fdfc52013-06-18 14:24:05 -070093 # crbug.com/251395
94 'dev_server_test.py',
Aviv Keshet6e161a72013-06-26 17:24:48 -070095 'full_release_test.py',
Eric Li861b2d52011-02-04 14:50:35 -080096 ))
97
Aviv Keshet1f23b692013-05-14 11:13:55 -070098LONG_TESTS = (REQUIRES_MYSQLDB |
jamesren92c18592010-02-25 19:10:32 +000099 REQUIRES_GWT |
jamesren37dfa9d2010-04-12 18:24:31 +0000100 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +0000101 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +0000102 REQUIRES_PROTOBUFS |
Aviv Keshet78f446d2013-06-18 10:28:57 -0700103 REQUIRES_SELENIUM |
Prathmesh Prabhu0ddb5412013-11-19 16:47:16 +0000104 LONG_RUNTIME)
jamesren92c18592010-02-25 19:10:32 +0000105
mbligh780fa7f2009-07-02 19:01:53 +0000106ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +0000107
Dan Shi8af6fbe2013-02-13 14:20:16 -0800108# The set of files in LONG_TESTS with its full path
109LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
mbligheeb13572008-07-30 00:04:01 +0000110
Aviv Keshet78b05952013-02-13 14:37:37 -0800111class TestFailure(Exception):
112 """Exception type for any test failure."""
113 pass
mbligh671c5922008-07-28 19:34:38 +0000114
115
mbligh780fa7f2009-07-02 19:01:53 +0000116def run_test(mod_names, options):
117 """
118 @param mod_names: A list of individual parts of the module name to import
119 and run as a test suite.
120 @param options: optparse options.
121 """
mbligh43758df2008-09-04 19:54:45 +0000122 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000123 parallel.redirect_io()
124
mbligh780fa7f2009-07-02 19:01:53 +0000125 print "Running %s" % '.'.join(mod_names)
126 mod = common.setup_modules.import_module(mod_names[-1],
127 '.'.join(mod_names[:-1]))
mbligh5f554842009-12-21 21:50:18 +0000128 for ut_module in [unittest, custom_unittest]:
129 test = ut_module.defaultTestLoader.loadTestsFromModule(mod)
130 suite = ut_module.TestSuite(test)
131 runner = ut_module.TextTestRunner(verbosity=2)
132 result = runner.run(suite)
133 if result.errors or result.failures:
134 msg = '%s had %d failures and %d errors.'
135 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
136 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000137
138
Eric Li861b2d52011-02-04 14:50:35 -0800139def scan_for_modules(start, options):
Dan Shi8af6fbe2013-02-13 14:20:16 -0800140 """Scan folders and find all test modules that are not included in the
141 blacklist (defined in LONG_TESTS).
142
Aviv Keshet78b05952013-02-13 14:37:37 -0800143 @param start: The absolute directory to look for tests under.
144 @param options: optparse options.
145 @return a list of modules to be executed.
146 """
mbligh780fa7f2009-07-02 19:01:53 +0000147 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800148
149 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000150 if options.skip_tests:
151 skip_tests.update(options.skip_tests.split())
Dan Shi8af6fbe2013-02-13 14:20:16 -0800152 skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
mbligh780fa7f2009-07-02 19:01:53 +0000153
Dan Shi8af6fbe2013-02-13 14:20:16 -0800154 for dir_path, sub_dirs, file_names in os.walk(start):
mbligh780fa7f2009-07-02 19:01:53 +0000155 # Only look in and below subdirectories that are python modules.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800156 if '__init__.py' not in file_names:
mbligha64df1a2009-09-18 16:54:39 +0000157 if options.full:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800158 for file_name in file_names:
159 if file_name.endswith('.pyc'):
160 os.unlink(os.path.join(dir_path, file_name))
mbligh780fa7f2009-07-02 19:01:53 +0000161 # Skip all subdirectories below this one, it is not a module.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800162 del sub_dirs[:]
mbligh780fa7f2009-07-02 19:01:53 +0000163 if options.debug:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800164 print 'Skipping', dir_path
mbligh780fa7f2009-07-02 19:01:53 +0000165 continue # Skip this directory.
166
167 # Look for unittest files.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800168 for file_name in file_names:
169 if (file_name.endswith('_unittest.py') or
170 file_name.endswith('_test.py')):
171 file_path = os.path.join(dir_path, file_name)
172 if (not options.full and
173 (file_name in LONG_TESTS or
174 file_path in LONG_TESTS_FULL_PATH)):
mbligh780fa7f2009-07-02 19:01:53 +0000175 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800176 if (file_name in skip_tests or
177 file_path in skip_tests_full_path):
jamesren74e77fc2010-02-19 21:56:39 +0000178 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800179 path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
mbligh780fa7f2009-07-02 19:01:53 +0000180 assert path_no_py.startswith(ROOT)
181 names = path_no_py[len(ROOT)+1:].split('/')
182 modules.append(['autotest_lib'] + names)
183 if options.debug:
184 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800185 return modules
186
187def find_and_run_tests(start, options):
188 """
189 Find and run Python unittest suites below the given directory. Only look
190 in subdirectories of start that are actual importable Python modules.
191
192 @param start: The absolute directory to look for tests under.
193 @param options: optparse options.
194 """
195 if options.module_list:
196 modules = []
197 for m in options.module_list:
198 modules.append(m.split('.'))
199 else:
200 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000201
202 if options.debug:
203 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000204
showardcc85e812008-08-08 20:33:30 +0000205 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000206 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +0000207 # Create a function that'll test a particular module. module=module
208 # is a hack to force python to evaluate the params now. We then
209 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000210 run_module = lambda module=module_names: run_test(module, options)
211 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000212 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000213 functions[run_module] = set()
214
mbligheeb13572008-07-30 00:04:01 +0000215 try:
216 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000217 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000218 dargs['max_simultaneous_procs'] = 1
219 pe = parallel.ParallelExecute(functions, **dargs)
220 pe.run_until_completion()
221 except parallel.ParallelError, err:
222 return err.errors
223 return []
mblighf9751332008-04-08 18:25:33 +0000224
225
mbligheeb13572008-07-30 00:04:01 +0000226def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800227 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000228 options, args = parser.parse_args()
229 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800230 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000231
showardcc85e812008-08-08 20:33:30 +0000232 # Strip the arguments off the command line, so that the unit tests do not
233 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000234 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000235
mbligh780fa7f2009-07-02 19:01:53 +0000236 absolute_start = os.path.join(ROOT, options.start)
237 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000238 if errors:
239 print "%d tests resulted in an error/failure:" % len(errors)
240 for error in errors:
241 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000242 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000243 sys.exit(1)
244 else:
245 print "All passed!"
246 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000247
mbligh780fa7f2009-07-02 19:01:53 +0000248
mbligheeb13572008-07-30 00:04:01 +0000249if __name__ == "__main__":
250 main()