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 | |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 21 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 22 | REQUIRES_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', |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 34 | 'site_rpc_utils_unittest.py', |
jamesren | 1468135 | 2010-04-09 20:46:09 +0000 | [diff] [blame] | 35 | 'execution_engine_unittest.py', |
jamesren | cd7a81a | 2010-04-21 20:39:08 +0000 | [diff] [blame] | 36 | 'service_proxy_lib_test.py', |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 37 | )) |
| 38 | |
| 39 | REQUIRES_MYSQLDB = set(( |
| 40 | 'migrate_unittest.py', |
| 41 | 'db_utils_unittest.py', |
| 42 | )) |
| 43 | |
| 44 | REQUIRES_GWT = set(( |
jamesren | 12c9fdd | 2010-03-02 00:00:39 +0000 | [diff] [blame] | 45 | 'client_compilation_unittest.py', |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 46 | )) |
| 47 | |
| 48 | REQUIRES_SIMPLEJSON = set(( |
| 49 | 'resources_test.py', |
| 50 | 'serviceHandler_unittest.py', |
| 51 | )) |
| 52 | |
jamesren | 6461fff | 2010-04-06 23:33:05 +0000 | [diff] [blame] | 53 | REQUIRES_AUTH = set (( |
| 54 | 'trigger_unittest.py', |
| 55 | )) |
| 56 | |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 57 | REQUIRES_HTTPLIB2 = set(( |
| 58 | )) |
| 59 | |
jamesren | 34a0f3b | 2010-06-08 20:38:27 +0000 | [diff] [blame] | 60 | REQUIRES_PROTOBUFS = set(( |
| 61 | 'job_serializer_unittest.py', |
| 62 | )) |
| 63 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 64 | LONG_RUNTIME = set(( |
Alex Miller | 13b83c5 | 2013-02-14 16:07:34 -0800 | [diff] [blame^] | 65 | 'auth_server_unittest.py', |
mbligh | 999fb13 | 2010-04-23 17:22:03 +0000 | [diff] [blame] | 66 | 'base_barrier_unittest.py', |
showard | ef1edaf | 2009-07-01 22:21:30 +0000 | [diff] [blame] | 67 | 'logging_manager_test.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 68 | )) |
| 69 | |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 70 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 71 | SKIP = set(( |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 72 | # This particular KVM autotest test is not a unittest |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 73 | 'guest_test.py', |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 74 | 'ap_configurator_test.py' |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 75 | )) |
| 76 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 77 | LONG_TESTS = (REQUIRES_DJANGO | |
| 78 | REQUIRES_MYSQLDB | |
| 79 | REQUIRES_GWT | |
| 80 | REQUIRES_SIMPLEJSON | |
jamesren | 37dfa9d | 2010-04-12 18:24:31 +0000 | [diff] [blame] | 81 | REQUIRES_HTTPLIB2 | |
jamesren | 6461fff | 2010-04-06 23:33:05 +0000 | [diff] [blame] | 82 | REQUIRES_AUTH | |
jamesren | 34a0f3b | 2010-06-08 20:38:27 +0000 | [diff] [blame] | 83 | REQUIRES_PROTOBUFS | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 84 | LONG_RUNTIME) |
| 85 | |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 86 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 87 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 88 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 89 | |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 90 | class TestFailure(Exception): |
| 91 | """Exception type for any test failure.""" |
| 92 | pass |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 93 | |
| 94 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 95 | def run_test(mod_names, options): |
| 96 | """ |
| 97 | @param mod_names: A list of individual parts of the module name to import |
| 98 | and run as a test suite. |
| 99 | @param options: optparse options. |
| 100 | """ |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 101 | if not options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 102 | parallel.redirect_io() |
| 103 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 104 | print "Running %s" % '.'.join(mod_names) |
| 105 | mod = common.setup_modules.import_module(mod_names[-1], |
| 106 | '.'.join(mod_names[:-1])) |
mbligh | 5f55484 | 2009-12-21 21:50:18 +0000 | [diff] [blame] | 107 | for ut_module in [unittest, custom_unittest]: |
| 108 | test = ut_module.defaultTestLoader.loadTestsFromModule(mod) |
| 109 | suite = ut_module.TestSuite(test) |
| 110 | runner = ut_module.TextTestRunner(verbosity=2) |
| 111 | result = runner.run(suite) |
| 112 | if result.errors or result.failures: |
| 113 | msg = '%s had %d failures and %d errors.' |
| 114 | msg %= '.'.join(mod_names), len(result.failures), len(result.errors) |
| 115 | raise TestFailure(msg) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 116 | |
| 117 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 118 | def scan_for_modules(start, options): |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 119 | """Scan folders and find all test modules. |
| 120 | @param start: The absolute directory to look for tests under. |
| 121 | @param options: optparse options. |
| 122 | @return a list of modules to be executed. |
| 123 | """ |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 124 | modules = [] |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 125 | |
| 126 | skip_tests = SKIP |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 127 | if options.skip_tests: |
| 128 | skip_tests.update(options.skip_tests.split()) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 129 | |
| 130 | for dirpath, subdirs, filenames in os.walk(start): |
| 131 | # Only look in and below subdirectories that are python modules. |
| 132 | if '__init__.py' not in filenames: |
mbligh | a64df1a | 2009-09-18 16:54:39 +0000 | [diff] [blame] | 133 | if options.full: |
| 134 | for filename in filenames: |
| 135 | if filename.endswith('.pyc'): |
| 136 | os.unlink(os.path.join(dirpath, filename)) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 137 | # Skip all subdirectories below this one, it is not a module. |
| 138 | del subdirs[:] |
| 139 | if options.debug: |
| 140 | print 'Skipping', dirpath |
| 141 | continue # Skip this directory. |
| 142 | |
| 143 | # Look for unittest files. |
| 144 | for fname in filenames: |
| 145 | if fname.endswith('_unittest.py') or fname.endswith('_test.py'): |
| 146 | if not options.full and fname in LONG_TESTS: |
| 147 | continue |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 148 | if fname in skip_tests: |
| 149 | continue |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 150 | path_no_py = os.path.join(dirpath, fname).rstrip('.py') |
| 151 | assert path_no_py.startswith(ROOT) |
| 152 | names = path_no_py[len(ROOT)+1:].split('/') |
| 153 | modules.append(['autotest_lib'] + names) |
| 154 | if options.debug: |
| 155 | print 'testing', path_no_py |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 156 | return modules |
| 157 | |
| 158 | def find_and_run_tests(start, options): |
| 159 | """ |
| 160 | Find and run Python unittest suites below the given directory. Only look |
| 161 | in subdirectories of start that are actual importable Python modules. |
| 162 | |
| 163 | @param start: The absolute directory to look for tests under. |
| 164 | @param options: optparse options. |
| 165 | """ |
| 166 | if options.module_list: |
| 167 | modules = [] |
| 168 | for m in options.module_list: |
| 169 | modules.append(m.split('.')) |
| 170 | else: |
| 171 | modules = scan_for_modules(start, options) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 172 | |
| 173 | if options.debug: |
| 174 | print 'Number of test modules found:', len(modules) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 175 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 176 | functions = {} |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 177 | for module_names in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 178 | # Create a function that'll test a particular module. module=module |
| 179 | # is a hack to force python to evaluate the params now. We then |
| 180 | # rename the function to make error reporting nicer. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 181 | run_module = lambda module=module_names: run_test(module, options) |
| 182 | name = '.'.join(module_names) |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 183 | run_module.__name__ = name |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 184 | functions[run_module] = set() |
| 185 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 186 | try: |
| 187 | dargs = {} |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 188 | if options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 189 | dargs['max_simultaneous_procs'] = 1 |
| 190 | pe = parallel.ParallelExecute(functions, **dargs) |
| 191 | pe.run_until_completion() |
| 192 | except parallel.ParallelError, err: |
| 193 | return err.errors |
| 194 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 195 | |
| 196 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 197 | def main(): |
Aviv Keshet | 78b0595 | 2013-02-13 14:37:37 -0800 | [diff] [blame] | 198 | """Entry point for unittest_suite.py""" |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 199 | options, args = parser.parse_args() |
| 200 | if args: |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 201 | options.module_list = args |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 202 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 203 | # Strip the arguments off the command line, so that the unit tests do not |
| 204 | # see them. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 205 | del sys.argv[1:] |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 206 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 207 | absolute_start = os.path.join(ROOT, options.start) |
| 208 | errors = find_and_run_tests(absolute_start, options) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 209 | if errors: |
| 210 | print "%d tests resulted in an error/failure:" % len(errors) |
| 211 | for error in errors: |
| 212 | print "\t%s" % error |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 213 | print "Rerun", sys.argv[0], "--debug to see the failure details." |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 214 | sys.exit(1) |
| 215 | else: |
| 216 | print "All passed!" |
| 217 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 218 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 219 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 220 | if __name__ == "__main__": |
| 221 | main() |