blob: cb9150af1f0e2a18ac78e56fb309e986eea475f8 [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 +00008root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
9
10parser = optparse.OptionParser()
11parser.add_option("-r", action="store", type="string", dest="start",
12 default='',
13 help="root directory to start running unittests")
14parser.add_option("--full", action="store_true", dest="full", default=False,
15 help="whether to run the shortened version of the test")
mbligh43758df2008-09-04 19:54:45 +000016parser.add_option("--debug", action="store_true", dest="debug", default=False,
17 help="run in debug mode")
mblighf9751332008-04-08 18:25:33 +000018
mbligh671c5922008-07-28 19:34:38 +000019
20LONG_TESTS = set((
21 'monitor_db_unittest.py',
22 'barrier_unittest.py',
mbligheeb13572008-07-30 00:04:01 +000023 'migrate_unittest.py',
mbligh671c5922008-07-28 19:34:38 +000024 'frontend_unittest.py',
showard363cdb52009-05-12 17:21:36 +000025 'client_compilation_unittest.py',
26 'csv_encoder_unittest.py',
showardf8b19042009-05-12 17:22:49 +000027 'rpc_interface_unittest.py',
showardef1edaf2009-07-01 22:21:30 +000028 'logging_manager_test.py',
mbligh671c5922008-07-28 19:34:38 +000029 ))
30
mbligh671c5922008-07-28 19:34:38 +000031modules = []
32
mbligheeb13572008-07-30 00:04:01 +000033
34def lister(full, dirname, files):
jadmanski80718c72009-02-26 00:04:07 +000035 if not os.path.exists(os.path.join(dirname, '__init__.py')):
36 return
jadmanski0afbb632008-06-06 21:10:57 +000037 for f in files:
showardef1edaf2009-07-01 22:21:30 +000038 if f.endswith('_unittest.py') or f.endswith('_test.py'):
mbligheeb13572008-07-30 00:04:01 +000039 if not full and f in LONG_TESTS:
mbligh671c5922008-07-28 19:34:38 +000040 continue
mbligh4205d892008-07-14 16:23:20 +000041 temp = os.path.join(dirname, f).strip('.py')
42 mod_name = ['autotest_lib'] + temp[len(root)+1:].split('/')
mbligh671c5922008-07-28 19:34:38 +000043 modules.append(mod_name)
44
45
46def run_test(mod_name):
mbligh43758df2008-09-04 19:54:45 +000047 if not options.debug:
mbligheeb13572008-07-30 00:04:01 +000048 parallel.redirect_io()
49
50 print "Running %s" % '.'.join(mod_name)
mbligh671c5922008-07-28 19:34:38 +000051 mod = common.setup_modules.import_module(mod_name[-1],
52 '.'.join(mod_name[:-1]))
53 test = unittest.defaultTestLoader.loadTestsFromModule(mod)
54 suite = unittest.TestSuite(test)
55 runner = unittest.TextTestRunner(verbosity=2)
56 result = runner.run(suite)
mbligheeb13572008-07-30 00:04:01 +000057 if result.errors or result.failures:
58 raise Exception("%s failed" % '.'.join(mod_name))
mbligh671c5922008-07-28 19:34:38 +000059
60
mbligheeb13572008-07-30 00:04:01 +000061def run_tests(start, full=False):
62 os.path.walk(start, lister, full)
mbligh671c5922008-07-28 19:34:38 +000063
showardcc85e812008-08-08 20:33:30 +000064 functions = {}
mbligh671c5922008-07-28 19:34:38 +000065 for module in modules:
mbligheeb13572008-07-30 00:04:01 +000066 # Create a function that'll test a particular module. module=module
67 # is a hack to force python to evaluate the params now. We then
68 # rename the function to make error reporting nicer.
69 run_module = lambda module=module: run_test(module)
showardcc85e812008-08-08 20:33:30 +000070 name = '.'.join(module)
71 run_module.__name__ = name
showardcc85e812008-08-08 20:33:30 +000072 functions[run_module] = set()
73
mbligheeb13572008-07-30 00:04:01 +000074 try:
75 dargs = {}
mbligh43758df2008-09-04 19:54:45 +000076 if options.debug:
mbligheeb13572008-07-30 00:04:01 +000077 dargs['max_simultaneous_procs'] = 1
78 pe = parallel.ParallelExecute(functions, **dargs)
79 pe.run_until_completion()
80 except parallel.ParallelError, err:
81 return err.errors
82 return []
mblighf9751332008-04-08 18:25:33 +000083
84
mbligheeb13572008-07-30 00:04:01 +000085def main():
mbligh43758df2008-09-04 19:54:45 +000086 global options, args
mbligheeb13572008-07-30 00:04:01 +000087 options, args = parser.parse_args()
88 if args:
89 parser.error('Unexpected argument(s): %s' % args)
90 parser.print_help()
91 sys.exit(1)
mbligh671c5922008-07-28 19:34:38 +000092
showardcc85e812008-08-08 20:33:30 +000093 # Strip the arguments off the command line, so that the unit tests do not
94 # see them.
95 sys.argv = [sys.argv[0]]
96
mbligheeb13572008-07-30 00:04:01 +000097 errors = run_tests(os.path.join(root, options.start), options.full)
mbligh671c5922008-07-28 19:34:38 +000098 if errors:
99 print "%d tests resulted in an error/failure:" % len(errors)
100 for error in errors:
101 print "\t%s" % error
102 sys.exit(1)
103 else:
104 print "All passed!"
105 sys.exit(0)
mbligheeb13572008-07-30 00:04:01 +0000106
107if __name__ == "__main__":
108 main()