1#!/usr/bin/python2 -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        'frontend_unittest.py',
31        'csv_encoder_unittest.py',
32        'rpc_interface_unittest.py',
33        'models_test.py',
34        'rpc_utils_unittest.py',
35        'site_rpc_utils_unittest.py',
36        'execution_engine_unittest.py',
37        'service_proxy_lib_test.py',
38        'site_parse_unittest.py',
39        'server_manager_unittest.py',
40        ))
41
42REQUIRES_MYSQLDB = set((
43        'migrate_unittest.py',
44        'db_utils_unittest.py',
45        ))
46
47REQUIRES_GWT = set((
48        'client_compilation_unittest.py',
49        ))
50
51REQUIRES_SIMPLEJSON = set((
52        'serviceHandler_unittest.py',
53        ))
54
55REQUIRES_AUTH = set ((
56    'trigger_unittest.py',
57    ))
58
59REQUIRES_HTTPLIB2 = set((
60        ))
61
62REQUIRES_PROTOBUFS = set((
63        'cloud_console_client_unittest.py',
64        'job_serializer_unittest.py',
65        ))
66
67REQUIRES_SELENIUM = set((
68        'ap_configurator_factory_unittest.py',
69        'ap_batch_locker_unittest.py'
70    ))
71
72LONG_RUNTIME = set((
73    'barrier_unittest.py',
74    'logging_manager_test.py',
75    'task_loop_unittest.py'  # crbug.com/254030
76    ))
77
78# Unitests that only work in chroot. The names are for module name, thus no
79# file extension of ".py".
80REQUIRES_CHROOT = set((
81    'mbim_channel_unittest',
82    ))
83
84SKIP = set((
85    # This particular KVM autotest test is not a unittest
86    'guest_test.py',
87    'ap_configurator_test.py',
88    'chaos_base_test.py',
89    'chaos_interop_test.py',
90    # crbug.com/251395
91    'dev_server_test.py',
92    'full_release_test.py',
93    'scheduler_lib_unittest.py',
94    'webstore_test.py',
95    # crbug.com/432621 These files are not tests, and will disappear soon.
96    'des_01_test.py',
97    'des_02_test.py',
98    # Require lxc to be installed
99    'base_image_unittest.py',
100    'container_bucket_unittest.py',
101    'container_factory_unittest.py',
102    'container_unittest.py',
103    'lxc_functional_test.py',
104    'service_unittest.py',
105    'zygote_unittest.py',
106    # Require sponge utils installed in site-packages
107    'sponge_utils_functional_test.py',
108    ))
109
110LONG_TESTS = (REQUIRES_MYSQLDB |
111              REQUIRES_GWT |
112              REQUIRES_HTTPLIB2 |
113              REQUIRES_AUTH |
114              REQUIRES_PROTOBUFS |
115              REQUIRES_SELENIUM |
116              LONG_RUNTIME)
117
118ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
119
120# The set of files in LONG_TESTS with its full path
121LONG_TESTS_FULL_PATH = {os.path.join(ROOT, t) for t in LONG_TESTS}
122
123class TestFailure(Exception):
124    """Exception type for any test failure."""
125    pass
126
127
128def run_test(mod_names, options):
129    """
130    @param mod_names: A list of individual parts of the module name to import
131            and run as a test suite.
132    @param options: optparse options.
133    """
134    if not options.debug:
135        parallel.redirect_io()
136
137    print "Running %s" % '.'.join(mod_names)
138    mod = common.setup_modules.import_module(mod_names[-1],
139                                             '.'.join(mod_names[:-1]))
140    test = unittest.defaultTestLoader.loadTestsFromModule(mod)
141    suite = unittest.TestSuite(test)
142    runner = unittest.TextTestRunner(verbosity=2)
143    result = runner.run(suite)
144    if result.errors or result.failures:
145        msg = '%s had %d failures and %d errors.'
146        msg %= '.'.join(mod_names), len(result.failures), len(result.errors)
147        raise TestFailure(msg)
148
149
150def scan_for_modules(start, options):
151    """Scan folders and find all test modules that are not included in the
152    denylist (defined in LONG_TESTS).
153
154    @param start: The absolute directory to look for tests under.
155    @param options: optparse options.
156    @return a list of modules to be executed.
157    """
158    modules = []
159
160    skip_tests = SKIP
161    if options.skip_tests:
162        skip_tests.update(options.skip_tests.split())
163    skip_tests_full_path = {os.path.join(ROOT, t) for t in skip_tests}
164
165    for dir_path, sub_dirs, file_names in os.walk(start):
166        # Only look in and below subdirectories that are python modules.
167        if '__init__.py' not in file_names:
168            if options.full:
169                for file_name in file_names:
170                    if file_name.endswith('.pyc'):
171                        os.unlink(os.path.join(dir_path, file_name))
172            # Skip all subdirectories below this one, it is not a module.
173            del sub_dirs[:]
174            if options.debug:
175                print 'Skipping', dir_path
176            continue  # Skip this directory.
177
178        # Look for unittest files.
179        for file_name in file_names:
180            if (file_name.endswith('_unittest.py') or
181                file_name.endswith('_test.py')):
182                file_path = os.path.join(dir_path, file_name)
183                if (not options.full and
184                    (file_name in LONG_TESTS or
185                     file_path in LONG_TESTS_FULL_PATH)):
186                    continue
187                if (file_name in skip_tests or
188                    file_path in skip_tests_full_path):
189                    continue
190                path_no_py = os.path.join(dir_path, file_name).rstrip('.py')
191                assert path_no_py.startswith(ROOT)
192                names = path_no_py[len(ROOT)+1:].split('/')
193                modules.append(['autotest_lib'] + names)
194                if options.debug:
195                    print 'testing', path_no_py
196    return modules
197
198
199def is_inside_chroot():
200    """Check if the process is running inside the chroot.
201
202    @return: True if the process is running inside the chroot, False otherwise.
203    """
204    return os.path.exists('/etc/cros_chroot_version')
205
206
207def find_and_run_tests(start, options):
208    """
209    Find and run Python unittest suites below the given directory.  Only look
210    in subdirectories of start that are actual importable Python modules.
211
212    @param start: The absolute directory to look for tests under.
213    @param options: optparse options.
214    """
215    if options.module_list:
216        modules = []
217        for m in options.module_list:
218            modules.append(m.split('.'))
219    else:
220        modules = scan_for_modules(start, options)
221
222    if options.debug:
223        print 'Number of test modules found:', len(modules)
224
225    chroot = is_inside_chroot()
226    functions = {}
227    for module_names in modules:
228        if not chroot and module_names[-1] in REQUIRES_CHROOT:
229            if options.debug:
230                print ('Test %s requires to run in chroot, skipped.' %
231                       module_names[-1])
232            continue
233        # Create a function that'll test a particular module.  module=module
234        # is a hack to force python to evaluate the params now.  We then
235        # rename the function to make error reporting nicer.
236        run_module = lambda module=module_names: run_test(module, options)
237        name = '.'.join(module_names)
238        run_module.__name__ = name
239        functions[run_module] = set()
240
241    try:
242        dargs = {}
243        if options.debug:
244            dargs['max_simultaneous_procs'] = 1
245        pe = parallel.ParallelExecute(functions, **dargs)
246        pe.run_until_completion()
247    except parallel.ParallelError, err:
248        return err.errors
249    return []
250
251
252def main():
253    """Entry point for unittest_suite.py"""
254    options, args = parser.parse_args()
255    if args:
256        options.module_list = args
257
258    # Strip the arguments off the command line, so that the unit tests do not
259    # see them.
260    del sys.argv[1:]
261
262    absolute_start = os.path.join(ROOT, options.start)
263    errors = find_and_run_tests(absolute_start, options)
264    if errors:
265        print "%d tests resulted in an error/failure:" % len(errors)
266        for error in errors:
267            print "\t%s" % error
268        print "Rerun", sys.argv[0], "--debug to see the failure details."
269        sys.exit(1)
270    else:
271        print "All passed!"
272        sys.exit(0)
273
274
275if __name__ == "__main__":
276    main()
277