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