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