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 | |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 19 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 20 | REQUIRES_DJANGO = set(( |
| 21 | 'monitor_db_unittest.py', |
| 22 | 'monitor_db_functional_test.py', |
| 23 | 'monitor_db_cleanup_test.py', |
| 24 | 'frontend_unittest.py', |
| 25 | 'csv_encoder_unittest.py', |
| 26 | 'rpc_interface_unittest.py', |
| 27 | 'models_test.py', |
| 28 | 'scheduler_models_unittest.py', |
| 29 | 'metahost_scheduler_unittest.py', |
| 30 | 'site_metahost_scheduler_unittest.py', |
| 31 | 'rpc_utils_unittest.py', |
| 32 | )) |
| 33 | |
| 34 | REQUIRES_MYSQLDB = set(( |
| 35 | 'migrate_unittest.py', |
| 36 | 'db_utils_unittest.py', |
| 37 | )) |
| 38 | |
| 39 | REQUIRES_GWT = set(( |
jamesren | 12c9fdd | 2010-03-02 00:00:39 +0000 | [diff] [blame^] | 40 | 'client_compilation_unittest.py', |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 41 | )) |
| 42 | |
| 43 | REQUIRES_SIMPLEJSON = set(( |
| 44 | 'resources_test.py', |
| 45 | 'serviceHandler_unittest.py', |
| 46 | )) |
| 47 | |
| 48 | LONG_RUNTIME = set(( |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 49 | 'barrier_unittest.py', |
showard | ef1edaf | 2009-07-01 22:21:30 +0000 | [diff] [blame] | 50 | 'logging_manager_test.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 51 | )) |
| 52 | |
jamesren | 92c1859 | 2010-02-25 19:10:32 +0000 | [diff] [blame] | 53 | LONG_TESTS = (REQUIRES_DJANGO | |
| 54 | REQUIRES_MYSQLDB | |
| 55 | REQUIRES_GWT | |
| 56 | REQUIRES_SIMPLEJSON | |
| 57 | LONG_RUNTIME) |
| 58 | |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 59 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 60 | ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 61 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 62 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 63 | class TestFailure(Exception): pass |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 64 | |
| 65 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 66 | def run_test(mod_names, options): |
| 67 | """ |
| 68 | @param mod_names: A list of individual parts of the module name to import |
| 69 | and run as a test suite. |
| 70 | @param options: optparse options. |
| 71 | """ |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 72 | if not options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 73 | parallel.redirect_io() |
| 74 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 75 | print "Running %s" % '.'.join(mod_names) |
| 76 | mod = common.setup_modules.import_module(mod_names[-1], |
| 77 | '.'.join(mod_names[:-1])) |
mbligh | 5f55484 | 2009-12-21 21:50:18 +0000 | [diff] [blame] | 78 | for ut_module in [unittest, custom_unittest]: |
| 79 | test = ut_module.defaultTestLoader.loadTestsFromModule(mod) |
| 80 | suite = ut_module.TestSuite(test) |
| 81 | runner = ut_module.TextTestRunner(verbosity=2) |
| 82 | result = runner.run(suite) |
| 83 | if result.errors or result.failures: |
| 84 | msg = '%s had %d failures and %d errors.' |
| 85 | msg %= '.'.join(mod_names), len(result.failures), len(result.errors) |
| 86 | raise TestFailure(msg) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 87 | |
| 88 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 89 | def find_and_run_tests(start, options): |
| 90 | """ |
| 91 | Find and run Python unittest suites below the given directory. Only look |
| 92 | in subdirectories of start that are actual importable Python modules. |
| 93 | |
| 94 | @param start: The absolute directory to look for tests under. |
| 95 | @param options: optparse options. |
| 96 | """ |
| 97 | modules = [] |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 98 | skip_tests = set() |
| 99 | if options.skip_tests: |
| 100 | skip_tests.update(options.skip_tests.split()) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 101 | |
| 102 | for dirpath, subdirs, filenames in os.walk(start): |
| 103 | # Only look in and below subdirectories that are python modules. |
| 104 | if '__init__.py' not in filenames: |
mbligh | a64df1a | 2009-09-18 16:54:39 +0000 | [diff] [blame] | 105 | if options.full: |
| 106 | for filename in filenames: |
| 107 | if filename.endswith('.pyc'): |
| 108 | os.unlink(os.path.join(dirpath, filename)) |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 109 | # Skip all subdirectories below this one, it is not a module. |
| 110 | del subdirs[:] |
| 111 | if options.debug: |
| 112 | print 'Skipping', dirpath |
| 113 | continue # Skip this directory. |
| 114 | |
| 115 | # Look for unittest files. |
| 116 | for fname in filenames: |
| 117 | if fname.endswith('_unittest.py') or fname.endswith('_test.py'): |
| 118 | if not options.full and fname in LONG_TESTS: |
| 119 | continue |
jamesren | 74e77fc | 2010-02-19 21:56:39 +0000 | [diff] [blame] | 120 | if fname in skip_tests: |
| 121 | continue |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 122 | path_no_py = os.path.join(dirpath, fname).rstrip('.py') |
| 123 | assert path_no_py.startswith(ROOT) |
| 124 | names = path_no_py[len(ROOT)+1:].split('/') |
| 125 | modules.append(['autotest_lib'] + names) |
| 126 | if options.debug: |
| 127 | print 'testing', path_no_py |
| 128 | |
| 129 | if options.debug: |
| 130 | print 'Number of test modules found:', len(modules) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 131 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 132 | functions = {} |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 133 | for module_names in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 134 | # Create a function that'll test a particular module. module=module |
| 135 | # is a hack to force python to evaluate the params now. We then |
| 136 | # rename the function to make error reporting nicer. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 137 | run_module = lambda module=module_names: run_test(module, options) |
| 138 | name = '.'.join(module_names) |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 139 | run_module.__name__ = name |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 140 | functions[run_module] = set() |
| 141 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 142 | try: |
| 143 | dargs = {} |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 144 | if options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 145 | dargs['max_simultaneous_procs'] = 1 |
| 146 | pe = parallel.ParallelExecute(functions, **dargs) |
| 147 | pe.run_until_completion() |
| 148 | except parallel.ParallelError, err: |
| 149 | return err.errors |
| 150 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 151 | |
| 152 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 153 | def main(): |
| 154 | options, args = parser.parse_args() |
| 155 | if args: |
| 156 | parser.error('Unexpected argument(s): %s' % args) |
| 157 | parser.print_help() |
| 158 | sys.exit(1) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 159 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 160 | # Strip the arguments off the command line, so that the unit tests do not |
| 161 | # see them. |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 162 | del sys.argv[1:] |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 163 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 164 | absolute_start = os.path.join(ROOT, options.start) |
| 165 | errors = find_and_run_tests(absolute_start, options) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 166 | if errors: |
| 167 | print "%d tests resulted in an error/failure:" % len(errors) |
| 168 | for error in errors: |
| 169 | print "\t%s" % error |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 170 | print "Rerun", sys.argv[0], "--debug to see the failure details." |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 171 | sys.exit(1) |
| 172 | else: |
| 173 | print "All passed!" |
| 174 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 175 | |
mbligh | 780fa7f | 2009-07-02 19:01:53 +0000 | [diff] [blame] | 176 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 177 | if __name__ == "__main__": |
| 178 | main() |