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 | |
| 8 | debug = False |
| 9 | root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 10 | |
| 11 | parser = optparse.OptionParser() |
| 12 | parser.add_option("-r", action="store", type="string", dest="start", |
| 13 | default='', |
| 14 | help="root directory to start running unittests") |
| 15 | parser.add_option("--full", action="store_true", dest="full", default=False, |
| 16 | help="whether to run the shortened version of the test") |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 17 | |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 18 | |
| 19 | LONG_TESTS = set(( |
| 20 | 'monitor_db_unittest.py', |
| 21 | 'barrier_unittest.py', |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 22 | 'migrate_unittest.py', |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 23 | 'frontend_unittest.py', |
| 24 | )) |
| 25 | |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 26 | modules = [] |
| 27 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 28 | |
| 29 | def lister(full, dirname, files): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 30 | for f in files: |
| 31 | if f.endswith('_unittest.py'): |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 32 | if not full and f in LONG_TESTS: |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 33 | continue |
mbligh | 4205d89 | 2008-07-14 16:23:20 +0000 | [diff] [blame] | 34 | temp = os.path.join(dirname, f).strip('.py') |
| 35 | mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/') |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 36 | modules.append(mod_name) |
| 37 | |
| 38 | |
| 39 | def run_test(mod_name): |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 40 | if not debug: |
| 41 | parallel.redirect_io() |
| 42 | |
| 43 | print "Running %s" % '.'.join(mod_name) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 44 | mod = common.setup_modules.import_module(mod_name[-1], |
| 45 | '.'.join(mod_name[:-1])) |
| 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: |
| 51 | raise Exception("%s failed" % '.'.join(mod_name)) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 52 | |
| 53 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 54 | def run_tests(start, full=False): |
| 55 | os.path.walk(start, lister, full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 56 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 57 | functions = [] |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 58 | for module in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 59 | # Create a function that'll test a particular module. module=module |
| 60 | # is a hack to force python to evaluate the params now. We then |
| 61 | # rename the function to make error reporting nicer. |
| 62 | run_module = lambda module=module: run_test(module) |
| 63 | run_module.__name__ = '.'.join(module) |
| 64 | functions.append(run_module) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 65 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 66 | try: |
| 67 | dargs = {} |
| 68 | if debug: |
| 69 | dargs['max_simultaneous_procs'] = 1 |
| 70 | pe = parallel.ParallelExecute(functions, **dargs) |
| 71 | pe.run_until_completion() |
| 72 | except parallel.ParallelError, err: |
| 73 | return err.errors |
| 74 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 75 | |
| 76 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 77 | def main(): |
| 78 | options, args = parser.parse_args() |
| 79 | if args: |
| 80 | parser.error('Unexpected argument(s): %s' % args) |
| 81 | parser.print_help() |
| 82 | sys.exit(1) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 83 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 84 | errors = run_tests(os.path.join(root, options.start), options.full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 85 | if errors: |
| 86 | print "%d tests resulted in an error/failure:" % len(errors) |
| 87 | for error in errors: |
| 88 | print "\t%s" % error |
| 89 | sys.exit(1) |
| 90 | else: |
| 91 | print "All passed!" |
| 92 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 93 | |
| 94 | if __name__ == "__main__": |
| 95 | main() |