unittest_suite.py revision f828c77299496a78d8e7f5afe608f7e73851fbd0
1#!/usr/bin/python -u 2 3import os, sys, unittest, optparse 4import common 5from autotest_lib.utils import parallel 6from autotest_lib.client.common_lib.test_utils import unittest as custom_unittest 7 8parser = optparse.OptionParser() 9parser.add_option("-r", action="store", type="string", dest="start", 10 default='', 11 help="root directory to start running unittests") 12parser.add_option("--full", action="store_true", dest="full", default=False, 13 help="whether to run the shortened version of the test") 14parser.add_option("--debug", action="store_true", dest="debug", default=False, 15 help="run in debug mode") 16 17LONG_TESTS = set(( 18 'monitor_db_unittest.py', 19 'monitor_db_functional_test.py', 20 'monitor_db_cleanup_test.py', 21 'barrier_unittest.py', 22 'migrate_unittest.py', 23 'frontend_unittest.py', 24 'client_compilation_unittest.py', 25 'csv_encoder_unittest.py', 26 'rpc_interface_unittest.py', 27 'resources_test.py', 28 'logging_manager_test.py', 29 'models_test.py', 30 )) 31 32ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) 33 34 35class TestFailure(Exception): pass 36 37 38def run_test(mod_names, options): 39 """ 40 @param mod_names: A list of individual parts of the module name to import 41 and run as a test suite. 42 @param options: optparse options. 43 """ 44 if not options.debug: 45 parallel.redirect_io() 46 47 print "Running %s" % '.'.join(mod_names) 48 mod = common.setup_modules.import_module(mod_names[-1], 49 '.'.join(mod_names[:-1])) 50 for ut_module in [unittest, custom_unittest]: 51 test = ut_module.defaultTestLoader.loadTestsFromModule(mod) 52 suite = ut_module.TestSuite(test) 53 runner = ut_module.TextTestRunner(verbosity=2) 54 result = runner.run(suite) 55 if result.errors or result.failures: 56 msg = '%s had %d failures and %d errors.' 57 msg %= '.'.join(mod_names), len(result.failures), len(result.errors) 58 raise TestFailure(msg) 59 60 61def find_and_run_tests(start, options): 62 """ 63 Find and run Python unittest suites below the given directory. Only look 64 in subdirectories of start that are actual importable Python modules. 65 66 @param start: The absolute directory to look for tests under. 67 @param options: optparse options. 68 """ 69 modules = [] 70 71 for dirpath, subdirs, filenames in os.walk(start): 72 # Only look in and below subdirectories that are python modules. 73 if '__init__.py' not in filenames: 74 if options.full: 75 for filename in filenames: 76 if filename.endswith('.pyc'): 77 os.unlink(os.path.join(dirpath, filename)) 78 # Skip all subdirectories below this one, it is not a module. 79 del subdirs[:] 80 if options.debug: 81 print 'Skipping', dirpath 82 continue # Skip this directory. 83 84 # Look for unittest files. 85 for fname in filenames: 86 if fname.endswith('_unittest.py') or fname.endswith('_test.py'): 87 if not options.full and fname in LONG_TESTS: 88 continue 89 path_no_py = os.path.join(dirpath, fname).rstrip('.py') 90 assert path_no_py.startswith(ROOT) 91 names = path_no_py[len(ROOT)+1:].split('/') 92 modules.append(['autotest_lib'] + names) 93 if options.debug: 94 print 'testing', path_no_py 95 96 if options.debug: 97 print 'Number of test modules found:', len(modules) 98 99 functions = {} 100 for module_names in modules: 101 # Create a function that'll test a particular module. module=module 102 # is a hack to force python to evaluate the params now. We then 103 # rename the function to make error reporting nicer. 104 run_module = lambda module=module_names: run_test(module, options) 105 name = '.'.join(module_names) 106 run_module.__name__ = name 107 functions[run_module] = set() 108 109 try: 110 dargs = {} 111 if options.debug: 112 dargs['max_simultaneous_procs'] = 1 113 pe = parallel.ParallelExecute(functions, **dargs) 114 pe.run_until_completion() 115 except parallel.ParallelError, err: 116 return err.errors 117 return [] 118 119 120def main(): 121 options, args = parser.parse_args() 122 if args: 123 parser.error('Unexpected argument(s): %s' % args) 124 parser.print_help() 125 sys.exit(1) 126 127 # Strip the arguments off the command line, so that the unit tests do not 128 # see them. 129 del sys.argv[1:] 130 131 absolute_start = os.path.join(ROOT, options.start) 132 errors = find_and_run_tests(absolute_start, options) 133 if errors: 134 print "%d tests resulted in an error/failure:" % len(errors) 135 for error in errors: 136 print "\t%s" % error 137 print "Rerun", sys.argv[0], "--debug to see the failure details." 138 sys.exit(1) 139 else: 140 print "All passed!" 141 sys.exit(0) 142 143 144if __name__ == "__main__": 145 main() 146