Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 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. |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 4 | |
| 5 | import datetime |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 6 | import errno |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 7 | import functools |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 8 | import logging |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 9 | import os |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 10 | import re |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 11 | import stat |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 12 | import sys |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 13 | import time |
| 14 | |
| 15 | import common |
| 16 | |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 17 | from autotest_lib.client.bin import utils as client_utils |
Dan Shi | 49d451f | 2016-04-19 09:25:01 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib import android_utils |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 19 | from autotest_lib.client.common_lib import error |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import global_config |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 21 | from autotest_lib.client.common_lib.cros import dev_server |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 22 | from autotest_lib.client.common_lib.cros import retry |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 23 | from autotest_lib.server import afe_utils |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 24 | from autotest_lib.server import autoserv_parser |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 25 | from autotest_lib.server import constants as server_constants |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 26 | from autotest_lib.server import utils |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 27 | from autotest_lib.server.cros import provision |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 28 | from autotest_lib.server.cros.dynamic_suite import tools |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 29 | from autotest_lib.server.cros.dynamic_suite import constants |
Simran Basi | 724b8a5 | 2013-09-30 11:19:31 -0700 | [diff] [blame] | 30 | from autotest_lib.server.hosts import abstract_ssh |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 31 | from autotest_lib.server.hosts import adb_label |
| 32 | from autotest_lib.server.hosts import base_label |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 33 | from autotest_lib.server.hosts import teststation_host |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 34 | |
| 35 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 36 | CONFIG = global_config.global_config |
| 37 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 38 | ADB_CMD = 'adb' |
| 39 | FASTBOOT_CMD = 'fastboot' |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 40 | SHELL_CMD = 'shell' |
Filipe Brandenburger | 3436339 | 2015-08-13 14:57:45 -0700 | [diff] [blame] | 41 | # Some devices have no serial, then `adb serial` has output such as: |
| 42 | # (no serial number) device |
| 43 | # ?????????? device |
| 44 | DEVICE_NO_SERIAL_MSG = '(no serial number)' |
| 45 | DEVICE_NO_SERIAL_TAG = '<NO_SERIAL>' |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 46 | # Regex to find an adb device. Examples: |
| 47 | # 0146B5580B01801B device |
| 48 | # 018e0ecb20c97a62 device |
| 49 | # 172.22.75.141:5555 device |
Kevin Cheng | 224415e | 2016-04-22 11:32:10 -0700 | [diff] [blame] | 50 | # localhost:22 device |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 51 | DEVICE_FINDER_REGEX = (r'^(?P<SERIAL>([\w-]+)|((tcp:)?' + |
| 52 | '\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}([:]5555)?)|' + |
Kevin Cheng | 224415e | 2016-04-22 11:32:10 -0700 | [diff] [blame] | 53 | '((tcp:)?localhost([:]22)?)|' + |
Filipe Brandenburger | 3436339 | 2015-08-13 14:57:45 -0700 | [diff] [blame] | 54 | re.escape(DEVICE_NO_SERIAL_MSG) + |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 55 | r')[ \t]+(?:device|fastboot)') |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 56 | CMD_OUTPUT_PREFIX = 'ADB_CMD_OUTPUT' |
| 57 | CMD_OUTPUT_REGEX = ('(?P<OUTPUT>[\s\S]*)%s:(?P<EXIT_CODE>\d{1,3})' % |
| 58 | CMD_OUTPUT_PREFIX) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 59 | RELEASE_FILE = 'ro.build.version.release' |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 60 | BOARD_FILE = 'ro.product.device' |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 61 | SDK_FILE = 'ro.build.version.sdk' |
Simran Basi | 242f8de | 2016-06-08 14:23:43 -0700 | [diff] [blame] | 62 | LOGCAT_FILE_FMT = 'logcat_%s.log' |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 63 | TMP_DIR = '/data/local/tmp' |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 64 | # Regex to pull out file type, perms and symlink. Example: |
Kevin Cheng | aaabd0c | 2015-11-10 16:05:04 -0800 | [diff] [blame] | 65 | # lrwxrwx--- 1 6 root system 2015-09-12 19:21 blah_link -> ./blah |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 66 | FILE_INFO_REGEX = '^(?P<TYPE>[dl-])(?P<PERMS>[rwx-]{9})' |
| 67 | FILE_SYMLINK_REGEX = '^.*-> (?P<SYMLINK>.+)' |
| 68 | # List of the perm stats indexed by the order they are listed in the example |
| 69 | # supplied above. |
| 70 | FILE_PERMS_FLAGS = [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, |
| 71 | stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP, |
| 72 | stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH] |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 73 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 74 | # Default maximum number of seconds to wait for a device to be down. |
| 75 | DEFAULT_WAIT_DOWN_TIME_SECONDS = 10 |
| 76 | # Default maximum number of seconds to wait for a device to be up. |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 77 | DEFAULT_WAIT_UP_TIME_SECONDS = 300 |
| 78 | # Maximum number of seconds to wait for a device to be up after it's wiped. |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 79 | WAIT_UP_AFTER_WIPE_TIME_SECONDS = 1200 |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 80 | |
Dan Shi | 7075f55 | 2016-04-21 15:42:41 -0700 | [diff] [blame] | 81 | # Default timeout for retrying adb/fastboot command. |
Dan Shi | 1cbf381 | 2016-07-18 23:06:38 +0000 | [diff] [blame] | 82 | DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS = 10 |
Dan Shi | 7075f55 | 2016-04-21 15:42:41 -0700 | [diff] [blame] | 83 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 84 | OS_TYPE_ANDROID = 'android' |
| 85 | OS_TYPE_BRILLO = 'brillo' |
| 86 | |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 87 | # Regex to parse build name to get the detailed build information. |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 88 | BUILD_REGEX = ('(?P<BRANCH>([^/]+))/(?P<BUILD_TARGET>([^/]+))-' |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 89 | '(?P<BUILD_TYPE>([^/]+))/(?P<BUILD_ID>([^/]+))') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 90 | # Regex to parse devserver url to get the detailed build information. Sample |
| 91 | # url: http://$devserver:8080/static/branch/target/build_id |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 92 | DEVSERVER_URL_REGEX = '.*/%s/*' % BUILD_REGEX |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 93 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 94 | ANDROID_IMAGE_FILE_FMT = '%(build_target)s-img-%(build_id)s.zip' |
Dan Shi | 49d451f | 2016-04-19 09:25:01 -0700 | [diff] [blame] | 95 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 96 | BRILLO_VENDOR_PARTITIONS_FILE_FMT = ( |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 97 | '%(build_target)s-vendor_partitions-%(build_id)s.zip') |
| 98 | AUTOTEST_SERVER_PACKAGE_FILE_FMT = ( |
| 99 | '%(build_target)s-autotest_server_package-%(build_id)s.tar.bz2') |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 100 | ADB_DEVICE_PREFIXES = ['product:', 'model:', 'device:'] |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 101 | |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 102 | # Map of product names to build target name. |
| 103 | PRODUCT_TARGET_MAP = {'dragon' : 'ryu', |
| 104 | 'flo' : 'razor', |
| 105 | 'flo_lte' : 'razorg', |
| 106 | 'gm4g_sprout' : 'seed_l8150', |
| 107 | 'flounder' : 'volantis', |
| 108 | 'flounder_lte' : 'volantisg'} |
| 109 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 110 | # Command to provision a Brillo device. |
| 111 | # os_image_dir: The full path of the directory that contains all the Android image |
| 112 | # files (from the image zip file). |
| 113 | # vendor_partition_dir: The full path of the directory that contains all the |
| 114 | # Brillo vendor partitions, and provision-device script. |
| 115 | BRILLO_PROVISION_CMD = ( |
Simran Basi | 7e52c62 | 2016-01-05 15:43:51 -0800 | [diff] [blame] | 116 | 'sudo ANDROID_PROVISION_OS_PARTITIONS=%(os_image_dir)s ' |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 117 | 'ANDROID_PROVISION_VENDOR_PARTITIONS=%(vendor_partition_dir)s ' |
| 118 | '%(vendor_partition_dir)s/provision-device') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 119 | |
Dan Shi | 12a4f66 | 2016-05-10 14:49:42 -0700 | [diff] [blame] | 120 | # Default timeout in minutes for fastboot commands. |
| 121 | DEFAULT_FASTBOOT_RETRY_TIMEOUT_MIN = 10 |
| 122 | |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 123 | # Default permissions for files/dirs copied from the device. |
| 124 | _DEFAULT_FILE_PERMS = 0o600 |
| 125 | _DEFAULT_DIR_PERMS = 0o700 |
| 126 | |
Dan Shi | 626d541 | 2016-05-16 16:05:13 -0700 | [diff] [blame] | 127 | # Constants for getprop return value for a given property. |
| 128 | PROPERTY_VALUE_TRUE = '1' |
| 129 | |
Dan Shi | c716ac6 | 2016-05-24 16:47:57 -0700 | [diff] [blame] | 130 | # Timeout used for retrying installing apk. After reinstall apk failed, we try |
| 131 | # to reboot the device and try again. |
| 132 | APK_INSTALL_TIMEOUT_MIN = 5 |
| 133 | |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 134 | # Directory where (non-Brillo) Android stores tombstone crash logs. |
| 135 | ANDROID_TOMBSTONE_CRASH_LOG_DIR = '/data/tombstones' |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 136 | # Directory where Brillo stores crash logs for native (non-Java) crashes. |
| 137 | BRILLO_NATIVE_CRASH_LOG_DIR = '/data/misc/crash_reporter/crash' |
| 138 | |
Dan Shi | 35a39be | 2016-07-25 00:20:31 -0700 | [diff] [blame] | 139 | # A specific string value to return when a timeout has occurred. |
| 140 | TIMEOUT_MSG = 'TIMEOUT_OCCURRED' |
| 141 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 142 | class AndroidInstallError(error.InstallError): |
| 143 | """Generic error for Android installation related exceptions.""" |
| 144 | |
| 145 | |
Simran Basi | 724b8a5 | 2013-09-30 11:19:31 -0700 | [diff] [blame] | 146 | class ADBHost(abstract_ssh.AbstractSSHHost): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 147 | """This class represents a host running an ADB server.""" |
| 148 | |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 149 | VERSION_PREFIX = provision.ANDROID_BUILD_VERSION_PREFIX |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 150 | _LABEL_FUNCTIONS = [] |
| 151 | _DETECTABLE_LABELS = [] |
| 152 | label_decorator = functools.partial(utils.add_label_detector, |
| 153 | _LABEL_FUNCTIONS, |
| 154 | _DETECTABLE_LABELS) |
| 155 | |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 156 | _parser = autoserv_parser.autoserv_parser |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 157 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 158 | # Minimum build id that supports server side packaging. Older builds may |
| 159 | # not have server side package built or with Autotest code change to support |
| 160 | # server-side packaging. |
| 161 | MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value( |
| 162 | 'AUTOSERV', 'min_launch_control_build_id_support_ssp', type=int) |
| 163 | |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 164 | @staticmethod |
| 165 | def check_host(host, timeout=10): |
| 166 | """ |
| 167 | Check if the given host is an adb host. |
| 168 | |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 169 | If SSH connectivity can't be established, check_host will try to use |
| 170 | user 'adb' as well. If SSH connectivity still can't be established |
| 171 | then the original SSH user is restored. |
| 172 | |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 173 | @param host: An ssh host representing a device. |
| 174 | @param timeout: The timeout for the run command. |
| 175 | |
| 176 | |
| 177 | @return: True if the host device has adb. |
| 178 | |
| 179 | @raises AutoservRunError: If the command failed. |
| 180 | @raises AutoservSSHTimeout: Ssh connection has timed out. |
| 181 | """ |
Dan Shi | 64e130f | 2015-12-16 14:45:44 -0800 | [diff] [blame] | 182 | # host object may not have user attribute if it's a LocalHost object. |
| 183 | current_user = host.user if hasattr(host, 'user') else None |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 184 | try: |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 185 | if not (host.hostname == 'localhost' or |
| 186 | host.verify_ssh_user_access()): |
Simran Basi | 1621c63 | 2015-10-14 12:22:23 -0700 | [diff] [blame] | 187 | host.user = 'adb' |
Simran Basi | 933c8af | 2015-04-29 14:05:07 -0700 | [diff] [blame] | 188 | result = host.run( |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 189 | 'test -f %s' % server_constants.ANDROID_TESTER_FILEFLAG, |
Simran Basi | 933c8af | 2015-04-29 14:05:07 -0700 | [diff] [blame] | 190 | timeout=timeout) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 191 | except (error.GenericHostRunError, error.AutoservSSHTimeout): |
Dan Shi | 64e130f | 2015-12-16 14:45:44 -0800 | [diff] [blame] | 192 | if current_user is not None: |
| 193 | host.user = current_user |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 194 | return False |
| 195 | return result.exit_status == 0 |
| 196 | |
| 197 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 198 | # TODO(garnold) Remove the 'serials' argument once all clients are made to |
| 199 | # not use it. |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 200 | def _initialize(self, hostname='localhost', serials=None, |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 201 | adb_serial=None, fastboot_serial=None, |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 202 | teststation=None, *args, **dargs): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 203 | """Initialize an ADB Host. |
| 204 | |
| 205 | This will create an ADB Host. Hostname should always refer to the |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 206 | test station connected to an Android DUT. This will be the DUT |
| 207 | to test with. If there are multiple, serial must be specified or an |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 208 | exception will be raised. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 209 | |
| 210 | @param hostname: Hostname of the machine running ADB. |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 211 | @param serials: DEPRECATED (to be removed) |
| 212 | @param adb_serial: An ADB device serial. If None, assume a single |
| 213 | device is attached (and fail otherwise). |
| 214 | @param fastboot_serial: A fastboot device serial. If None, defaults to |
| 215 | the ADB serial (or assumes a single device if |
| 216 | the latter is None). |
Kevin Cheng | 549beb4 | 2015-11-18 11:42:25 -0800 | [diff] [blame] | 217 | @param teststation: The teststation object ADBHost should use. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 218 | """ |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 219 | # Sets up the is_client_install_supported field. |
| 220 | super(ADBHost, self)._initialize(hostname=hostname, |
| 221 | is_client_install_supported=False, |
| 222 | *args, **dargs) |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 223 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 224 | self.tmp_dirs = [] |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 225 | self.labels = base_label.LabelRetriever(adb_label.ADB_LABELS) |
Kevin Cheng | 05ae2a4 | 2016-06-06 10:12:48 -0700 | [diff] [blame] | 226 | adb_serial = adb_serial or self._afe_host.attributes.get('serials') |
| 227 | fastboot_serial = (fastboot_serial or |
| 228 | self._afe_host.attributes.get('fastboot_serial')) |
| 229 | |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 230 | self.adb_serial = adb_serial |
Dan Shi | 3a011ed | 2016-04-26 12:26:53 -0700 | [diff] [blame] | 231 | if adb_serial: |
| 232 | adb_prefix = any(adb_serial.startswith(p) |
| 233 | for p in ADB_DEVICE_PREFIXES) |
| 234 | self.fastboot_serial = (fastboot_serial or |
| 235 | ('tcp:%s' % adb_serial.split(':')[0] if |
| 236 | ':' in adb_serial and not adb_prefix else adb_serial)) |
| 237 | self._use_tcpip = ':' in adb_serial and not adb_prefix |
| 238 | else: |
| 239 | self.fastboot_serial = fastboot_serial or adb_serial |
| 240 | self._use_tcpip = False |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 241 | self.teststation = (teststation if teststation |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 242 | else teststation_host.create_teststationhost( |
| 243 | hostname=hostname, |
| 244 | user=self.user, |
| 245 | password=self.password, |
| 246 | port=self.port |
| 247 | )) |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 248 | |
| 249 | msg ='Initializing ADB device on host: %s' % hostname |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 250 | if self.adb_serial: |
| 251 | msg += ', ADB serial: %s' % self.adb_serial |
| 252 | if self.fastboot_serial: |
| 253 | msg += ', fastboot serial: %s' % self.fastboot_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 254 | logging.debug(msg) |
| 255 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 256 | self._os_type = None |
| 257 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 258 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 259 | def _connect_over_tcpip_as_needed(self): |
| 260 | """Connect to the ADB device over TCP/IP if so configured.""" |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 261 | if not self._use_tcpip: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 262 | return |
| 263 | logging.debug('Connecting to device over TCP/IP') |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 264 | self.adb_run('connect %s' % self.adb_serial) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 265 | |
| 266 | |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 267 | def _restart_adbd_with_root_permissions(self): |
| 268 | """Restarts the adb daemon with root permissions.""" |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 269 | @retry.retry(error.GenericHostRunError, timeout_min=20/60.0, |
| 270 | delay_sec=1) |
Dan Shi | 922de30 | 2016-04-22 15:19:18 -0700 | [diff] [blame] | 271 | def run_adb_root(): |
| 272 | """Run command `adb root`.""" |
| 273 | self.adb_run('root') |
| 274 | |
| 275 | # adb command may flake with error "device not found". Retry the root |
| 276 | # command to reduce the chance of flake. |
| 277 | run_adb_root() |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 278 | # TODO(ralphnathan): Remove this sleep once b/19749057 is resolved. |
| 279 | time.sleep(1) |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 280 | self._connect_over_tcpip_as_needed() |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 281 | self.adb_run('wait-for-device') |
| 282 | |
| 283 | |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 284 | def _set_tcp_port(self): |
| 285 | """Ensure the device remains in tcp/ip mode after a reboot.""" |
| 286 | if not self._use_tcpip: |
| 287 | return |
| 288 | port = self.adb_serial.split(':')[-1] |
| 289 | self.run('setprop persist.adb.tcp.port %s' % port) |
| 290 | |
| 291 | |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 292 | def _reset_adbd_connection(self): |
| 293 | """Resets adbd connection to the device after a reboot/initialization""" |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 294 | self._connect_over_tcpip_as_needed() |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 295 | self._restart_adbd_with_root_permissions() |
| 296 | self._set_tcp_port() |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 297 | |
| 298 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 299 | # pylint: disable=missing-docstring |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 300 | def adb_run(self, command, **kwargs): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 301 | """Runs an adb command. |
| 302 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 303 | This command will launch on the test station. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 304 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 305 | Refer to _device_run method for docstring for parameters. |
| 306 | """ |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 307 | return self._device_run(ADB_CMD, command, **kwargs) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 308 | |
| 309 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 310 | # pylint: disable=missing-docstring |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 311 | def fastboot_run(self, command, **kwargs): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 312 | """Runs an fastboot command. |
| 313 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 314 | This command will launch on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 315 | |
| 316 | Refer to _device_run method for docstring for parameters. |
| 317 | """ |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 318 | return self._device_run(FASTBOOT_CMD, command, **kwargs) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 319 | |
| 320 | |
Dan Shi | 12a4f66 | 2016-05-10 14:49:42 -0700 | [diff] [blame] | 321 | # pylint: disable=missing-docstring |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 322 | @retry.retry(error.GenericHostRunError, |
Dan Shi | 12a4f66 | 2016-05-10 14:49:42 -0700 | [diff] [blame] | 323 | timeout_min=DEFAULT_FASTBOOT_RETRY_TIMEOUT_MIN) |
| 324 | def _fastboot_run_with_retry(self, command, **kwargs): |
| 325 | """Runs an fastboot command with retry. |
| 326 | |
| 327 | This command will launch on the test station. |
| 328 | |
| 329 | Refer to _device_run method for docstring for parameters. |
| 330 | """ |
| 331 | return self.fastboot_run(command, **kwargs) |
| 332 | |
| 333 | |
Simran Basi | e6c4f14 | 2016-06-23 17:10:04 -0700 | [diff] [blame] | 334 | def _log_adb_pid(self): |
| 335 | """Log the pid of adb server. |
| 336 | |
| 337 | adb's server is known to have bugs and randomly restart. BY logging |
| 338 | the server's pid it will allow us to better debug random adb failures. |
| 339 | """ |
| 340 | adb_pid = self.teststation.run('pgrep -f "adb.*server"') |
| 341 | logging.debug('ADB Server PID: %s', adb_pid.stdout) |
| 342 | |
| 343 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 344 | def _device_run(self, function, command, shell=False, |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 345 | timeout=3600, ignore_status=False, ignore_timeout=False, |
| 346 | stdout=utils.TEE_TO_LOGS, stderr=utils.TEE_TO_LOGS, |
| 347 | connect_timeout=30, options='', stdin=None, verbose=True, |
| 348 | require_sudo=False, args=()): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 349 | """Runs a command named `function` on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 350 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 351 | This command will launch on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 352 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 353 | @param command: Command to run. |
| 354 | @param shell: If true the command runs in the adb shell otherwise if |
| 355 | False it will be passed directly to adb. For example |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 356 | reboot with shell=False will call 'adb reboot'. This |
| 357 | option only applies to function adb. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 358 | @param timeout: Time limit in seconds before attempting to |
| 359 | kill the running process. The run() function |
| 360 | will take a few seconds longer than 'timeout' |
| 361 | to complete if it has to kill the process. |
| 362 | @param ignore_status: Do not raise an exception, no matter |
| 363 | what the exit code of the command is. |
| 364 | @param ignore_timeout: Bool True if command timeouts should be |
| 365 | ignored. Will return None on command timeout. |
| 366 | @param stdout: Redirect stdout. |
| 367 | @param stderr: Redirect stderr. |
| 368 | @param connect_timeout: Connection timeout (in seconds) |
| 369 | @param options: String with additional ssh command options |
| 370 | @param stdin: Stdin to pass (a string) to the executed command |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 371 | @param require_sudo: True to require sudo to run the command. Default is |
| 372 | False. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 373 | @param args: Sequence of strings to pass as arguments to command by |
| 374 | quoting them in " and escaping their contents if |
| 375 | necessary. |
| 376 | |
| 377 | @returns a CMDResult object. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 378 | """ |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 379 | if function == ADB_CMD: |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 380 | serial = self.adb_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 381 | elif function == FASTBOOT_CMD: |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 382 | serial = self.fastboot_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 383 | else: |
| 384 | raise NotImplementedError('Mode %s is not supported' % function) |
| 385 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 386 | if function != ADB_CMD and shell: |
| 387 | raise error.CmdError('shell option is only applicable to `adb`.') |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 388 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 389 | cmd = '%s%s ' % ('sudo -n ' if require_sudo else '', function) |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 390 | |
| 391 | if serial: |
| 392 | cmd += '-s %s ' % serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 393 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 394 | if shell: |
| 395 | cmd += '%s ' % SHELL_CMD |
| 396 | cmd += command |
| 397 | |
Simran Basi | e6c4f14 | 2016-06-23 17:10:04 -0700 | [diff] [blame] | 398 | self._log_adb_pid() |
| 399 | |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 400 | if verbose: |
| 401 | logging.debug('Command: %s', cmd) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 402 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 403 | return self.teststation.run(cmd, timeout=timeout, |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 404 | ignore_status=ignore_status, |
| 405 | ignore_timeout=ignore_timeout, stdout_tee=stdout, |
| 406 | stderr_tee=stderr, options=options, stdin=stdin, |
| 407 | connect_timeout=connect_timeout, args=args) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 408 | |
| 409 | |
Dan Shi | 8c51bda | 2016-05-26 12:21:02 -0700 | [diff] [blame] | 410 | def _run_output_with_retry(self, cmd): |
| 411 | """Call run_output method for the given command with retry. |
| 412 | |
Dan Shi | 6809412 | 2016-06-10 14:29:06 -0700 | [diff] [blame] | 413 | adb command can be flaky some time, and the command may fail or return |
| 414 | empty string. It may take several retries until a value can be returned. |
Dan Shi | 8c51bda | 2016-05-26 12:21:02 -0700 | [diff] [blame] | 415 | |
| 416 | @param cmd: The command to run. |
| 417 | |
| 418 | @return: Return value from the command after retry. |
| 419 | """ |
| 420 | try: |
| 421 | return client_utils.poll_for_condition( |
Dan Shi | 6809412 | 2016-06-10 14:29:06 -0700 | [diff] [blame] | 422 | lambda: self.run_output(cmd, ignore_status=True), |
Dan Shi | 8c51bda | 2016-05-26 12:21:02 -0700 | [diff] [blame] | 423 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 424 | sleep_interval=0.5, |
| 425 | desc='Get return value for command `%s`' % cmd) |
| 426 | except client_utils.TimeoutError: |
| 427 | return '' |
| 428 | |
| 429 | |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 430 | def get_board_name(self): |
| 431 | """Get the name of the board, e.g., shamu, dragonboard etc. |
| 432 | """ |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 433 | product = self.run_output('getprop %s' % BOARD_FILE) |
| 434 | return PRODUCT_TARGET_MAP.get(product, product) |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 435 | |
| 436 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 437 | @label_decorator() |
Christopher Wiley | 8e6b08e | 2013-10-11 12:34:26 -0700 | [diff] [blame] | 438 | def get_board(self): |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 439 | """Determine the correct board label for the device. |
| 440 | |
| 441 | @returns a string representing this device's board. |
| 442 | """ |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 443 | board = self.get_board_name() |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 444 | board_os = self.get_os_type() |
Kevin Cheng | 49f7b81 | 2015-12-15 15:24:23 -0800 | [diff] [blame] | 445 | return constants.BOARD_PREFIX + '-'.join([board_os, board]) |
Christopher Wiley | 8e6b08e | 2013-10-11 12:34:26 -0700 | [diff] [blame] | 446 | |
| 447 | |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 448 | def job_start(self): |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 449 | """Overload of parent which intentionally doesn't log certain files. |
Dan Shi | 2d279cf | 2016-05-27 22:06:10 +0000 | [diff] [blame] | 450 | |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 451 | The parent implementation attempts to log certain Linux files, such as |
| 452 | /var/log, which do not exist on Android, thus there is no call to the |
| 453 | parent's job_start(). The sync call is made so that logcat logs can be |
| 454 | approximately matched to server logs. |
Dan Shi | 2d279cf | 2016-05-27 22:06:10 +0000 | [diff] [blame] | 455 | """ |
Justin Giorgi | dd05a94 | 2016-07-05 20:53:12 -0700 | [diff] [blame] | 456 | # Try resetting the ADB daemon on the device, however if we are |
| 457 | # creating the host to do a repair job, the device maybe inaccesible |
| 458 | # via ADB. |
| 459 | try: |
| 460 | self._reset_adbd_connection() |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 461 | except error.GenericHostRunError as e: |
Justin Giorgi | dd05a94 | 2016-07-05 20:53:12 -0700 | [diff] [blame] | 462 | logging.error('Unable to reset the device adb daemon connection: ' |
| 463 | '%s.', e) |
| 464 | |
Simran Basi | d6b4979 | 2016-06-07 17:22:03 -0700 | [diff] [blame] | 465 | if self.is_up(): |
| 466 | self._sync_time() |
Bryan Lewandowski | 8c96bb2 | 2016-07-19 12:56:03 -0700 | [diff] [blame] | 467 | self._enable_native_crash_logging() |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 468 | |
| 469 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 470 | def run(self, command, timeout=3600, ignore_status=False, |
| 471 | ignore_timeout=False, stdout_tee=utils.TEE_TO_LOGS, |
| 472 | stderr_tee=utils.TEE_TO_LOGS, connect_timeout=30, options='', |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 473 | stdin=None, verbose=True, args=()): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 474 | """Run a command on the adb device. |
| 475 | |
| 476 | The command given will be ran directly on the adb device; for example |
| 477 | 'ls' will be ran as: 'abd shell ls' |
| 478 | |
| 479 | @param command: The command line string. |
| 480 | @param timeout: Time limit in seconds before attempting to |
| 481 | kill the running process. The run() function |
| 482 | will take a few seconds longer than 'timeout' |
| 483 | to complete if it has to kill the process. |
| 484 | @param ignore_status: Do not raise an exception, no matter |
| 485 | what the exit code of the command is. |
| 486 | @param ignore_timeout: Bool True if command timeouts should be |
| 487 | ignored. Will return None on command timeout. |
| 488 | @param stdout_tee: Redirect stdout. |
| 489 | @param stderr_tee: Redirect stderr. |
| 490 | @param connect_timeout: Connection timeout (in seconds). |
| 491 | @param options: String with additional ssh command options. |
| 492 | @param stdin: Stdin to pass (a string) to the executed command |
| 493 | @param args: Sequence of strings to pass as arguments to command by |
| 494 | quoting them in " and escaping their contents if |
| 495 | necessary. |
| 496 | |
| 497 | @returns A CMDResult object or None if the call timed out and |
| 498 | ignore_timeout is True. |
| 499 | |
| 500 | @raises AutoservRunError: If the command failed. |
| 501 | @raises AutoservSSHTimeout: Ssh connection has timed out. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 502 | """ |
Filipe Brandenburger | 68a8007 | 2015-07-14 10:39:33 -0700 | [diff] [blame] | 503 | command = ('"%s; echo %s:\$?"' % |
Dan Shi | 35a39be | 2016-07-25 00:20:31 -0700 | [diff] [blame] | 504 | (utils.sh_escape(command), CMD_OUTPUT_PREFIX)) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 505 | |
Dan Shi | 35a39be | 2016-07-25 00:20:31 -0700 | [diff] [blame] | 506 | def _run(): |
| 507 | """Run the command and try to parse the exit code. |
| 508 | """ |
| 509 | result = self.adb_run( |
| 510 | command, shell=True, timeout=timeout, |
| 511 | ignore_status=ignore_status, ignore_timeout=ignore_timeout, |
| 512 | stdout=stdout_tee, stderr=stderr_tee, |
| 513 | connect_timeout=connect_timeout, options=options, |
| 514 | stdin=stdin, verbose=verbose, args=args) |
| 515 | if not result: |
| 516 | # In case of timeouts. Set the return to a specific string |
| 517 | # value. That way the caller of poll_for_condition knows |
| 518 | # a timeout occurs and should return None. Return None here will |
| 519 | # lead to the command to be retried. |
| 520 | return TIMEOUT_MSG |
| 521 | parse_output = re.match(CMD_OUTPUT_REGEX, result.stdout) |
| 522 | if not parse_output and not ignore_status: |
| 523 | logging.error('Failed to parse the exit code for command: `%s`.' |
| 524 | ' result: `%s`', command, result.stdout) |
| 525 | return None |
| 526 | elif parse_output: |
| 527 | result.stdout = parse_output.group('OUTPUT') |
| 528 | result.exit_status = int(parse_output.group('EXIT_CODE')) |
| 529 | if result.exit_status != 0 and not ignore_status: |
| 530 | raise error.AutoservRunError(command, result) |
| 531 | return result |
| 532 | |
| 533 | result = client_utils.poll_for_condition( |
| 534 | lambda: _run(), |
| 535 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 536 | sleep_interval=0.5, |
| 537 | desc='Run command `%s`' % command) |
| 538 | return None if result == TIMEOUT_MSG else result |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 539 | |
| 540 | |
Dan Shi | cf7d256 | 2016-06-16 14:08:23 -0700 | [diff] [blame] | 541 | def check_boot_to_adb_complete(self, exception_type=error.TimeoutException): |
| 542 | """Check if the device has finished booting and accessible by adb. |
| 543 | |
| 544 | @param exception_type: Type of exception to raise. Default is set to |
| 545 | error.TimeoutException for retry. |
| 546 | |
| 547 | @raise exception_type: If the device has not finished booting yet, raise |
| 548 | an exception of type `exception_type`. |
| 549 | """ |
| 550 | bootcomplete = self._run_output_with_retry('getprop dev.bootcomplete') |
| 551 | if bootcomplete != PROPERTY_VALUE_TRUE: |
| 552 | raise exception_type('dev.bootcomplete is %s.' % bootcomplete) |
| 553 | if self.get_os_type() == OS_TYPE_ANDROID: |
| 554 | boot_completed = self._run_output_with_retry( |
| 555 | 'getprop sys.boot_completed') |
| 556 | if boot_completed != PROPERTY_VALUE_TRUE: |
| 557 | raise exception_type('sys.boot_completed is %s.' % |
| 558 | boot_completed) |
| 559 | |
| 560 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 561 | def wait_up(self, timeout=DEFAULT_WAIT_UP_TIME_SECONDS, command=ADB_CMD): |
| 562 | """Wait until the remote host is up or the timeout expires. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 563 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 564 | Overrides wait_down from AbstractSSHHost. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 565 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 566 | @param timeout: Time limit in seconds before returning even if the host |
| 567 | is not up. |
| 568 | @param command: The command used to test if a device is up, i.e., |
| 569 | accessible by the given command. Default is set to `adb`. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 570 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 571 | @returns True if the host was found to be up before the timeout expires, |
| 572 | False otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 573 | """ |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 574 | @retry.retry(error.TimeoutException, timeout_min=timeout/60.0, |
| 575 | delay_sec=1) |
| 576 | def _wait_up(): |
| 577 | if not self.is_up(command=command): |
| 578 | raise error.TimeoutException('Device is still down.') |
Dan Shi | cf7d256 | 2016-06-16 14:08:23 -0700 | [diff] [blame] | 579 | if command == ADB_CMD: |
| 580 | self.check_boot_to_adb_complete() |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 581 | return True |
| 582 | |
| 583 | try: |
| 584 | _wait_up() |
| 585 | logging.debug('Host %s is now up, and can be accessed by %s.', |
| 586 | self.hostname, command) |
| 587 | return True |
| 588 | except error.TimeoutException: |
| 589 | logging.debug('Host %s is still down after waiting %d seconds', |
| 590 | self.hostname, timeout) |
| 591 | return False |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 592 | |
| 593 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 594 | def wait_down(self, timeout=DEFAULT_WAIT_DOWN_TIME_SECONDS, |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 595 | warning_timer=None, old_boot_id=None, command=ADB_CMD, |
| 596 | boot_id=None): |
| 597 | """Wait till the host goes down. |
| 598 | |
| 599 | Return when the host is down (not accessible via the command) OR when |
| 600 | the device's boot_id changes (if a boot_id was provided). |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 601 | |
| 602 | Overrides wait_down from AbstractSSHHost. |
| 603 | |
| 604 | @param timeout: Time in seconds to wait for the host to go down. |
| 605 | @param warning_timer: Time limit in seconds that will generate |
| 606 | a warning if the host is not down yet. |
| 607 | Currently ignored. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 608 | @param old_boot_id: Not applicable for adb_host. |
| 609 | @param command: `adb`, test if the device can be accessed by adb |
| 610 | command, or `fastboot`, test if the device can be accessed by |
| 611 | fastboot command. Default is set to `adb`. |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 612 | @param boot_id: UUID of previous boot (consider the device down when the |
| 613 | boot_id changes from this value). Ignored if None. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 614 | |
| 615 | @returns True if the device goes down before the timeout, False |
| 616 | otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 617 | """ |
| 618 | @retry.retry(error.TimeoutException, timeout_min=timeout/60.0, |
| 619 | delay_sec=1) |
| 620 | def _wait_down(): |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 621 | up = self.is_up(command=command) |
| 622 | if not up: |
| 623 | return True |
| 624 | if boot_id: |
| 625 | try: |
| 626 | new_boot_id = self.get_boot_id() |
| 627 | if new_boot_id != boot_id: |
| 628 | return True |
Justin Giorgi | f6ffe0c | 2016-08-16 10:12:47 -0700 | [diff] [blame] | 629 | except error.GenericHostRunError: |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 630 | pass |
| 631 | raise error.TimeoutException('Device is still up.') |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 632 | |
| 633 | try: |
| 634 | _wait_down() |
| 635 | logging.debug('Host %s is now down', self.hostname) |
| 636 | return True |
| 637 | except error.TimeoutException: |
| 638 | logging.debug('Host %s is still up after waiting %d seconds', |
| 639 | self.hostname, timeout) |
| 640 | return False |
| 641 | |
| 642 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 643 | def reboot(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 644 | """Reboot the android device via adb. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 645 | |
| 646 | @raises AutoservRebootError if reboot failed. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 647 | """ |
| 648 | # Not calling super.reboot() as we want to reboot the ADB device not |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 649 | # the test station we are running ADB on. |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 650 | boot_id = self.get_boot_id() |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 651 | self.adb_run('reboot', timeout=10, ignore_timeout=True) |
Justin Giorgi | ea3de26 | 2016-08-08 15:28:57 -0700 | [diff] [blame] | 652 | if not self.wait_down(boot_id=boot_id): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 653 | raise error.AutoservRebootError( |
| 654 | 'ADB Device is still up after reboot') |
| 655 | if not self.wait_up(): |
| 656 | raise error.AutoservRebootError( |
| 657 | 'ADB Device failed to return from reboot.') |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 658 | self._reset_adbd_connection() |
Roshan Pius | 50c1f9c | 2015-11-30 11:37:49 -0800 | [diff] [blame] | 659 | |
| 660 | |
Alexandru Branciog | 969ff7c | 2016-03-30 14:07:15 +0300 | [diff] [blame] | 661 | def fastboot_reboot(self): |
| 662 | """Do a fastboot reboot to go back to adb. |
| 663 | |
| 664 | @raises AutoservRebootError if reboot failed. |
| 665 | """ |
| 666 | self.fastboot_run('reboot') |
| 667 | if not self.wait_down(command=FASTBOOT_CMD): |
| 668 | raise error.AutoservRebootError( |
| 669 | 'Device is still in fastboot mode after reboot') |
| 670 | if not self.wait_up(): |
| 671 | raise error.AutoservRebootError( |
| 672 | 'Device failed to boot to adb after fastboot reboot.') |
| 673 | self._reset_adbd_connection() |
| 674 | |
| 675 | |
Ralph Nathan | b45eb67 | 2015-11-18 20:04:39 -0800 | [diff] [blame] | 676 | def remount(self): |
| 677 | """Remounts paritions on the device read-write. |
| 678 | |
| 679 | Specifically, the /system, /vendor (if present) and /oem (if present) |
| 680 | partitions on the device are remounted read-write. |
| 681 | """ |
Ralph Nathan | b45eb67 | 2015-11-18 20:04:39 -0800 | [diff] [blame] | 682 | self.adb_run('remount') |
| 683 | |
| 684 | |
Kevin Cheng | 549beb4 | 2015-11-18 11:42:25 -0800 | [diff] [blame] | 685 | @staticmethod |
| 686 | def parse_device_serials(devices_output): |
| 687 | """Return a list of parsed serials from the output. |
| 688 | |
| 689 | @param devices_output: Output from either an adb or fastboot command. |
| 690 | |
| 691 | @returns List of device serials |
| 692 | """ |
| 693 | devices = [] |
| 694 | for line in devices_output.splitlines(): |
| 695 | match = re.search(DEVICE_FINDER_REGEX, line) |
| 696 | if match: |
| 697 | serial = match.group('SERIAL') |
| 698 | if serial == DEVICE_NO_SERIAL_MSG or re.match(r'^\?+$', serial): |
| 699 | serial = DEVICE_NO_SERIAL_TAG |
| 700 | logging.debug('Found Device: %s', serial) |
| 701 | devices.append(serial) |
| 702 | return devices |
| 703 | |
| 704 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 705 | def _get_devices(self, use_adb): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 706 | """Get a list of devices currently attached to the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 707 | |
| 708 | @params use_adb: True to get adb accessible devices. Set to False to |
| 709 | get fastboot accessible devices. |
| 710 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 711 | @returns a list of devices attached to the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 712 | """ |
| 713 | if use_adb: |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 714 | result = self.adb_run('devices').stdout |
| 715 | if self.adb_serial and self.adb_serial not in result: |
| 716 | self._connect_over_tcpip_as_needed() |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 717 | else: |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 718 | result = self.fastboot_run('devices').stdout |
| 719 | if (self.fastboot_serial and |
| 720 | self.fastboot_serial not in result): |
| 721 | # fastboot devices won't list the devices using TCP |
| 722 | try: |
| 723 | if 'product' in self.fastboot_run('getvar product', |
| 724 | timeout=2).stderr: |
| 725 | result += '\n%s\tfastboot' % self.fastboot_serial |
Kevin Cheng | cfcb2cf | 2016-04-13 10:04:36 -0700 | [diff] [blame] | 726 | # The main reason we do a general Exception catch here instead |
| 727 | # of setting ignore_timeout/status to True is because even when |
| 728 | # the fastboot process has been nuked, it still stays around and |
| 729 | # so bgjob wants to warn us of this and tries to read the |
| 730 | # /proc/<pid>/stack file which then promptly returns an |
| 731 | # 'Operation not permitted' error since we're running as moblab |
| 732 | # and we don't have permission to read those files. |
| 733 | except Exception: |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 734 | pass |
| 735 | return self.parse_device_serials(result) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 736 | |
| 737 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 738 | def adb_devices(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 739 | """Get a list of devices currently attached to the test station and |
| 740 | accessible with the adb command.""" |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 741 | devices = self._get_devices(use_adb=True) |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 742 | if self.adb_serial is None and len(devices) > 1: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 743 | raise error.AutoservError( |
| 744 | 'Not given ADB serial but multiple devices detected') |
| 745 | return devices |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 746 | |
| 747 | |
| 748 | def fastboot_devices(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 749 | """Get a list of devices currently attached to the test station and |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 750 | accessible by fastboot command. |
| 751 | """ |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 752 | devices = self._get_devices(use_adb=False) |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 753 | if self.fastboot_serial is None and len(devices) > 1: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 754 | raise error.AutoservError( |
| 755 | 'Not given fastboot serial but multiple devices detected') |
| 756 | return devices |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 757 | |
| 758 | |
| 759 | def is_up(self, timeout=0, command=ADB_CMD): |
| 760 | """Determine if the specified adb device is up with expected mode. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 761 | |
| 762 | @param timeout: Not currently used. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 763 | @param command: `adb`, the device can be accessed by adb command, |
| 764 | or `fastboot`, the device can be accessed by fastboot command. |
| 765 | Default is set to `adb`. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 766 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 767 | @returns True if the device is detectable by given command, False |
| 768 | otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 769 | |
| 770 | """ |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 771 | if command == ADB_CMD: |
| 772 | devices = self.adb_devices() |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 773 | serial = self.adb_serial |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 774 | # ADB has a device state, if the device is not online, no |
| 775 | # subsequent ADB command will complete. |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 776 | # DUT with single device connected may not have adb_serial set. |
| 777 | # Therefore, skip checking if serial is in the list of adb devices |
| 778 | # if self.adb_serial is not set. |
| 779 | if (serial and serial not in devices) or not self.is_device_ready(): |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 780 | logging.debug('Waiting for device to enter the ready state.') |
| 781 | return False |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 782 | elif command == FASTBOOT_CMD: |
| 783 | devices = self.fastboot_devices() |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 784 | serial = self.fastboot_serial |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 785 | else: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 786 | raise NotImplementedError('Mode %s is not supported' % command) |
| 787 | |
| 788 | return bool(devices and (not serial or serial in devices)) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 789 | |
| 790 | |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 791 | def stop_loggers(self): |
| 792 | """Inherited stop_loggers function. |
| 793 | |
| 794 | Calls parent function and captures logcat, since the end of the run |
| 795 | is logically the end/stop of the logcat log. |
| 796 | """ |
| 797 | super(ADBHost, self).stop_loggers() |
| 798 | # Record logcat log to a temporary file on the teststation. |
| 799 | tmp_dir = self.teststation.get_tmp_dir() |
Simran Basi | 242f8de | 2016-06-08 14:23:43 -0700 | [diff] [blame] | 800 | logcat_filename = LOGCAT_FILE_FMT % self.adb_serial |
| 801 | teststation_filename = os.path.join(tmp_dir, logcat_filename) |
Bryan Lewandowski | ea4a06d | 2016-06-03 11:25:54 -0700 | [diff] [blame] | 802 | try: |
| 803 | self.adb_run('logcat -v time -d > "%s"' % (teststation_filename), |
| 804 | timeout=20) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 805 | except (error.GenericHostRunError, error.AutoservSSHTimeout, |
| 806 | error.CmdTimeoutError): |
Bryan Lewandowski | ea4a06d | 2016-06-03 11:25:54 -0700 | [diff] [blame] | 807 | return |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 808 | # Copy-back the log to the drone's results directory. |
Simran Basi | 242f8de | 2016-06-08 14:23:43 -0700 | [diff] [blame] | 809 | results_logcat_filename = os.path.join(self.job.resultdir, |
| 810 | logcat_filename) |
| 811 | self.teststation.get_file(teststation_filename, |
| 812 | results_logcat_filename) |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 813 | try: |
| 814 | self.teststation.run('rm -rf %s' % tmp_dir) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 815 | except (error.GenericHostRunError, error.AutoservSSHTimeout) as e: |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 816 | logging.warn('failed to remove dir %s: %s', tmp_dir, e) |
| 817 | |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 818 | self._collect_crash_logs() |
| 819 | |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 820 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 821 | def close(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 822 | """Close the ADBHost object. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 823 | |
| 824 | Called as the test ends. Will return the device to USB mode and kill |
| 825 | the ADB server. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 826 | """ |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 827 | # TODO(sbasi) Originally, we would kill the server after each test to |
| 828 | # reduce the opportunity for bad server state to hang around. |
| 829 | # Unfortunately, there is a period of time after each kill during which |
| 830 | # the Android device becomes unusable, and if we start the next test |
| 831 | # too quickly, we'll get an error complaining about no ADB device |
| 832 | # attached. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 833 | #self.adb_run('kill-server') |
Roshan Pius | 3994260 | 2015-12-17 13:42:07 -0800 | [diff] [blame] | 834 | # |close| the associated teststation as well. |
| 835 | self.teststation.close() |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 836 | return super(ADBHost, self).close() |
Kris Rambish | 60461dc | 2014-03-11 11:10:18 -0700 | [diff] [blame] | 837 | |
| 838 | |
| 839 | def syslog(self, message, tag='autotest'): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 840 | """Logs a message to syslog on the device. |
Kris Rambish | 60461dc | 2014-03-11 11:10:18 -0700 | [diff] [blame] | 841 | |
| 842 | @param message String message to log into syslog |
| 843 | @param tag String tag prefix for syslog |
| 844 | |
| 845 | """ |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 846 | self.run('log -t "%s" "%s"' % (tag, message)) |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 847 | |
| 848 | |
| 849 | def get_autodir(self): |
| 850 | """Return the directory to install autotest for client side tests.""" |
| 851 | return '/data/autotest' |
| 852 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 853 | |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 854 | def is_device_ready(self): |
| 855 | """Return the if the device is ready for ADB commands.""" |
Dan Shi | 7075f55 | 2016-04-21 15:42:41 -0700 | [diff] [blame] | 856 | try: |
| 857 | # Retry to avoid possible flakes. |
| 858 | is_ready = client_utils.poll_for_condition( |
| 859 | lambda: self.adb_run('get-state').stdout.strip() == 'device', |
| 860 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, sleep_interval=1, |
| 861 | desc='Waiting for device state to be `device`') |
| 862 | except client_utils.TimeoutError: |
| 863 | is_ready = False |
| 864 | |
| 865 | logging.debug('Device state is %sready', '' if is_ready else 'NOT ') |
| 866 | return is_ready |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 867 | |
| 868 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 869 | def verify_connectivity(self): |
| 870 | """Verify we can connect to the device.""" |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 871 | if not self.is_device_ready(): |
| 872 | raise error.AutoservHostError('device state is not in the ' |
| 873 | '\'device\' state.') |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 874 | |
| 875 | |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 876 | def verify_software(self): |
| 877 | """Verify working software on an adb_host. |
| 878 | |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 879 | TODO (crbug.com/532222): Actually implement this method. |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 880 | """ |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 881 | # Check if adb and fastboot are present. |
| 882 | self.teststation.run('which adb') |
| 883 | self.teststation.run('which fastboot') |
| 884 | self.teststation.run('which unzip') |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 885 | |
Dan Shi | 626d541 | 2016-05-16 16:05:13 -0700 | [diff] [blame] | 886 | # Apply checks only for Android device. |
Dan Shi | 1e2a98a | 2016-05-18 12:08:08 -0700 | [diff] [blame] | 887 | if self.get_os_type() == OS_TYPE_ANDROID: |
Dan Shi | 626d541 | 2016-05-16 16:05:13 -0700 | [diff] [blame] | 888 | # Make sure ro.boot.hardware and ro.build.product match. |
Dan Shi | 8c51bda | 2016-05-26 12:21:02 -0700 | [diff] [blame] | 889 | hardware = self._run_output_with_retry('getprop ro.boot.hardware') |
| 890 | product = self._run_output_with_retry('getprop ro.build.product') |
Dan Shi | 1e2a98a | 2016-05-18 12:08:08 -0700 | [diff] [blame] | 891 | if hardware != product: |
| 892 | raise error.AutoservHostError('ro.boot.hardware: %s does not ' |
| 893 | 'match to ro.build.product: %s' % |
| 894 | (hardware, product)) |
| 895 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 896 | |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 897 | def verify_job_repo_url(self, tag=''): |
| 898 | """Make sure job_repo_url of this host is valid. |
| 899 | |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 900 | TODO (crbug.com/532223): Actually implement this method. |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 901 | |
| 902 | @param tag: The tag from the server job, in the format |
| 903 | <job_id>-<user>/<hostname>, or <hostless> for a server job. |
| 904 | """ |
| 905 | return |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 906 | |
| 907 | |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 908 | def repair(self): |
| 909 | """Attempt to get the DUT to pass `self.verify()`.""" |
| 910 | try: |
| 911 | self.ensure_adb_mode(timeout=30) |
| 912 | return |
| 913 | except error.AutoservError as e: |
| 914 | logging.error(e) |
| 915 | logging.debug('Verifying the device is accessible via fastboot.') |
| 916 | self.ensure_bootloader_mode() |
| 917 | if not self.job.run_test( |
| 918 | 'provision_AndroidUpdate', host=self, value=None, |
| 919 | force=True, repair=True): |
| 920 | raise error.AutoservRepairTotalFailure( |
| 921 | 'Unable to repair the device.') |
| 922 | |
| 923 | |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 924 | def send_file(self, source, dest, delete_dest=False, |
| 925 | preserve_symlinks=False): |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 926 | """Copy files from the drone to the device. |
| 927 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 928 | Just a note, there is the possibility the test station is localhost |
| 929 | which makes some of these steps redundant (e.g. creating tmp dir) but |
| 930 | that scenario will undoubtedly be a development scenario (test station |
| 931 | is also the moblab) and not the typical live test running scenario so |
| 932 | the redundancy I think is harmless. |
| 933 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 934 | @param source: The file/directory on the drone to send to the device. |
| 935 | @param dest: The destination path on the device to copy to. |
| 936 | @param delete_dest: A flag set to choose whether or not to delete |
| 937 | dest on the device if it exists. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 938 | @param preserve_symlinks: Controls if symlinks on the source will be |
| 939 | copied as such on the destination or |
| 940 | transformed into the referenced |
| 941 | file/directory. |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 942 | """ |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 943 | # If we need to preserve symlinks, let's check if the source is a |
| 944 | # symlink itself and if so, just create it on the device. |
| 945 | if preserve_symlinks: |
| 946 | symlink_target = None |
| 947 | try: |
| 948 | symlink_target = os.readlink(source) |
| 949 | except OSError: |
| 950 | # Guess it's not a symlink. |
| 951 | pass |
| 952 | |
| 953 | if symlink_target is not None: |
| 954 | # Once we create the symlink, let's get out of here. |
| 955 | self.run('ln -s %s %s' % (symlink_target, dest)) |
| 956 | return |
| 957 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 958 | # Stage the files on the test station. |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 959 | tmp_dir = self.teststation.get_tmp_dir() |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 960 | src_path = os.path.join(tmp_dir, os.path.basename(dest)) |
| 961 | # Now copy the file over to the test station so you can reference the |
| 962 | # file in the push command. |
| 963 | self.teststation.send_file(source, src_path, |
| 964 | preserve_symlinks=preserve_symlinks) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 965 | |
| 966 | if delete_dest: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 967 | self.run('rm -rf %s' % dest) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 968 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 969 | self.adb_run('push %s %s' % (src_path, dest)) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 970 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 971 | # Cleanup the test station. |
| 972 | try: |
| 973 | self.teststation.run('rm -rf %s' % tmp_dir) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 974 | except (error.GenericHostRunError, error.AutoservSSHTimeout) as e: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 975 | logging.warn('failed to remove dir %s: %s', tmp_dir, e) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 976 | |
| 977 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 978 | def _get_file_info(self, dest): |
| 979 | """Get permission and possible symlink info about file on the device. |
| 980 | |
| 981 | These files are on the device so we only have shell commands (via adb) |
| 982 | to get the info we want. We'll use 'ls' to get it all. |
| 983 | |
| 984 | @param dest: File to get info about. |
| 985 | |
| 986 | @returns a dict of the file permissions and symlink. |
| 987 | """ |
Kevin Cheng | aaabd0c | 2015-11-10 16:05:04 -0800 | [diff] [blame] | 988 | # Grab file info. |
Casey Dahlin | f6bc8ed | 2016-04-13 14:33:48 -0700 | [diff] [blame] | 989 | file_info = self.run_output('ls -ld %s' % dest) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 990 | symlink = None |
| 991 | perms = 0 |
| 992 | match = re.match(FILE_INFO_REGEX, file_info) |
| 993 | if match: |
| 994 | # Check if it's a symlink and grab the linked dest if it is. |
| 995 | if match.group('TYPE') == 'l': |
| 996 | symlink_match = re.match(FILE_SYMLINK_REGEX, file_info) |
| 997 | if symlink_match: |
| 998 | symlink = symlink_match.group('SYMLINK') |
| 999 | |
| 1000 | # Set the perms. |
| 1001 | for perm, perm_flag in zip(match.group('PERMS'), FILE_PERMS_FLAGS): |
| 1002 | if perm != '-': |
| 1003 | perms |= perm_flag |
| 1004 | |
| 1005 | return {'perms': perms, |
| 1006 | 'symlink': symlink} |
| 1007 | |
| 1008 | |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 1009 | def get_file(self, source, dest, delete_dest=False, preserve_perm=True, |
| 1010 | preserve_symlinks=False): |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1011 | """Copy files from the device to the drone. |
| 1012 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1013 | Just a note, there is the possibility the test station is localhost |
| 1014 | which makes some of these steps redundant (e.g. creating tmp dir) but |
| 1015 | that scenario will undoubtedly be a development scenario (test station |
| 1016 | is also the moblab) and not the typical live test running scenario so |
| 1017 | the redundancy I think is harmless. |
| 1018 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1019 | @param source: The file/directory on the device to copy back to the |
| 1020 | drone. |
| 1021 | @param dest: The destination path on the drone to copy to. |
| 1022 | @param delete_dest: A flag set to choose whether or not to delete |
| 1023 | dest on the drone if it exists. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 1024 | @param preserve_perm: Tells get_file() to try to preserve the sources |
| 1025 | permissions on files and dirs. |
| 1026 | @param preserve_symlinks: Try to preserve symlinks instead of |
| 1027 | transforming them into files/dirs on copy. |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1028 | """ |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 1029 | # Stage the files on the test station under teststation_temp_dir. |
| 1030 | teststation_temp_dir = self.teststation.get_tmp_dir() |
| 1031 | teststation_dest = os.path.join(teststation_temp_dir, |
| 1032 | os.path.basename(source)) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1033 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 1034 | source_info = {} |
| 1035 | if preserve_symlinks or preserve_perm: |
| 1036 | source_info = self._get_file_info(source) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1037 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 1038 | # If we want to preserve symlinks, just create it here, otherwise pull |
| 1039 | # the file off the device. |
Casey Dahlin | f6bc8ed | 2016-04-13 14:33:48 -0700 | [diff] [blame] | 1040 | # |
| 1041 | # TODO(sadmac): Directories containing symlinks won't behave as |
| 1042 | # expected. |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 1043 | if preserve_symlinks and source_info['symlink']: |
| 1044 | os.symlink(source_info['symlink'], dest) |
| 1045 | else: |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 1046 | self.adb_run('pull %s %s' % (source, teststation_temp_dir)) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 1047 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1048 | # Copy over the file from the test station and clean up. |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 1049 | self.teststation.get_file(teststation_dest, dest, |
| 1050 | delete_dest=delete_dest) |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1051 | try: |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 1052 | self.teststation.run('rm -rf %s' % teststation_temp_dir) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1053 | except (error.GenericHostRunError, error.AutoservSSHTimeout) as e: |
David Pursell | e01548b | 2016-05-11 10:00:42 -0700 | [diff] [blame] | 1054 | logging.warn('failed to remove dir %s: %s', |
| 1055 | teststation_temp_dir, e) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 1056 | |
David Pursell | 788b1b5 | 2016-05-19 09:03:31 -0700 | [diff] [blame] | 1057 | # Source will be copied under dest if either: |
| 1058 | # 1. Source is a directory and doesn't end with /. |
| 1059 | # 2. Source is a file and dest is a directory. |
Bindu Mahadev | 594a80e | 2016-06-01 10:39:49 -0700 | [diff] [blame] | 1060 | command = '[ -d %s ]' % source |
| 1061 | source_is_dir = self.run(command, |
David Pursell | 788b1b5 | 2016-05-19 09:03:31 -0700 | [diff] [blame] | 1062 | ignore_status=True).exit_status == 0 |
David Pursell | 2e28393 | 2016-08-12 10:48:40 -0700 | [diff] [blame] | 1063 | logging.debug('%s on the device %s a directory', source, |
| 1064 | 'is' if source_is_dir else 'is not') |
Bindu Mahadev | 594a80e | 2016-06-01 10:39:49 -0700 | [diff] [blame] | 1065 | |
David Pursell | 788b1b5 | 2016-05-19 09:03:31 -0700 | [diff] [blame] | 1066 | if ((source_is_dir and not source.endswith(os.sep)) or |
| 1067 | (not source_is_dir and os.path.isdir(dest))): |
| 1068 | receive_path = os.path.join(dest, os.path.basename(source)) |
| 1069 | else: |
| 1070 | receive_path = dest |
Casey Dahlin | f6bc8ed | 2016-04-13 14:33:48 -0700 | [diff] [blame] | 1071 | |
David Pursell | 2e28393 | 2016-08-12 10:48:40 -0700 | [diff] [blame] | 1072 | if not os.path.exists(receive_path): |
| 1073 | logging.warning('Expected file %s does not exist; skipping' |
| 1074 | ' permissions copy', receive_path) |
| 1075 | return |
| 1076 | |
David Pursell | 788b1b5 | 2016-05-19 09:03:31 -0700 | [diff] [blame] | 1077 | # Set the permissions of the received file/dirs. |
| 1078 | if os.path.isdir(receive_path): |
| 1079 | for root, _dirs, files in os.walk(receive_path): |
| 1080 | def process(rel_path, default_perm): |
| 1081 | info = self._get_file_info(os.path.join(source, |
| 1082 | rel_path)) |
| 1083 | if info['perms'] != 0: |
| 1084 | target = os.path.join(receive_path, rel_path) |
| 1085 | if preserve_perm: |
| 1086 | os.chmod(target, info['perms']) |
| 1087 | else: |
| 1088 | os.chmod(target, default_perm) |
Casey Dahlin | f6bc8ed | 2016-04-13 14:33:48 -0700 | [diff] [blame] | 1089 | |
David Pursell | 788b1b5 | 2016-05-19 09:03:31 -0700 | [diff] [blame] | 1090 | rel_root = os.path.relpath(root, receive_path) |
| 1091 | process(rel_root, _DEFAULT_DIR_PERMS) |
| 1092 | for f in files: |
| 1093 | process(os.path.join(rel_root, f), _DEFAULT_FILE_PERMS) |
| 1094 | elif preserve_perm: |
| 1095 | os.chmod(receive_path, source_info['perms']) |
| 1096 | else: |
| 1097 | os.chmod(receive_path, _DEFAULT_FILE_PERMS) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 1098 | |
| 1099 | |
| 1100 | def get_release_version(self): |
| 1101 | """Get the release version from the RELEASE_FILE on the device. |
| 1102 | |
| 1103 | @returns The release string in the RELEASE_FILE. |
| 1104 | |
| 1105 | """ |
| 1106 | return self.run_output('getprop %s' % RELEASE_FILE) |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 1107 | |
| 1108 | |
| 1109 | def get_tmp_dir(self, parent=''): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1110 | """Return a suitable temporary directory on the device. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 1111 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1112 | We ensure this is a subdirectory of /data/local/tmp. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 1113 | |
| 1114 | @param parent: Parent directory of the returned tmp dir. |
| 1115 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 1116 | @returns a path to the temp directory on the host. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 1117 | """ |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1118 | # TODO(kevcheng): Refactor the cleanup of tmp dir to be inherited |
| 1119 | # from the parent. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 1120 | if not parent.startswith(TMP_DIR): |
| 1121 | parent = os.path.join(TMP_DIR, parent.lstrip(os.path.sep)) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1122 | self.run('mkdir -p %s' % parent) |
| 1123 | tmp_dir = self.run_output('mktemp -d -p %s' % parent) |
| 1124 | self.tmp_dirs.append(tmp_dir) |
| 1125 | return tmp_dir |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 1126 | |
| 1127 | |
| 1128 | def get_platform(self): |
| 1129 | """Determine the correct platform label for this host. |
| 1130 | |
| 1131 | TODO (crbug.com/536250): Figure out what we want to do for adb_host's |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1132 | get_platform. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 1133 | |
| 1134 | @returns a string representing this host's platform. |
| 1135 | """ |
| 1136 | return 'adb' |
| 1137 | |
| 1138 | |
Gilad Arnold | a76bef0 | 2015-09-29 13:55:15 -0700 | [diff] [blame] | 1139 | def get_os_type(self): |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1140 | """Get the OS type of the DUT, e.g., android or brillo. |
| 1141 | """ |
| 1142 | if not self._os_type: |
| 1143 | if self.run_output('getprop ro.product.brand') == 'Brillo': |
| 1144 | self._os_type = OS_TYPE_BRILLO |
| 1145 | else: |
| 1146 | self._os_type = OS_TYPE_ANDROID |
| 1147 | |
| 1148 | return self._os_type |
Gilad Arnold | e452b1f | 2015-10-06 08:48:34 -0700 | [diff] [blame] | 1149 | |
| 1150 | |
| 1151 | def _forward(self, reverse, args): |
| 1152 | """Execute a forwarding command. |
| 1153 | |
| 1154 | @param reverse: Whether this is reverse forwarding (Boolean). |
| 1155 | @param args: List of command arguments. |
| 1156 | """ |
| 1157 | cmd = '%s %s' % ('reverse' if reverse else 'forward', ' '.join(args)) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1158 | self.adb_run(cmd) |
Gilad Arnold | e452b1f | 2015-10-06 08:48:34 -0700 | [diff] [blame] | 1159 | |
| 1160 | |
| 1161 | def add_forwarding(self, src, dst, reverse=False, rebind=True): |
| 1162 | """Forward a port between the ADB host and device. |
| 1163 | |
| 1164 | Port specifications are any strings accepted as such by ADB, for |
| 1165 | example 'tcp:8080'. |
| 1166 | |
| 1167 | @param src: Port specification to forward from. |
| 1168 | @param dst: Port specification to forward to. |
| 1169 | @param reverse: Do reverse forwarding from device to host (Boolean). |
| 1170 | @param rebind: Allow rebinding an already bound port (Boolean). |
| 1171 | """ |
| 1172 | args = [] |
| 1173 | if not rebind: |
| 1174 | args.append('--no-rebind') |
| 1175 | args += [src, dst] |
| 1176 | self._forward(reverse, args) |
| 1177 | |
| 1178 | |
| 1179 | def remove_forwarding(self, src=None, reverse=False): |
| 1180 | """Removes forwarding on port. |
| 1181 | |
| 1182 | @param src: Port specification, or None to remove all forwarding. |
| 1183 | @param reverse: Whether this is reverse forwarding (Boolean). |
| 1184 | """ |
| 1185 | args = [] |
| 1186 | if src is None: |
| 1187 | args.append('--remove-all') |
| 1188 | else: |
| 1189 | args += ['--remove', src] |
| 1190 | self._forward(reverse, args) |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 1191 | |
| 1192 | |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 1193 | def create_ssh_tunnel(self, port, local_port): |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 1194 | """ |
| 1195 | Forwards a port securely through a tunnel process from the server |
| 1196 | to the DUT for RPC server connection. |
| 1197 | Add a 'ADB forward' rule to forward the RPC packets from the AdbHost |
| 1198 | to the DUT. |
| 1199 | |
| 1200 | @param port: remote port on the DUT. |
| 1201 | @param local_port: local forwarding port. |
| 1202 | |
| 1203 | @return: the tunnel process. |
| 1204 | """ |
| 1205 | self.add_forwarding('tcp:%s' % port, 'tcp:%s' % port) |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 1206 | return super(ADBHost, self).create_ssh_tunnel(port, local_port) |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 1207 | |
| 1208 | |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 1209 | def disconnect_ssh_tunnel(self, tunnel_proc, port): |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 1210 | """ |
| 1211 | Disconnects a previously forwarded port from the server to the DUT for |
| 1212 | RPC server connection. |
| 1213 | Remove the previously added 'ADB forward' rule to forward the RPC |
| 1214 | packets from the AdbHost to the DUT. |
| 1215 | |
| 1216 | @param tunnel_proc: the original tunnel process returned from |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 1217 | |create_ssh_tunnel|. |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 1218 | @param port: remote port on the DUT. |
| 1219 | |
| 1220 | """ |
| 1221 | self.remove_forwarding('tcp:%s' % port) |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 1222 | super(ADBHost, self).disconnect_ssh_tunnel(tunnel_proc, port) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1223 | |
| 1224 | |
| 1225 | def ensure_bootloader_mode(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1226 | """Ensure the device is in bootloader mode. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1227 | |
| 1228 | @raise: error.AutoservError if the device failed to reboot into |
| 1229 | bootloader mode. |
| 1230 | """ |
| 1231 | if self.is_up(command=FASTBOOT_CMD): |
| 1232 | return |
| 1233 | self.adb_run('reboot bootloader') |
| 1234 | if not self.wait_up(command=FASTBOOT_CMD): |
| 1235 | raise error.AutoservError( |
| 1236 | 'The device failed to reboot into bootloader mode.') |
| 1237 | |
| 1238 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1239 | def ensure_adb_mode(self, timeout=DEFAULT_WAIT_UP_TIME_SECONDS): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1240 | """Ensure the device is up and can be accessed by adb command. |
| 1241 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1242 | @param timeout: Time limit in seconds before returning even if the host |
| 1243 | is not up. |
| 1244 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1245 | @raise: error.AutoservError if the device failed to reboot into |
| 1246 | adb mode. |
| 1247 | """ |
| 1248 | if self.is_up(): |
| 1249 | return |
Dan Shi | 0498037 | 2016-03-22 10:57:47 -0700 | [diff] [blame] | 1250 | # Ignore timeout error to allow `fastboot reboot` to fail quietly and |
| 1251 | # check if the device is in adb mode. |
| 1252 | self.fastboot_run('reboot', timeout=timeout, ignore_timeout=True) |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1253 | if not self.wait_up(timeout=timeout): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1254 | raise error.AutoservError( |
| 1255 | 'The device failed to reboot into adb mode.') |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 1256 | self._reset_adbd_connection() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1257 | |
| 1258 | |
| 1259 | @classmethod |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1260 | def get_build_info_from_build_url(cls, build_url): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1261 | """Get the Android build information from the build url. |
| 1262 | |
| 1263 | @param build_url: The url to use for downloading Android artifacts. |
| 1264 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1265 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1266 | @return: A dictionary of build information, including keys: |
| 1267 | build_target, branch, target, build_id. |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1268 | @raise AndroidInstallError: If failed to parse build_url. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1269 | """ |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1270 | if not build_url: |
| 1271 | raise AndroidInstallError('Need build_url to download image files.') |
| 1272 | |
| 1273 | try: |
| 1274 | match = re.match(DEVSERVER_URL_REGEX, build_url) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1275 | return {'build_target': match.group('BUILD_TARGET'), |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1276 | 'branch': match.group('BRANCH'), |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1277 | 'target': ('%s-%s' % (match.group('BUILD_TARGET'), |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1278 | match.group('BUILD_TYPE'))), |
| 1279 | 'build_id': match.group('BUILD_ID')} |
| 1280 | except (AttributeError, IndexError, ValueError) as e: |
| 1281 | raise AndroidInstallError( |
| 1282 | 'Failed to parse build url: %s\nError: %s' % (build_url, e)) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1283 | |
| 1284 | |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1285 | @retry.retry(error.GenericHostRunError, timeout_min=10) |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1286 | def download_file(self, build_url, file, dest_dir, unzip=False, |
| 1287 | unzip_dest=None): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1288 | """Download the given file from the build url. |
| 1289 | |
| 1290 | @param build_url: The url to use for downloading Android artifacts. |
| 1291 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1292 | @param file: Name of the file to be downloaded, e.g., boot.img. |
| 1293 | @param dest_dir: Destination folder for the file to be downloaded to. |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1294 | @param unzip: If True, unzip the downloaded file. |
| 1295 | @param unzip_dest: Location to unzip the downloaded file to. If not |
| 1296 | provided, dest_dir is used. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1297 | """ |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1298 | # Append the file name to the url if build_url is linked to the folder |
| 1299 | # containing the file. |
| 1300 | if not build_url.endswith('/%s' % file): |
| 1301 | src_url = os.path.join(build_url, file) |
| 1302 | else: |
| 1303 | src_url = build_url |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1304 | dest_file = os.path.join(dest_dir, file) |
| 1305 | try: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1306 | self.teststation.run('wget -q -O "%s" "%s"' % (dest_file, src_url)) |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1307 | if unzip: |
| 1308 | unzip_dest = unzip_dest or dest_dir |
| 1309 | self.teststation.run('unzip "%s/%s" -x -d "%s"' % |
| 1310 | (dest_dir, file, unzip_dest)) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1311 | except: |
| 1312 | # Delete the destination file if download failed. |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1313 | self.teststation.run('rm -f "%s"' % dest_file) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1314 | raise |
| 1315 | |
| 1316 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1317 | def stage_android_image_files(self, build_url): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1318 | """Download required image files from the given build_url to a local |
| 1319 | directory in the machine runs fastboot command. |
| 1320 | |
| 1321 | @param build_url: The url to use for downloading Android artifacts. |
| 1322 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1323 | |
| 1324 | @return: Path to the directory contains image files. |
| 1325 | """ |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1326 | build_info = self.get_build_info_from_build_url(build_url) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1327 | |
| 1328 | zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 1329 | image_dir = self.teststation.get_tmp_dir() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1330 | |
| 1331 | try: |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1332 | self.download_file(build_url, zipped_image_file, image_dir, |
| 1333 | unzip=True) |
Dan Shi | 49d451f | 2016-04-19 09:25:01 -0700 | [diff] [blame] | 1334 | images = android_utils.AndroidImageFiles.get_standalone_images( |
| 1335 | build_info['build_target']) |
| 1336 | for image_file in images: |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1337 | self.download_file(build_url, image_file, image_dir) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1338 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1339 | return image_dir |
| 1340 | except: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1341 | self.teststation.run('rm -rf %s' % image_dir) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1342 | raise |
| 1343 | |
| 1344 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1345 | def stage_brillo_image_files(self, build_url): |
| 1346 | """Download required brillo image files from the given build_url to a |
| 1347 | local directory in the machine runs fastboot command. |
| 1348 | |
| 1349 | @param build_url: The url to use for downloading Android artifacts. |
| 1350 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1351 | |
| 1352 | @return: Path to the directory contains image files. |
| 1353 | """ |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1354 | build_info = self.get_build_info_from_build_url(build_url) |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1355 | |
| 1356 | zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info |
| 1357 | vendor_partitions_file = BRILLO_VENDOR_PARTITIONS_FILE_FMT % build_info |
| 1358 | image_dir = self.teststation.get_tmp_dir() |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1359 | |
| 1360 | try: |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1361 | self.download_file(build_url, zipped_image_file, image_dir, |
| 1362 | unzip=True) |
| 1363 | self.download_file(build_url, vendor_partitions_file, image_dir, |
| 1364 | unzip=True, |
| 1365 | unzip_dest=os.path.join(image_dir, 'vendor')) |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1366 | return image_dir |
| 1367 | except: |
| 1368 | self.teststation.run('rm -rf %s' % image_dir) |
| 1369 | raise |
| 1370 | |
| 1371 | |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1372 | def stage_build_for_install(self, build_name, os_type=None): |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1373 | """Stage a build on a devserver and return the build_url and devserver. |
| 1374 | |
| 1375 | @param build_name: a name like git-master/shamu-userdebug/2040953 |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1376 | |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1377 | @returns a tuple with an update URL like: |
| 1378 | http://172.22.50.122:8080/git-master/shamu-userdebug/2040953 |
| 1379 | and the devserver instance. |
| 1380 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1381 | os_type = os_type or self.get_os_type() |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1382 | logging.info('Staging build for installation: %s', build_name) |
Dan Shi | 216389c | 2015-12-22 11:03:06 -0800 | [diff] [blame] | 1383 | devserver = dev_server.AndroidBuildServer.resolve(build_name, |
| 1384 | self.hostname) |
Dan Shi | ba94353 | 2015-12-03 08:58:00 -0800 | [diff] [blame] | 1385 | build_name = devserver.translate(build_name) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1386 | branch, target, build_id = utils.parse_launch_control_build(build_name) |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1387 | devserver.trigger_download(target, build_id, branch, |
Justin Giorgi | dd05a94 | 2016-07-05 20:53:12 -0700 | [diff] [blame] | 1388 | os=os_type, synchronous=False) |
Dan Shi | f2fd076 | 2015-12-01 10:09:32 -0800 | [diff] [blame] | 1389 | return '%s/static/%s' % (devserver.url(), build_name), devserver |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1390 | |
| 1391 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1392 | def install_android(self, build_url, build_local_path=None, wipe=True, |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1393 | flash_all=False): |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1394 | """Install the Android DUT. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1395 | |
| 1396 | Following are the steps used here to provision an android device: |
| 1397 | 1. If build_local_path is not set, download the image zip file, e.g., |
| 1398 | shamu-img-2284311.zip, unzip it. |
| 1399 | 2. Run fastboot to install following artifacts: |
| 1400 | bootloader, radio, boot, system, vendor(only if exists) |
| 1401 | |
| 1402 | Repair is not supported for Android devices yet. |
| 1403 | |
| 1404 | @param build_url: The url to use for downloading Android artifacts. |
| 1405 | pattern: http://$devserver:###/static/$build |
| 1406 | @param build_local_path: The path to a local folder that contains the |
| 1407 | image files needed to provision the device. Note that the folder |
| 1408 | is in the machine running adb command, rather than the drone. |
| 1409 | @param wipe: If true, userdata will be wiped before flashing. |
| 1410 | @param flash_all: If True, all img files found in img_path will be |
| 1411 | flashed. Otherwise, only boot and system are flashed. |
| 1412 | |
| 1413 | @raises AndroidInstallError if any error occurs. |
| 1414 | """ |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1415 | # If the build is not staged in local server yet, clean up the temp |
| 1416 | # folder used to store image files after the provision is completed. |
| 1417 | delete_build_folder = bool(not build_local_path) |
| 1418 | |
| 1419 | try: |
| 1420 | # Download image files needed for provision to a local directory. |
| 1421 | if not build_local_path: |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1422 | build_local_path = self.stage_android_image_files(build_url) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1423 | |
| 1424 | # Device needs to be in bootloader mode for flashing. |
| 1425 | self.ensure_bootloader_mode() |
| 1426 | |
| 1427 | if wipe: |
Dan Shi | 12a4f66 | 2016-05-10 14:49:42 -0700 | [diff] [blame] | 1428 | self._fastboot_run_with_retry('-w') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1429 | |
| 1430 | # Get all *.img file in the build_local_path. |
| 1431 | list_file_cmd = 'ls -d %s' % os.path.join(build_local_path, '*.img') |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1432 | image_files = self.teststation.run( |
| 1433 | list_file_cmd).stdout.strip().split() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1434 | images = dict([(os.path.basename(f), f) for f in image_files]) |
Dan Shi | 49d451f | 2016-04-19 09:25:01 -0700 | [diff] [blame] | 1435 | build_info = self.get_build_info_from_build_url(build_url) |
| 1436 | board = build_info['build_target'] |
| 1437 | all_images = ( |
| 1438 | android_utils.AndroidImageFiles.get_standalone_images(board) |
| 1439 | + android_utils.AndroidImageFiles.get_zipped_images(board)) |
| 1440 | |
| 1441 | # Sort images to be flashed, bootloader needs to be the first one. |
| 1442 | bootloader = android_utils.AndroidImageFiles.BOOTLOADER |
| 1443 | sorted_images = sorted( |
| 1444 | images.items(), |
| 1445 | key=lambda pair: 0 if pair[0] == bootloader else 1) |
| 1446 | for image, image_file in sorted_images: |
| 1447 | if image not in all_images: |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1448 | continue |
| 1449 | logging.info('Flashing %s...', image_file) |
Dan Shi | 12a4f66 | 2016-05-10 14:49:42 -0700 | [diff] [blame] | 1450 | self._fastboot_run_with_retry('-S 256M flash %s %s' % |
| 1451 | (image[:-4], image_file)) |
Dan Shi | 49d451f | 2016-04-19 09:25:01 -0700 | [diff] [blame] | 1452 | if image == android_utils.AndroidImageFiles.BOOTLOADER: |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1453 | self.fastboot_run('reboot-bootloader') |
| 1454 | self.wait_up(command=FASTBOOT_CMD) |
| 1455 | except Exception as e: |
| 1456 | logging.error('Install Android build failed with error: %s', e) |
| 1457 | # Re-raise the exception with type of AndroidInstallError. |
| 1458 | raise AndroidInstallError, sys.exc_info()[1], sys.exc_info()[2] |
| 1459 | finally: |
| 1460 | if delete_build_folder: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1461 | self.teststation.run('rm -rf %s' % build_local_path) |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1462 | timeout = (WAIT_UP_AFTER_WIPE_TIME_SECONDS if wipe else |
| 1463 | DEFAULT_WAIT_UP_TIME_SECONDS) |
| 1464 | self.ensure_adb_mode(timeout=timeout) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1465 | logging.info('Successfully installed Android build staged at %s.', |
| 1466 | build_url) |
| 1467 | |
| 1468 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1469 | def install_brillo(self, build_url, build_local_path=None): |
| 1470 | """Install the Brillo DUT. |
| 1471 | |
| 1472 | Following are the steps used here to provision an android device: |
| 1473 | 1. If build_local_path is not set, download the image zip file, e.g., |
| 1474 | dragonboard-img-123456.zip, unzip it. And download the vendor |
| 1475 | partition zip file, e.g., dragonboard-vendor_partitions-123456.zip, |
| 1476 | unzip it to vendor folder. |
| 1477 | 2. Run provision_device script to install OS images and vendor |
| 1478 | partitions. |
| 1479 | |
| 1480 | @param build_url: The url to use for downloading Android artifacts. |
| 1481 | pattern: http://$devserver:###/static/$build |
| 1482 | @param build_local_path: The path to a local folder that contains the |
| 1483 | image files needed to provision the device. Note that the folder |
| 1484 | is in the machine running adb command, rather than the drone. |
| 1485 | |
| 1486 | @raises AndroidInstallError if any error occurs. |
| 1487 | """ |
| 1488 | # If the build is not staged in local server yet, clean up the temp |
| 1489 | # folder used to store image files after the provision is completed. |
| 1490 | delete_build_folder = bool(not build_local_path) |
| 1491 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1492 | try: |
| 1493 | # Download image files needed for provision to a local directory. |
| 1494 | if not build_local_path: |
| 1495 | build_local_path = self.stage_brillo_image_files(build_url) |
| 1496 | |
| 1497 | # Device needs to be in bootloader mode for flashing. |
| 1498 | self.ensure_bootloader_mode() |
| 1499 | |
| 1500 | # Run provision_device command to install image files and vendor |
| 1501 | # partitions. |
| 1502 | vendor_partition_dir = os.path.join(build_local_path, 'vendor') |
| 1503 | cmd = (BRILLO_PROVISION_CMD % |
| 1504 | {'os_image_dir': build_local_path, |
| 1505 | 'vendor_partition_dir': vendor_partition_dir}) |
Dan Shi | 86bd8c0 | 2016-03-10 09:21:51 -0800 | [diff] [blame] | 1506 | if self.fastboot_serial: |
Dan Shi | 91b4235 | 2016-03-10 22:12:22 -0800 | [diff] [blame] | 1507 | cmd += ' -s %s ' % self.fastboot_serial |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1508 | self.teststation.run(cmd) |
| 1509 | except Exception as e: |
| 1510 | logging.error('Install Brillo build failed with error: %s', e) |
| 1511 | # Re-raise the exception with type of AndroidInstallError. |
| 1512 | raise AndroidInstallError, sys.exc_info()[1], sys.exc_info()[2] |
| 1513 | finally: |
| 1514 | if delete_build_folder: |
| 1515 | self.teststation.run('rm -rf %s' % build_local_path) |
| 1516 | self.ensure_adb_mode() |
| 1517 | logging.info('Successfully installed Android build staged at %s.', |
| 1518 | build_url) |
| 1519 | |
| 1520 | |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1521 | @property |
| 1522 | def job_repo_url_attribute(self): |
| 1523 | """Get the host attribute name for job_repo_url, which should append the |
| 1524 | adb serial. |
| 1525 | """ |
| 1526 | return '%s_%s' % (constants.JOB_REPO_URL, self.adb_serial) |
| 1527 | |
| 1528 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1529 | def machine_install(self, build_url=None, build_local_path=None, wipe=True, |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1530 | flash_all=False, os_type=None): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1531 | """Install the DUT. |
| 1532 | |
| 1533 | @param build_url: The url to use for downloading Android artifacts. |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1534 | pattern: http://$devserver:###/static/$build. If build_url is |
| 1535 | set to None, the code may try _parser.options.image to do the |
| 1536 | installation. If none of them is set, machine_install will fail. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1537 | @param build_local_path: The path to a local directory that contains the |
| 1538 | image files needed to provision the device. |
| 1539 | @param wipe: If true, userdata will be wiped before flashing. |
| 1540 | @param flash_all: If True, all img files found in img_path will be |
| 1541 | flashed. Otherwise, only boot and system are flashed. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1542 | |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1543 | @returns A tuple of (image_name, host_attributes). |
| 1544 | image_name is the name of image installed, e.g., |
| 1545 | git_mnc-release/shamu-userdebug/1234 |
| 1546 | host_attributes is a dictionary of (attribute, value), which |
| 1547 | can be saved to afe_host_attributes table in database. This |
| 1548 | method returns a dictionary with a single entry of |
| 1549 | `job_repo_url_[adb_serial]`: devserver_url, where devserver_url |
| 1550 | is a url to the build staged on devserver. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1551 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1552 | os_type = os_type or self.get_os_type() |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1553 | if not build_url and self._parser.options.image: |
| 1554 | build_url, _ = self.stage_build_for_install( |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1555 | self._parser.options.image, os_type=os_type) |
| 1556 | if os_type == OS_TYPE_ANDROID: |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1557 | self.install_android( |
| 1558 | build_url=build_url, build_local_path=build_local_path, |
| 1559 | wipe=wipe, flash_all=flash_all) |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1560 | elif os_type == OS_TYPE_BRILLO: |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1561 | self.install_brillo( |
| 1562 | build_url=build_url, build_local_path=build_local_path) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1563 | else: |
| 1564 | raise error.InstallError( |
| 1565 | 'Installation of os type %s is not supported.' % |
| 1566 | self.get_os_type()) |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1567 | return (build_url.split('static/')[-1], |
| 1568 | {self.job_repo_url_attribute: build_url}) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1569 | |
| 1570 | |
| 1571 | def list_files_glob(self, path_glob): |
| 1572 | """Get a list of files on the device given glob pattern path. |
| 1573 | |
| 1574 | @param path_glob: The path glob that we want to return the list of |
| 1575 | files that match the glob. Relative paths will not work as |
| 1576 | expected. Supply an absolute path to get the list of files |
| 1577 | you're hoping for. |
| 1578 | |
| 1579 | @returns List of files that match the path_glob. |
| 1580 | """ |
| 1581 | # This is just in case path_glob has no path separator. |
| 1582 | base_path = os.path.dirname(path_glob) or '.' |
| 1583 | result = self.run('find %s -path \'%s\' -print' % |
Christopher Wiley | d21d66a | 2016-03-01 10:58:13 -0800 | [diff] [blame] | 1584 | (base_path, path_glob), ignore_status=True) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1585 | if result.exit_status != 0: |
| 1586 | return [] |
| 1587 | return result.stdout.splitlines() |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1588 | |
| 1589 | |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1590 | @retry.retry(error.GenericHostRunError, timeout_min=APK_INSTALL_TIMEOUT_MIN) |
Dan Shi | da99500 | 2016-04-25 23:12:58 -0700 | [diff] [blame] | 1591 | def install_apk(self, apk, force_reinstall=True): |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1592 | """Install the specified apk. |
| 1593 | |
| 1594 | This will install the apk and override it if it's already installed and |
| 1595 | will also allow for downgraded apks. |
| 1596 | |
| 1597 | @param apk: The path to apk file. |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1598 | @param force_reinstall: True to reinstall the apk even if it's already |
Dan Shi | da99500 | 2016-04-25 23:12:58 -0700 | [diff] [blame] | 1599 | installed. Default is set to True. |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1600 | |
| 1601 | @returns a CMDResult object. |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1602 | """ |
Dan Shi | c716ac6 | 2016-05-24 16:47:57 -0700 | [diff] [blame] | 1603 | try: |
| 1604 | client_utils.poll_for_condition( |
| 1605 | lambda: self.run('pm list packages', |
| 1606 | ignore_status=True).exit_status == 0, |
| 1607 | timeout=120) |
| 1608 | client_utils.poll_for_condition( |
| 1609 | lambda: self.run('service list | grep mount', |
| 1610 | ignore_status=True).exit_status == 0, |
| 1611 | timeout=120) |
| 1612 | return self.adb_run('install %s -d %s' % |
| 1613 | ('-r' if force_reinstall else '', apk)) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1614 | except error.GenericHostRunError: |
Dan Shi | c716ac6 | 2016-05-24 16:47:57 -0700 | [diff] [blame] | 1615 | self.reboot() |
| 1616 | raise |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1617 | |
| 1618 | |
Justin Giorgi | 38a859f | 2016-08-15 20:19:32 -0700 | [diff] [blame] | 1619 | def uninstall_package(self, package): |
| 1620 | """Remove the specified package. |
| 1621 | |
| 1622 | @param package: Android package name. |
| 1623 | |
| 1624 | @raises GenericHostRunError: uninstall failed |
| 1625 | """ |
| 1626 | result = self.adb_run('uninstall %s' % package) |
| 1627 | |
| 1628 | if self.is_apk_installed(package): |
| 1629 | raise error.GenericHostRunError('Uninstall of "%s" failed.' |
| 1630 | % package, result) |
| 1631 | |
| 1632 | |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1633 | @retry.retry(error.GenericHostRunError, timeout_min=0.2) |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1634 | def _confirm_apk_installed(self, package_name): |
| 1635 | """Confirm if apk is already installed with the given name. |
| 1636 | |
| 1637 | `pm list packages` command is not reliable some time. The retry helps to |
| 1638 | reduce the chance of false negative. |
| 1639 | |
| 1640 | @param package_name: Name of the package, e.g., com.android.phone. |
| 1641 | |
| 1642 | @raise AutoservRunError: If the package is not found or pm list command |
| 1643 | failed for any reason. |
| 1644 | """ |
| 1645 | name = 'package:%s' % package_name |
| 1646 | self.adb_run('shell pm list packages | grep -w "%s"' % name) |
| 1647 | |
| 1648 | |
| 1649 | def is_apk_installed(self, package_name): |
| 1650 | """Check if apk is already installed with the given name. |
| 1651 | |
| 1652 | @param package_name: Name of the package, e.g., com.android.phone. |
| 1653 | |
| 1654 | @return: True if package is installed. False otherwise. |
| 1655 | """ |
| 1656 | try: |
| 1657 | self._confirm_apk_installed(package_name) |
| 1658 | return True |
| 1659 | except: |
| 1660 | return False |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1661 | |
| 1662 | |
| 1663 | def get_attributes_to_clear_before_provision(self): |
| 1664 | """Get a list of attributes to be cleared before machine_install starts. |
| 1665 | """ |
| 1666 | return [self.job_repo_url_attribute] |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 1667 | |
| 1668 | |
| 1669 | def get_labels(self): |
| 1670 | """Return a list of the labels gathered from the devices connected. |
| 1671 | |
| 1672 | @return: A list of strings that denote the labels from all the devices |
| 1673 | connected. |
| 1674 | """ |
| 1675 | return self.labels.get_labels(self) |
| 1676 | |
| 1677 | |
| 1678 | def update_labels(self): |
| 1679 | """Update the labels for this testbed.""" |
| 1680 | self.labels.update_labels(self) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1681 | |
| 1682 | |
| 1683 | def stage_server_side_package(self, image=None): |
| 1684 | """Stage autotest server-side package on devserver. |
| 1685 | |
| 1686 | @param image: A build name, e.g., git_mnc_dev/shamu-eng/123 |
| 1687 | |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 1688 | @return: A url to the autotest server-side package. |
| 1689 | |
| 1690 | @raise: error.AutoservError if fail to locate the build to test with, or |
| 1691 | fail to stage server-side package. |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1692 | """ |
Dan Shi | d37736b | 2016-07-06 15:10:29 -0700 | [diff] [blame] | 1693 | # If enable_drone_in_restricted_subnet is False, do not set hostname |
| 1694 | # in devserver.resolve call, so a devserver in non-restricted subnet |
| 1695 | # is picked to stage autotest server package for drone to download. |
| 1696 | hostname = self.hostname |
| 1697 | if not utils.ENABLE_DRONE_IN_RESTRICTED_SUBNET: |
| 1698 | hostname = None |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1699 | if image: |
Dan Shi | d37736b | 2016-07-06 15:10:29 -0700 | [diff] [blame] | 1700 | ds = dev_server.AndroidBuildServer.resolve(image, hostname) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1701 | else: |
| 1702 | job_repo_url = afe_utils.get_host_attribute( |
| 1703 | self, self.job_repo_url_attribute) |
| 1704 | if job_repo_url: |
| 1705 | devserver_url, image = ( |
| 1706 | tools.get_devserver_build_from_package_url( |
| 1707 | job_repo_url, True)) |
Dan Shi | d37736b | 2016-07-06 15:10:29 -0700 | [diff] [blame] | 1708 | # If enable_drone_in_restricted_subnet is True, use the |
| 1709 | # existing devserver. Otherwise, resolve a new one in |
| 1710 | # non-restricted subnet. |
| 1711 | if utils.ENABLE_DRONE_IN_RESTRICTED_SUBNET: |
| 1712 | ds = dev_server.AndroidBuildServer(devserver_url) |
| 1713 | else: |
| 1714 | ds = dev_server.AndroidBuildServer.resolve(image) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1715 | else: |
| 1716 | labels = afe_utils.get_labels(self, self.VERSION_PREFIX) |
| 1717 | if not labels: |
| 1718 | raise error.AutoservError( |
| 1719 | 'Failed to stage server-side package. The host has ' |
| 1720 | 'no job_report_url attribute or version label.') |
Kevin Cheng | 84a71ba | 2016-07-14 11:03:57 -0700 | [diff] [blame] | 1721 | image = labels[0][len(self.VERSION_PREFIX + ':'):] |
Dan Shi | d37736b | 2016-07-06 15:10:29 -0700 | [diff] [blame] | 1722 | ds = dev_server.AndroidBuildServer.resolve(image, hostname) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1723 | |
| 1724 | branch, target, build_id = utils.parse_launch_control_build(image) |
| 1725 | build_target, _ = utils.parse_launch_control_target(target) |
| 1726 | |
| 1727 | # For any build older than MIN_VERSION_SUPPORT_SSP, server side |
| 1728 | # packaging is not supported. |
| 1729 | try: |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 1730 | # Some build ids may have special character before the actual |
| 1731 | # number, skip such characters. |
| 1732 | actual_build_id = build_id |
| 1733 | if build_id.startswith('P'): |
| 1734 | actual_build_id = build_id[1:] |
| 1735 | if int(actual_build_id) < self.MIN_VERSION_SUPPORT_SSP: |
| 1736 | raise error.AutoservError( |
| 1737 | 'Build %s is older than %s. Server side packaging is ' |
| 1738 | 'disabled.' % (image, self.MIN_VERSION_SUPPORT_SSP)) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1739 | except ValueError: |
Dan Shi | 14de762 | 2016-08-22 11:09:06 -0700 | [diff] [blame] | 1740 | raise error.AutoservError( |
| 1741 | 'Failed to compare build id in %s with the minimum ' |
| 1742 | 'version that supports server side packaging. Server ' |
| 1743 | 'side packaging is disabled.' % image) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1744 | |
| 1745 | ds.stage_artifacts(target, build_id, branch, |
| 1746 | artifacts=['autotest_server_package']) |
| 1747 | autotest_server_package_name = (AUTOTEST_SERVER_PACKAGE_FILE_FMT % |
| 1748 | {'build_target': build_target, |
| 1749 | 'build_id': build_id}) |
| 1750 | return '%s/static/%s/%s' % (ds.url(), image, |
| 1751 | autotest_server_package_name) |
Bryan Lewandowski | 0f3f011 | 2016-05-27 15:17:19 -0700 | [diff] [blame] | 1752 | |
| 1753 | |
| 1754 | def _sync_time(self): |
| 1755 | """Approximate synchronization of time between host and ADB device. |
| 1756 | |
| 1757 | This sets the ADB/Android device's clock to approximately the same |
| 1758 | time as the Autotest host for the purposes of comparing Android system |
| 1759 | logs such as logcat to logs from the Autotest host system. |
| 1760 | """ |
| 1761 | command = 'date ' |
| 1762 | sdk_version = int(self.run('getprop %s' % SDK_FILE).stdout) |
| 1763 | if sdk_version < 23: |
| 1764 | # Android L and earlier use this format: date -s (format). |
| 1765 | command += ('-s %s' % |
| 1766 | datetime.datetime.now().strftime('%Y%m%d.%H%M%S')) |
| 1767 | else: |
Alex Deymo | b244ddb | 2016-07-20 19:51:30 -0700 | [diff] [blame] | 1768 | # Android M and later use this format: date -u (format). |
| 1769 | command += ('-u %s' % |
| 1770 | datetime.datetime.utcnow().strftime('%m%d%H%M%Y.%S')) |
Bryan Lewandowski | ea4a06d | 2016-06-03 11:25:54 -0700 | [diff] [blame] | 1771 | self.run(command, timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 1772 | ignore_timeout=True) |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1773 | |
| 1774 | |
| 1775 | def _enable_native_crash_logging(self): |
| 1776 | """Enable native (non-Java) crash logging. |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1777 | """ |
Ralph Nathan | 3b758a3 | 2016-08-11 17:48:46 -0700 | [diff] [blame] | 1778 | # TODO(b/30820403): Enable Brillo native crash logging. |
| 1779 | # if self.get_os_type() == OS_TYPE_BRILLO: |
| 1780 | # self._enable_brillo_native_crash_logging() |
| 1781 | if self.get_os_type() == OS_TYPE_ANDROID: |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1782 | self._enable_android_native_crash_logging() |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1783 | |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1784 | |
| 1785 | def _enable_brillo_native_crash_logging(self): |
| 1786 | """Enables native crash logging for a Brillo DUT. |
| 1787 | """ |
Bryan Lewandowski | 8c96bb2 | 2016-07-19 12:56:03 -0700 | [diff] [blame] | 1788 | try: |
| 1789 | self.run('touch /data/misc/metrics/enabled', |
| 1790 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 1791 | ignore_timeout=True) |
| 1792 | # If running, crash_sender will delete crash files every hour. |
| 1793 | self.run('stop crash_sender', |
| 1794 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 1795 | ignore_timeout=True) |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1796 | except error.GenericHostRunError as e: |
Bryan Lewandowski | 8c96bb2 | 2016-07-19 12:56:03 -0700 | [diff] [blame] | 1797 | logging.warn(e) |
| 1798 | logging.warn('Failed to enable Brillo native crash logging.') |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1799 | |
| 1800 | |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1801 | def _enable_android_native_crash_logging(self): |
| 1802 | """Enables native crash logging for an Android DUT. |
| 1803 | """ |
| 1804 | # debuggerd should be enabled by default on Android. |
| 1805 | result = self.run('pgrep debuggerd', |
| 1806 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS, |
| 1807 | ignore_timeout=True, ignore_status=True) |
| 1808 | if not result or result.exit_status != 0: |
| 1809 | logging.debug('Unable to confirm that debuggerd is running.') |
| 1810 | |
| 1811 | |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1812 | def _collect_crash_logs(self): |
| 1813 | """Copies crash log files from the DUT to the drone. |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1814 | """ |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1815 | if self.get_os_type() == OS_TYPE_BRILLO: |
| 1816 | self._collect_crash_logs_dut(BRILLO_NATIVE_CRASH_LOG_DIR) |
| 1817 | elif self.get_os_type() == OS_TYPE_ANDROID: |
| 1818 | self._collect_crash_logs_dut(ANDROID_TOMBSTONE_CRASH_LOG_DIR) |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1819 | |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1820 | |
| 1821 | def _collect_crash_logs_dut(self, log_directory): |
| 1822 | """Copies native crash logs from the Android/Brillo DUT to the drone. |
| 1823 | |
| 1824 | @param log_directory: absolute path of the directory on the DUT where |
| 1825 | log files are stored. |
| 1826 | """ |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1827 | files = None |
| 1828 | try: |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1829 | result = self.run('find %s -maxdepth 1 -type f' % log_directory, |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1830 | timeout=DEFAULT_COMMAND_RETRY_TIMEOUT_SECONDS) |
| 1831 | files = result.stdout.strip().split() |
Justin Giorgi | 6ee6453 | 2016-08-10 11:32:04 -0700 | [diff] [blame] | 1832 | except (error.GenericHostRunError, error.AutoservSSHTimeout, |
| 1833 | error.CmdTimeoutError): |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1834 | logging.debug('Unable to call find %s, unable to find crash logs', |
| 1835 | log_directory) |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1836 | if not files: |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1837 | logging.debug('There are no crash logs on the DUT.') |
Bryan Lewandowski | 11f8aed | 2016-06-29 15:44:40 -0700 | [diff] [blame] | 1838 | return |
| 1839 | |
| 1840 | crash_dir = os.path.join(self.job.resultdir, 'crash') |
| 1841 | try: |
| 1842 | os.mkdir(crash_dir) |
| 1843 | except OSError as e: |
| 1844 | if e.errno != errno.EEXIST: |
| 1845 | raise e |
| 1846 | |
| 1847 | for f in files: |
Bryan Lewandowski | 68646b6 | 2016-07-12 10:24:57 -0700 | [diff] [blame] | 1848 | logging.debug('DUT native crash file produced: %s', f) |
| 1849 | dest = os.path.join(crash_dir, os.path.basename(f)) |
| 1850 | self.get_file(source=f, dest=dest) |