mbligh | 1ee9ad7 | 2008-08-01 16:15:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 2 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 3 | import os, sys, unittest, optparse |
mbligh | dc90601 | 2008-06-27 19:29:11 +0000 | [diff] [blame] | 4 | import common |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 5 | from autotest_lib.utils import parallel |
mbligh | 5f55484 | 2009-12-21 21:50:18 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib.test_utils import unittest as custom_unittest |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 7 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 8 | parser = optparse.OptionParser() |
| 9 | parser.add_option("-r", action="store", type="string", dest="start", |
| 10 | default='', |
| 11 | help="root directory to start running unittests") |
| 12 | parser.add_option("--full", action="store_true", dest="full", default=False, |
| 13 | help="whether to run the shortened version of the test") |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 14 | parser.add_option("--debug", action="store_true", dest="debug", default=False, |
| 15 | help="run in debug mode") |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 16 | parser.add_option("--skip-tests", dest="skip_tests", default=[], |
| 17 | help="A space separated list of tests to skip") |
| 18 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 19 | parser.set_defaults(module_list=None) |
| 20 | |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 21 | # Following sets are used to define a collection of modules that are optional |
| 22 | # tests and do not need to be executed in unittest suite for various reasons. |
| 23 | # Each entry can be file name or relative path that's relative to the parent |
| 24 | # folder of the folder containing this file (unittest_suite.py). The list |
| 25 | # will be used to filter any test file with matching name or matching full |
| 26 | # path. If a file's name is too general and has a chance to collide with files |
| 27 | # in other folder, it is recommended to specify its relative path here, e.g., |
| 28 | # using 'mirror/trigger_unittest.py', instead of 'trigger_unittest.py' only. |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 29 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 30 | REQUIRES_DJANGO = set(( |
| 31 | 'monitor_db_unittest.py', |
| 32 | 'monitor_db_functional_test.py', |
| 33 | 'monitor_db_cleanup_test.py', |
| 34 | 'frontend_unittest.py', |
| 35 | 'csv_encoder_unittest.py', |
| 36 | 'rpc_interface_unittest.py', |
| 37 | 'models_test.py', |
| 38 | 'scheduler_models_unittest.py', |
| 39 | 'metahost_scheduler_unittest.py', |
| 40 | 'site_metahost_scheduler_unittest.py', |
| 41 | 'rpc_utils_unittest.py', |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 42 | 'site_rpc_utils_unittest.py', |
jamesren | 1468135 | 2010-04-09 20:46:09 +0000 | [diff] [blame] | 43 | 'execution_engine_unittest.py', |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 44 | 'service_proxy_lib_test.py', |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 45 | )) |
| 46 | |
| 47 | REQUIRES_MYSQLDB = set(( |
| 48 | 'migrate_unittest.py', |
| 49 | 'db_utils_unittest.py', |
| 50 | )) |
| 51 | |
| 52 | REQUIRES_GWT = set(( |
jamesren | 12c9fdd | 2010-03-02 00:00:39 +0000 | [diff] [blame] | 53 | 'client_compilation_unittest.py', |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 54 | )) |
| 55 | |
| 56 | REQUIRES_SIMPLEJSON = set(( |
| 57 | 'resources_test.py', |
| 58 | 'serviceHandler_unittest.py', |
| 59 | )) |
| 60 | |
jamesren | 6461fff | 2010-04-06 23:33:05 +0000 | [diff] [blame] | 61 | REQUIRES_AUTH = set (( |
| 62 | 'trigger_unittest.py', |
| 63 | )) |
| 64 | |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 65 | REQUIRES_HTTPLIB2 = set(( |
| 66 | )) |
| 67 | |
jamesren | 34a0f3b | 2010-06-08 20:38:27 +0000 | [diff] [blame] | 68 | REQUIRES_PROTOBUFS = set(( |
| 69 | 'job_serializer_unittest.py', |
| 70 | )) |
| 71 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 72 | LONG_RUNTIME = set(( |
Alex Miller | 13b83c5 | 2013-02-14 16:07:34 -0800 | [diff] [blame] | 73 | 'auth_server_unittest.py', |
mbligh | 999fb13 | 2010-04-23 17:22:03 +0000 | [diff] [blame] | 74 | 'base_barrier_unittest.py', |
showard | ef1edaf | 2009-07-01 22:21:30 +0000 | [diff] [blame] | 75 | 'logging_manager_test.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 76 | )) |
| 77 | |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 78 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 79 | SKIP = set(( |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 80 | # This particular KVM autotest test is not a unittest |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 81 | 'guest_test.py', |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 82 | 'ap_configurator_test.py' |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 83 | )) |
| 84 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 85 | LONG_TESTS = (REQUIRES_DJANGO | |
| 86 | REQUIRES_MYSQLDB | |
| 87 | REQUIRES_GWT | |
| 88 | REQUIRES_SIMPLEJSON | |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 89 | REQUIRES_HTTPLIB2 | |
jamesren | 6461fff | 2010-04-06 23:33:05 +0000 | [diff] [blame] | 90 | REQUIRES_AUTH | |
jamesren | 34a0f3b | 2010-06-08 20:38:27 +0000 | [diff] [blame] | 91 | REQUIRES_PROTOBUFS | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 92 | LONG_RUNTIME) |
| 93 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 94 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 95 | |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 96 | # The set of files in LONG_TESTS with its full path |
| 97 | LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS} |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 98 | |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 99 | class TestFailure(Exception): |
| 100 | """Exception type for any test failure.""" |
| 101 | pass |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 102 | |
| 103 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 104 | def run_test(mod_names, options): |
| 105 | """ |
| 106 | @param mod_names: A list of individual parts of the module name to import |
| 107 | and run as a test suite. |
| 108 | @param options: optparse options. |
| 109 | """ |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 110 | if not options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 111 | parallel.redirect_io() |
| 112 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 113 | print "Running %s" % '.'.join(mod_names) |
| 114 | mod = common.setup_modules.import_module(mod_names[-1], |
| 115 | '.'.join(mod_names[:-1])) |
mbligh | 5f55484 | 2009-12-21 21:50:18 +0000 | [diff] [blame] | 116 | for ut_module in [unittest, custom_unittest]: |
| 117 | test = ut_module.defaultTestLoader.loadTestsFromModule(mod) |
| 118 | suite = ut_module.TestSuite(test) |
| 119 | runner = ut_module.TextTestRunner(verbosity=2) |
| 120 | result = runner.run(suite) |
| 121 | if result.errors or result.failures: |
| 122 | msg = '%s had %d failures and %d errors.' |
| 123 | msg %= '.'.join(mod_names), len(result.failures), len(result.errors) |
| 124 | raise TestFailure(msg) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 125 | |
| 126 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 127 | def scan_for_modules(start, options): |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 128 | """Scan folders and find all test modules that are not included in the |
| 129 | blacklist (defined in LONG_TESTS). |
| 130 | |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 131 | @param start: The absolute directory to look for tests under. |
| 132 | @param options: optparse options. |
| 133 | @return a list of modules to be executed. |
| 134 | """ |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 135 | modules = [] |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 136 | |
| 137 | skip_tests = SKIP |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 138 | if options.skip_tests: |
| 139 | skip_tests.update(options.skip_tests.split()) |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 140 | skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests} |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 141 | |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 142 | for dir_path, sub_dirs, file_names in os.walk(start): |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 143 | # Only look in and below subdirectories that are python modules. |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 144 | if '__init__.py' not in file_names: |
mbligh | a64df1a | 2009-09-18 16:54:39 +0000 | [diff] [blame] | 145 | if options.full: |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 146 | for file_name in file_names: |
| 147 | if file_name.endswith('.pyc'): |
| 148 | os.unlink(os.path.join(dir_path, file_name)) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 149 | # Skip all subdirectories below this one, it is not a module. |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 150 | del sub_dirs[:] |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 151 | if options.debug: |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 152 | print 'Skipping', dir_path |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 153 | continue # Skip this directory. |
| 154 | |
| 155 | # Look for unittest files. |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 156 | for file_name in file_names: |
| 157 | if (file_name.endswith('_unittest.py') or |
| 158 | file_name.endswith('_test.py')): |
| 159 | file_path = os.path.join(dir_path, file_name) |
| 160 | if (not options.full and |
| 161 | (file_name in LONG_TESTS or |
| 162 | file_path in LONG_TESTS_FULL_PATH)): |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 163 | continue |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 164 | if (file_name in skip_tests or |
| 165 | file_path in skip_tests_full_path): |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 166 | continue |
Dan Shi | 8af6fbe | 2013-02-13 14:20:16 -0800 | [diff] [blame^] | 167 | path_no_py = os.path.join(dir_path, file_name).rstrip('.py') |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 168 | assert path_no_py.startswith(ROOT) |
| 169 | names = path_no_py[len(ROOT)+1:].split('/') |
| 170 | modules.append(['autotest_lib'] + names) |
| 171 | if options.debug: |
| 172 | print 'testing', path_no_py |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 173 | return modules |
| 174 | |
| 175 | def find_and_run_tests(start, options): |
| 176 | """ |
| 177 | Find and run Python unittest suites below the given directory. Only look |
| 178 | in subdirectories of start that are actual importable Python modules. |
| 179 | |
| 180 | @param start: The absolute directory to look for tests under. |
| 181 | @param options: optparse options. |
| 182 | """ |
| 183 | if options.module_list: |
| 184 | modules = [] |
| 185 | for m in options.module_list: |
| 186 | modules.append(m.split('.')) |
| 187 | else: |
| 188 | modules = scan_for_modules(start, options) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 189 | |
| 190 | if options.debug: |
| 191 | print 'Number of test modules found:', len(modules) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 192 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 193 | functions = {} |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 194 | for module_names in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 195 | # Create a function that'll test a particular module. module=module |
| 196 | # is a hack to force python to evaluate the params now. We then |
| 197 | # rename the function to make error reporting nicer. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 198 | run_module = lambda module=module_names: run_test(module, options) |
| 199 | name = '.'.join(module_names) |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 200 | run_module.__name__ = name |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 201 | functions[run_module] = set() |
| 202 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 203 | try: |
| 204 | dargs = {} |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 205 | if options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 206 | dargs['max_simultaneous_procs'] = 1 |
| 207 | pe = parallel.ParallelExecute(functions, **dargs) |
| 208 | pe.run_until_completion() |
| 209 | except parallel.ParallelError, err: |
| 210 | return err.errors |
| 211 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 212 | |
| 213 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 214 | def main(): |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 215 | """Entry point for unittest_suite.py""" |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 216 | options, args = parser.parse_args() |
| 217 | if args: |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 218 | options.module_list = args |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 219 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 220 | # Strip the arguments off the command line, so that the unit tests do not |
| 221 | # see them. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 222 | del sys.argv[1:] |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 223 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 224 | absolute_start = os.path.join(ROOT, options.start) |
| 225 | errors = find_and_run_tests(absolute_start, options) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 226 | if errors: |
| 227 | print "%d tests resulted in an error/failure:" % len(errors) |
| 228 | for error in errors: |
| 229 | print "\t%s" % error |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 230 | print "Rerun", sys.argv[0], "--debug to see the failure details." |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 231 | sys.exit(1) |
| 232 | else: |
| 233 | print "All passed!" |
| 234 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 235 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 236 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 237 | if __name__ == "__main__": |
| 238 | main() |