blob: 10756d2c550de55073127d0c8088957efc7fffae [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',
Prashanth B0e960282014-05-13 19:38:28 -070038 'scheduler_lib_unittest.py',
Jakob Juelich8a764d12014-10-14 19:24:21 -070039 'site_parse_unittest.py',
Dan Shi784df0c2014-11-26 10:11:15 -080040 'server_manager_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000041 ))
42
43REQUIRES_MYSQLDB = set((
44 'migrate_unittest.py',
45 'db_utils_unittest.py',
46 ))
47
48REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000049 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000050 ))
51
52REQUIRES_SIMPLEJSON = set((
jamesren92c18592010-02-25 19:10:32 +000053 'serviceHandler_unittest.py',
54 ))
55
jamesren6461fff2010-04-06 23:33:05 +000056REQUIRES_AUTH = set ((
57 'trigger_unittest.py',
58 ))
59
jamesren37dfa9d2010-04-12 18:24:31 +000060REQUIRES_HTTPLIB2 = set((
61 ))
62
jamesren34a0f3b2010-06-08 20:38:27 +000063REQUIRES_PROTOBUFS = set((
Michael Tange8bc9592017-07-06 10:59:32 -070064 'cloud_console_client_unittest.py',
jamesren34a0f3b2010-06-08 20:38:27 +000065 'job_serializer_unittest.py',
66 ))
67
Aviv Keshet78f446d2013-06-18 10:28:57 -070068REQUIRES_SELENIUM = set((
69 'ap_configurator_factory_unittest.py',
Aviv Keshet78f446d2013-06-18 10:28:57 -070070 'ap_batch_locker_unittest.py'
71 ))
72
jamesren92c18592010-02-25 19:10:32 +000073LONG_RUNTIME = set((
Allen Li38c99602017-02-03 17:07:33 -080074 'barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000075 'logging_manager_test.py',
Prathmesh Prabhuaf048572013-06-25 12:06:00 -070076 'task_loop_unittest.py' # crbug.com/254030
mbligh671c5922008-07-28 19:34:38 +000077 ))
78
Dan Shi8e9f9f12015-02-17 10:51:34 -080079# Unitests that only work in chroot. The names are for module name, thus no
80# file extension of ".py".
81REQUIRES_CHROOT = set((
82 'mbim_channel_unittest',
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',
Aviv Keshetc0fdfc52013-06-18 14:24:05 -070091 # crbug.com/251395
92 'dev_server_test.py',
Aviv Keshet6e161a72013-06-26 17:24:48 -070093 'full_release_test.py',
Prashanth B0e960282014-05-13 19:38:28 -070094 'scheduler_lib_unittest.py',
Nathan Stoddardab583042014-08-11 10:41:47 -070095 'webstore_test.py',
Prathmesh Prabhud456bf02014-11-14 17:28:42 -080096 # crbug.com/432621 These files are not tests, and will disappear soon.
97 'des_01_test.py',
98 'des_02_test.py',
Ben Kwac0ce5452017-07-12 12:12:46 +080099 # Require lxc to be installed
Ben Kwad48cbcb2017-08-25 08:59:21 -0700100 'base_image_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800101 'container_bucket_unittest.py',
Ben Kwad2bb0a52017-09-06 12:28:28 -0700102 'container_factory_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800103 'container_unittest.py',
Dan Shi767dced2015-02-01 00:21:07 -0800104 'lxc_functional_test.py',
Ben Kwa5e2bb5c2017-11-01 16:44:16 -0700105 'service_unittest.py',
Ben Kwac0ce5452017-07-12 12:12:46 +0800106 'zygote_unittest.py',
Dan Shi4f46aaf2016-06-25 00:21:41 -0700107 # Require sponge utils installed in site-packages
108 'sponge_utils_functional_test.py',
Eric Li861b2d52011-02-04 14:50:35 -0800109 ))
110
Aviv Keshet1f23b692013-05-14 11:13:55 -0700111LONG_TESTS = (REQUIRES_MYSQLDB |
jamesren92c18592010-02-25 19:10:32 +0000112 REQUIRES_GWT |
jamesren37dfa9d2010-04-12 18:24:31 +0000113 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +0000114 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +0000115 REQUIRES_PROTOBUFS |
Aviv Keshet78f446d2013-06-18 10:28:57 -0700116 REQUIRES_SELENIUM |
Prathmesh Prabhu0ddb5412013-11-19 16:47:16 +0000117 LONG_RUNTIME)
jamesren92c18592010-02-25 19:10:32 +0000118
mbligh780fa7f2009-07-02 19:01:53 +0000119ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +0000120
Dan Shi8af6fbe2013-02-13 14:20:16 -0800121# The set of files in LONG_TESTS with its full path
122LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
mbligheeb13572008-07-30 00:04:01 +0000123
Aviv Keshet78b05952013-02-13 14:37:37 -0800124class TestFailure(Exception):
125 """Exception type for any test failure."""
126 pass
mbligh671c5922008-07-28 19:34:38 +0000127
128
mbligh780fa7f2009-07-02 19:01:53 +0000129def run_test(mod_names, options):
130 """
131 @param mod_names: A list of individual parts of the module name to import
132 and run as a test suite.
133 @param options: optparse options.
134 """
mbligh43758df2008-09-04 19:54:45 +0000135 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000136 parallel.redirect_io()
137
mbligh780fa7f2009-07-02 19:01:53 +0000138 print "Running %s" % '.'.join(mod_names)
139 mod = common.setup_modules.import_module(mod_names[-1],
140 '.'.join(mod_names[:-1]))
Justin Giorgi67ad67d2016-06-29 14:41:04 -0700141 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
142 suite = unittest.TestSuite(test)
143 runner = unittest.TextTestRunner(verbosity=2)
144 result = runner.run(suite)
145 if result.errors or result.failures:
146 msg = '%s had %d failures and %d errors.'
147 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
148 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000149
150
Eric Li861b2d52011-02-04 14:50:35 -0800151def scan_for_modules(start, options):
Dan Shi8af6fbe2013-02-13 14:20:16 -0800152 """Scan folders and find all test modules that are not included in the
153 blacklist (defined in LONG_TESTS).
154
Aviv Keshet78b05952013-02-13 14:37:37 -0800155 @param start: The absolute directory to look for tests under.
156 @param options: optparse options.
157 @return a list of modules to be executed.
158 """
mbligh780fa7f2009-07-02 19:01:53 +0000159 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800160
161 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000162 if options.skip_tests:
163 skip_tests.update(options.skip_tests.split())
Dan Shi8af6fbe2013-02-13 14:20:16 -0800164 skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
mbligh780fa7f2009-07-02 19:01:53 +0000165
Dan Shi8af6fbe2013-02-13 14:20:16 -0800166 for dir_path, sub_dirs, file_names in os.walk(start):
mbligh780fa7f2009-07-02 19:01:53 +0000167 # Only look in and below subdirectories that are python modules.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800168 if '__init__.py' not in file_names:
mbligha64df1a2009-09-18 16:54:39 +0000169 if options.full:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800170 for file_name in file_names:
171 if file_name.endswith('.pyc'):
172 os.unlink(os.path.join(dir_path, file_name))
mbligh780fa7f2009-07-02 19:01:53 +0000173 # Skip all subdirectories below this one, it is not a module.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800174 del sub_dirs[:]
mbligh780fa7f2009-07-02 19:01:53 +0000175 if options.debug:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800176 print 'Skipping', dir_path
mbligh780fa7f2009-07-02 19:01:53 +0000177 continue # Skip this directory.
178
179 # Look for unittest files.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800180 for file_name in file_names:
181 if (file_name.endswith('_unittest.py') or
182 file_name.endswith('_test.py')):
183 file_path = os.path.join(dir_path, file_name)
184 if (not options.full and
185 (file_name in LONG_TESTS or
186 file_path in LONG_TESTS_FULL_PATH)):
mbligh780fa7f2009-07-02 19:01:53 +0000187 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800188 if (file_name in skip_tests or
189 file_path in skip_tests_full_path):
jamesren74e77fc2010-02-19 21:56:39 +0000190 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800191 path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
mbligh780fa7f2009-07-02 19:01:53 +0000192 assert path_no_py.startswith(ROOT)
193 names = path_no_py[len(ROOT)+1:].split('/')
194 modules.append(['autotest_lib'] + names)
195 if options.debug:
196 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800197 return modules
198
Dan Shi8e9f9f12015-02-17 10:51:34 -0800199
200def is_inside_chroot():
201 """Check if the process is running inside the chroot.
202
203 @return: True if the process is running inside the chroot, False otherwise.
204 """
205 try:
206 # chromite may not be setup, e.g., in vm, therefore the ImportError
207 # needs to be handled.
208 from chromite.lib import cros_build_lib
209 return cros_build_lib.IsInsideChroot()
210 except ImportError:
211 return False
212
213
Eric Li861b2d52011-02-04 14:50:35 -0800214def find_and_run_tests(start, options):
215 """
216 Find and run Python unittest suites below the given directory. Only look
217 in subdirectories of start that are actual importable Python modules.
218
219 @param start: The absolute directory to look for tests under.
220 @param options: optparse options.
221 """
222 if options.module_list:
223 modules = []
224 for m in options.module_list:
225 modules.append(m.split('.'))
226 else:
227 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000228
229 if options.debug:
230 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000231
Dan Shi8e9f9f12015-02-17 10:51:34 -0800232 chroot = is_inside_chroot()
showardcc85e812008-08-08 20:33:30 +0000233 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000234 for module_names in modules:
Dan Shi8e9f9f12015-02-17 10:51:34 -0800235 if not chroot and module_names[-1] in REQUIRES_CHROOT:
236 if options.debug:
237 print ('Test %s requires to run in chroot, skipped.' %
238 module_names[-1])
239 continue
mbligheeb13572008-07-30 00:04:01 +0000240 # Create a function that'll test a particular module. module=module
241 # is a hack to force python to evaluate the params now. We then
242 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000243 run_module = lambda module=module_names: run_test(module, options)
244 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000245 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000246 functions[run_module] = set()
247
mbligheeb13572008-07-30 00:04:01 +0000248 try:
249 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000250 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000251 dargs['max_simultaneous_procs'] = 1
252 pe = parallel.ParallelExecute(functions, **dargs)
253 pe.run_until_completion()
254 except parallel.ParallelError, err:
255 return err.errors
256 return []
mblighf9751332008-04-08 18:25:33 +0000257
258
mbligheeb13572008-07-30 00:04:01 +0000259def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800260 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000261 options, args = parser.parse_args()
262 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800263 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000264
showardcc85e812008-08-08 20:33:30 +0000265 # Strip the arguments off the command line, so that the unit tests do not
266 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000267 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000268
mbligh780fa7f2009-07-02 19:01:53 +0000269 absolute_start = os.path.join(ROOT, options.start)
270 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000271 if errors:
272 print "%d tests resulted in an error/failure:" % len(errors)
273 for error in errors:
274 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000275 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000276 sys.exit(1)
277 else:
278 print "All passed!"
279 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000280
mbligh780fa7f2009-07-02 19:01:53 +0000281
mbligheeb13572008-07-30 00:04:01 +0000282if __name__ == "__main__":
283 main()