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