• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python -u
2
3import os, sys, unittest, optparse
4import common
5from autotest_lib.utils import parallel
6
7parser = optparse.OptionParser()
8parser.add_option("-r", action="store", type="string", dest="start",
9                  default='',
10                  help="root directory to start running unittests")
11parser.add_option("--full", action="store_true", dest="full", default=False,
12                  help="whether to run the shortened version of the test")
13parser.add_option("--debug", action="store_true", dest="debug", default=False,
14                  help="run in debug mode")
15parser.add_option("--skip-tests", dest="skip_tests",  default=[],
16                  help="A space separated list of tests to skip")
17
18parser.set_defaults(module_list=None)
19
20# Following sets are used to define a collection of modules that are optional
21# tests and do not need to be executed in unittest suite for various reasons.
22# Each entry can be file name or relative path that's relative to the parent
23# folder of the folder containing this file (unittest_suite.py). The list
24# will be used to filter any test file with matching name or matching full
25# path. If a file's name is too general and has a chance to collide with files
26# in other folder, it is recommended to specify its relative path here, e.g.,
27# using 'mirror/trigger_unittest.py', instead of 'trigger_unittest.py' only.
28
29REQUIRES_DJANGO = set((
30        'monitor_db_unittest.py',
31        'monitor_db_functional_test.py',
32        'monitor_db_cleanup_test.py',
33        'frontend_unittest.py',
34        'csv_encoder_unittest.py',
35        'rpc_interface_unittest.py',
36        'models_test.py',
37        'scheduler_models_unittest.py',
38        'rpc_utils_unittest.py',
39        'site_rpc_utils_unittest.py',
40        'execution_engine_unittest.py',
41        'service_proxy_lib_test.py',
42        'rdb_integration_tests.py',
43        'rdb_unittest.py',
44        'rdb_hosts_unittest.py',
45        'rdb_cache_unittests.py',
46        'scheduler_lib_unittest.py',
47        'host_scheduler_unittests.py',
48        'site_parse_unittest.py',
49        'shard_client_integration_tests.py',
50        'server_manager_unittest.py',
51        ))
52
53REQUIRES_MYSQLDB = set((
54        'migrate_unittest.py',
55        'db_utils_unittest.py',
56        ))
57
58REQUIRES_GWT = set((
59        'client_compilation_unittest.py',
60        ))
61
62REQUIRES_SIMPLEJSON = set((
63        'serviceHandler_unittest.py',
64        ))
65
66REQUIRES_AUTH = set ((
67    'trigger_unittest.py',
68    ))
69
70REQUIRES_HTTPLIB2 = set((
71        ))
72
73REQUIRES_PROTOBUFS = set((
74        'job_serializer_unittest.py',
75        ))
76
77REQUIRES_SELENIUM = set((
78        'ap_configurator_factory_unittest.py',
79        'ap_batch_locker_unittest.py'
80    ))
81
82LONG_RUNTIME = set((
83    'barrier_unittest.py',
84    'logging_manager_test.py',
85    'task_loop_unittest.py'  # crbug.com/254030
86    ))
87
88# Unitests that only work in chroot. The names are for module name, thus no
89# file extension of ".py".
90REQUIRES_CHROOT = set((
91    'mbim_channel_unittest',
92    ))
93
94SKIP = set((
95    # This particular KVM autotest test is not a unittest
96    'guest_test.py',
97    'ap_configurator_test.py',
98    'chaos_base_test.py',
99    'chaos_interop_test.py',
100    'only_if_needed_unittests.py',
101    # crbug.com/251395
102    'dev_server_test.py',
103    'full_release_test.py',
104    'scheduler_lib_unittest.py',
105    'webstore_test.py',
106    # crbug.com/432621 These files are not tests, and will disappear soon.
107    'des_01_test.py',
108    'des_02_test.py',
109    # Require lxc to be installed
110    'container_bucket_unittest.py',
111    'container_unittest.py',
112    'lxc_functional_test.py',
113    'zygote_unittest.py',
114    # Require sponge utils installed in site-packages
115    'sponge_utils_functional_test.py',
116    ))
117
118LONG_TESTS = (REQUIRES_MYSQLDB |
119              REQUIRES_GWT |
120              REQUIRES_HTTPLIB2 |
121              REQUIRES_AUTH |
122              REQUIRES_PROTOBUFS |
123              REQUIRES_SELENIUM |
124              LONG_RUNTIME)
125
126ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
127
128# The set of files in LONG_TESTS with its full path
129LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
130
131class TestFailure(Exception):
132    """Exception type for any test failure."""
133    pass
134
135
136def run_test(mod_names, options):
137    """
138    @param mod_names: A list of individual parts of the module name to import
139            and run as a test suite.
140    @param options: optparse options.
141    """
142    if not options.debug:
143        parallel.redirect_io()
144
145    print "Running %s" % '.'.join(mod_names)
146    mod = common.setup_modules.import_module(mod_names[-1],
147                                             '.'.join(mod_names[:-1]))
148    test = unittest.defaultTestLoader.loadTestsFromModule(mod)
149    suite = unittest.TestSuite(test)
150    runner = unittest.TextTestRunner(verbosity=2)
151    result = runner.run(suite)
152    if result.errors or result.failures:
153        msg = '%s had %d failures and %d errors.'
154        msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
155        raise TestFailure(msg)
156
157
158def scan_for_modules(start, options):
159    """Scan folders and find all test modules that are not included in the
160    blacklist (defined in LONG_TESTS).
161
162    @param start: The absolute directory to look for tests under.
163    @param options: optparse options.
164    @return a list of modules to be executed.
165    """
166    modules = []
167
168    skip_tests = SKIP
169    if options.skip_tests:
170        skip_tests.update(options.skip_tests.split())
171    skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
172
173    for dir_path, sub_dirs, file_names in os.walk(start):
174        # Only look in and below subdirectories that are python modules.
175        if '__init__.py' not in file_names:
176            if options.full:
177                for file_name in file_names:
178                    if file_name.endswith('.pyc'):
179                        os.unlink(os.path.join(dir_path, file_name))
180            # Skip all subdirectories below this one, it is not a module.
181            del sub_dirs[:]
182            if options.debug:
183                print 'Skipping', dir_path
184            continue  # Skip this directory.
185
186        # Look for unittest files.
187        for file_name in file_names:
188            if (file_name.endswith('_unittest.py') or
189                file_name.endswith('_test.py')):
190                file_path = os.path.join(dir_path, file_name)
191                if (not options.full and
192                    (file_name in LONG_TESTS or
193                     file_path in LONG_TESTS_FULL_PATH)):
194                    continue
195                if (file_name in skip_tests or
196                    file_path in skip_tests_full_path):
197                    continue
198                path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
199                assert path_no_py.startswith(ROOT)
200                names = path_no_py[len(ROOT)+1:].split('/')
201                modules.append(['autotest_lib'] + names)
202                if options.debug:
203                    print 'testing', path_no_py
204    return modules
205
206
207def is_inside_chroot():
208    """Check if the process is running inside the chroot.
209
210    @return: True if the process is running inside the chroot, False otherwise.
211    """
212    try:
213        # chromite may not be setup, e.g., in vm, therefore the ImportError
214        # needs to be handled.
215        from chromite.lib import cros_build_lib
216        return cros_build_lib.IsInsideChroot()
217    except ImportError:
218        return False
219
220
221def find_and_run_tests(start, options):
222    """
223    Find and run Python unittest suites below the given directory.  Only look
224    in subdirectories of start that are actual importable Python modules.
225
226    @param start: The absolute directory to look for tests under.
227    @param options: optparse options.
228    """
229    if options.module_list:
230        modules = []
231        for m in options.module_list:
232            modules.append(m.split('.'))
233    else:
234        modules = scan_for_modules(start, options)
235
236    if options.debug:
237        print 'Number of test modules found:', len(modules)
238
239    chroot = is_inside_chroot()
240    functions = {}
241    for module_names in modules:
242        if not chroot and module_names[-1] in REQUIRES_CHROOT:
243            if options.debug:
244                print ('Test %s requires to run in chroot, skipped.' %
245                       module_names[-1])
246            continue
247        # Create a function that'll test a particular module.  module=module
248        # is a hack to force python to evaluate the params now.  We then
249        # rename the function to make error reporting nicer.
250        run_module = lambda module=module_names: run_test(module, options)
251        name = '.'.join(module_names)
252        run_module.__name__ = name
253        functions[run_module] = set()
254
255    try:
256        dargs = {}
257        if options.debug:
258            dargs['max_simultaneous_procs'] = 1
259        pe = parallel.ParallelExecute(functions, **dargs)
260        pe.run_until_completion()
261    except parallel.ParallelError, err:
262        return err.errors
263    return []
264
265
266def main():
267    """Entry point for unittest_suite.py"""
268    options, args = parser.parse_args()
269    if args:
270        options.module_list = args
271
272    # Strip the arguments off the command line, so that the unit tests do not
273    # see them.
274    del sys.argv[1:]
275
276    absolute_start = os.path.join(ROOT, options.start)
277    errors = find_and_run_tests(absolute_start, options)
278    if errors:
279        print "%d tests resulted in an error/failure:" % len(errors)
280        for error in errors:
281            print "\t%s" % error
282        print "Rerun", sys.argv[0], "--debug to see the failure details."
283        sys.exit(1)
284    else:
285        print "All passed!"
286        sys.exit(0)
287
288
289if __name__ == "__main__":
290    main()
291