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 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame^] | 26 | DEPENDENCIES = { |
| 27 | # Annotate dependencies here. The format is |
| 28 | # module: [list of modules on which it is dependent] |
| 29 | # (All modules in the list must run before this module can) |
| 30 | |
| 31 | # Note: Do not make a short test dependent on a long one. This will cause |
| 32 | # the suite to fail if it is run without the --full flag, since the module |
| 33 | # that the short test depends on will not be run. |
| 34 | |
| 35 | |
| 36 | # The next two dependencies are not really dependencies. This is actually a |
| 37 | # hack to keep these three modules from running at the same time, since they |
| 38 | # all create and destroy a database with the same name. |
| 39 | 'autotest_lib.frontend.frontend_unittest': |
| 40 | ['autotest_lib.migrate.migrate_unittest'], |
| 41 | |
| 42 | 'autotest_lib.scheduler.monitor_db_unittest': |
| 43 | ['autotest_lib.frontend.frontend_unittest', |
| 44 | 'autotest_lib.migrate.migrate_unittest'], |
| 45 | } |
| 46 | |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 47 | modules = [] |
| 48 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 49 | |
| 50 | def lister(full, dirname, files): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 51 | for f in files: |
| 52 | if f.endswith('_unittest.py'): |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 53 | if not full and f in LONG_TESTS: |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 54 | continue |
mbligh | 4205d89 | 2008-07-14 16:23:20 +0000 | [diff] [blame] | 55 | temp = os.path.join(dirname, f).strip('.py') |
| 56 | mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/') |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 57 | modules.append(mod_name) |
| 58 | |
| 59 | |
| 60 | def run_test(mod_name): |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 61 | if not debug: |
| 62 | parallel.redirect_io() |
| 63 | |
| 64 | print "Running %s" % '.'.join(mod_name) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 65 | mod = common.setup_modules.import_module(mod_name[-1], |
| 66 | '.'.join(mod_name[:-1])) |
| 67 | test = unittest.defaultTestLoader.loadTestsFromModule(mod) |
| 68 | suite = unittest.TestSuite(test) |
| 69 | runner = unittest.TextTestRunner(verbosity=2) |
| 70 | result = runner.run(suite) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 71 | if result.errors or result.failures: |
| 72 | raise Exception("%s failed" % '.'.join(mod_name)) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 73 | |
| 74 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 75 | def run_tests(start, full=False): |
| 76 | os.path.walk(start, lister, full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 77 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame^] | 78 | functions = {} |
| 79 | names_to_functions = {} |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 80 | for module in modules: |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 81 | # Create a function that'll test a particular module. module=module |
| 82 | # is a hack to force python to evaluate the params now. We then |
| 83 | # rename the function to make error reporting nicer. |
| 84 | run_module = lambda module=module: run_test(module) |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame^] | 85 | name = '.'.join(module) |
| 86 | run_module.__name__ = name |
| 87 | names_to_functions[name] = run_module |
| 88 | functions[run_module] = set() |
| 89 | |
| 90 | for fn, deps in DEPENDENCIES.iteritems(): |
| 91 | if fn in names_to_functions: |
| 92 | functions[names_to_functions[fn]] = set( |
| 93 | names_to_functions[dep] for dep in deps) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 94 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 95 | try: |
| 96 | dargs = {} |
| 97 | if debug: |
| 98 | dargs['max_simultaneous_procs'] = 1 |
| 99 | pe = parallel.ParallelExecute(functions, **dargs) |
| 100 | pe.run_until_completion() |
| 101 | except parallel.ParallelError, err: |
| 102 | return err.errors |
| 103 | return [] |
mbligh | f975133 | 2008-04-08 18:25:33 +0000 | [diff] [blame] | 104 | |
| 105 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 106 | def main(): |
| 107 | options, args = parser.parse_args() |
| 108 | if args: |
| 109 | parser.error('Unexpected argument(s): %s' % args) |
| 110 | parser.print_help() |
| 111 | sys.exit(1) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 112 | |
showard | cc85e81 | 2008-08-08 20:33:30 +0000 | [diff] [blame^] | 113 | # Strip the arguments off the command line, so that the unit tests do not |
| 114 | # see them. |
| 115 | sys.argv = [sys.argv[0]] |
| 116 | |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 117 | errors = run_tests(os.path.join(root, options.start), options.full) |
mbligh | 671c592 | 2008-07-28 19:34:38 +0000 | [diff] [blame] | 118 | if errors: |
| 119 | print "%d tests resulted in an error/failure:" % len(errors) |
| 120 | for error in errors: |
| 121 | print "\t%s" % error |
| 122 | sys.exit(1) |
| 123 | else: |
| 124 | print "All passed!" |
| 125 | sys.exit(0) |
mbligh | eeb1357 | 2008-07-30 00:04:01 +0000 | [diff] [blame] | 126 | |
| 127 | if __name__ == "__main__": |
| 128 | main() |