test_droid.py revision a97d180703eab25b1dd0a8d0fb01d5d320487975
1#!/usr/bin/python
2# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import os
8import sys
9
10import logging
11# Turn the logging level to INFO before importing other autotest
12# code, to avoid having failed import logging messages confuse the
13# test_droid user.
14logging.basicConfig(level=logging.INFO)
15
16
17import common
18# Unfortunately, autotest depends on external packages for assorted
19# functionality regardless of whether or not it is needed in a particular
20# context.
21# Since we can't depend on people to import these utilities in any principled
22# way, we dynamically download code before any autotest imports.
23try:
24    import chromite.lib.terminal  # pylint: disable=unused-import
25    import django.http  # pylint: disable=unused-import
26except ImportError:
27    # Ensure the chromite site-package is installed.
28    import subprocess
29    build_externals_path = os.path.join(
30            os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
31            'utils', 'build_externals.py')
32    subprocess.check_call([build_externals_path, 'chromiterepo', 'django'])
33    # Restart the script so python now finds the autotest site-packages.
34    sys.exit(os.execv(__file__, sys.argv))
35
36from autotest_lib.site_utils import test_runner_utils
37from autotest_lib.site_utils import tester_feedback
38
39
40def parse_arguments(argv):
41    """
42    Parse command line arguments
43
44    @param argv: argument list to parse
45
46    @returns:    parsed arguments
47
48    @raises SystemExit if arguments are malformed, or required arguments
49            are not present.
50    """
51    return _parse_arguments_internal(argv)[0]
52
53
54def _parse_arguments_internal(argv):
55    """
56    Parse command line arguments
57
58    @param argv: argument list to parse
59
60    @returns:    tuple of parsed arguments and argv suitable for remote runs
61
62    @raises SystemExit if arguments are malformed, or required arguments
63            are not present.
64    """
65
66    parser = argparse.ArgumentParser(description='Run remote tests.')
67
68    parser.add_argument('serials', metavar='SERIALS',
69                        help='Comma separate list of device serials under '
70                             'test.')
71    parser.add_argument('-r', '--remote', metavar='REMOTE',
72                        default='localhost',
73                        help='hostname[:port] if the ADB device is connected '
74                             'to a remote machine. Ensure this workstation '
75                             'is configured for passwordless ssh access as '
76                             'users "root" or "adb"')
77    parser.add_argument('-i', '--interactive', action='store_true',
78                        help='Enable interactive feedback requests from tests.')
79    test_runner_utils.add_common_args(parser)
80    return parser.parse_args(argv)
81
82
83def main(argv):
84    """
85    Entry point for test_droid script.
86
87    @param argv: arguments list
88    """
89    arguments = _parse_arguments_internal(argv)
90
91    results_directory = test_runner_utils.create_results_directory(
92            arguments.results_dir)
93    arguments.results_dir = results_directory
94
95    autotest_path = os.path.dirname(os.path.dirname(
96            os.path.realpath(__file__)))
97    site_utils_path = os.path.join(autotest_path, 'site_utils')
98    realpath = os.path.realpath(__file__)
99    site_utils_path = os.path.realpath(site_utils_path)
100    host_attributes = {'serials' : arguments.serials,
101                       'os_type' : 'android'}
102
103    fb_service = None
104    try:
105        # Start the feedback service if needed.
106        if arguments.interactive:
107            fb_service = tester_feedback.FeedbackService()
108            fb_service.start()
109
110            if arguments.args:
111                arguments.args += ' '
112            else:
113                arguments.args = ''
114            arguments.args += (
115                    'feedback=interactive feedback_args=localhost:%d' %
116                    fb_service.server_port)
117
118        return test_runner_utils.perform_run_from_autotest_root(
119                    autotest_path, argv, arguments.tests, arguments.remote,
120                    args=arguments.args, ignore_deps=not arguments.enforce_deps,
121                    results_directory=results_directory,
122                    iterations=arguments.iterations,
123                    fast_mode=arguments.fast_mode, debug=arguments.debug,
124                    host_attributes=host_attributes)
125    finally:
126        if fb_service is not None:
127            fb_service.stop()
128
129
130if __name__ == '__main__':
131    sys.exit(main(sys.argv[1:]))
132