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