blob: d9283ccb8834c07c5c6c38b9cacf868920cd2aae [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
mbligh5f554842009-12-21 21:50:18 +00006from autotest_lib.client.common_lib.test_utils import unittest as custom_unittest
mbligheeb13572008-07-30 00:04:01 +00007
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',
showard34ab0992009-10-05 22:47:57 +000019 'monitor_db_functional_test.py',
mbligh671c5922008-07-28 19:34:38 +000020 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000021 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000022 'frontend_unittest.py',
showard363cdb52009-05-12 17:21:36 +000023 'client_compilation_unittest.py',
24 'csv_encoder_unittest.py',
showardf8b19042009-05-12 17:22:49 +000025 'rpc_interface_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000026 'logging_manager_test.py',
showarded2afea2009-07-07 20:54:07 +000027 'models_test.py',
mbligh671c5922008-07-28 19:34:38 +000028 ))
29
mbligh780fa7f2009-07-02 19:01:53 +000030ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh671c5922008-07-28 19:34:38 +000031
mbligheeb13572008-07-30 00:04:01 +000032
mbligh780fa7f2009-07-02 19:01:53 +000033class TestFailure(Exception): pass
mbligh671c5922008-07-28 19:34:38 +000034
35
mbligh780fa7f2009-07-02 19:01:53 +000036def run_test(mod_names, options):
37 """
38 @param mod_names: A list of individual parts of the module name to import
39 and run as a test suite.
40 @param options: optparse options.
41 """
mbligh43758df2008-09-04 19:54:45 +000042 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000043 parallel.redirect_io()
44
mbligh780fa7f2009-07-02 19:01:53 +000045 print "Running %s" % '.'.join(mod_names)
46 mod = common.setup_modules.import_module(mod_names[-1],
47 '.'.join(mod_names[:-1]))
mbligh5f554842009-12-21 21:50:18 +000048 for ut_module in [unittest, custom_unittest]:
49 test = ut_module.defaultTestLoader.loadTestsFromModule(mod)
50 suite = ut_module.TestSuite(test)
51 runner = ut_module.TextTestRunner(verbosity=2)
52 result = runner.run(suite)
53 if result.errors or result.failures:
54 msg = '%s had %d failures and %d errors.'
55 msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
56 raise TestFailure(msg)
mbligh671c5922008-07-28 19:34:38 +000057
58
mbligh780fa7f2009-07-02 19:01:53 +000059def find_and_run_tests(start, options):
60 """
61 Find and run Python unittest suites below the given directory. Only look
62 in subdirectories of start that are actual importable Python modules.
63
64 @param start: The absolute directory to look for tests under.
65 @param options: optparse options.
66 """
67 modules = []
68
69 for dirpath, subdirs, filenames in os.walk(start):
70 # Only look in and below subdirectories that are python modules.
71 if '__init__.py' not in filenames:
mbligha64df1a2009-09-18 16:54:39 +000072 if options.full:
73 for filename in filenames:
74 if filename.endswith('.pyc'):
75 os.unlink(os.path.join(dirpath, filename))
mbligh780fa7f2009-07-02 19:01:53 +000076 # Skip all subdirectories below this one, it is not a module.
77 del subdirs[:]
78 if options.debug:
79 print 'Skipping', dirpath
80 continue # Skip this directory.
81
82 # Look for unittest files.
83 for fname in filenames:
84 if fname.endswith('_unittest.py') or fname.endswith('_test.py'):
85 if not options.full and fname in LONG_TESTS:
86 continue
87 path_no_py = os.path.join(dirpath, fname).rstrip('.py')
88 assert path_no_py.startswith(ROOT)
89 names = path_no_py[len(ROOT)+1:].split('/')
90 modules.append(['autotest_lib'] + names)
91 if options.debug:
92 print 'testing', path_no_py
93
94 if options.debug:
95 print 'Number of test modules found:', len(modules)
mbligh671c5922008-07-28 19:34:38 +000096
showardcc85e812008-08-08 20:33:30 +000097 functions = {}
mbligh780fa7f2009-07-02 19:01:53 +000098 for module_names in modules:
mbligheeb13572008-07-30 00:04:01 +000099 # Create a function that'll test a particular module. module=module
100 # is a hack to force python to evaluate the params now. We then
101 # rename the function to make error reporting nicer.
mbligh780fa7f2009-07-02 19:01:53 +0000102 run_module = lambda module=module_names: run_test(module, options)
103 name = '.'.join(module_names)
showardcc85e812008-08-08 20:33:30 +0000104 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +0000105 functions[run_module] = set()
106
mbligheeb13572008-07-30 00:04:01 +0000107 try:
108 dargs = {}
mbligh43758df2008-09-04 19:54:45 +0000109 if options.debug:
mbligheeb13572008-07-30 00:04:01 +0000110 dargs['max_simultaneous_procs'] = 1
111 pe = parallel.ParallelExecute(functions, **dargs)
112 pe.run_until_completion()
113 except parallel.ParallelError, err:
114 return err.errors
115 return []
mblighf9751332008-04-08 18:25:33 +0000116
117
mbligheeb13572008-07-30 00:04:01 +0000118def main():
119 options, args = parser.parse_args()
120 if args:
121 parser.error('Unexpected argument(s): %s' % args)
122 parser.print_help()
123 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +0000124
showardcc85e812008-08-08 20:33:30 +0000125 # Strip the arguments off the command line, so that the unit tests do not
126 # see them.
mbligh780fa7f2009-07-02 19:01:53 +0000127 del sys.argv[1:]
showardcc85e812008-08-08 20:33:30 +0000128
mbligh780fa7f2009-07-02 19:01:53 +0000129 absolute_start = os.path.join(ROOT, options.start)
130 errors = find_and_run_tests(absolute_start, options)
mbligh671c5922008-07-28 19:34:38 +0000131 if errors:
132 print "%d tests resulted in an error/failure:" % len(errors)
133 for error in errors:
134 print "\t%s" % error
mbligh780fa7f2009-07-02 19:01:53 +0000135 print "Rerun", sys.argv[0], "--debug to see the failure details."
mbligh671c5922008-07-28 19:34:38 +0000136 sys.exit(1)
137 else:
138 print "All passed!"
139 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000140
mbligh780fa7f2009-07-02 19:01:53 +0000141
mbligheeb13572008-07-30 00:04:01 +0000142if __name__ == "__main__":
143 main()