blob: 09221c002e96c8dd44a5f0e212f4f63a483b8676 [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
mblighf9751332008-04-08 18:25:33 +000021
jamesren92c18592010-02-25 19:10:32 +000022REQUIRES_DJANGO = set((
23 'monitor_db_unittest.py',
24 'monitor_db_functional_test.py',
25 'monitor_db_cleanup_test.py',
26 'frontend_unittest.py',
27 'csv_encoder_unittest.py',
28 'rpc_interface_unittest.py',
29 'models_test.py',
30 'scheduler_models_unittest.py',
31 'metahost_scheduler_unittest.py',
32 'site_metahost_scheduler_unittest.py',
33 'rpc_utils_unittest.py',
jamesren37dfa9d2010-04-12 18:24:31 +000034 'site_rpc_utils_unittest.py',
jamesren14681352010-04-09 20:46:09 +000035 'execution_engine_unittest.py',
jamesrencd7a81a2010-04-21 20:39:08 +000036 'service_proxy_lib_test.py',
jamesren92c18592010-02-25 19:10:32 +000037 ))
38
39REQUIRES_MYSQLDB = set((
40 'migrate_unittest.py',
41 'db_utils_unittest.py',
42 ))
43
44REQUIRES_GWT = set((
jamesren12c9fdd2010-03-02 00:00:39 +000045 'client_compilation_unittest.py',
jamesren92c18592010-02-25 19:10:32 +000046 ))
47
48REQUIRES_SIMPLEJSON = set((
49 'resources_test.py',
50 'serviceHandler_unittest.py',
51 ))
52
jamesren6461fff2010-04-06 23:33:05 +000053REQUIRES_AUTH = set ((
54 'trigger_unittest.py',
55 ))
56
jamesren37dfa9d2010-04-12 18:24:31 +000057REQUIRES_HTTPLIB2 = set((
58 ))
59
jamesren34a0f3b2010-06-08 20:38:27 +000060REQUIRES_PROTOBUFS = set((
61 'job_serializer_unittest.py',
62 ))
63
jamesren92c18592010-02-25 19:10:32 +000064LONG_RUNTIME = set((
mbligh999fb132010-04-23 17:22:03 +000065 'base_barrier_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000066 'logging_manager_test.py',
mbligh671c5922008-07-28 19:34:38 +000067 ))
68
Aviv Keshet78b05952013-02-13 14:37:37 -080069
Eric Li861b2d52011-02-04 14:50:35 -080070SKIP = set((
Aviv Keshet78b05952013-02-13 14:37:37 -080071 # This particular KVM autotest test is not a unittest
Eric Li861b2d52011-02-04 14:50:35 -080072 'guest_test.py',
Aviv Keshet78b05952013-02-13 14:37:37 -080073 'ap_configurator_test.py'
Eric Li861b2d52011-02-04 14:50:35 -080074 ))
75
jamesren92c18592010-02-25 19:10:32 +000076LONG_TESTS = (REQUIRES_DJANGO |
77 REQUIRES_MYSQLDB |
78 REQUIRES_GWT |
79 REQUIRES_SIMPLEJSON |
jamesren37dfa9d2010-04-12 18:24:31 +000080 REQUIRES_HTTPLIB2 |
jamesren6461fff2010-04-06 23:33:05 +000081 REQUIRES_AUTH |
jamesren34a0f3b2010-06-08 20:38:27 +000082 REQUIRES_PROTOBUFS |
jamesren92c18592010-02-25 19:10:32 +000083 LONG_RUNTIME)
84
jamesren74e77fc2010-02-19 21:56:39 +000085
mbligh780fa7f2009-07-02 19:01:53 +000086ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +000087
mbligheeb13572008-07-30 00:04:01 +000088
Aviv Keshet78b05952013-02-13 14:37:37 -080089class TestFailure(Exception):
90 """Exception type for any test failure."""
91 pass
mbligh671c5922008-07-28 19:34:38 +000092
93
mbligh780fa7f2009-07-02 19:01:53 +000094def run_test(mod_names, options):
95 """
96 @param mod_names: A list of individual parts of the module name to import
97 and run as a test suite.
98 @param options: optparse options.
99 """
mbligh43758df2008-09-04 19:54:45 +0000100 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +0000101 parallel.redirect_io()
102
mbligh780fa7f2009-07-02 19:01:53 +0000103 print "Running %s" % '.'.join(mod_names)
104 mod = common.setup_modules.import_module(mod_names[-1],
105 '.'.join(mod_names[:-1]))
mbligh5f554842009-12-21 21:50:18 +0000106 for ut_module in [unittest, custom_unittest]:
107 test = ut_module.defaultTestLoader.loadTestsFromModule(mod)
108 suite = ut_module.TestSuite(test)
109 runner = ut_module.TextTestRunner(verbosity=2)
110 result = runner.run(suite)
111 if result.errors or result.failures:
112 msg = '%s had %d failures and %d errors.'
113 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
114 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +0000115
116
Eric Li861b2d52011-02-04 14:50:35 -0800117def scan_for_modules(start, options):
Aviv Keshet78b05952013-02-13 14:37:37 -0800118 """Scan folders and find all test modules.
119 @param start: The absolute directory to look for tests under.
120 @param options: optparse options.
121 @return a list of modules to be executed.
122 """
mbligh780fa7f2009-07-02 19:01:53 +0000123 modules = []
Eric Li861b2d52011-02-04 14:50:35 -0800124
125 skip_tests = SKIP
jamesren74e77fc2010-02-19 21:56:39 +0000126 if options.skip_tests:
127 skip_tests.update(options.skip_tests.split())
mbligh780fa7f2009-07-02 19:01:53 +0000128
129 for dirpath, subdirs, filenames in os.walk(start):
130 # Only look in and below subdirectories that are python modules.
131 if '__init__.py' not in filenames:
mbligha64df1a2009-09-18 16:54:39 +0000132 if options.full:
133 for filename in filenames:
134 if filename.endswith('.pyc'):
135 os.unlink(os.path.join(dirpath, filename))
mbligh780fa7f2009-07-02 19:01:53 +0000136 # Skip all subdirectories below this one, it is not a module.
137 del subdirs[:]
138 if options.debug:
139 print 'Skipping', dirpath
140 continue # Skip this directory.
141
142 # Look for unittest files.
143 for fname in filenames:
144 if fname.endswith('_unittest.py') or fname.endswith('_test.py'):
145 if not options.full and fname in LONG_TESTS:
146 continue
jamesren74e77fc2010-02-19 21:56:39 +0000147 if fname in skip_tests:
148 continue
mbligh780fa7f2009-07-02 19:01:53 +0000149 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
150 assert path_no_py.startswith(ROOT)
151 names = path_no_py[len(ROOT)+1:].split('/')
152 modules.append(['autotest_lib'] + names)
153 if options.debug:
154 print 'testing', path_no_py
Eric Li861b2d52011-02-04 14:50:35 -0800155 return modules
156
157def find_and_run_tests(start, options):
158 """
159 Find and run Python unittest suites below the given directory. Only look
160 in subdirectories of start that are actual importable Python modules.
161
162 @param start: The absolute directory to look for tests under.
163 @param options: optparse options.
164 """
165 if options.module_list:
166 modules = []
167 for m in options.module_list:
168 modules.append(m.split('.'))
169 else:
170 modules = scan_for_modules(start, options)
mbligh780fa7f2009-07-02 19:01:53 +0000171
172 if options.debug:
173 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +0000174
showardcc85e812008-08-08 20:33:30 +0000175 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +0000176 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +0000177 # Create a function that'll test a particular module. module=module
178 # is a hack to force python to evaluate the params now. We then
179 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000180 run_module = lambda module=module_names: run_test(module, options)
181 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000182 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000183 functions[run_module] = set()
184
mbligheeb13572008-07-30 00:04:01 +0000185 try:
186 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000187 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000188 dargs['max_simultaneous_procs'] = 1
189 pe = parallel.ParallelExecute(functions, **dargs)
190 pe.run_until_completion()
191 except parallel.ParallelError, err:
192 return err.errors
193 return []
mblighf9751332008-04-08 18:25:33 +0000194
195
mbligheeb13572008-07-30 00:04:01 +0000196def main():
Aviv Keshet78b05952013-02-13 14:37:37 -0800197 """Entry point for unittest_suite.py"""
mbligheeb13572008-07-30 00:04:01 +0000198 options, args = parser.parse_args()
199 if args:
Eric Li861b2d52011-02-04 14:50:35 -0800200 options.module_list = args
mbligh671c5922008-07-28 19:34:38 +0000201
showardcc85e812008-08-08 20:33:30 +0000202 # Strip the arguments off the command line, so that the unit tests do not
203 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000204 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000205
mbligh780fa7f2009-07-02 19:01:53 +0000206 absolute_start = os.path.join(ROOT, options.start)
207 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000208 if errors:
209 print "%d tests resulted in an error/failure:" % len(errors)
210 for error in errors:
211 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000212 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000213 sys.exit(1)
214 else:
215 print "All passed!"
216 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000217
mbligh780fa7f2009-07-02 19:01:53 +0000218
mbligheeb13572008-07-30 00:04:01 +0000219if __name__ == "__main__":
220 main()