blob: dd401b89d333be4905706aa372e5da2268d2d502 [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((
74 'job_serializer_unittest.py',
75 ))
76
Aviv Keshet78f446d2013-06-18 10:28:57 -070077REQUIRES_SELENIUM = set((
78 'ap_configurator_factory_unittest.py',
Aviv Keshet78f446d2013-06-18 10:28:57 -070079 'ap_batch_locker_unittest.py'
80 ))
81
jamesren92c18592010-02-25 19:10:32 +000082LONG_RUNTIME = set((
Allen Li38c99602017-02-03 17:07:33 -080083 'barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000084 'logging_manager_test.py',
Prathmesh Prabhuaf048572013-06-25 12:06:00 -070085 'task_loop_unittest.py' # crbug.com/254030
mbligh671c5922008-07-28 19:34:38 +000086 ))
87
Dan Shi8e9f9f12015-02-17 10:51:34 -080088# Unitests that only work in chroot. The names are for module name, thus no
89# file extension of ".py".
90REQUIRES_CHROOT = set((
91 'mbim_channel_unittest',
92 ))
Aviv Keshet78b05952013-02-13 14:37:37 -080093
Eric Li861b2d52011-02-04 14:50:35 -080094SKIP = set((
Aviv Keshet78b05952013-02-13 14:37:37 -080095 # This particular KVM autotest test is not a unittest
Eric Li861b2d52011-02-04 14:50:35 -080096 'guest_test.py',
Aviv Keshet3c5a1f82013-05-10 13:42:18 -070097 'ap_configurator_test.py',
98 'chaos_base_test.py',
Aviv Keshet1f23b692013-05-14 11:13:55 -070099 'chaos_interop_test.py',
beeps7d8273b2013-11-06 09:44:34 -0800100 'atomic_group_unittests.py',
Aviv Keshetc0fdfc52013-06-18 14:24:05 -0700101 # crbug.com/251395
102 'dev_server_test.py',
Aviv Keshet6e161a72013-06-26 17:24:48 -0700103 'full_release_test.py',
Prashanth B0e960282014-05-13 19:38:28 -0700104 'scheduler_lib_unittest.py',
Nathan Stoddardab583042014-08-11 10:41:47 -0700105 'webstore_test.py',
Prathmesh Prabhud456bf02014-11-14 17:28:42 -0800106 # crbug.com/432621 These files are not tests, and will disappear soon.
107 'des_01_test.py',
108 'des_02_test.py',
Dan Shi767dced2015-02-01 00:21:07 -0800109 # Rquire lxc to be installed
110 'lxc_functional_test.py',
Dan Shi4f46aaf2016-06-25 00:21:41 -0700111 # Require sponge utils installed in site-packages
112 'sponge_utils_functional_test.py',
Eric Li861b2d52011-02-04 14:50:35 -0800113 ))
114
Aviv Keshet1f23b692013-05-14 11:13:55 -0700115LONG_TESTS = (REQUIRES_MYSQLDB |
jamesren92c18592010-02-25 19:10:32 +0000116 REQUIRES_GWT |
jamesren37dfa9d2010-04-12 18:24:31 +0000117 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +0000118 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +0000119 REQUIRES_PROTOBUFS |
Aviv Keshet78f446d2013-06-18 10:28:57 -0700120 REQUIRES_SELENIUM |
Prathmesh Prabhu0ddb5412013-11-19 16:47:16 +0000121 LONG_RUNTIME)
jamesren92c18592010-02-25 19:10:32 +0000122
mbligh780fa7f2009-07-02 19:01:53 +0000123ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +0000124
Dan Shi8af6fbe2013-02-13 14:20:16 -0800125# The set of files in LONG_TESTS with its full path
126LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
mbligheeb13572008-07-30 00:04:01 +0000127
Aviv Keshet78b05952013-02-13 14:37:37 -0800128class TestFailure(Exception):
129 """Exception type for any test failure."""
130 pass
mbligh671c5922008-07-28 19:34:38 +0000131
132
mbligh780fa7f2009-07-02 19:01:53 +0000133def run_test(mod_names, options):
134 """
135 @param mod_names: A list of individual parts of the module name to import
136 and run as a test suite.
137 @param options: optparse options.
138 """
mbligh43758df2008-09-04 19:54:45 +0000139 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000140 parallel.redirect_io()
141
mbligh780fa7f2009-07-02 19:01:53 +0000142 print "Running %s" % '.'.join(mod_names)
143 mod = common.setup_modules.import_module(mod_names[-1],
144 '.'.join(mod_names[:-1]))
Justin Giorgi67ad67d2016-06-29 14:41:04 -0700145 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
146 suite = unittest.TestSuite(test)
147 runner = unittest.TextTestRunner(verbosity=2)
148 result = runner.run(suite)
149 if result.errors or result.failures:
150 msg = '%s had %d failures and %d errors.'
151 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
152 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000153
154
Eric Li861b2d52011-02-04 14:50:35 -0800155def scan_for_modules(start, options):
Dan Shi8af6fbe2013-02-13 14:20:16 -0800156 """Scan folders and find all test modules that are not included in the
157 blacklist (defined in LONG_TESTS).
158
Aviv Keshet78b05952013-02-13 14:37:37 -0800159 @param start: The absolute directory to look for tests under.
160 @param options: optparse options.
161 @return a list of modules to be executed.
162 """
mbligh780fa7f2009-07-02 19:01:53 +0000163 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800164
165 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000166 if options.skip_tests:
167 skip_tests.update(options.skip_tests.split())
Dan Shi8af6fbe2013-02-13 14:20:16 -0800168 skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
mbligh780fa7f2009-07-02 19:01:53 +0000169
Dan Shi8af6fbe2013-02-13 14:20:16 -0800170 for dir_path, sub_dirs, file_names in os.walk(start):
mbligh780fa7f2009-07-02 19:01:53 +0000171 # Only look in and below subdirectories that are python modules.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800172 if '__init__.py' not in file_names:
mbligha64df1a2009-09-18 16:54:39 +0000173 if options.full:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800174 for file_name in file_names:
175 if file_name.endswith('.pyc'):
176 os.unlink(os.path.join(dir_path, file_name))
mbligh780fa7f2009-07-02 19:01:53 +0000177 # Skip all subdirectories below this one, it is not a module.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800178 del sub_dirs[:]
mbligh780fa7f2009-07-02 19:01:53 +0000179 if options.debug:
Dan Shi8af6fbe2013-02-13 14:20:16 -0800180 print 'Skipping', dir_path
mbligh780fa7f2009-07-02 19:01:53 +0000181 continue # Skip this directory.
182
183 # Look for unittest files.
Dan Shi8af6fbe2013-02-13 14:20:16 -0800184 for file_name in file_names:
185 if (file_name.endswith('_unittest.py') or
186 file_name.endswith('_test.py')):
187 file_path = os.path.join(dir_path, file_name)
188 if (not options.full and
189 (file_name in LONG_TESTS or
190 file_path in LONG_TESTS_FULL_PATH)):
mbligh780fa7f2009-07-02 19:01:53 +0000191 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800192 if (file_name in skip_tests or
193 file_path in skip_tests_full_path):
jamesren74e77fc2010-02-19 21:56:39 +0000194 continue
Dan Shi8af6fbe2013-02-13 14:20:16 -0800195 path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
mbligh780fa7f2009-07-02 19:01:53 +0000196 assert path_no_py.startswith(ROOT)
197 names = path_no_py[len(ROOT)+1:].split('/')
198 modules.append(['autotest_lib'] + names)
199 if options.debug:
200 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800201 return modules
202
Dan Shi8e9f9f12015-02-17 10:51:34 -0800203
204def is_inside_chroot():
205 """Check if the process is running inside the chroot.
206
207 @return: True if the process is running inside the chroot, False otherwise.
208 """
209 try:
210 # chromite may not be setup, e.g., in vm, therefore the ImportError
211 # needs to be handled.
212 from chromite.lib import cros_build_lib
213 return cros_build_lib.IsInsideChroot()
214 except ImportError:
215 return False
216
217
Eric Li861b2d52011-02-04 14:50:35 -0800218def find_and_run_tests(start, options):
219 """
220 Find and run Python unittest suites below the given directory. Only look
221 in subdirectories of start that are actual importable Python modules.
222
223 @param start: The absolute directory to look for tests under.
224 @param options: optparse options.
225 """
226 if options.module_list:
227 modules = []
228 for m in options.module_list:
229 modules.append(m.split('.'))
230 else:
231 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000232
233 if options.debug:
234 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000235
Dan Shi8e9f9f12015-02-17 10:51:34 -0800236 chroot = is_inside_chroot()
showardcc85e812008-08-08 20:33:30 +0000237 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000238 for module_names in modules:
Dan Shi8e9f9f12015-02-17 10:51:34 -0800239 if not chroot and module_names[-1] in REQUIRES_CHROOT:
240 if options.debug:
241 print ('Test %s requires to run in chroot, skipped.' %
242 module_names[-1])
243 continue
mbligheeb13572008-07-30 00:04:01 +0000244 # Create a function that'll test a particular module. module=module
245 # is a hack to force python to evaluate the params now. We then
246 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000247 run_module = lambda module=module_names: run_test(module, options)
248 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000249 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000250 functions[run_module] = set()
251
mbligheeb13572008-07-30 00:04:01 +0000252 try:
253 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000254 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000255 dargs['max_simultaneous_procs'] = 1
256 pe = parallel.ParallelExecute(functions, **dargs)
257 pe.run_until_completion()
258 except parallel.ParallelError, err:
259 return err.errors
260 return []
mblighf9751332008-04-08 18:25:33 +0000261
262
mbligheeb13572008-07-30 00:04:01 +0000263def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800264 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000265 options, args = parser.parse_args()
266 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800267 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000268
showardcc85e812008-08-08 20:33:30 +0000269 # Strip the arguments off the command line, so that the unit tests do not
270 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000271 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000272
mbligh780fa7f2009-07-02 19:01:53 +0000273 absolute_start = os.path.join(ROOT, options.start)
274 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000275 if errors:
276 print "%d tests resulted in an error/failure:" % len(errors)
277 for error in errors:
278 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000279 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000280 sys.exit(1)
281 else:
282 print "All passed!"
283 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000284
mbligh780fa7f2009-07-02 19:01:53 +0000285
mbligheeb13572008-07-30 00:04:01 +0000286if __name__ == "__main__":
287 main()