blob: e797c5f8d3934afde3d32eddf17f98bdd02ec891 [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
mbligheeb13572008-07-30 00:04:01 +00006
mbligheeb13572008-07-30 00:04:01 +00007parser = optparse.OptionParser()
8parser.add_option("-r", action="store", type="string", dest="start",
9 default='',
10 help="root directory to start running unittests")
11parser.add_option("--full", action="store_true", dest="full", default=False,
12 help="whether to run the shortened version of the test")
mbligh43758df2008-09-04 19:54:45 +000013parser.add_option("--debug", action="store_true", dest="debug", default=False,
14 help="run in debug mode")
jamesren74e77fc2010-02-19 21:56:39 +000015parser.add_option("--skip-tests", dest="skip_tests", default=[],
16 help="A space separated list of tests to skip")
17
Eric Li861b2d52011-02-04 14:50:35 -080018parser.set_defaults(module_list=None)
19
Dan Shi8af6fbe2013-02-13 14:20:16 -080020# Following sets are used to define a collection of modules that are optional
21# tests and do not need to be executed in unittest suite for various reasons.
22# Each entry can be file name or relative path that's relative to the parent
23# folder of the folder containing this file (unittest_suite.py). The list
24# will be used to filter any test file with matching name or matching full
25# path. If a file's name is too general and has a chance to collide with files
26# in other folder, it is recommended to specify its relative path here, e.g.,
27# using 'mirror/trigger_unittest.py', instead of 'trigger_unittest.py' only.
mblighf9751332008-04-08 18:25:33 +000028
jamesren92c18592010-02-25 19:10:32 +000029REQUIRES_DJANGO = set((
30 'monitor_db_unittest.py',
31 'monitor_db_functional_test.py',
32 'monitor_db_cleanup_test.py',
33 'frontend_unittest.py',
34 'csv_encoder_unittest.py',
35 'rpc_interface_unittest.py',
36 'models_test.py',
37 'scheduler_models_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000038 'rpc_utils_unittest.py',
jamesren37dfa9d2010-04-12 18:24:31 +000039 'site_rpc_utils_unittest.py',
jamesren14681352010-04-09 20:46:09 +000040 'execution_engine_unittest.py',
jamesrencd7a81a2010-04-21 20:39:08 +000041 'service_proxy_lib_test.py',
Prashanth B489b91d2014-03-15 12:17:16 -070042 'rdb_integration_tests.py',
Prashanth Bb474fdf2014-04-03 16:05:38 -070043 'rdb_unittest.py',
44 'rdb_hosts_unittest.py',
Prashanth B86934c82014-05-09 11:17:20 -070045 'rdb_cache_unittests.py',
Prashanth B0e960282014-05-13 19:38:28 -070046 'scheduler_lib_unittest.py',
Prashanth Bf66d51b2014-05-06 12:42:25 -070047 'host_scheduler_unittests.py',
Jakob Juelich8a764d12014-10-14 19:24:21 -070048 'site_parse_unittest.py',
Prashanth Balasubramanian22dd2262014-11-28 18:19:18 -080049 'shard_client_integration_tests.py',
Dan Shi784df0c2014-11-26 10:11:15 -080050 'server_manager_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000051 ))
52
53REQUIRES_MYSQLDB = set((
54 'migrate_unittest.py',
55 'db_utils_unittest.py',
56 ))
57
58REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000059 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000060 ))
61
62REQUIRES_SIMPLEJSON = set((
jamesren92c18592010-02-25 19:10:32 +000063 'serviceHandler_unittest.py',
64 ))
65
jamesren6461fff2010-04-06 23:33:05 +000066REQUIRES_AUTH = set ((
67 'trigger_unittest.py',
68 ))
69
jamesren37dfa9d2010-04-12 18:24:31 +000070REQUIRES_HTTPLIB2 = set((
71 ))
72
jamesren34a0f3b2010-06-08 20:38:27 +000073REQUIRES_PROTOBUFS = set((
Michael Tange8bc9592017-07-06 10:59:32 -070074 'cloud_console_client_unittest.py',
jamesren34a0f3b2010-06-08 20:38:27 +000075 'job_serializer_unittest.py',
76 ))
77
Aviv Keshet78f446d2013-06-18 10:28:57 -070078REQUIRES_SELENIUM = set((
79 'ap_configurator_factory_unittest.py',
Aviv Keshet78f446d2013-06-18 10:28:57 -070080 'ap_batch_locker_unittest.py'
81 ))
82
jamesren92c18592010-02-25 19:10:32 +000083LONG_RUNTIME = set((
Allen Li38c99602017-02-03 17:07:33 -080084 'barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000085 'logging_manager_test.py',
Prathmesh Prabhuaf048572013-06-25 12:06:00 -070086 'task_loop_unittest.py' # crbug.com/254030
mbligh671c5922008-07-28 19:34:38 +000087 ))
88
Dan Shi8e9f9f12015-02-17 10:51:34 -080089# Unitests that only work in chroot. The names are for module name, thus no
90# file extension of ".py".
91REQUIRES_CHROOT = set((
92 'mbim_channel_unittest',
93 ))
Aviv Keshet78b05952013-02-13 14:37:37 -080094
Eric Li861b2d52011-02-04 14:50:35 -080095SKIP = set((
Aviv Keshet78b05952013-02-13 14:37:37 -080096 # This particular KVM autotest test is not a unittest
Eric Li861b2d52011-02-04 14:50:35 -080097 'guest_test.py',
Aviv Keshet3c5a1f82013-05-10 13:42:18 -070098 'ap_configurator_test.py',
99 'chaos_base_test.py',
Aviv Keshet1f23b692013-05-14 11:13:55 -0700100 'chaos_interop_test.py',
Allen Li0f261de2017-02-01 17:11:06 -0800101 'only_if_needed_unittests.py',
Aviv Keshetc0fdfc52013-06-18 14:24:05 -0700102 # crbug.com/251395
103 'dev_server_test.py',
Aviv Keshet6e161a72013-06-26 17:24:48 -0700104 'full_release_test.py',
Prashanth B0e960282014-05-13 19:38:28 -0700105 'scheduler_lib_unittest.py',
Nathan Stoddardab583042014-08-11 10:41:47 -0700106 'webstore_test.py',
Prathmesh Prabhud456bf02014-11-14 17:28:42 -0800107 # crbug.com/432621 These files are not tests, and will disappear soon.
108 'des_01_test.py',
109 'des_02_test.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800110 # Require lxc to be installed
Ben Kwad48cbcb2017-08-25 08:59:21 -0700111 'base_image_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800112 'container_bucket_unittest.py',
Ben Kwad2bb0a52017-09-06 12:28:28 -0700113 'container_factory_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800114 'container_unittest.py',
Dan Shi767dced2015-02-01 00:21:07 -0800115 'lxc_functional_test.py',
Ben Kwa5e2bb5c2017-11-01 16:44:16 -0700116 'service_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800117 'zygote_unittest.py',
Dan Shi4f46aaf2016-06-25 00:21:41 -0700118 # Require sponge utils installed in site-packages
119 'sponge_utils_functional_test.py',
Eric Li861b2d52011-02-04 14:50:35 -0800120 ))
121
Aviv Keshet1f23b692013-05-14 11:13:55 -0700122LONG_TESTS = (REQUIRES_MYSQLDB |
jamesren92c18592010-02-25 19:10:32 +0000123 REQUIRES_GWT |
jamesren37dfa9d2010-04-12 18:24:31 +0000124 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +0000125 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +0000126 REQUIRES_PROTOBUFS |
Aviv Keshet78f446d2013-06-18 10:28:57 -0700127 REQUIRES_SELENIUM |
Prathmesh Prabhu0ddb5412013-11-19 16:47:16 +0000128 LONG_RUNTIME)
jamesren92c18592010-02-25 19:10:32 +0000129
mbligh780fa7f2009-07-02 19:01:53 +0000130ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +0000131
Dan Shi8af6fbe2013-02-13 14:20:16 -0800132# The set of files in LONG_TESTS with its full path
133LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
mbligheeb13572008-07-30 00:04:01 +0000134
Aviv Keshet78b05952013-02-13 14:37:37 -0800135class TestFailure(Exception):
136 """Exception type for any test failure."""
137 pass
mbligh671c5922008-07-28 19:34:38 +0000138
139
mbligh780fa7f2009-07-02 19:01:53 +0000140def run_test(mod_names, options):
141 """
142 @param mod_names: A list of individual parts of the module name to import
143 and run as a test suite.
144 @param options: optparse options.
145 """
mbligh43758df2008-09-04 19:54:45 +0000146 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000147 parallel.redirect_io()
148
mbligh780fa7f2009-07-02 19:01:53 +0000149 print "Running %s" % '.'.join(mod_names)
150 mod = common.setup_modules.import_module(mod_names[-1],
151 '.'.join(mod_names[:-1]))
Justin Giorgi67ad67d2016-06-29 14:41:04 -0700152 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
153 suite = unittest.TestSuite(test)
154 runner = unittest.TextTestRunner(verbosity=2)
155 result = runner.run(suite)
156 if result.errors or result.failures:
157 msg = '%s had %d failures and %d errors.'
158 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
159 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000160
161
Eric Li861b2d52011-02-04 14:50:35 -0800162def scan_for_modules(start, options):
Dan Shi8af6fbe2013-02-13 14:20:16 -0800163 """Scan folders and find all test modules that are not included in the
164 blacklist (defined in LONG_TESTS).
165
Aviv Keshet78b05952013-02-13 14:37:37 -0800166 @param start: The absolute directory to look for tests under.
167 @param options: optparse options.
168 @return a list of modules to be executed.
169 """
mbligh780fa7f2009-07-02 19:01:53 +0000170 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800171
172 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000173 if options.skip_tests:
174 skip_tests.update(options.skip_tests.split())
Dan Shi8af6fbe2013-02-13 14:20:16 -0800175 skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
mbligh780fa7f2009-07-02 19:01:53 +0000176
Dan Shi8af6fbe2013-02-13 14:20:16 -0800177 for dir_path, sub_dirs, file_names in os.walk(start):
mbligh780fa7f2009-07-02 19:01:53 +0000178 # Only look in and below subdirectories that are python modules.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800179 if '__init__.py' not in file_names:
mbligha64df1a2009-09-18 16:54:39 +0000180 if options.full:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800181 for file_name in file_names:
182 if file_name.endswith('.pyc'):
183 os.unlink(os.path.join(dir_path, file_name))
mbligh780fa7f2009-07-02 19:01:53 +0000184 # Skip all subdirectories below this one, it is not a module.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800185 del sub_dirs[:]
mbligh780fa7f2009-07-02 19:01:53 +0000186 if options.debug:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800187 print 'Skipping', dir_path
mbligh780fa7f2009-07-02 19:01:53 +0000188 continue # Skip this directory.
189
190 # Look for unittest files.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800191 for file_name in file_names:
192 if (file_name.endswith('_unittest.py') or
193 file_name.endswith('_test.py')):
194 file_path = os.path.join(dir_path, file_name)
195 if (not options.full and
196 (file_name in LONG_TESTS or
197 file_path in LONG_TESTS_FULL_PATH)):
mbligh780fa7f2009-07-02 19:01:53 +0000198 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800199 if (file_name in skip_tests or
200 file_path in skip_tests_full_path):
jamesren74e77fc2010-02-19 21:56:39 +0000201 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800202 path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
mbligh780fa7f2009-07-02 19:01:53 +0000203 assert path_no_py.startswith(ROOT)
204 names = path_no_py[len(ROOT)+1:].split('/')
205 modules.append(['autotest_lib'] + names)
206 if options.debug:
207 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800208 return modules
209
Dan Shi8e9f9f12015-02-17 10:51:34 -0800210
211def is_inside_chroot():
212 """Check if the process is running inside the chroot.
213
214 @return: True if the process is running inside the chroot, False otherwise.
215 """
216 try:
217 # chromite may not be setup, e.g., in vm, therefore the ImportError
218 # needs to be handled.
219 from chromite.lib import cros_build_lib
220 return cros_build_lib.IsInsideChroot()
221 except ImportError:
222 return False
223
224
Eric Li861b2d52011-02-04 14:50:35 -0800225def find_and_run_tests(start, options):
226 """
227 Find and run Python unittest suites below the given directory. Only look
228 in subdirectories of start that are actual importable Python modules.
229
230 @param start: The absolute directory to look for tests under.
231 @param options: optparse options.
232 """
233 if options.module_list:
234 modules = []
235 for m in options.module_list:
236 modules.append(m.split('.'))
237 else:
238 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000239
240 if options.debug:
241 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000242
Dan Shi8e9f9f12015-02-17 10:51:34 -0800243 chroot = is_inside_chroot()
showardcc85e812008-08-08 20:33:30 +0000244 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000245 for module_names in modules:
Dan Shi8e9f9f12015-02-17 10:51:34 -0800246 if not chroot and module_names[-1] in REQUIRES_CHROOT:
247 if options.debug:
248 print ('Test %s requires to run in chroot, skipped.' %
249 module_names[-1])
250 continue
mbligheeb13572008-07-30 00:04:01 +0000251 # Create a function that'll test a particular module. module=module
252 # is a hack to force python to evaluate the params now. We then
253 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000254 run_module = lambda module=module_names: run_test(module, options)
255 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000256 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000257 functions[run_module] = set()
258
mbligheeb13572008-07-30 00:04:01 +0000259 try:
260 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000261 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000262 dargs['max_simultaneous_procs'] = 1
263 pe = parallel.ParallelExecute(functions, **dargs)
264 pe.run_until_completion()
265 except parallel.ParallelError, err:
266 return err.errors
267 return []
mblighf9751332008-04-08 18:25:33 +0000268
269
mbligheeb13572008-07-30 00:04:01 +0000270def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800271 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000272 options, args = parser.parse_args()
273 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800274 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000275
showardcc85e812008-08-08 20:33:30 +0000276 # Strip the arguments off the command line, so that the unit tests do not
277 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000278 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000279
mbligh780fa7f2009-07-02 19:01:53 +0000280 absolute_start = os.path.join(ROOT, options.start)
281 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000282 if errors:
283 print "%d tests resulted in an error/failure:" % len(errors)
284 for error in errors:
285 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000286 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000287 sys.exit(1)
288 else:
289 print "All passed!"
290 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000291
mbligh780fa7f2009-07-02 19:01:53 +0000292
mbligheeb13572008-07-30 00:04:01 +0000293if __name__ == "__main__":
294 main()