Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import argparse |
| 6 | import logging |
Gilad Arnold | 44629d5 | 2015-11-03 07:23:19 -0800 | [diff] [blame^] | 7 | import os |
| 8 | import sys |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 9 | |
| 10 | import common |
| 11 | from autotest_lib.client.common_lib import error |
| 12 | from autotest_lib.server import hosts |
Gilad Arnold | 44629d5 | 2015-11-03 07:23:19 -0800 | [diff] [blame^] | 13 | from autotest_lib.server import utils |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 14 | from autotest_lib.server.hosts import moblab_host |
| 15 | |
| 16 | |
| 17 | _LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' |
Gilad Arnold | 44629d5 | 2015-11-03 07:23:19 -0800 | [diff] [blame^] | 18 | _TEST_LAUNCH_SCRIPT = 'brillo_test_launcher.py' |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 19 | |
| 20 | # Running against a virtual machine has several intricacies that we need to |
| 21 | # adjust for. Namely SSH requires the use of 'localhost' while HTTP requires |
| 22 | # the use of '127.0.0.1'. Also because we are forwarding the ports from the VM |
| 23 | # to the host system, the ports to use for these services are also different |
| 24 | # from running on a physical machine. |
| 25 | _VIRT_MACHINE_SSH_ADDR = 'localhost:9222' |
| 26 | _VIRT_MACHINE_AFE_ADDR = '127.0.0.1:8888' |
| 27 | _VIRT_MACHINE_DEVSERVER_PORT = '7777' |
| 28 | _PHYS_MACHINE_DEVSERVER_PORT = '8080' |
| 29 | |
| 30 | |
| 31 | class BrilloTestError(Exception): |
| 32 | """A general error while testing Brillo.""" |
| 33 | |
| 34 | |
| 35 | class BrilloMoblabInitializationError(BrilloTestError): |
| 36 | """An error during Moblab initialization or handling.""" |
| 37 | |
| 38 | |
| 39 | def get_moblab_and_devserver_port(moblab_hostname): |
| 40 | """Initializes and returns a MobLab Host Object. |
| 41 | |
| 42 | @params moblab_hostname: The Moblab hostname, None if using a local virtual |
| 43 | machine. |
| 44 | |
| 45 | @returns A pair consisting of a MoblabHost and a devserver port. |
| 46 | |
| 47 | @raise BrilloMoblabInitializationError: Failed to set up the Moblab. |
| 48 | """ |
| 49 | if moblab_hostname: |
| 50 | web_address = moblab_hostname |
| 51 | devserver_port = _PHYS_MACHINE_DEVSERVER_PORT |
Simran Basi | 8d955d1 | 2015-10-22 11:36:49 -0700 | [diff] [blame] | 52 | rpc_timeout_min = 2 |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 53 | else: |
| 54 | moblab_hostname = _VIRT_MACHINE_SSH_ADDR |
| 55 | web_address = _VIRT_MACHINE_AFE_ADDR |
| 56 | devserver_port = _VIRT_MACHINE_DEVSERVER_PORT |
Simran Basi | 8d955d1 | 2015-10-22 11:36:49 -0700 | [diff] [blame] | 57 | rpc_timeout_min = 5 |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 58 | |
| 59 | try: |
| 60 | host = hosts.create_host(moblab_hostname, |
| 61 | host_class=moblab_host.MoblabHost, |
| 62 | web_address=web_address, |
Simran Basi | 8d955d1 | 2015-10-22 11:36:49 -0700 | [diff] [blame] | 63 | retain_image_storage=True, |
| 64 | rpc_timeout_min=rpc_timeout_min) |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 65 | except error.AutoservRunError as e: |
| 66 | raise BrilloMoblabInitializationError( |
| 67 | 'Unable to connect to the MobLab: %s' % e) |
| 68 | |
| 69 | try: |
| 70 | host.afe.get_hosts() |
| 71 | except Exception as e: |
| 72 | raise BrilloMoblabInitializationError( |
| 73 | "Unable to communicate with the MobLab's web frontend, " |
| 74 | "please verify that it is up and running at http://%s/\n" |
| 75 | "Error: %s" % (host.web_address, e)) |
| 76 | |
| 77 | return host, devserver_port |
| 78 | |
| 79 | |
| 80 | def parse_args(description, setup_parser=None, validate_args=None): |
| 81 | """Parse command-line arguments. |
| 82 | |
| 83 | @param description: The script description in the help message. |
| 84 | @param setup_parser: Function that takes a parser object and adds |
| 85 | script-specific options to it. |
| 86 | @param validate_args: Function that takes a parser object and the parsed |
| 87 | arguments and validates the arguments. It should use |
| 88 | parser.error() to report errors. |
| 89 | |
| 90 | @return Parsed and validated arguments. |
| 91 | """ |
| 92 | parser = argparse.ArgumentParser(description=description) |
| 93 | if setup_parser: |
| 94 | setup_parser(parser) |
| 95 | |
| 96 | # Add common options. |
| 97 | parser.add_argument('-m', '--moblab_host', |
| 98 | help='MobLab hostname or IP to launch tests. If this ' |
| 99 | 'argument is not provided, the test launcher ' |
| 100 | 'will attempt to test a local virtual machine ' |
| 101 | 'instance of MobLab.') |
| 102 | parser.add_argument('-a', '--adb_host', |
| 103 | help='Hostname or IP of the adb_host connected to the ' |
| 104 | 'Brillo DUT. Default is to assume it is connected ' |
| 105 | 'directly to the MobLab.') |
Gilad Arnold | 44629d5 | 2015-11-03 07:23:19 -0800 | [diff] [blame^] | 106 | parser.add_argument('-q', '--quickmerge', action='store_true', |
| 107 | help='Rsync over modified Autotest code.') |
Gilad Arnold | 8eb72af | 2015-10-20 12:26:20 -0700 | [diff] [blame] | 108 | parser.add_argument('-d', '--debug', action='store_true', |
| 109 | help='Print log statements.') |
| 110 | |
| 111 | args = parser.parse_args() |
| 112 | |
| 113 | # Configure the root logger. |
| 114 | logging.getLogger().setLevel(logging.DEBUG if args.debug else logging.INFO) |
| 115 | for log_handler in logging.getLogger().handlers: |
| 116 | log_handler.setFormatter(logging.Formatter(fmt=_LOGGING_FORMAT)) |
| 117 | |
| 118 | if validate_args: |
| 119 | validate_args(parser, args) |
| 120 | |
| 121 | return args |
Gilad Arnold | 44629d5 | 2015-11-03 07:23:19 -0800 | [diff] [blame^] | 122 | |
| 123 | |
| 124 | def setup_test_action_parser(parser): |
| 125 | """Add parser options related to test action. |
| 126 | |
| 127 | @param parser: argparse.ArgumentParser of the script. |
| 128 | """ |
| 129 | launch_opts = parser.add_mutually_exclusive_group() |
| 130 | launch_opts.add_argument('-A', '--print_args', action='store_true', |
| 131 | help='Print test arguments to stdout instead of ' |
| 132 | 'launching the test.') |
| 133 | launch_opts.add_argument('-C', '--print_command', action='store_true', |
| 134 | help='Print complete test launch command instead ' |
| 135 | 'of launching the test.') |
| 136 | |
| 137 | |
| 138 | def _get_arg_strs(test_args): |
| 139 | """Converts an argument dictionary into a list of 'arg=val' strings.""" |
| 140 | return ['%s=%s' % kv for kv in test_args.iteritems()] |
| 141 | |
| 142 | |
| 143 | def _get_command(moblab, test_name, test_args, do_quickmerge, do_quote): |
| 144 | """Returns the test launch command. |
| 145 | |
| 146 | @param moblab: MoblabHost representing the MobLab being used for testing. |
| 147 | @param test_name: The name of the test to run. |
| 148 | @param test_args: Dictionary of test arguments. |
| 149 | @param do_quickmerge: If True, pass the --quickmerge flag. |
| 150 | @param do_quote: If True, add single-quotes around test arguments. |
| 151 | |
| 152 | @return Test launch command as a list of strings. |
| 153 | """ |
| 154 | # pylint: disable=missing-docstring |
| 155 | def quote(val): |
| 156 | return "'%s'" % val if do_quote else val |
| 157 | |
| 158 | cmd = [os.path.join(os.path.dirname(__file__), _TEST_LAUNCH_SCRIPT), |
| 159 | '-t', quote(test_name)] |
| 160 | if do_quickmerge: |
| 161 | cmd.append('-q') |
| 162 | if not moblab.hostname.startswith('localhost'): |
| 163 | cmd += ['-m', quote(moblab.hostname)] |
| 164 | for arg_str in _get_arg_strs(test_args): |
| 165 | cmd += ['-A', quote(arg_str)] |
| 166 | return cmd |
| 167 | |
| 168 | |
| 169 | def _print_args(test_args): |
| 170 | """Prints the test arguments to stdout, one per line. |
| 171 | |
| 172 | @param test_args: Dictionary of test arguments. |
| 173 | """ |
| 174 | print('\n'.join(_get_arg_strs(test_args))) |
| 175 | |
| 176 | |
| 177 | def _print_command(moblab, test_name, test_args, do_quickmerge): |
| 178 | """Prints the test launch command to stdout with quoting. |
| 179 | |
| 180 | @param moblab: MoblabHost representing the MobLab being used for testing. |
| 181 | @param test_name: The name of the test to run. |
| 182 | @param test_args: Dictionary of test arguments. |
| 183 | @param do_quickmerge: If True, pass the --quickmerge flag. |
| 184 | """ |
| 185 | print(' '.join( |
| 186 | _get_command(moblab, test_name, test_args, do_quickmerge, True))) |
| 187 | |
| 188 | |
| 189 | def _run_command(moblab, test_name, test_args, do_quickmerge): |
| 190 | """Runs the test launch script. |
| 191 | |
| 192 | @param moblab: MoblabHost representing the MobLab being used for testing. |
| 193 | @param test_name: The name of the test to run. |
| 194 | @param test_args: Dictionary of test arguments. |
| 195 | @param do_quickmerge: If True, pass the --quickmerge flag. |
| 196 | """ |
| 197 | utils.run(_get_command(moblab, test_name, test_args, do_quickmerge, False), |
| 198 | stdout_tee=sys.stdout, stderr_tee=sys.stderr) |
| 199 | |
| 200 | |
| 201 | def do_test_action(args, moblab, test_name, test_args): |
| 202 | """Performs the desired action related to the test. |
| 203 | |
| 204 | @param args: Parsed arguments. |
| 205 | @param moblab: MoblabHost representing the MobLab being used for testing. |
| 206 | @param test_name: The name of the test to run. |
| 207 | @param test_args: Dictionary of test arguments. |
| 208 | """ |
| 209 | if args.print_args: |
| 210 | logging.info('Printing test arguments') |
| 211 | _print_args(test_args) |
| 212 | elif args.print_command: |
| 213 | logging.info('Printing test launch command') |
| 214 | _print_command(moblab, test_name, test_args, args.quickmerge) |
| 215 | else: |
| 216 | logging.info('Launching test') |
| 217 | _run_command(moblab, test_name, test_args, args.quickmerge) |