1#!/usr/bin/python 2# Copyright (c) 2013 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 pipes 9import signal 10import subprocess 11import sys 12 13import logging 14# Turn the logging level to INFO before importing other autotest 15# code, to avoid having failed import logging messages confuse the 16# test_that user. 17logging.basicConfig(level=logging.INFO) 18 19import common 20from autotest_lib.client.common_lib import error, logging_manager 21from autotest_lib.server import server_logging_config 22from autotest_lib.server.cros.dynamic_suite import constants 23from autotest_lib.server.hosts import factory 24from autotest_lib.site_utils import test_runner_utils 25 26 27try: 28 from chromite.lib import cros_build_lib 29except ImportError: 30 print 'Unable to import chromite.' 31 print 'This script must be either:' 32 print ' - Be run in the chroot.' 33 print ' - (not yet supported) be run after running ' 34 print ' ../utils/build_externals.py' 35 36_QUICKMERGE_SCRIPTNAME = '/mnt/host/source/chromite/bin/autotest_quickmerge' 37 38 39def _get_board_from_host(remote): 40 """Get the board of the remote host. 41 42 @param remote: string representing the IP of the remote host. 43 44 @return: A string representing the board of the remote host. 45 """ 46 logging.info('Board unspecified, attempting to determine board from host.') 47 host = factory.create_host(remote) 48 try: 49 board = host.get_board().replace(constants.BOARD_PREFIX, '') 50 except error.AutoservRunError: 51 raise test_runner_utils.TestThatRunError( 52 'Cannot determine board, please specify a --board option.') 53 logging.info('Detected host board: %s', board) 54 return board 55 56 57def validate_arguments(arguments): 58 """ 59 Validates parsed arguments. 60 61 @param arguments: arguments object, as parsed by ParseArguments 62 @raises: ValueError if arguments were invalid. 63 """ 64 if arguments.remote == ':lab:': 65 if arguments.args: 66 raise ValueError('--args flag not supported when running against ' 67 ':lab:') 68 if arguments.pretend: 69 raise ValueError('--pretend flag not supported when running ' 70 'against :lab:') 71 if arguments.ssh_verbosity: 72 raise ValueError('--ssh_verbosity flag not supported when running ' 73 'against :lab:') 74 if not arguments.board or not arguments.build: 75 raise ValueError('--board and --build are both required when ' 76 'running against :lab:') 77 else: 78 if arguments.build is None: 79 arguments.build = test_runner_utils.NO_BUILD 80 if arguments.web: 81 raise ValueError('--web flag not supported when running locally') 82 83 84def parse_arguments(argv): 85 """ 86 Parse command line arguments 87 88 @param argv: argument list to parse 89 @returns: parsed arguments 90 @raises SystemExit if arguments are malformed, or required arguments 91 are not present. 92 """ 93 return _parse_arguments_internal(argv)[0] 94 95 96def _parse_arguments_internal(argv): 97 """ 98 Parse command line arguments 99 100 @param argv: argument list to parse 101 @returns: tuple of parsed arguments and argv suitable for remote runs 102 @raises SystemExit if arguments are malformed, or required arguments 103 are not present. 104 """ 105 local_parser, remote_argv = parse_local_arguments(argv) 106 107 parser = argparse.ArgumentParser(description='Run remote tests.', 108 parents=[local_parser]) 109 110 parser.add_argument('remote', metavar='REMOTE', 111 help='hostname[:port] for remote device. Specify ' 112 ':lab: to run in test lab. When tests are run in ' 113 'the lab, test_that will use the client autotest ' 114 'package for the build specified with --build, ' 115 'and the lab server code rather than local ' 116 'changes.') 117 test_runner_utils.add_common_args(parser) 118 default_board = cros_build_lib.GetDefaultBoard() 119 parser.add_argument('-b', '--board', metavar='BOARD', default=default_board, 120 action='store', 121 help='Board for which the test will run. Default: %s' % 122 (default_board or 'Not configured')) 123 parser.add_argument('-i', '--build', metavar='BUILD', 124 default=test_runner_utils.NO_BUILD, 125 help='Build to test. Device will be reimaged if ' 126 'necessary. Omit flag to skip reimage and test ' 127 'against already installed DUT image. Examples: ' 128 'link-paladin/R34-5222.0.0-rc2, ' 129 'lumpy-release/R34-5205.0.0') 130 parser.add_argument('-p', '--pool', metavar='POOL', default='suites', 131 help='Pool to use when running tests in the lab. ' 132 'Default is "suites"') 133 parser.add_argument('--autotest_dir', metavar='AUTOTEST_DIR', 134 help='Use AUTOTEST_DIR instead of normal board sysroot ' 135 'copy of autotest, and skip the quickmerge step.') 136 parser.add_argument('--no-quickmerge', action='store_true', default=False, 137 dest='no_quickmerge', 138 help='Skip the quickmerge step and use the sysroot ' 139 'as it currently is. May result in un-merged ' 140 'source tree changes not being reflected in the ' 141 'run. If using --autotest_dir, this flag is ' 142 'automatically applied.') 143 parser.add_argument('--whitelist-chrome-crashes', action='store_true', 144 default=False, dest='whitelist_chrome_crashes', 145 help='Ignore chrome crashes when producing test ' 146 'report. This flag gets passed along to the ' 147 'report generation tool.') 148 parser.add_argument('--ssh_private_key', action='store', 149 default=test_runner_utils.TEST_KEY_PATH, 150 help='Path to the private ssh key.') 151 return parser.parse_args(argv), remote_argv 152 153 154def parse_local_arguments(argv): 155 """ 156 Strips out arguments that are not to be passed through to runs. 157 158 Add any arguments that should not be passed to remote test_that runs here. 159 160 @param argv: argument list to parse. 161 @returns: tuple of local argument parser and remaining argv. 162 """ 163 parser = argparse.ArgumentParser(add_help=False) 164 parser.add_argument('-w', '--web', dest='web', default=None, 165 help='Address of a webserver to receive test requests.') 166 _, remaining_argv = parser.parse_known_args(argv) 167 return parser, remaining_argv 168 169 170def perform_bootstrap_into_autotest_root(arguments, autotest_path, argv): 171 """ 172 Perfoms a bootstrap to run test_that from the |autotest_path|. 173 174 This function is to be called from test_that's main() script, when 175 test_that is executed from the source tree location. It runs 176 autotest_quickmerge to update the sysroot unless arguments.no_quickmerge 177 is set. It then executes and waits on the version of test_that.py 178 in |autotest_path|. 179 180 @param arguments: A parsed arguments object, as returned from 181 test_that.parse_arguments(...). 182 @param autotest_path: Full absolute path to the autotest root directory. 183 @param argv: The arguments list, as passed to main(...) 184 185 @returns: The return code of the test_that script that was executed in 186 |autotest_path|. 187 """ 188 logging_manager.configure_logging( 189 server_logging_config.ServerLoggingConfig(), 190 use_console=True, 191 verbose=arguments.debug) 192 if arguments.no_quickmerge: 193 logging.info('Skipping quickmerge step.') 194 else: 195 logging.info('Running autotest_quickmerge step.') 196 command = [_QUICKMERGE_SCRIPTNAME, '--board='+arguments.board] 197 s = subprocess.Popen(command, 198 stdout=subprocess.PIPE, 199 stderr=subprocess.STDOUT) 200 for message in iter(s.stdout.readline, b''): 201 logging.info('quickmerge| %s', message.strip()) 202 return_code = s.wait() 203 if return_code: 204 raise test_runner_utils.TestThatRunError( 205 'autotest_quickmerge failed with error code %s.' % 206 return_code) 207 208 logging.info('Re-running test_that script in %s copy of autotest.', 209 autotest_path) 210 script_command = os.path.join(autotest_path, 'site_utils', 211 'test_that.py') 212 if not os.path.exists(script_command): 213 raise test_runner_utils.TestThatRunError( 214 'Unable to bootstrap to autotest root, %s not found.' % 215 script_command) 216 proc = None 217 def resend_sig(signum, stack_frame): 218 #pylint: disable-msg=C0111 219 if proc: 220 proc.send_signal(signum) 221 signal.signal(signal.SIGINT, resend_sig) 222 signal.signal(signal.SIGTERM, resend_sig) 223 224 proc = subprocess.Popen([script_command] + argv) 225 226 return proc.wait() 227 228 229def _main_for_local_run(argv, arguments): 230 """ 231 Effective entry point for local test_that runs. 232 233 @param argv: Script command line arguments. 234 @param arguments: Parsed command line arguments. 235 """ 236 if not cros_build_lib.IsInsideChroot(): 237 print >> sys.stderr, 'For local runs, script must be run inside chroot.' 238 return 1 239 240 results_directory = test_runner_utils.create_results_directory( 241 arguments.results_dir) 242 test_runner_utils.add_ssh_identity(results_directory, 243 arguments.ssh_private_key) 244 arguments.results_dir = results_directory 245 246 # If the board has not been specified through --board, and is not set in the 247 # default_board file, determine the board by ssh-ing into the host. Also 248 # prepend it to argv so we can re-use it when we run test_that from the 249 # sysroot. 250 if arguments.board is None: 251 arguments.board = _get_board_from_host(arguments.remote) 252 argv = ['--board', arguments.board] + argv 253 254 if arguments.autotest_dir: 255 autotest_path = arguments.autotest_dir 256 arguments.no_quickmerge = True 257 else: 258 sysroot_path = os.path.join('/build', arguments.board, '') 259 260 if not os.path.exists(sysroot_path): 261 print >> sys.stderr, ('%s does not exist. Have you run ' 262 'setup_board?' % sysroot_path) 263 return 1 264 265 path_ending = 'usr/local/build/autotest' 266 autotest_path = os.path.join(sysroot_path, path_ending) 267 268 site_utils_path = os.path.join(autotest_path, 'site_utils') 269 270 if not os.path.exists(autotest_path): 271 print >> sys.stderr, ('%s does not exist. Have you run ' 272 'build_packages? Or if you are using ' 273 '--autotest-dir, make sure it points to ' 274 'a valid autotest directory.' % autotest_path) 275 return 1 276 277 realpath = os.path.realpath(__file__) 278 site_utils_path = os.path.realpath(site_utils_path) 279 280 # If we are not running the sysroot version of script, perform 281 # a quickmerge if necessary and then re-execute 282 # the sysroot version of script with the same arguments. 283 if os.path.dirname(realpath) != site_utils_path: 284 return perform_bootstrap_into_autotest_root( 285 arguments, autotest_path, argv) 286 else: 287 return test_runner_utils.perform_run_from_autotest_root( 288 autotest_path, argv, arguments.tests, arguments.remote, 289 build=arguments.build, board=arguments.board, 290 args=arguments.args, ignore_deps=not arguments.enforce_deps, 291 results_directory=results_directory, 292 ssh_verbosity=arguments.ssh_verbosity, 293 ssh_options=arguments.ssh_options, 294 iterations=arguments.iterations, 295 fast_mode=arguments.fast_mode, debug=arguments.debug, 296 whitelist_chrome_crashes=arguments.whitelist_chrome_crashes) 297 298 299def _main_for_lab_run(argv, arguments): 300 """ 301 Effective entry point for lab test_that runs. 302 303 @param argv: Script command line arguments. 304 @param arguments: Parsed command line arguments. 305 """ 306 autotest_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 307 '..')) 308 flattened_argv = ' '.join([pipes.quote(item) for item in argv]) 309 command = [os.path.join(autotest_path, 'site_utils', 310 'run_suite.py'), 311 '--board', arguments.board, 312 '--build', arguments.build, 313 '--suite_name', 'test_that_wrapper', 314 '--pool', arguments.pool, 315 '--suite_args', flattened_argv] 316 if arguments.web: 317 command.extend(['--web', arguments.web]) 318 logging.info('About to start lab suite with command %s.', command) 319 return subprocess.call(command) 320 321 322def main(argv): 323 """ 324 Entry point for test_that script. 325 326 @param argv: arguments list 327 """ 328 arguments, remote_argv = _parse_arguments_internal(argv) 329 try: 330 validate_arguments(arguments) 331 except ValueError as err: 332 print >> sys.stderr, ('Invalid arguments. %s' % err.message) 333 return 1 334 335 if arguments.remote == ':lab:': 336 return _main_for_lab_run(remote_argv, arguments) 337 else: 338 return _main_for_local_run(argv, arguments) 339 340 341if __name__ == '__main__': 342 sys.exit(main(sys.argv[1:])) 343