blob: b290dde44c0c26c7787770625fefb88bb2780847 [file] [log] [blame]
mbligh1ee9ad72008-08-01 16:15:08 +00001#!/usr/bin/python -u
mblighf9751332008-04-08 18:25:33 +00002
mbligheeb13572008-07-30 00:04:01 +00003import os, sys, unittest, optparse
mblighdc906012008-06-27 19:29:11 +00004import common
mbligheeb13572008-07-30 00:04:01 +00005from autotest_lib.utils import parallel
6
7
mbligheeb13572008-07-30 00:04:01 +00008parser = 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")
mbligh43758df2008-09-04 19:54:45 +000014parser.add_option("--debug", action="store_true", dest="debug", default=False,
15 help="run in debug mode")
mblighf9751332008-04-08 18:25:33 +000016
mbligh671c5922008-07-28 19:34:38 +000017LONG_TESTS = set((
18 'monitor_db_unittest.py',
19 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000020 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000021 'frontend_unittest.py',
showard363cdb52009-05-12 17:21:36 +000022 'client_compilation_unittest.py',
23 'csv_encoder_unittest.py',
showardf8b19042009-05-12 17:22:49 +000024 'rpc_interface_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000025 'logging_manager_test.py',
mbligh671c5922008-07-28 19:34:38 +000026 ))
27
mbligh780fa7f2009-07-02 19:01:53 +000028ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +000029
mbligheeb13572008-07-30 00:04:01 +000030
mbligh780fa7f2009-07-02 19:01:53 +000031class TestFailure(Exception): pass
mbligh671c5922008-07-28 19:34:38 +000032
33
mbligh780fa7f2009-07-02 19:01:53 +000034def run_test(mod_names, options):
35 """
36 @param mod_names: A list of individual parts of the module name to import
37 and run as a test suite.
38 @param options: optparse options.
39 """
mbligh43758df2008-09-04 19:54:45 +000040 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000041 parallel.redirect_io()
42
mbligh780fa7f2009-07-02 19:01:53 +000043 print "Running %s" % '.'.join(mod_names)
44 mod = common.setup_modules.import_module(mod_names[-1],
45 '.'.join(mod_names[:-1]))
mbligh671c5922008-07-28 19:34:38 +000046 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
47 suite = unittest.TestSuite(test)
48 runner = unittest.TextTestRunner(verbosity=2)
49 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000050 if result.errors or result.failures:
mbligh780fa7f2009-07-02 19:01:53 +000051 raise TestFailure(
52 "%s had %d failures and %d errors." %
53 ('.'.join(mod_names), len(result.failures), len(result.errors)))
mbligh671c5922008-07-28 19:34:38 +000054
55
mbligh780fa7f2009-07-02 19:01:53 +000056def find_and_run_tests(start, options):
57 """
58 Find and run Python unittest suites below the given directory. Only look
59 in subdirectories of start that are actual importable Python modules.
60
61 @param start: The absolute directory to look for tests under.
62 @param options: optparse options.
63 """
64 modules = []
65
66 for dirpath, subdirs, filenames in os.walk(start):
67 # Only look in and below subdirectories that are python modules.
68 if '__init__.py' not in filenames:
69 # Skip all subdirectories below this one, it is not a module.
70 del subdirs[:]
71 if options.debug:
72 print 'Skipping', dirpath
73 continue # Skip this directory.
74
75 # Look for unittest files.
76 for fname in filenames:
77 if fname.endswith('_unittest.py') or fname.endswith('_test.py'):
78 if not options.full and fname in LONG_TESTS:
79 continue
80 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
81 assert path_no_py.startswith(ROOT)
82 names = path_no_py[len(ROOT)+1:].split('/')
83 modules.append(['autotest_lib'] + names)
84 if options.debug:
85 print 'testing', path_no_py
86
87 if options.debug:
88 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +000089
showardcc85e812008-08-08 20:33:30 +000090 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +000091 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +000092 # Create a function that'll test a particular module. module=module
93 # is a hack to force python to evaluate the params now. We then
94 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +000095 run_module = lambda module=module_names: run_test(module, options)
96 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +000097 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +000098 functions[run_module] = set()
99
mbligheeb13572008-07-30 00:04:01 +0000100 try:
101 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000102 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000103 dargs['max_simultaneous_procs'] = 1
104 pe = parallel.ParallelExecute(functions, **dargs)
105 pe.run_until_completion()
106 except parallel.ParallelError, err:
107 return err.errors
108 return []
mblighf9751332008-04-08 18:25:33 +0000109
110
mbligheeb13572008-07-30 00:04:01 +0000111def main():
112 options, args = parser.parse_args()
113 if args:
114 parser.error('Unexpected argument(s): %s' % args)
115 parser.print_help()
116 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000117
showardcc85e812008-08-08 20:33:30 +0000118 # Strip the arguments off the command line, so that the unit tests do not
119 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000120 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000121
mbligh780fa7f2009-07-02 19:01:53 +0000122 absolute_start = os.path.join(ROOT, options.start)
123 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000124 if errors:
125 print "%d tests resulted in an error/failure:" % len(errors)
126 for error in errors:
127 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000128 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000129 sys.exit(1)
130 else:
131 print "All passed!"
132 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000133
mbligh780fa7f2009-07-02 19:01:53 +0000134
mbligheeb13572008-07-30 00:04:01 +0000135if __name__ == "__main__":
136 main()