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 | root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 9 | |
| 10 | parser = optparse.OptionParser() |
| 11 | parser.add_option("-r", action="store", type="string", dest="start", |
| 12 | default='', |
| 13 | help="root directory to start running unittests") |
| 14 | parser.add_option("--full", action="store_true", dest="full", default=False, |
| 15 | help="whether to run the shortened version of the test") |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 16 | parser.add_option("--debug", action="store_true", dest="debug", default=False, |
| 17 | help="run in debug mode") |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 18 | |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 19 | |
| 20 | LONG_TESTS = set(( |
| 21 | 'monitor_db_unittest.py', |
| 22 | 'barrier_unittest.py', |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 23 | 'migrate_unittest.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 24 | 'frontend_unittest.py', |
showard | 363cdb5 | 2009-05-12 17:21:36 +0000 | [diff] [blame] | 25 | 'client_compilation_unittest.py', |
| 26 | 'csv_encoder_unittest.py', |
showard | f8b1904 | 2009-05-12 17:22:49 +0000 | [diff] [blame] | 27 | 'rpc_interface_unittest.py', |
showard | ef1edaf | 2009-07-01 22:21:30 +0000 | [diff] [blame^] | 28 | 'logging_manager_test.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 29 | )) |
| 30 | |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 31 | modules = [] |
| 32 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 33 | |
| 34 | def lister(full, dirname, files): |
jadmanski | 80718c7 | 2009-02-26 00:04:07 +0000 | [diff] [blame] | 35 | if not os.path.exists(os.path.join(dirname, '__init__.py')): |
| 36 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 37 | for f in files: |
showard | ef1edaf | 2009-07-01 22:21:30 +0000 | [diff] [blame^] | 38 | if f.endswith('_unittest.py') or f.endswith('_test.py'): |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 39 | if not full and f in LONG_TESTS: |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 40 | continue |
mbligh | 4205d89 | 2008-07-14 16:23:20 +0000 | [diff] [blame] | 41 | temp = os.path.join(dirname, f).strip('.py') |
| 42 | mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/') |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 43 | modules.append(mod_name) |
| 44 | |
| 45 | |
| 46 | def run_test(mod_name): |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 47 | if not options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 48 | parallel.redirect_io() |
| 49 | |
| 50 | print "Running %s" % '.'.join(mod_name) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 51 | mod = common.setup_modules.import_module(mod_name[-1], |
| 52 | '.'.join(mod_name[:-1])) |
| 53 | test = unittest.defaultTestLoader.loadTestsFromModule(mod) |
| 54 | suite = unittest.TestSuite(test) |
| 55 | runner = unittest.TextTestRunner(verbosity=2) |
| 56 | result = runner.run(suite) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 57 | if result.errors or result.failures: |
| 58 | raise Exception("%s failed" % '.'.join(mod_name)) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 59 | |
| 60 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 61 | def run_tests(start, full=False): |
| 62 | os.path.walk(start, lister, full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 63 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 64 | functions = {} |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 65 | for module in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 66 | # Create a function that'll test a particular module. module=module |
| 67 | # is a hack to force python to evaluate the params now. We then |
| 68 | # rename the function to make error reporting nicer. |
| 69 | run_module = lambda module=module: run_test(module) |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 70 | name = '.'.join(module) |
| 71 | run_module.__name__ = name |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 72 | functions[run_module] = set() |
| 73 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 74 | try: |
| 75 | dargs = {} |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 76 | if options.debug: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 77 | dargs['max_simultaneous_procs'] = 1 |
| 78 | pe = parallel.ParallelExecute(functions, **dargs) |
| 79 | pe.run_until_completion() |
| 80 | except parallel.ParallelError, err: |
| 81 | return err.errors |
| 82 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 83 | |
| 84 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 85 | def main(): |
mbligh | 43758df | 2008-09-04 19:54:45 +0000 | [diff] [blame] | 86 | global options, args |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 87 | options, args = parser.parse_args() |
| 88 | if args: |
| 89 | parser.error('Unexpected argument(s): %s' % args) |
| 90 | parser.print_help() |
| 91 | sys.exit(1) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 92 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame] | 93 | # Strip the arguments off the command line, so that the unit tests do not |
| 94 | # see them. |
| 95 | sys.argv = [sys.argv[0]] |
| 96 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 97 | errors = run_tests(os.path.join(root, options.start), options.full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 98 | if errors: |
| 99 | print "%d tests resulted in an error/failure:" % len(errors) |
| 100 | for error in errors: |
| 101 | print "\t%s" % error |
| 102 | sys.exit(1) |
| 103 | else: |
| 104 | print "All passed!" |
| 105 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 106 | |
| 107 | if __name__ == "__main__": |
| 108 | main() |