blob: 31f12477ac0a8464fe3c3eb9b943ba99d207b465 [file] [log] [blame]
Mike Frysinger0e2cb7a2019-08-20 17:04:52 -04001#!/usr/bin/python2 -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((
jamesren92c18592010-02-25 19:10:32 +000030 'frontend_unittest.py',
31 'csv_encoder_unittest.py',
32 'rpc_interface_unittest.py',
33 'models_test.py',
jamesren92c18592010-02-25 19:10:32 +000034 'rpc_utils_unittest.py',
jamesren37dfa9d2010-04-12 18:24:31 +000035 'site_rpc_utils_unittest.py',
jamesren14681352010-04-09 20:46:09 +000036 'execution_engine_unittest.py',
jamesrencd7a81a2010-04-21 20:39:08 +000037 'service_proxy_lib_test.py',
Jakob Juelich8a764d12014-10-14 19:24:21 -070038 'site_parse_unittest.py',
Dan Shi784df0c2014-11-26 10:11:15 -080039 'server_manager_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000040 ))
41
42REQUIRES_MYSQLDB = set((
43 'migrate_unittest.py',
44 'db_utils_unittest.py',
45 ))
46
47REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000048 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000049 ))
50
51REQUIRES_SIMPLEJSON = set((
jamesren92c18592010-02-25 19:10:32 +000052 'serviceHandler_unittest.py',
53 ))
54
jamesren6461fff2010-04-06 23:33:05 +000055REQUIRES_AUTH = set ((
56 'trigger_unittest.py',
57 ))
58
jamesren37dfa9d2010-04-12 18:24:31 +000059REQUIRES_HTTPLIB2 = set((
60 ))
61
jamesren34a0f3b2010-06-08 20:38:27 +000062REQUIRES_PROTOBUFS = set((
Michael Tange8bc9592017-07-06 10:59:32 -070063 'cloud_console_client_unittest.py',
jamesren34a0f3b2010-06-08 20:38:27 +000064 'job_serializer_unittest.py',
65 ))
66
Aviv Keshet78f446d2013-06-18 10:28:57 -070067REQUIRES_SELENIUM = set((
68 'ap_configurator_factory_unittest.py',
Aviv Keshet78f446d2013-06-18 10:28:57 -070069 'ap_batch_locker_unittest.py'
70 ))
71
jamesren92c18592010-02-25 19:10:32 +000072LONG_RUNTIME = set((
Allen Li38c99602017-02-03 17:07:33 -080073 'barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000074 'logging_manager_test.py',
Prathmesh Prabhuaf048572013-06-25 12:06:00 -070075 'task_loop_unittest.py' # crbug.com/254030
mbligh671c5922008-07-28 19:34:38 +000076 ))
77
Dan Shi8e9f9f12015-02-17 10:51:34 -080078# Unitests that only work in chroot. The names are for module name, thus no
79# file extension of ".py".
80REQUIRES_CHROOT = set((
81 'mbim_channel_unittest',
82 ))
Aviv Keshet78b05952013-02-13 14:37:37 -080083
Eric Li861b2d52011-02-04 14:50:35 -080084SKIP = set((
Aviv Keshet78b05952013-02-13 14:37:37 -080085 # This particular KVM autotest test is not a unittest
Eric Li861b2d52011-02-04 14:50:35 -080086 'guest_test.py',
Aviv Keshet3c5a1f82013-05-10 13:42:18 -070087 'ap_configurator_test.py',
88 'chaos_base_test.py',
Aviv Keshet1f23b692013-05-14 11:13:55 -070089 'chaos_interop_test.py',
Aviv Keshetc0fdfc52013-06-18 14:24:05 -070090 # crbug.com/251395
91 'dev_server_test.py',
Aviv Keshet6e161a72013-06-26 17:24:48 -070092 'full_release_test.py',
Prashanth B0e960282014-05-13 19:38:28 -070093 'scheduler_lib_unittest.py',
Nathan Stoddardab583042014-08-11 10:41:47 -070094 'webstore_test.py',
Prathmesh Prabhud456bf02014-11-14 17:28:42 -080095 # crbug.com/432621 These files are not tests, and will disappear soon.
96 'des_01_test.py',
97 'des_02_test.py',
Ben Kwac0ce5452017-07-12 12:12:46 +080098 # Require lxc to be installed
Ben Kwad48cbcb2017-08-25 08:59:21 -070099 'base_image_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800100 'container_bucket_unittest.py',
Ben Kwad2bb0a52017-09-06 12:28:28 -0700101 'container_factory_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800102 'container_unittest.py',
Dan Shi767dced2015-02-01 00:21:07 -0800103 'lxc_functional_test.py',
Ben Kwa5e2bb5c2017-11-01 16:44:16 -0700104 'service_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800105 'zygote_unittest.py',
Dan Shi4f46aaf2016-06-25 00:21:41 -0700106 # Require sponge utils installed in site-packages
107 'sponge_utils_functional_test.py',
Eric Li861b2d52011-02-04 14:50:35 -0800108 ))
109
Aviv Keshet1f23b692013-05-14 11:13:55 -0700110LONG_TESTS = (REQUIRES_MYSQLDB |
jamesren92c18592010-02-25 19:10:32 +0000111 REQUIRES_GWT |
jamesren37dfa9d2010-04-12 18:24:31 +0000112 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +0000113 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +0000114 REQUIRES_PROTOBUFS |
Aviv Keshet78f446d2013-06-18 10:28:57 -0700115 REQUIRES_SELENIUM |
Prathmesh Prabhu0ddb5412013-11-19 16:47:16 +0000116 LONG_RUNTIME)
jamesren92c18592010-02-25 19:10:32 +0000117
mbligh780fa7f2009-07-02 19:01:53 +0000118ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +0000119
Dan Shi8af6fbe2013-02-13 14:20:16 -0800120# The set of files in LONG_TESTS with its full path
121LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
mbligheeb13572008-07-30 00:04:01 +0000122
Aviv Keshet78b05952013-02-13 14:37:37 -0800123class TestFailure(Exception):
124 """Exception type for any test failure."""
125 pass
mbligh671c5922008-07-28 19:34:38 +0000126
127
mbligh780fa7f2009-07-02 19:01:53 +0000128def run_test(mod_names, options):
129 """
130 @param mod_names: A list of individual parts of the module name to import
131 and run as a test suite.
132 @param options: optparse options.
133 """
mbligh43758df2008-09-04 19:54:45 +0000134 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000135 parallel.redirect_io()
136
mbligh780fa7f2009-07-02 19:01:53 +0000137 print "Running %s" % '.'.join(mod_names)
138 mod = common.setup_modules.import_module(mod_names[-1],
139 '.'.join(mod_names[:-1]))
Justin Giorgi67ad67d2016-06-29 14:41:04 -0700140 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
141 suite = unittest.TestSuite(test)
142 runner = unittest.TextTestRunner(verbosity=2)
143 result = runner.run(suite)
144 if result.errors or result.failures:
145 msg = '%s had %d failures and %d errors.'
146 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
147 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000148
149
Eric Li861b2d52011-02-04 14:50:35 -0800150def scan_for_modules(start, options):
Dan Shi8af6fbe2013-02-13 14:20:16 -0800151 """Scan folders and find all test modules that are not included in the
152 blacklist (defined in LONG_TESTS).
153
Aviv Keshet78b05952013-02-13 14:37:37 -0800154 @param start: The absolute directory to look for tests under.
155 @param options: optparse options.
156 @return a list of modules to be executed.
157 """
mbligh780fa7f2009-07-02 19:01:53 +0000158 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800159
160 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000161 if options.skip_tests:
162 skip_tests.update(options.skip_tests.split())
Dan Shi8af6fbe2013-02-13 14:20:16 -0800163 skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
mbligh780fa7f2009-07-02 19:01:53 +0000164
Dan Shi8af6fbe2013-02-13 14:20:16 -0800165 for dir_path, sub_dirs, file_names in os.walk(start):
mbligh780fa7f2009-07-02 19:01:53 +0000166 # Only look in and below subdirectories that are python modules.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800167 if '__init__.py' not in file_names:
mbligha64df1a2009-09-18 16:54:39 +0000168 if options.full:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800169 for file_name in file_names:
170 if file_name.endswith('.pyc'):
171 os.unlink(os.path.join(dir_path, file_name))
mbligh780fa7f2009-07-02 19:01:53 +0000172 # Skip all subdirectories below this one, it is not a module.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800173 del sub_dirs[:]
mbligh780fa7f2009-07-02 19:01:53 +0000174 if options.debug:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800175 print 'Skipping', dir_path
mbligh780fa7f2009-07-02 19:01:53 +0000176 continue # Skip this directory.
177
178 # Look for unittest files.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800179 for file_name in file_names:
180 if (file_name.endswith('_unittest.py') or
181 file_name.endswith('_test.py')):
182 file_path = os.path.join(dir_path, file_name)
183 if (not options.full and
184 (file_name in LONG_TESTS or
185 file_path in LONG_TESTS_FULL_PATH)):
mbligh780fa7f2009-07-02 19:01:53 +0000186 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800187 if (file_name in skip_tests or
188 file_path in skip_tests_full_path):
jamesren74e77fc2010-02-19 21:56:39 +0000189 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800190 path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
mbligh780fa7f2009-07-02 19:01:53 +0000191 assert path_no_py.startswith(ROOT)
192 names = path_no_py[len(ROOT)+1:].split('/')
193 modules.append(['autotest_lib'] + names)
194 if options.debug:
195 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800196 return modules
197
Dan Shi8e9f9f12015-02-17 10:51:34 -0800198
199def is_inside_chroot():
200 """Check if the process is running inside the chroot.
201
202 @return: True if the process is running inside the chroot, False otherwise.
203 """
204 try:
205 # chromite may not be setup, e.g., in vm, therefore the ImportError
206 # needs to be handled.
207 from chromite.lib import cros_build_lib
208 return cros_build_lib.IsInsideChroot()
209 except ImportError:
210 return False
211
212
Eric Li861b2d52011-02-04 14:50:35 -0800213def find_and_run_tests(start, options):
214 """
215 Find and run Python unittest suites below the given directory. Only look
216 in subdirectories of start that are actual importable Python modules.
217
218 @param start: The absolute directory to look for tests under.
219 @param options: optparse options.
220 """
221 if options.module_list:
222 modules = []
223 for m in options.module_list:
224 modules.append(m.split('.'))
225 else:
226 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000227
228 if options.debug:
229 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000230
Dan Shi8e9f9f12015-02-17 10:51:34 -0800231 chroot = is_inside_chroot()
showardcc85e812008-08-08 20:33:30 +0000232 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000233 for module_names in modules:
Dan Shi8e9f9f12015-02-17 10:51:34 -0800234 if not chroot and module_names[-1] in REQUIRES_CHROOT:
235 if options.debug:
236 print ('Test %s requires to run in chroot, skipped.' %
237 module_names[-1])
238 continue
mbligheeb13572008-07-30 00:04:01 +0000239 # Create a function that'll test a particular module. module=module
240 # is a hack to force python to evaluate the params now. We then
241 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000242 run_module = lambda module=module_names: run_test(module, options)
243 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000244 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000245 functions[run_module] = set()
246
mbligheeb13572008-07-30 00:04:01 +0000247 try:
248 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000249 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000250 dargs['max_simultaneous_procs'] = 1
251 pe = parallel.ParallelExecute(functions, **dargs)
252 pe.run_until_completion()
253 except parallel.ParallelError, err:
254 return err.errors
255 return []
mblighf9751332008-04-08 18:25:33 +0000256
257
mbligheeb13572008-07-30 00:04:01 +0000258def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800259 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000260 options, args = parser.parse_args()
261 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800262 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000263
showardcc85e812008-08-08 20:33:30 +0000264 # Strip the arguments off the command line, so that the unit tests do not
265 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000266 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000267
mbligh780fa7f2009-07-02 19:01:53 +0000268 absolute_start = os.path.join(ROOT, options.start)
269 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000270 if errors:
271 print "%d tests resulted in an error/failure:" % len(errors)
272 for error in errors:
273 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000274 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000275 sys.exit(1)
276 else:
277 print "All passed!"
278 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000279
mbligh780fa7f2009-07-02 19:01:53 +0000280
mbligheeb13572008-07-30 00:04:01 +0000281if __name__ == "__main__":
282 main()