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