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. |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 4 | import functools |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 5 | import logging |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 6 | import os |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 7 | import re |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 8 | import stat |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 9 | import sys |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 10 | import time |
| 11 | |
| 12 | import common |
| 13 | |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 14 | from autotest_lib.client.bin import utils as client_utils |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import error |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import global_config |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 17 | from autotest_lib.client.common_lib.cros import dev_server |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib.cros import retry |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 19 | from autotest_lib.server import afe_utils |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 20 | from autotest_lib.server import autoserv_parser |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 21 | from autotest_lib.server import constants as server_constants |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 22 | from autotest_lib.server import utils |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 23 | from autotest_lib.server.cros import provision |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 24 | from autotest_lib.server.cros.dynamic_suite import tools |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 25 | from autotest_lib.server.cros.dynamic_suite import constants |
Simran Basi | 724b8a5 | 2013-09-30 11:19:31 -0700 | [diff] [blame] | 26 | from autotest_lib.server.hosts import abstract_ssh |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 27 | from autotest_lib.server.hosts import adb_label |
| 28 | from autotest_lib.server.hosts import base_label |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 29 | from autotest_lib.server.hosts import teststation_host |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 30 | |
| 31 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 32 | CONFIG = global_config.global_config |
| 33 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 34 | ADB_CMD = 'adb' |
| 35 | FASTBOOT_CMD = 'fastboot' |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 36 | SHELL_CMD = 'shell' |
Filipe Brandenburger | 3436339 | 2015-08-13 14:57:45 -0700 | [diff] [blame] | 37 | # Some devices have no serial, then `adb serial` has output such as: |
| 38 | # (no serial number) device |
| 39 | # ?????????? device |
| 40 | DEVICE_NO_SERIAL_MSG = '(no serial number)' |
| 41 | DEVICE_NO_SERIAL_TAG = '<NO_SERIAL>' |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 42 | # Regex to find an adb device. Examples: |
| 43 | # 0146B5580B01801B device |
| 44 | # 018e0ecb20c97a62 device |
| 45 | # 172.22.75.141:5555 device |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 46 | DEVICE_FINDER_REGEX = (r'^(?P<SERIAL>([\w-]+)|((tcp:)?' + |
| 47 | '\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}([:]5555)?)|' + |
Filipe Brandenburger | 3436339 | 2015-08-13 14:57:45 -0700 | [diff] [blame] | 48 | re.escape(DEVICE_NO_SERIAL_MSG) + |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 49 | r')[ \t]+(?:device|fastboot)') |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 50 | CMD_OUTPUT_PREFIX = 'ADB_CMD_OUTPUT' |
| 51 | CMD_OUTPUT_REGEX = ('(?P<OUTPUT>[\s\S]*)%s:(?P<EXIT_CODE>\d{1,3})' % |
| 52 | CMD_OUTPUT_PREFIX) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 53 | RELEASE_FILE = 'ro.build.version.release' |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 54 | BOARD_FILE = 'ro.product.device' |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 55 | TMP_DIR = '/data/local/tmp' |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 56 | # Regex to pull out file type, perms and symlink. Example: |
Kevin Cheng | aaabd0c | 2015-11-10 16:05:04 -0800 | [diff] [blame] | 57 | # 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] | 58 | FILE_INFO_REGEX = '^(?P<TYPE>[dl-])(?P<PERMS>[rwx-]{9})' |
| 59 | FILE_SYMLINK_REGEX = '^.*-> (?P<SYMLINK>.+)' |
| 60 | # List of the perm stats indexed by the order they are listed in the example |
| 61 | # supplied above. |
| 62 | FILE_PERMS_FLAGS = [stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR, |
| 63 | stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP, |
| 64 | stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH] |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 65 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 66 | # Default maximum number of seconds to wait for a device to be down. |
| 67 | DEFAULT_WAIT_DOWN_TIME_SECONDS = 10 |
| 68 | # 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] | 69 | DEFAULT_WAIT_UP_TIME_SECONDS = 300 |
| 70 | # 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] | 71 | WAIT_UP_AFTER_WIPE_TIME_SECONDS = 1200 |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 72 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 73 | OS_TYPE_ANDROID = 'android' |
| 74 | OS_TYPE_BRILLO = 'brillo' |
| 75 | |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 76 | # Regex to parse build name to get the detailed build information. |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 77 | BUILD_REGEX = ('(?P<BRANCH>([^/]+))/(?P<BUILD_TARGET>([^/]+))-' |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 78 | '(?P<BUILD_TYPE>([^/]+))/(?P<BUILD_ID>([^/]+))') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 79 | # Regex to parse devserver url to get the detailed build information. Sample |
| 80 | # url: http://$devserver:8080/static/branch/target/build_id |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 81 | DEVSERVER_URL_REGEX = '.*/%s/*' % BUILD_REGEX |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 82 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 83 | ANDROID_IMAGE_FILE_FMT = '%(build_target)s-img-%(build_id)s.zip' |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 84 | ANDROID_BOOTLOADER = 'bootloader.img' |
| 85 | ANDROID_RADIO = 'radio.img' |
| 86 | ANDROID_BOOT = 'boot.img' |
| 87 | ANDROID_SYSTEM = 'system.img' |
| 88 | ANDROID_VENDOR = 'vendor.img' |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 89 | BRILLO_VENDOR_PARTITIONS_FILE_FMT = ( |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 90 | '%(build_target)s-vendor_partitions-%(build_id)s.zip') |
| 91 | AUTOTEST_SERVER_PACKAGE_FILE_FMT = ( |
| 92 | '%(build_target)s-autotest_server_package-%(build_id)s.tar.bz2') |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 93 | ADB_DEVICE_PREFIXES = ['product:', 'model:', 'device:'] |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 94 | |
| 95 | # Image files not inside the image zip file. These files should be downloaded |
| 96 | # directly from devserver. |
| 97 | ANDROID_STANDALONE_IMAGES = [ANDROID_BOOTLOADER, ANDROID_RADIO] |
| 98 | # Image files that are packaged in a zip file, e.g., shamu-img-123456.zip |
| 99 | ANDROID_ZIPPED_IMAGES = [ANDROID_BOOT, ANDROID_SYSTEM, ANDROID_VENDOR] |
| 100 | # All image files to be flashed to an Android device. |
| 101 | ANDROID_IMAGES = ANDROID_STANDALONE_IMAGES + ANDROID_ZIPPED_IMAGES |
| 102 | |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 103 | # Map of product names to build target name. |
| 104 | PRODUCT_TARGET_MAP = {'dragon' : 'ryu', |
| 105 | 'flo' : 'razor', |
| 106 | 'flo_lte' : 'razorg', |
| 107 | 'gm4g_sprout' : 'seed_l8150', |
| 108 | 'flounder' : 'volantis', |
| 109 | 'flounder_lte' : 'volantisg'} |
| 110 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 111 | # Command to provision a Brillo device. |
| 112 | # os_image_dir: The full path of the directory that contains all the Android image |
| 113 | # files (from the image zip file). |
| 114 | # vendor_partition_dir: The full path of the directory that contains all the |
| 115 | # Brillo vendor partitions, and provision-device script. |
| 116 | BRILLO_PROVISION_CMD = ( |
Simran Basi | 7e52c62 | 2016-01-05 15:43:51 -0800 | [diff] [blame] | 117 | 'sudo ANDROID_PROVISION_OS_PARTITIONS=%(os_image_dir)s ' |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 118 | 'ANDROID_PROVISION_VENDOR_PARTITIONS=%(vendor_partition_dir)s ' |
| 119 | '%(vendor_partition_dir)s/provision-device') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 120 | |
| 121 | class AndroidInstallError(error.InstallError): |
| 122 | """Generic error for Android installation related exceptions.""" |
| 123 | |
| 124 | |
Simran Basi | 724b8a5 | 2013-09-30 11:19:31 -0700 | [diff] [blame] | 125 | class ADBHost(abstract_ssh.AbstractSSHHost): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 126 | """This class represents a host running an ADB server.""" |
| 127 | |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 128 | VERSION_PREFIX = provision.ANDROID_BUILD_VERSION_PREFIX |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 129 | _LABEL_FUNCTIONS = [] |
| 130 | _DETECTABLE_LABELS = [] |
| 131 | label_decorator = functools.partial(utils.add_label_detector, |
| 132 | _LABEL_FUNCTIONS, |
| 133 | _DETECTABLE_LABELS) |
| 134 | |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 135 | _parser = autoserv_parser.autoserv_parser |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 136 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 137 | # Minimum build id that supports server side packaging. Older builds may |
| 138 | # not have server side package built or with Autotest code change to support |
| 139 | # server-side packaging. |
| 140 | MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value( |
| 141 | 'AUTOSERV', 'min_launch_control_build_id_support_ssp', type=int) |
| 142 | |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 143 | @staticmethod |
| 144 | def check_host(host, timeout=10): |
| 145 | """ |
| 146 | Check if the given host is an adb host. |
| 147 | |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 148 | If SSH connectivity can't be established, check_host will try to use |
| 149 | user 'adb' as well. If SSH connectivity still can't be established |
| 150 | then the original SSH user is restored. |
| 151 | |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 152 | @param host: An ssh host representing a device. |
| 153 | @param timeout: The timeout for the run command. |
| 154 | |
| 155 | |
| 156 | @return: True if the host device has adb. |
| 157 | |
| 158 | @raises AutoservRunError: If the command failed. |
| 159 | @raises AutoservSSHTimeout: Ssh connection has timed out. |
| 160 | """ |
Dan Shi | 64e130f | 2015-12-16 14:45:44 -0800 | [diff] [blame] | 161 | # host object may not have user attribute if it's a LocalHost object. |
| 162 | current_user = host.user if hasattr(host, 'user') else None |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 163 | try: |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 164 | if not (host.hostname == 'localhost' or |
| 165 | host.verify_ssh_user_access()): |
Simran Basi | 1621c63 | 2015-10-14 12:22:23 -0700 | [diff] [blame] | 166 | host.user = 'adb' |
Simran Basi | 933c8af | 2015-04-29 14:05:07 -0700 | [diff] [blame] | 167 | result = host.run( |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 168 | 'test -f %s' % server_constants.ANDROID_TESTER_FILEFLAG, |
Simran Basi | 933c8af | 2015-04-29 14:05:07 -0700 | [diff] [blame] | 169 | timeout=timeout) |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 170 | except (error.AutoservRunError, error.AutoservSSHTimeout): |
Dan Shi | 64e130f | 2015-12-16 14:45:44 -0800 | [diff] [blame] | 171 | if current_user is not None: |
| 172 | host.user = current_user |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 173 | return False |
| 174 | return result.exit_status == 0 |
| 175 | |
| 176 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 177 | # TODO(garnold) Remove the 'serials' argument once all clients are made to |
| 178 | # not use it. |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 179 | def _initialize(self, hostname='localhost', serials=None, |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 180 | adb_serial=None, fastboot_serial=None, |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 181 | teststation=None, *args, **dargs): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 182 | """Initialize an ADB Host. |
| 183 | |
| 184 | This will create an ADB Host. Hostname should always refer to the |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 185 | test station connected to an Android DUT. This will be the DUT |
| 186 | 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] | 187 | exception will be raised. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 188 | |
| 189 | @param hostname: Hostname of the machine running ADB. |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 190 | @param serials: DEPRECATED (to be removed) |
| 191 | @param adb_serial: An ADB device serial. If None, assume a single |
| 192 | device is attached (and fail otherwise). |
| 193 | @param fastboot_serial: A fastboot device serial. If None, defaults to |
| 194 | the ADB serial (or assumes a single device if |
| 195 | the latter is None). |
Kevin Cheng | 549beb4 | 2015-11-18 11:42:25 -0800 | [diff] [blame] | 196 | @param teststation: The teststation object ADBHost should use. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 197 | """ |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 198 | # Sets up the is_client_install_supported field. |
| 199 | super(ADBHost, self)._initialize(hostname=hostname, |
| 200 | is_client_install_supported=False, |
| 201 | *args, **dargs) |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 202 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 203 | self.tmp_dirs = [] |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 204 | self.labels = base_label.LabelRetriever(adb_label.ADB_LABELS) |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 205 | # TODO (sbasi/kevcheng): Once the teststation host is committed, |
| 206 | # refactor the serial retrieval. |
| 207 | adb_serial = adb_serial or self.host_attributes.get('serials', None) |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 208 | self.adb_serial = adb_serial |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 209 | adb_prefix = any(adb_serial.startswith(p) for p in ADB_DEVICE_PREFIXES) |
| 210 | self.fastboot_serial = (fastboot_serial or |
| 211 | ('tcp:%s' % adb_serial.split(':')[0] if |
| 212 | ':' in adb_serial and not adb_prefix else adb_serial)) |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 213 | self.teststation = (teststation if teststation |
| 214 | else teststation_host.create_teststationhost(hostname=hostname)) |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 215 | |
| 216 | msg ='Initializing ADB device on host: %s' % hostname |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 217 | if self.adb_serial: |
| 218 | msg += ', ADB serial: %s' % self.adb_serial |
| 219 | if self.fastboot_serial: |
| 220 | msg += ', fastboot serial: %s' % self.fastboot_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 221 | logging.debug(msg) |
| 222 | |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 223 | self._use_tcpip = ':' in adb_serial and not adb_prefix |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 224 | # Try resetting the ADB daemon on the device, however if we are |
| 225 | # creating the host to do a repair job, the device maybe inaccesible |
| 226 | # via ADB. |
| 227 | try: |
| 228 | self._reset_adbd_connection() |
| 229 | except (error.AutotestHostRunError, error.AutoservRunError) as e: |
| 230 | logging.error('Unable to reset the device adb daemon connection: ' |
| 231 | '%s.', e) |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 232 | self._os_type = None |
| 233 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 234 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 235 | def _connect_over_tcpip_as_needed(self): |
| 236 | """Connect to the ADB device over TCP/IP if so configured.""" |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 237 | if not self._use_tcpip: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 238 | return |
| 239 | logging.debug('Connecting to device over TCP/IP') |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 240 | self.adb_run('connect %s' % self.adb_serial) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 241 | |
| 242 | |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 243 | def _restart_adbd_with_root_permissions(self): |
| 244 | """Restarts the adb daemon with root permissions.""" |
| 245 | self.adb_run('root') |
| 246 | # TODO(ralphnathan): Remove this sleep once b/19749057 is resolved. |
| 247 | time.sleep(1) |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 248 | self._connect_over_tcpip_as_needed() |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 249 | self.adb_run('wait-for-device') |
| 250 | |
| 251 | |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 252 | def _set_tcp_port(self): |
| 253 | """Ensure the device remains in tcp/ip mode after a reboot.""" |
| 254 | if not self._use_tcpip: |
| 255 | return |
| 256 | port = self.adb_serial.split(':')[-1] |
| 257 | self.run('setprop persist.adb.tcp.port %s' % port) |
| 258 | |
| 259 | |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 260 | def _reset_adbd_connection(self): |
| 261 | """Resets adbd connection to the device after a reboot/initialization""" |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 262 | self._connect_over_tcpip_as_needed() |
Simran Basi | 9228a6f | 2016-03-29 12:03:37 -0700 | [diff] [blame] | 263 | self._restart_adbd_with_root_permissions() |
| 264 | self._set_tcp_port() |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 265 | |
| 266 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 267 | # pylint: disable=missing-docstring |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 268 | def adb_run(self, command, **kwargs): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 269 | """Runs an adb command. |
| 270 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 271 | This command will launch on the test station. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 272 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 273 | Refer to _device_run method for docstring for parameters. |
| 274 | """ |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 275 | return self._device_run(ADB_CMD, command, **kwargs) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 276 | |
| 277 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 278 | # pylint: disable=missing-docstring |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 279 | def fastboot_run(self, command, **kwargs): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 280 | """Runs an fastboot command. |
| 281 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 282 | This command will launch on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 283 | |
| 284 | Refer to _device_run method for docstring for parameters. |
| 285 | """ |
Gilad Arnold | f17f6fd | 2015-11-12 14:44:07 -0800 | [diff] [blame] | 286 | return self._device_run(FASTBOOT_CMD, command, **kwargs) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 287 | |
| 288 | |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 289 | def _device_run(self, function, command, shell=False, |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 290 | timeout=3600, ignore_status=False, ignore_timeout=False, |
| 291 | stdout=utils.TEE_TO_LOGS, stderr=utils.TEE_TO_LOGS, |
| 292 | connect_timeout=30, options='', stdin=None, verbose=True, |
| 293 | require_sudo=False, args=()): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 294 | """Runs a command named `function` on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 295 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 296 | This command will launch on the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 297 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 298 | @param command: Command to run. |
| 299 | @param shell: If true the command runs in the adb shell otherwise if |
| 300 | False it will be passed directly to adb. For example |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 301 | reboot with shell=False will call 'adb reboot'. This |
| 302 | option only applies to function adb. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 303 | @param timeout: Time limit in seconds before attempting to |
| 304 | kill the running process. The run() function |
| 305 | will take a few seconds longer than 'timeout' |
| 306 | to complete if it has to kill the process. |
| 307 | @param ignore_status: Do not raise an exception, no matter |
| 308 | what the exit code of the command is. |
| 309 | @param ignore_timeout: Bool True if command timeouts should be |
| 310 | ignored. Will return None on command timeout. |
| 311 | @param stdout: Redirect stdout. |
| 312 | @param stderr: Redirect stderr. |
| 313 | @param connect_timeout: Connection timeout (in seconds) |
| 314 | @param options: String with additional ssh command options |
| 315 | @param stdin: Stdin to pass (a string) to the executed command |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 316 | @param require_sudo: True to require sudo to run the command. Default is |
| 317 | False. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 318 | @param args: Sequence of strings to pass as arguments to command by |
| 319 | quoting them in " and escaping their contents if |
| 320 | necessary. |
| 321 | |
| 322 | @returns a CMDResult object. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 323 | """ |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 324 | if function == ADB_CMD: |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 325 | serial = self.adb_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 326 | elif function == FASTBOOT_CMD: |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 327 | serial = self.fastboot_serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 328 | else: |
| 329 | raise NotImplementedError('Mode %s is not supported' % function) |
| 330 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 331 | if function != ADB_CMD and shell: |
| 332 | raise error.CmdError('shell option is only applicable to `adb`.') |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 333 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 334 | cmd = '%s%s ' % ('sudo -n ' if require_sudo else '', function) |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 335 | |
| 336 | if serial: |
| 337 | cmd += '-s %s ' % serial |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 338 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 339 | if shell: |
| 340 | cmd += '%s ' % SHELL_CMD |
| 341 | cmd += command |
| 342 | |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 343 | if verbose: |
| 344 | logging.debug('Command: %s', cmd) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 345 | |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 346 | return self.teststation.run(cmd, timeout=timeout, |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 347 | ignore_status=ignore_status, |
| 348 | ignore_timeout=ignore_timeout, stdout_tee=stdout, |
| 349 | stderr_tee=stderr, options=options, stdin=stdin, |
| 350 | connect_timeout=connect_timeout, args=args) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 351 | |
| 352 | |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 353 | def get_board_name(self): |
| 354 | """Get the name of the board, e.g., shamu, dragonboard etc. |
| 355 | """ |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 356 | product = self.run_output('getprop %s' % BOARD_FILE) |
| 357 | return PRODUCT_TARGET_MAP.get(product, product) |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 358 | |
| 359 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 360 | @label_decorator() |
Christopher Wiley | 8e6b08e | 2013-10-11 12:34:26 -0700 | [diff] [blame] | 361 | def get_board(self): |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 362 | """Determine the correct board label for the device. |
| 363 | |
| 364 | @returns a string representing this device's board. |
| 365 | """ |
Dan Shi | e234dea | 2016-01-20 17:15:17 -0800 | [diff] [blame] | 366 | board = self.get_board_name() |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 367 | board_os = self.get_os_type() |
Kevin Cheng | 49f7b81 | 2015-12-15 15:24:23 -0800 | [diff] [blame] | 368 | return constants.BOARD_PREFIX + '-'.join([board_os, board]) |
Christopher Wiley | 8e6b08e | 2013-10-11 12:34:26 -0700 | [diff] [blame] | 369 | |
| 370 | |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 371 | def job_start(self): |
| 372 | """ |
| 373 | Disable log collection on adb_hosts. |
| 374 | |
| 375 | TODO(sbasi): crbug.com/305427 |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 376 | """ |
| 377 | |
| 378 | |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 379 | def run(self, command, timeout=3600, ignore_status=False, |
| 380 | ignore_timeout=False, stdout_tee=utils.TEE_TO_LOGS, |
| 381 | stderr_tee=utils.TEE_TO_LOGS, connect_timeout=30, options='', |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 382 | stdin=None, verbose=True, args=()): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 383 | """Run a command on the adb device. |
| 384 | |
| 385 | The command given will be ran directly on the adb device; for example |
| 386 | 'ls' will be ran as: 'abd shell ls' |
| 387 | |
| 388 | @param command: The command line string. |
| 389 | @param timeout: Time limit in seconds before attempting to |
| 390 | kill the running process. The run() function |
| 391 | will take a few seconds longer than 'timeout' |
| 392 | to complete if it has to kill the process. |
| 393 | @param ignore_status: Do not raise an exception, no matter |
| 394 | what the exit code of the command is. |
| 395 | @param ignore_timeout: Bool True if command timeouts should be |
| 396 | ignored. Will return None on command timeout. |
| 397 | @param stdout_tee: Redirect stdout. |
| 398 | @param stderr_tee: Redirect stderr. |
| 399 | @param connect_timeout: Connection timeout (in seconds). |
| 400 | @param options: String with additional ssh command options. |
| 401 | @param stdin: Stdin to pass (a string) to the executed command |
| 402 | @param args: Sequence of strings to pass as arguments to command by |
| 403 | quoting them in " and escaping their contents if |
| 404 | necessary. |
| 405 | |
| 406 | @returns A CMDResult object or None if the call timed out and |
| 407 | ignore_timeout is True. |
| 408 | |
| 409 | @raises AutoservRunError: If the command failed. |
| 410 | @raises AutoservSSHTimeout: Ssh connection has timed out. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 411 | """ |
Filipe Brandenburger | 68a8007 | 2015-07-14 10:39:33 -0700 | [diff] [blame] | 412 | command = ('"%s; echo %s:\$?"' % |
| 413 | (utils.sh_escape(command), CMD_OUTPUT_PREFIX)) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 414 | result = self.adb_run( |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 415 | command, shell=True, timeout=timeout, |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 416 | ignore_status=ignore_status, ignore_timeout=ignore_timeout, |
| 417 | stdout=stdout_tee, stderr=stderr_tee, |
| 418 | connect_timeout=connect_timeout, options=options, stdin=stdin, |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 419 | verbose=verbose, args=args) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 420 | if not result: |
| 421 | # In case of timeouts. |
| 422 | return None |
| 423 | |
| 424 | parse_output = re.match(CMD_OUTPUT_REGEX, result.stdout) |
Roshan Pius | 5ba5d18 | 2015-10-28 09:19:41 -0700 | [diff] [blame] | 425 | if not parse_output and not ignore_status: |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 426 | raise error.AutoservRunError( |
Roshan Pius | 5ba5d18 | 2015-10-28 09:19:41 -0700 | [diff] [blame] | 427 | 'Failed to parse the exit code for command: %s' % |
| 428 | command, result) |
| 429 | elif parse_output: |
| 430 | result.stdout = parse_output.group('OUTPUT') |
| 431 | result.exit_status = int(parse_output.group('EXIT_CODE')) |
| 432 | if result.exit_status != 0 and not ignore_status: |
| 433 | raise error.AutoservRunError(command, result) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 434 | return result |
| 435 | |
| 436 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 437 | def wait_up(self, timeout=DEFAULT_WAIT_UP_TIME_SECONDS, command=ADB_CMD): |
| 438 | """Wait until the remote host is up or the timeout expires. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 439 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 440 | Overrides wait_down from AbstractSSHHost. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 441 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 442 | @param timeout: Time limit in seconds before returning even if the host |
| 443 | is not up. |
| 444 | @param command: The command used to test if a device is up, i.e., |
| 445 | accessible by the given command. Default is set to `adb`. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 446 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 447 | @returns True if the host was found to be up before the timeout expires, |
| 448 | False otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 449 | """ |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 450 | @retry.retry(error.TimeoutException, timeout_min=timeout/60.0, |
| 451 | delay_sec=1) |
| 452 | def _wait_up(): |
| 453 | if not self.is_up(command=command): |
| 454 | raise error.TimeoutException('Device is still down.') |
| 455 | return True |
| 456 | |
| 457 | try: |
| 458 | _wait_up() |
| 459 | logging.debug('Host %s is now up, and can be accessed by %s.', |
| 460 | self.hostname, command) |
| 461 | return True |
| 462 | except error.TimeoutException: |
| 463 | logging.debug('Host %s is still down after waiting %d seconds', |
| 464 | self.hostname, timeout) |
| 465 | return False |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 466 | |
| 467 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 468 | def wait_down(self, timeout=DEFAULT_WAIT_DOWN_TIME_SECONDS, |
| 469 | warning_timer=None, old_boot_id=None, command=ADB_CMD): |
| 470 | """Wait till the host goes down, i.e., not accessible by given command. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 471 | |
| 472 | Overrides wait_down from AbstractSSHHost. |
| 473 | |
| 474 | @param timeout: Time in seconds to wait for the host to go down. |
| 475 | @param warning_timer: Time limit in seconds that will generate |
| 476 | a warning if the host is not down yet. |
| 477 | Currently ignored. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 478 | @param old_boot_id: Not applicable for adb_host. |
| 479 | @param command: `adb`, test if the device can be accessed by adb |
| 480 | command, or `fastboot`, test if the device can be accessed by |
| 481 | fastboot command. Default is set to `adb`. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 482 | |
| 483 | @returns True if the device goes down before the timeout, False |
| 484 | otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 485 | """ |
| 486 | @retry.retry(error.TimeoutException, timeout_min=timeout/60.0, |
| 487 | delay_sec=1) |
| 488 | def _wait_down(): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 489 | if self.is_up(command=command): |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 490 | raise error.TimeoutException('Device is still up.') |
| 491 | return True |
| 492 | |
| 493 | try: |
| 494 | _wait_down() |
| 495 | logging.debug('Host %s is now down', self.hostname) |
| 496 | return True |
| 497 | except error.TimeoutException: |
| 498 | logging.debug('Host %s is still up after waiting %d seconds', |
| 499 | self.hostname, timeout) |
| 500 | return False |
| 501 | |
| 502 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 503 | def reboot(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 504 | """Reboot the android device via adb. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 505 | |
| 506 | @raises AutoservRebootError if reboot failed. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 507 | """ |
| 508 | # 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] | 509 | # the test station we are running ADB on. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 510 | self.adb_run('reboot', timeout=10, ignore_timeout=True) |
| 511 | if not self.wait_down(): |
| 512 | raise error.AutoservRebootError( |
| 513 | 'ADB Device is still up after reboot') |
| 514 | if not self.wait_up(): |
| 515 | raise error.AutoservRebootError( |
| 516 | 'ADB Device failed to return from reboot.') |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 517 | self._reset_adbd_connection() |
Roshan Pius | 50c1f9c | 2015-11-30 11:37:49 -0800 | [diff] [blame] | 518 | |
| 519 | |
Alexandru Branciog | 969ff7c | 2016-03-30 14:07:15 +0300 | [diff] [blame] | 520 | def fastboot_reboot(self): |
| 521 | """Do a fastboot reboot to go back to adb. |
| 522 | |
| 523 | @raises AutoservRebootError if reboot failed. |
| 524 | """ |
| 525 | self.fastboot_run('reboot') |
| 526 | if not self.wait_down(command=FASTBOOT_CMD): |
| 527 | raise error.AutoservRebootError( |
| 528 | 'Device is still in fastboot mode after reboot') |
| 529 | if not self.wait_up(): |
| 530 | raise error.AutoservRebootError( |
| 531 | 'Device failed to boot to adb after fastboot reboot.') |
| 532 | self._reset_adbd_connection() |
| 533 | |
| 534 | |
Ralph Nathan | b45eb67 | 2015-11-18 20:04:39 -0800 | [diff] [blame] | 535 | def remount(self): |
| 536 | """Remounts paritions on the device read-write. |
| 537 | |
| 538 | Specifically, the /system, /vendor (if present) and /oem (if present) |
| 539 | partitions on the device are remounted read-write. |
| 540 | """ |
Ralph Nathan | b45eb67 | 2015-11-18 20:04:39 -0800 | [diff] [blame] | 541 | self.adb_run('remount') |
| 542 | |
| 543 | |
Kevin Cheng | 549beb4 | 2015-11-18 11:42:25 -0800 | [diff] [blame] | 544 | @staticmethod |
| 545 | def parse_device_serials(devices_output): |
| 546 | """Return a list of parsed serials from the output. |
| 547 | |
| 548 | @param devices_output: Output from either an adb or fastboot command. |
| 549 | |
| 550 | @returns List of device serials |
| 551 | """ |
| 552 | devices = [] |
| 553 | for line in devices_output.splitlines(): |
| 554 | match = re.search(DEVICE_FINDER_REGEX, line) |
| 555 | if match: |
| 556 | serial = match.group('SERIAL') |
| 557 | if serial == DEVICE_NO_SERIAL_MSG or re.match(r'^\?+$', serial): |
| 558 | serial = DEVICE_NO_SERIAL_TAG |
| 559 | logging.debug('Found Device: %s', serial) |
| 560 | devices.append(serial) |
| 561 | return devices |
| 562 | |
| 563 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 564 | def _get_devices(self, use_adb): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 565 | """Get a list of devices currently attached to the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 566 | |
| 567 | @params use_adb: True to get adb accessible devices. Set to False to |
| 568 | get fastboot accessible devices. |
| 569 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 570 | @returns a list of devices attached to the test station. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 571 | """ |
| 572 | if use_adb: |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 573 | result = self.adb_run('devices').stdout |
| 574 | if self.adb_serial and self.adb_serial not in result: |
| 575 | self._connect_over_tcpip_as_needed() |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 576 | else: |
Alexandru Branciog | ea380fb | 2016-04-01 16:01:34 +0300 | [diff] [blame] | 577 | result = self.fastboot_run('devices').stdout |
| 578 | if (self.fastboot_serial and |
| 579 | self.fastboot_serial not in result): |
| 580 | # fastboot devices won't list the devices using TCP |
| 581 | try: |
| 582 | if 'product' in self.fastboot_run('getvar product', |
| 583 | timeout=2).stderr: |
| 584 | result += '\n%s\tfastboot' % self.fastboot_serial |
| 585 | except error.AutotestHostRunError: |
| 586 | pass |
| 587 | return self.parse_device_serials(result) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 588 | |
| 589 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 590 | def adb_devices(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 591 | """Get a list of devices currently attached to the test station and |
| 592 | accessible with the adb command.""" |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 593 | devices = self._get_devices(use_adb=True) |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 594 | if self.adb_serial is None and len(devices) > 1: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 595 | raise error.AutoservError( |
| 596 | 'Not given ADB serial but multiple devices detected') |
| 597 | return devices |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 598 | |
| 599 | |
| 600 | def fastboot_devices(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 601 | """Get a list of devices currently attached to the test station and |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 602 | accessible by fastboot command. |
| 603 | """ |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 604 | devices = self._get_devices(use_adb=False) |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 605 | if self.fastboot_serial is None and len(devices) > 1: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 606 | raise error.AutoservError( |
| 607 | 'Not given fastboot serial but multiple devices detected') |
| 608 | return devices |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 609 | |
| 610 | |
| 611 | def is_up(self, timeout=0, command=ADB_CMD): |
| 612 | """Determine if the specified adb device is up with expected mode. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 613 | |
| 614 | @param timeout: Not currently used. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 615 | @param command: `adb`, the device can be accessed by adb command, |
| 616 | or `fastboot`, the device can be accessed by fastboot command. |
| 617 | Default is set to `adb`. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 618 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 619 | @returns True if the device is detectable by given command, False |
| 620 | otherwise. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 621 | |
| 622 | """ |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 623 | if command == ADB_CMD: |
| 624 | devices = self.adb_devices() |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 625 | serial = self.adb_serial |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 626 | # ADB has a device state, if the device is not online, no |
| 627 | # subsequent ADB command will complete. |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 628 | # DUT with single device connected may not have adb_serial set. |
| 629 | # Therefore, skip checking if serial is in the list of adb devices |
| 630 | # if self.adb_serial is not set. |
| 631 | 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] | 632 | logging.debug('Waiting for device to enter the ready state.') |
| 633 | return False |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 634 | elif command == FASTBOOT_CMD: |
| 635 | devices = self.fastboot_devices() |
Dan Shi | 50a412a | 2016-01-05 10:52:40 -0800 | [diff] [blame] | 636 | serial = self.fastboot_serial |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 637 | else: |
Gilad Arnold | 2cfa552 | 2015-10-30 13:58:02 -0700 | [diff] [blame] | 638 | raise NotImplementedError('Mode %s is not supported' % command) |
| 639 | |
| 640 | return bool(devices and (not serial or serial in devices)) |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 641 | |
| 642 | |
| 643 | def close(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 644 | """Close the ADBHost object. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 645 | |
| 646 | Called as the test ends. Will return the device to USB mode and kill |
| 647 | the ADB server. |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 648 | """ |
Christopher Wiley | 08849d5 | 2013-11-22 08:57:58 -0800 | [diff] [blame] | 649 | # TODO(sbasi) Originally, we would kill the server after each test to |
| 650 | # reduce the opportunity for bad server state to hang around. |
| 651 | # Unfortunately, there is a period of time after each kill during which |
| 652 | # the Android device becomes unusable, and if we start the next test |
| 653 | # too quickly, we'll get an error complaining about no ADB device |
| 654 | # attached. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 655 | #self.adb_run('kill-server') |
Roshan Pius | 3994260 | 2015-12-17 13:42:07 -0800 | [diff] [blame] | 656 | # |close| the associated teststation as well. |
| 657 | self.teststation.close() |
Simran Basi | 431010f | 2013-09-04 10:42:41 -0700 | [diff] [blame] | 658 | return super(ADBHost, self).close() |
Kris Rambish | 60461dc | 2014-03-11 11:10:18 -0700 | [diff] [blame] | 659 | |
| 660 | |
| 661 | def syslog(self, message, tag='autotest'): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 662 | """Logs a message to syslog on the device. |
Kris Rambish | 60461dc | 2014-03-11 11:10:18 -0700 | [diff] [blame] | 663 | |
| 664 | @param message String message to log into syslog |
| 665 | @param tag String tag prefix for syslog |
| 666 | |
| 667 | """ |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 668 | self.run('log -t "%s" "%s"' % (tag, message)) |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 669 | |
| 670 | |
| 671 | def get_autodir(self): |
| 672 | """Return the directory to install autotest for client side tests.""" |
| 673 | return '/data/autotest' |
| 674 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 675 | |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 676 | def is_device_ready(self): |
| 677 | """Return the if the device is ready for ADB commands.""" |
| 678 | dev_state = self.adb_run('get-state').stdout.strip() |
| 679 | logging.debug('Current device state: %s', dev_state) |
| 680 | return dev_state == 'device' |
| 681 | |
| 682 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 683 | def verify_connectivity(self): |
| 684 | """Verify we can connect to the device.""" |
Kris Rambish | de8f9d1 | 2015-12-16 12:42:41 -0800 | [diff] [blame] | 685 | if not self.is_device_ready(): |
| 686 | raise error.AutoservHostError('device state is not in the ' |
| 687 | '\'device\' state.') |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 688 | |
| 689 | |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 690 | def verify_software(self): |
| 691 | """Verify working software on an adb_host. |
| 692 | |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 693 | TODO (crbug.com/532222): Actually implement this method. |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 694 | """ |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 695 | # Check if adb and fastboot are present. |
| 696 | self.teststation.run('which adb') |
| 697 | self.teststation.run('which fastboot') |
| 698 | self.teststation.run('which unzip') |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 699 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 700 | |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 701 | def verify_job_repo_url(self, tag=''): |
| 702 | """Make sure job_repo_url of this host is valid. |
| 703 | |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 704 | TODO (crbug.com/532223): Actually implement this method. |
Simran Basi | d3ba3fb | 2015-09-11 14:35:07 -0700 | [diff] [blame] | 705 | |
| 706 | @param tag: The tag from the server job, in the format |
| 707 | <job_id>-<user>/<hostname>, or <hostless> for a server job. |
| 708 | """ |
| 709 | return |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 710 | |
| 711 | |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 712 | def repair(self): |
| 713 | """Attempt to get the DUT to pass `self.verify()`.""" |
| 714 | try: |
| 715 | self.ensure_adb_mode(timeout=30) |
| 716 | return |
| 717 | except error.AutoservError as e: |
| 718 | logging.error(e) |
| 719 | logging.debug('Verifying the device is accessible via fastboot.') |
| 720 | self.ensure_bootloader_mode() |
| 721 | if not self.job.run_test( |
| 722 | 'provision_AndroidUpdate', host=self, value=None, |
| 723 | force=True, repair=True): |
| 724 | raise error.AutoservRepairTotalFailure( |
| 725 | 'Unable to repair the device.') |
| 726 | |
| 727 | |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 728 | def send_file(self, source, dest, delete_dest=False, |
| 729 | preserve_symlinks=False): |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 730 | """Copy files from the drone to the device. |
| 731 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 732 | Just a note, there is the possibility the test station is localhost |
| 733 | which makes some of these steps redundant (e.g. creating tmp dir) but |
| 734 | that scenario will undoubtedly be a development scenario (test station |
| 735 | is also the moblab) and not the typical live test running scenario so |
| 736 | the redundancy I think is harmless. |
| 737 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 738 | @param source: The file/directory on the drone to send to the device. |
| 739 | @param dest: The destination path on the device to copy to. |
| 740 | @param delete_dest: A flag set to choose whether or not to delete |
| 741 | dest on the device if it exists. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 742 | @param preserve_symlinks: Controls if symlinks on the source will be |
| 743 | copied as such on the destination or |
| 744 | transformed into the referenced |
| 745 | file/directory. |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 746 | """ |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 747 | # If we need to preserve symlinks, let's check if the source is a |
| 748 | # symlink itself and if so, just create it on the device. |
| 749 | if preserve_symlinks: |
| 750 | symlink_target = None |
| 751 | try: |
| 752 | symlink_target = os.readlink(source) |
| 753 | except OSError: |
| 754 | # Guess it's not a symlink. |
| 755 | pass |
| 756 | |
| 757 | if symlink_target is not None: |
| 758 | # Once we create the symlink, let's get out of here. |
| 759 | self.run('ln -s %s %s' % (symlink_target, dest)) |
| 760 | return |
| 761 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 762 | # Stage the files on the test station. |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 763 | tmp_dir = self.teststation.get_tmp_dir() |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 764 | src_path = os.path.join(tmp_dir, os.path.basename(dest)) |
| 765 | # Now copy the file over to the test station so you can reference the |
| 766 | # file in the push command. |
| 767 | self.teststation.send_file(source, src_path, |
| 768 | preserve_symlinks=preserve_symlinks) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 769 | |
| 770 | if delete_dest: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 771 | self.run('rm -rf %s' % dest) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 772 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 773 | self.adb_run('push %s %s' % (src_path, dest)) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 774 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 775 | # Cleanup the test station. |
| 776 | try: |
| 777 | self.teststation.run('rm -rf %s' % tmp_dir) |
| 778 | except (error.AutoservRunError, error.AutoservSSHTimeout) as e: |
| 779 | logging.warn('failed to remove dir %s: %s', tmp_dir, e) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 780 | |
| 781 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 782 | def _get_file_info(self, dest): |
| 783 | """Get permission and possible symlink info about file on the device. |
| 784 | |
| 785 | These files are on the device so we only have shell commands (via adb) |
| 786 | to get the info we want. We'll use 'ls' to get it all. |
| 787 | |
| 788 | @param dest: File to get info about. |
| 789 | |
| 790 | @returns a dict of the file permissions and symlink. |
| 791 | """ |
Kevin Cheng | aaabd0c | 2015-11-10 16:05:04 -0800 | [diff] [blame] | 792 | # Grab file info. |
| 793 | file_info = self.run_output('ls -l %s' % dest) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 794 | symlink = None |
| 795 | perms = 0 |
| 796 | match = re.match(FILE_INFO_REGEX, file_info) |
| 797 | if match: |
| 798 | # Check if it's a symlink and grab the linked dest if it is. |
| 799 | if match.group('TYPE') == 'l': |
| 800 | symlink_match = re.match(FILE_SYMLINK_REGEX, file_info) |
| 801 | if symlink_match: |
| 802 | symlink = symlink_match.group('SYMLINK') |
| 803 | |
| 804 | # Set the perms. |
| 805 | for perm, perm_flag in zip(match.group('PERMS'), FILE_PERMS_FLAGS): |
| 806 | if perm != '-': |
| 807 | perms |= perm_flag |
| 808 | |
| 809 | return {'perms': perms, |
| 810 | 'symlink': symlink} |
| 811 | |
| 812 | |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 813 | def get_file(self, source, dest, delete_dest=False, preserve_perm=True, |
| 814 | preserve_symlinks=False): |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 815 | """Copy files from the device to the drone. |
| 816 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 817 | Just a note, there is the possibility the test station is localhost |
| 818 | which makes some of these steps redundant (e.g. creating tmp dir) but |
| 819 | that scenario will undoubtedly be a development scenario (test station |
| 820 | is also the moblab) and not the typical live test running scenario so |
| 821 | the redundancy I think is harmless. |
| 822 | |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 823 | @param source: The file/directory on the device to copy back to the |
| 824 | drone. |
| 825 | @param dest: The destination path on the drone to copy to. |
| 826 | @param delete_dest: A flag set to choose whether or not to delete |
| 827 | dest on the drone if it exists. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 828 | @param preserve_perm: Tells get_file() to try to preserve the sources |
| 829 | permissions on files and dirs. |
| 830 | @param preserve_symlinks: Try to preserve symlinks instead of |
| 831 | transforming them into files/dirs on copy. |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 832 | """ |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 833 | # Stage the files on the test station. |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 834 | tmp_dir = self.teststation.get_tmp_dir() |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 835 | dest_path = os.path.join(tmp_dir, os.path.basename(source)) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 836 | |
| 837 | if delete_dest: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 838 | self.teststation.run('rm -rf %s' % dest) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 839 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 840 | source_info = {} |
| 841 | if preserve_symlinks or preserve_perm: |
| 842 | source_info = self._get_file_info(source) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 843 | |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 844 | # If we want to preserve symlinks, just create it here, otherwise pull |
| 845 | # the file off the device. |
| 846 | if preserve_symlinks and source_info['symlink']: |
| 847 | os.symlink(source_info['symlink'], dest) |
| 848 | else: |
Roshan Pius | 9556714 | 2015-11-03 09:56:08 -0800 | [diff] [blame] | 849 | self.adb_run('pull %s %s' % (source, dest_path)) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 850 | |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 851 | # Copy over the file from the test station and clean up. |
| 852 | self.teststation.get_file(dest_path, dest) |
| 853 | try: |
| 854 | self.teststation.run('rm -rf %s' % tmp_dir) |
| 855 | except (error.AutoservRunError, error.AutoservSSHTimeout) as e: |
| 856 | logging.warn('failed to remove dir %s: %s', tmp_dir, e) |
Kevin Cheng | 92fe6ae | 2015-10-21 11:45:34 -0700 | [diff] [blame] | 857 | |
| 858 | if preserve_perm: |
| 859 | os.chmod(dest, source_info['perms']) |
Kevin Cheng | 018db35 | 2015-09-20 02:22:08 -0700 | [diff] [blame] | 860 | |
| 861 | |
| 862 | def get_release_version(self): |
| 863 | """Get the release version from the RELEASE_FILE on the device. |
| 864 | |
| 865 | @returns The release string in the RELEASE_FILE. |
| 866 | |
| 867 | """ |
| 868 | return self.run_output('getprop %s' % RELEASE_FILE) |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 869 | |
| 870 | |
| 871 | def get_tmp_dir(self, parent=''): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 872 | """Return a suitable temporary directory on the device. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 873 | |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 874 | We ensure this is a subdirectory of /data/local/tmp. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 875 | |
| 876 | @param parent: Parent directory of the returned tmp dir. |
| 877 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 878 | @returns a path to the temp directory on the host. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 879 | """ |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 880 | # TODO(kevcheng): Refactor the cleanup of tmp dir to be inherited |
| 881 | # from the parent. |
Simran Basi | 38f7ddf | 2015-09-18 12:25:03 -0700 | [diff] [blame] | 882 | if not parent.startswith(TMP_DIR): |
| 883 | parent = os.path.join(TMP_DIR, parent.lstrip(os.path.sep)) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 884 | self.run('mkdir -p %s' % parent) |
| 885 | tmp_dir = self.run_output('mktemp -d -p %s' % parent) |
| 886 | self.tmp_dirs.append(tmp_dir) |
| 887 | return tmp_dir |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 888 | |
| 889 | |
| 890 | def get_platform(self): |
| 891 | """Determine the correct platform label for this host. |
| 892 | |
| 893 | 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] | 894 | get_platform. |
Simran Basi | 1b02376 | 2015-09-25 12:12:20 -0700 | [diff] [blame] | 895 | |
| 896 | @returns a string representing this host's platform. |
| 897 | """ |
| 898 | return 'adb' |
| 899 | |
| 900 | |
Gilad Arnold | a76bef0 | 2015-09-29 13:55:15 -0700 | [diff] [blame] | 901 | def get_os_type(self): |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 902 | """Get the OS type of the DUT, e.g., android or brillo. |
| 903 | """ |
| 904 | if not self._os_type: |
| 905 | if self.run_output('getprop ro.product.brand') == 'Brillo': |
| 906 | self._os_type = OS_TYPE_BRILLO |
| 907 | else: |
| 908 | self._os_type = OS_TYPE_ANDROID |
| 909 | |
| 910 | return self._os_type |
Gilad Arnold | e452b1f | 2015-10-06 08:48:34 -0700 | [diff] [blame] | 911 | |
| 912 | |
| 913 | def _forward(self, reverse, args): |
| 914 | """Execute a forwarding command. |
| 915 | |
| 916 | @param reverse: Whether this is reverse forwarding (Boolean). |
| 917 | @param args: List of command arguments. |
| 918 | """ |
| 919 | cmd = '%s %s' % ('reverse' if reverse else 'forward', ' '.join(args)) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 920 | self.adb_run(cmd) |
Gilad Arnold | e452b1f | 2015-10-06 08:48:34 -0700 | [diff] [blame] | 921 | |
| 922 | |
| 923 | def add_forwarding(self, src, dst, reverse=False, rebind=True): |
| 924 | """Forward a port between the ADB host and device. |
| 925 | |
| 926 | Port specifications are any strings accepted as such by ADB, for |
| 927 | example 'tcp:8080'. |
| 928 | |
| 929 | @param src: Port specification to forward from. |
| 930 | @param dst: Port specification to forward to. |
| 931 | @param reverse: Do reverse forwarding from device to host (Boolean). |
| 932 | @param rebind: Allow rebinding an already bound port (Boolean). |
| 933 | """ |
| 934 | args = [] |
| 935 | if not rebind: |
| 936 | args.append('--no-rebind') |
| 937 | args += [src, dst] |
| 938 | self._forward(reverse, args) |
| 939 | |
| 940 | |
| 941 | def remove_forwarding(self, src=None, reverse=False): |
| 942 | """Removes forwarding on port. |
| 943 | |
| 944 | @param src: Port specification, or None to remove all forwarding. |
| 945 | @param reverse: Whether this is reverse forwarding (Boolean). |
| 946 | """ |
| 947 | args = [] |
| 948 | if src is None: |
| 949 | args.append('--remove-all') |
| 950 | else: |
| 951 | args += ['--remove', src] |
| 952 | self._forward(reverse, args) |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 953 | |
| 954 | |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 955 | def create_ssh_tunnel(self, port, local_port): |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 956 | """ |
| 957 | Forwards a port securely through a tunnel process from the server |
| 958 | to the DUT for RPC server connection. |
| 959 | Add a 'ADB forward' rule to forward the RPC packets from the AdbHost |
| 960 | to the DUT. |
| 961 | |
| 962 | @param port: remote port on the DUT. |
| 963 | @param local_port: local forwarding port. |
| 964 | |
| 965 | @return: the tunnel process. |
| 966 | """ |
| 967 | self.add_forwarding('tcp:%s' % port, 'tcp:%s' % port) |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 968 | return super(ADBHost, self).create_ssh_tunnel(port, local_port) |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 969 | |
| 970 | |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 971 | def disconnect_ssh_tunnel(self, tunnel_proc, port): |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 972 | """ |
| 973 | Disconnects a previously forwarded port from the server to the DUT for |
| 974 | RPC server connection. |
| 975 | Remove the previously added 'ADB forward' rule to forward the RPC |
| 976 | packets from the AdbHost to the DUT. |
| 977 | |
| 978 | @param tunnel_proc: the original tunnel process returned from |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 979 | |create_ssh_tunnel|. |
Roshan Pius | 58e5dd3 | 2015-10-16 15:16:42 -0700 | [diff] [blame] | 980 | @param port: remote port on the DUT. |
| 981 | |
| 982 | """ |
| 983 | self.remove_forwarding('tcp:%s' % port) |
xixuan | 6cf6d2f | 2016-01-29 15:29:00 -0800 | [diff] [blame] | 984 | super(ADBHost, self).disconnect_ssh_tunnel(tunnel_proc, port) |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 985 | |
| 986 | |
| 987 | def ensure_bootloader_mode(self): |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 988 | """Ensure the device is in bootloader mode. |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 989 | |
| 990 | @raise: error.AutoservError if the device failed to reboot into |
| 991 | bootloader mode. |
| 992 | """ |
| 993 | if self.is_up(command=FASTBOOT_CMD): |
| 994 | return |
| 995 | self.adb_run('reboot bootloader') |
| 996 | if not self.wait_up(command=FASTBOOT_CMD): |
| 997 | raise error.AutoservError( |
| 998 | 'The device failed to reboot into bootloader mode.') |
| 999 | |
| 1000 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1001 | def ensure_adb_mode(self, timeout=DEFAULT_WAIT_UP_TIME_SECONDS): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1002 | """Ensure the device is up and can be accessed by adb command. |
| 1003 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1004 | @param timeout: Time limit in seconds before returning even if the host |
| 1005 | is not up. |
| 1006 | |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1007 | @raise: error.AutoservError if the device failed to reboot into |
| 1008 | adb mode. |
| 1009 | """ |
| 1010 | if self.is_up(): |
| 1011 | return |
Dan Shi | 0498037 | 2016-03-22 10:57:47 -0700 | [diff] [blame] | 1012 | # Ignore timeout error to allow `fastboot reboot` to fail quietly and |
| 1013 | # check if the device is in adb mode. |
| 1014 | self.fastboot_run('reboot', timeout=timeout, ignore_timeout=True) |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1015 | if not self.wait_up(timeout=timeout): |
Dan Shi | 6ea3e1c | 2015-10-28 15:19:04 -0700 | [diff] [blame] | 1016 | raise error.AutoservError( |
| 1017 | 'The device failed to reboot into adb mode.') |
Roshan Pius | 4d7540c | 2015-12-16 13:30:32 -0800 | [diff] [blame] | 1018 | self._reset_adbd_connection() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1019 | |
| 1020 | |
| 1021 | @classmethod |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1022 | def get_build_info_from_build_url(cls, build_url): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1023 | """Get the Android build information from the build url. |
| 1024 | |
| 1025 | @param build_url: The url to use for downloading Android artifacts. |
| 1026 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1027 | |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1028 | @return: A dictionary of build information, including keys: |
| 1029 | build_target, branch, target, build_id. |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1030 | @raise AndroidInstallError: If failed to parse build_url. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1031 | """ |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1032 | if not build_url: |
| 1033 | raise AndroidInstallError('Need build_url to download image files.') |
| 1034 | |
| 1035 | try: |
| 1036 | match = re.match(DEVSERVER_URL_REGEX, build_url) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1037 | return {'build_target': match.group('BUILD_TARGET'), |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1038 | 'branch': match.group('BRANCH'), |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1039 | 'target': ('%s-%s' % (match.group('BUILD_TARGET'), |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1040 | match.group('BUILD_TYPE'))), |
| 1041 | 'build_id': match.group('BUILD_ID')} |
| 1042 | except (AttributeError, IndexError, ValueError) as e: |
| 1043 | raise AndroidInstallError( |
| 1044 | 'Failed to parse build url: %s\nError: %s' % (build_url, e)) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1045 | |
| 1046 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1047 | @retry.retry(error.AutoservRunError, timeout_min=10) |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1048 | def download_file(self, build_url, file, dest_dir, unzip=False, |
| 1049 | unzip_dest=None): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1050 | """Download the given file from the build url. |
| 1051 | |
| 1052 | @param build_url: The url to use for downloading Android artifacts. |
| 1053 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1054 | @param file: Name of the file to be downloaded, e.g., boot.img. |
| 1055 | @param dest_dir: Destination folder for the file to be downloaded to. |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1056 | @param unzip: If True, unzip the downloaded file. |
| 1057 | @param unzip_dest: Location to unzip the downloaded file to. If not |
| 1058 | provided, dest_dir is used. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1059 | """ |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1060 | # Append the file name to the url if build_url is linked to the folder |
| 1061 | # containing the file. |
| 1062 | if not build_url.endswith('/%s' % file): |
| 1063 | src_url = os.path.join(build_url, file) |
| 1064 | else: |
| 1065 | src_url = build_url |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1066 | dest_file = os.path.join(dest_dir, file) |
| 1067 | try: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1068 | self.teststation.run('wget -q -O "%s" "%s"' % (dest_file, src_url)) |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1069 | if unzip: |
| 1070 | unzip_dest = unzip_dest or dest_dir |
| 1071 | self.teststation.run('unzip "%s/%s" -x -d "%s"' % |
| 1072 | (dest_dir, file, unzip_dest)) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1073 | except: |
| 1074 | # Delete the destination file if download failed. |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1075 | self.teststation.run('rm -f "%s"' % dest_file) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1076 | raise |
| 1077 | |
| 1078 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1079 | def stage_android_image_files(self, build_url): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1080 | """Download required image files from the given build_url to a local |
| 1081 | directory in the machine runs fastboot command. |
| 1082 | |
| 1083 | @param build_url: The url to use for downloading Android artifacts. |
| 1084 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1085 | |
| 1086 | @return: Path to the directory contains image files. |
| 1087 | """ |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1088 | build_info = self.get_build_info_from_build_url(build_url) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1089 | |
| 1090 | zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info |
Kevin Cheng | 85e864a | 2015-11-30 11:49:34 -0800 | [diff] [blame] | 1091 | image_dir = self.teststation.get_tmp_dir() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1092 | |
| 1093 | try: |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1094 | self.download_file(build_url, zipped_image_file, image_dir, |
| 1095 | unzip=True) |
| 1096 | for image_file in ANDROID_STANDALONE_IMAGES: |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1097 | self.download_file(build_url, image_file, image_dir) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1098 | |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1099 | return image_dir |
| 1100 | except: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1101 | self.teststation.run('rm -rf %s' % image_dir) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1102 | raise |
| 1103 | |
| 1104 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1105 | def stage_brillo_image_files(self, build_url): |
| 1106 | """Download required brillo image files from the given build_url to a |
| 1107 | local directory in the machine runs fastboot command. |
| 1108 | |
| 1109 | @param build_url: The url to use for downloading Android artifacts. |
| 1110 | pattern: http://$devserver:###/static/branch/target/build_id |
| 1111 | |
| 1112 | @return: Path to the directory contains image files. |
| 1113 | """ |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1114 | build_info = self.get_build_info_from_build_url(build_url) |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1115 | |
| 1116 | zipped_image_file = ANDROID_IMAGE_FILE_FMT % build_info |
| 1117 | vendor_partitions_file = BRILLO_VENDOR_PARTITIONS_FILE_FMT % build_info |
| 1118 | image_dir = self.teststation.get_tmp_dir() |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1119 | |
| 1120 | try: |
Simran Basi | 7756a0b | 2016-03-16 13:10:07 -0700 | [diff] [blame] | 1121 | self.download_file(build_url, zipped_image_file, image_dir, |
| 1122 | unzip=True) |
| 1123 | self.download_file(build_url, vendor_partitions_file, image_dir, |
| 1124 | unzip=True, |
| 1125 | unzip_dest=os.path.join(image_dir, 'vendor')) |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1126 | return image_dir |
| 1127 | except: |
| 1128 | self.teststation.run('rm -rf %s' % image_dir) |
| 1129 | raise |
| 1130 | |
| 1131 | |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1132 | def stage_build_for_install(self, build_name, os_type=None): |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1133 | """Stage a build on a devserver and return the build_url and devserver. |
| 1134 | |
| 1135 | @param build_name: a name like git-master/shamu-userdebug/2040953 |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1136 | |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1137 | @returns a tuple with an update URL like: |
| 1138 | http://172.22.50.122:8080/git-master/shamu-userdebug/2040953 |
| 1139 | and the devserver instance. |
| 1140 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1141 | os_type = os_type or self.get_os_type() |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1142 | logging.info('Staging build for installation: %s', build_name) |
Dan Shi | 216389c | 2015-12-22 11:03:06 -0800 | [diff] [blame] | 1143 | devserver = dev_server.AndroidBuildServer.resolve(build_name, |
| 1144 | self.hostname) |
Dan Shi | ba94353 | 2015-12-03 08:58:00 -0800 | [diff] [blame] | 1145 | build_name = devserver.translate(build_name) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1146 | branch, target, build_id = utils.parse_launch_control_build(build_name) |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1147 | is_brillo = os_type == OS_TYPE_BRILLO |
Dan Shi | 08ff128 | 2016-02-18 19:51:16 -0800 | [diff] [blame] | 1148 | devserver.trigger_download(target, build_id, branch, |
| 1149 | is_brillo=is_brillo, synchronous=False) |
Dan Shi | f2fd076 | 2015-12-01 10:09:32 -0800 | [diff] [blame] | 1150 | return '%s/static/%s' % (devserver.url(), build_name), devserver |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1151 | |
| 1152 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1153 | def install_android(self, build_url, build_local_path=None, wipe=True, |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1154 | flash_all=False): |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1155 | """Install the Android DUT. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1156 | |
| 1157 | Following are the steps used here to provision an android device: |
| 1158 | 1. If build_local_path is not set, download the image zip file, e.g., |
| 1159 | shamu-img-2284311.zip, unzip it. |
| 1160 | 2. Run fastboot to install following artifacts: |
| 1161 | bootloader, radio, boot, system, vendor(only if exists) |
| 1162 | |
| 1163 | Repair is not supported for Android devices yet. |
| 1164 | |
| 1165 | @param build_url: The url to use for downloading Android artifacts. |
| 1166 | pattern: http://$devserver:###/static/$build |
| 1167 | @param build_local_path: The path to a local folder that contains the |
| 1168 | image files needed to provision the device. Note that the folder |
| 1169 | is in the machine running adb command, rather than the drone. |
| 1170 | @param wipe: If true, userdata will be wiped before flashing. |
| 1171 | @param flash_all: If True, all img files found in img_path will be |
| 1172 | flashed. Otherwise, only boot and system are flashed. |
| 1173 | |
| 1174 | @raises AndroidInstallError if any error occurs. |
| 1175 | """ |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1176 | # If the build is not staged in local server yet, clean up the temp |
| 1177 | # folder used to store image files after the provision is completed. |
| 1178 | delete_build_folder = bool(not build_local_path) |
| 1179 | |
| 1180 | try: |
| 1181 | # Download image files needed for provision to a local directory. |
| 1182 | if not build_local_path: |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1183 | build_local_path = self.stage_android_image_files(build_url) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1184 | |
| 1185 | # Device needs to be in bootloader mode for flashing. |
| 1186 | self.ensure_bootloader_mode() |
| 1187 | |
| 1188 | if wipe: |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1189 | self.fastboot_run('-w') |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1190 | |
| 1191 | # Get all *.img file in the build_local_path. |
| 1192 | 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] | 1193 | image_files = self.teststation.run( |
| 1194 | list_file_cmd).stdout.strip().split() |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1195 | images = dict([(os.path.basename(f), f) for f in image_files]) |
| 1196 | for image, image_file in images.items(): |
| 1197 | if image not in ANDROID_IMAGES: |
| 1198 | continue |
| 1199 | logging.info('Flashing %s...', image_file) |
Dan Shi | 70c3019 | 2016-03-22 23:14:12 -0700 | [diff] [blame] | 1200 | self.fastboot_run('-S 256M flash %s %s' % |
Dan Shi | e9913d3 | 2016-03-09 14:17:26 -0800 | [diff] [blame] | 1201 | (image[:-4], image_file)) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1202 | if image == ANDROID_BOOTLOADER: |
| 1203 | self.fastboot_run('reboot-bootloader') |
| 1204 | self.wait_up(command=FASTBOOT_CMD) |
| 1205 | except Exception as e: |
| 1206 | logging.error('Install Android build failed with error: %s', e) |
| 1207 | # Re-raise the exception with type of AndroidInstallError. |
| 1208 | raise AndroidInstallError, sys.exc_info()[1], sys.exc_info()[2] |
| 1209 | finally: |
| 1210 | if delete_build_folder: |
Kevin Cheng | 5f6ed6a | 2015-10-28 16:18:12 -0400 | [diff] [blame] | 1211 | self.teststation.run('rm -rf %s' % build_local_path) |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1212 | timeout = (WAIT_UP_AFTER_WIPE_TIME_SECONDS if wipe else |
| 1213 | DEFAULT_WAIT_UP_TIME_SECONDS) |
| 1214 | self.ensure_adb_mode(timeout=timeout) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1215 | logging.info('Successfully installed Android build staged at %s.', |
| 1216 | build_url) |
| 1217 | |
| 1218 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1219 | def install_brillo(self, build_url, build_local_path=None): |
| 1220 | """Install the Brillo DUT. |
| 1221 | |
| 1222 | Following are the steps used here to provision an android device: |
| 1223 | 1. If build_local_path is not set, download the image zip file, e.g., |
| 1224 | dragonboard-img-123456.zip, unzip it. And download the vendor |
| 1225 | partition zip file, e.g., dragonboard-vendor_partitions-123456.zip, |
| 1226 | unzip it to vendor folder. |
| 1227 | 2. Run provision_device script to install OS images and vendor |
| 1228 | partitions. |
| 1229 | |
| 1230 | @param build_url: The url to use for downloading Android artifacts. |
| 1231 | pattern: http://$devserver:###/static/$build |
| 1232 | @param build_local_path: The path to a local folder that contains the |
| 1233 | image files needed to provision the device. Note that the folder |
| 1234 | is in the machine running adb command, rather than the drone. |
| 1235 | |
| 1236 | @raises AndroidInstallError if any error occurs. |
| 1237 | """ |
| 1238 | # If the build is not staged in local server yet, clean up the temp |
| 1239 | # folder used to store image files after the provision is completed. |
| 1240 | delete_build_folder = bool(not build_local_path) |
| 1241 | |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1242 | try: |
| 1243 | # Download image files needed for provision to a local directory. |
| 1244 | if not build_local_path: |
| 1245 | build_local_path = self.stage_brillo_image_files(build_url) |
| 1246 | |
| 1247 | # Device needs to be in bootloader mode for flashing. |
| 1248 | self.ensure_bootloader_mode() |
| 1249 | |
| 1250 | # Run provision_device command to install image files and vendor |
| 1251 | # partitions. |
| 1252 | vendor_partition_dir = os.path.join(build_local_path, 'vendor') |
| 1253 | cmd = (BRILLO_PROVISION_CMD % |
| 1254 | {'os_image_dir': build_local_path, |
| 1255 | 'vendor_partition_dir': vendor_partition_dir}) |
Dan Shi | 86bd8c0 | 2016-03-10 09:21:51 -0800 | [diff] [blame] | 1256 | if self.fastboot_serial: |
Dan Shi | 91b4235 | 2016-03-10 22:12:22 -0800 | [diff] [blame] | 1257 | cmd += ' -s %s ' % self.fastboot_serial |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1258 | self.teststation.run(cmd) |
| 1259 | except Exception as e: |
| 1260 | logging.error('Install Brillo build failed with error: %s', e) |
| 1261 | # Re-raise the exception with type of AndroidInstallError. |
| 1262 | raise AndroidInstallError, sys.exc_info()[1], sys.exc_info()[2] |
| 1263 | finally: |
| 1264 | if delete_build_folder: |
| 1265 | self.teststation.run('rm -rf %s' % build_local_path) |
| 1266 | self.ensure_adb_mode() |
| 1267 | logging.info('Successfully installed Android build staged at %s.', |
| 1268 | build_url) |
| 1269 | |
| 1270 | |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1271 | @property |
| 1272 | def job_repo_url_attribute(self): |
| 1273 | """Get the host attribute name for job_repo_url, which should append the |
| 1274 | adb serial. |
| 1275 | """ |
| 1276 | return '%s_%s' % (constants.JOB_REPO_URL, self.adb_serial) |
| 1277 | |
| 1278 | |
Dan Shi | e4e807b | 2015-12-10 09:04:03 -0800 | [diff] [blame] | 1279 | 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] | 1280 | flash_all=False, os_type=None): |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1281 | """Install the DUT. |
| 1282 | |
| 1283 | @param build_url: The url to use for downloading Android artifacts. |
Dan Shi | 225b904 | 2015-11-18 10:25:21 -0800 | [diff] [blame] | 1284 | pattern: http://$devserver:###/static/$build. If build_url is |
| 1285 | set to None, the code may try _parser.options.image to do the |
| 1286 | installation. If none of them is set, machine_install will fail. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1287 | @param build_local_path: The path to a local directory that contains the |
| 1288 | image files needed to provision the device. |
| 1289 | @param wipe: If true, userdata will be wiped before flashing. |
| 1290 | @param flash_all: If True, all img files found in img_path will be |
| 1291 | flashed. Otherwise, only boot and system are flashed. |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1292 | |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1293 | @returns A tuple of (image_name, host_attributes). |
| 1294 | image_name is the name of image installed, e.g., |
| 1295 | git_mnc-release/shamu-userdebug/1234 |
| 1296 | host_attributes is a dictionary of (attribute, value), which |
| 1297 | can be saved to afe_host_attributes table in database. This |
| 1298 | method returns a dictionary with a single entry of |
| 1299 | `job_repo_url_[adb_serial]`: devserver_url, where devserver_url |
| 1300 | is a url to the build staged on devserver. |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1301 | """ |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1302 | os_type = os_type or self.get_os_type() |
Simran Basi | 5ace6f2 | 2016-01-06 17:30:44 -0800 | [diff] [blame] | 1303 | if not build_url and self._parser.options.image: |
| 1304 | build_url, _ = self.stage_build_for_install( |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1305 | self._parser.options.image, os_type=os_type) |
| 1306 | if os_type == OS_TYPE_ANDROID: |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1307 | self.install_android( |
| 1308 | build_url=build_url, build_local_path=build_local_path, |
| 1309 | wipe=wipe, flash_all=flash_all) |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 1310 | elif os_type == OS_TYPE_BRILLO: |
Dan Shi | ab99972 | 2015-12-04 14:27:08 -0800 | [diff] [blame] | 1311 | self.install_brillo( |
| 1312 | build_url=build_url, build_local_path=build_local_path) |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 1313 | else: |
| 1314 | raise error.InstallError( |
| 1315 | 'Installation of os type %s is not supported.' % |
| 1316 | self.get_os_type()) |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1317 | return (build_url.split('static/')[-1], |
| 1318 | {self.job_repo_url_attribute: build_url}) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1319 | |
| 1320 | |
| 1321 | def list_files_glob(self, path_glob): |
| 1322 | """Get a list of files on the device given glob pattern path. |
| 1323 | |
| 1324 | @param path_glob: The path glob that we want to return the list of |
| 1325 | files that match the glob. Relative paths will not work as |
| 1326 | expected. Supply an absolute path to get the list of files |
| 1327 | you're hoping for. |
| 1328 | |
| 1329 | @returns List of files that match the path_glob. |
| 1330 | """ |
| 1331 | # This is just in case path_glob has no path separator. |
| 1332 | base_path = os.path.dirname(path_glob) or '.' |
| 1333 | result = self.run('find %s -path \'%s\' -print' % |
Christopher Wiley | d21d66a | 2016-03-01 10:58:13 -0800 | [diff] [blame] | 1334 | (base_path, path_glob), ignore_status=True) |
Kevin Cheng | d19e6c6 | 2015-10-28 16:39:39 -0700 | [diff] [blame] | 1335 | if result.exit_status != 0: |
| 1336 | return [] |
| 1337 | return result.stdout.splitlines() |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1338 | |
| 1339 | |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1340 | def install_apk(self, apk, force_reinstall=False): |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1341 | """Install the specified apk. |
| 1342 | |
| 1343 | This will install the apk and override it if it's already installed and |
| 1344 | will also allow for downgraded apks. |
| 1345 | |
| 1346 | @param apk: The path to apk file. |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1347 | @param force_reinstall: True to reinstall the apk even if it's already |
| 1348 | installed. Default is set to False. |
| 1349 | |
| 1350 | @returns a CMDResult object. |
Kevin Cheng | 3135594 | 2016-01-05 14:23:35 -0800 | [diff] [blame] | 1351 | """ |
Simran Basi | 9c5d398 | 2016-04-01 18:49:44 -0700 | [diff] [blame] | 1352 | client_utils.poll_for_condition( |
| 1353 | lambda: self.run('pm list packages', |
| 1354 | ignore_status=True).exit_status == 0, |
| 1355 | timeout=120) |
Dan Shi | 043c45b | 2016-04-11 17:18:49 -0700 | [diff] [blame^] | 1356 | client_utils.poll_for_condition( |
| 1357 | lambda: self.run('service list | grep mount', |
| 1358 | ignore_status=True).exit_status == 0, |
| 1359 | timeout=120) |
Dan Shi | db0366c | 2016-02-19 10:36:18 -0800 | [diff] [blame] | 1360 | return self.adb_run('install %s -d %s' % |
| 1361 | ('-r' if force_reinstall else '', apk)) |
| 1362 | |
| 1363 | |
| 1364 | @retry.retry(error.AutoservRunError, timeout_min=0.2) |
| 1365 | def _confirm_apk_installed(self, package_name): |
| 1366 | """Confirm if apk is already installed with the given name. |
| 1367 | |
| 1368 | `pm list packages` command is not reliable some time. The retry helps to |
| 1369 | reduce the chance of false negative. |
| 1370 | |
| 1371 | @param package_name: Name of the package, e.g., com.android.phone. |
| 1372 | |
| 1373 | @raise AutoservRunError: If the package is not found or pm list command |
| 1374 | failed for any reason. |
| 1375 | """ |
| 1376 | name = 'package:%s' % package_name |
| 1377 | self.adb_run('shell pm list packages | grep -w "%s"' % name) |
| 1378 | |
| 1379 | |
| 1380 | def is_apk_installed(self, package_name): |
| 1381 | """Check if apk is already installed with the given name. |
| 1382 | |
| 1383 | @param package_name: Name of the package, e.g., com.android.phone. |
| 1384 | |
| 1385 | @return: True if package is installed. False otherwise. |
| 1386 | """ |
| 1387 | try: |
| 1388 | self._confirm_apk_installed(package_name) |
| 1389 | return True |
| 1390 | except: |
| 1391 | return False |
Dan Shi | be3636a | 2016-02-14 22:48:01 -0800 | [diff] [blame] | 1392 | |
| 1393 | |
| 1394 | def get_attributes_to_clear_before_provision(self): |
| 1395 | """Get a list of attributes to be cleared before machine_install starts. |
| 1396 | """ |
| 1397 | return [self.job_repo_url_attribute] |
Kevin Cheng | c6a645a | 2015-12-18 11:15:10 -0800 | [diff] [blame] | 1398 | |
| 1399 | |
| 1400 | def get_labels(self): |
| 1401 | """Return a list of the labels gathered from the devices connected. |
| 1402 | |
| 1403 | @return: A list of strings that denote the labels from all the devices |
| 1404 | connected. |
| 1405 | """ |
| 1406 | return self.labels.get_labels(self) |
| 1407 | |
| 1408 | |
| 1409 | def update_labels(self): |
| 1410 | """Update the labels for this testbed.""" |
| 1411 | self.labels.update_labels(self) |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 1412 | |
| 1413 | |
| 1414 | def stage_server_side_package(self, image=None): |
| 1415 | """Stage autotest server-side package on devserver. |
| 1416 | |
| 1417 | @param image: A build name, e.g., git_mnc_dev/shamu-eng/123 |
| 1418 | |
| 1419 | @return: A url to the autotest server-side package. Return None if |
| 1420 | server-side package is not supported. |
| 1421 | @raise: error.AutoservError if fail to locate the build to test with. |
| 1422 | """ |
| 1423 | if image: |
| 1424 | ds = dev_server.AndroidBuildServer.resolve(image, self.hostname) |
| 1425 | else: |
| 1426 | job_repo_url = afe_utils.get_host_attribute( |
| 1427 | self, self.job_repo_url_attribute) |
| 1428 | if job_repo_url: |
| 1429 | devserver_url, image = ( |
| 1430 | tools.get_devserver_build_from_package_url( |
| 1431 | job_repo_url, True)) |
| 1432 | ds = dev_server.AndroidBuildServer(devserver_url) |
| 1433 | else: |
| 1434 | labels = afe_utils.get_labels(self, self.VERSION_PREFIX) |
| 1435 | if not labels: |
| 1436 | raise error.AutoservError( |
| 1437 | 'Failed to stage server-side package. The host has ' |
| 1438 | 'no job_report_url attribute or version label.') |
| 1439 | image = labels[0].name[len(self.VERSION_PREFIX)+1:] |
| 1440 | ds = dev_server.AndroidBuildServer.resolve(image, self.hostname) |
| 1441 | |
| 1442 | branch, target, build_id = utils.parse_launch_control_build(image) |
| 1443 | build_target, _ = utils.parse_launch_control_target(target) |
| 1444 | |
| 1445 | # For any build older than MIN_VERSION_SUPPORT_SSP, server side |
| 1446 | # packaging is not supported. |
| 1447 | try: |
| 1448 | if int(build_id) < self.MIN_VERSION_SUPPORT_SSP: |
| 1449 | logging.warn('Build %s is older than %s. Server side packaging ' |
| 1450 | 'is disabled.', image, |
| 1451 | self.MIN_VERSION_SUPPORT_SSP) |
| 1452 | return None |
| 1453 | except ValueError: |
| 1454 | logging.warn('Failed to compare build id in %s with the minimum ' |
| 1455 | 'version that supports server side packaging. Server ' |
| 1456 | 'side packaging is disabled.', image) |
| 1457 | return None |
| 1458 | |
| 1459 | ds.stage_artifacts(target, build_id, branch, |
| 1460 | artifacts=['autotest_server_package']) |
| 1461 | autotest_server_package_name = (AUTOTEST_SERVER_PACKAGE_FILE_FMT % |
| 1462 | {'build_target': build_target, |
| 1463 | 'build_id': build_id}) |
| 1464 | return '%s/static/%s/%s' % (ds.url(), image, |
| 1465 | autotest_server_package_name) |