J. Richard Barnette | 24adbf4 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Dale Curtis | aa5eedb | 2011-08-23 16:18:52 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 5 | import functools |
Darren Krahn | 0bd18e8 | 2015-10-28 23:30:46 +0000 | [diff] [blame] | 6 | import json |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 7 | import logging |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 8 | import os |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 9 | import re |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 10 | import time |
| 11 | |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 12 | import common |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 13 | from autotest_lib.client.bin import utils |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import autotemp |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import error |
| 16 | from autotest_lib.client.common_lib import global_config |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import lsbrelease_utils |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib.cros import autoupdater |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 19 | from autotest_lib.client.common_lib.cros import dev_server |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 20 | from autotest_lib.client.common_lib.cros.graphite import autotest_es |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 21 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Hsinyu Chao | e0b08e6 | 2015-08-11 10:50:37 +0000 | [diff] [blame] | 22 | from autotest_lib.client.cros import constants as client_constants |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 23 | from autotest_lib.client.cros import cros_ui |
Cheng-Yi Chiang | f4104ff | 2014-12-23 19:39:01 +0800 | [diff] [blame] | 24 | from autotest_lib.client.cros.audio import cras_utils |
Katherine Threlkeld | ab83d39 | 2015-06-18 16:45:57 -0700 | [diff] [blame] | 25 | from autotest_lib.client.cros.input_playback import input_playback |
Mussa | 5b58905 | 2015-10-26 17:55:26 -0700 | [diff] [blame] | 26 | from autotest_lib.client.cros.video import constants as video_test_constants |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 27 | from autotest_lib.server import autoserv_parser |
| 28 | from autotest_lib.server import autotest |
| 29 | from autotest_lib.server import constants |
| 30 | from autotest_lib.server import crashcollect |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 31 | from autotest_lib.server import utils as server_utils |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 32 | from autotest_lib.server.cros import provision |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 33 | from autotest_lib.server.cros.dynamic_suite import constants as ds_constants |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 34 | from autotest_lib.server.cros.dynamic_suite import tools, frontend_wrappers |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 35 | from autotest_lib.server.cros.faft.config.config import Config as FAFTConfig |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 36 | from autotest_lib.server.hosts import abstract_ssh |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 37 | from autotest_lib.server.hosts import chameleon_host |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 38 | from autotest_lib.server.hosts import servo_host |
Simran Basi | dcff425 | 2012-11-20 16:13:20 -0800 | [diff] [blame] | 39 | from autotest_lib.site_utils.rpm_control_system import rpm_client |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 40 | |
| 41 | |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 42 | CONFIG = global_config.global_config |
| 43 | |
Eric Caruso | ee673ac | 2015-08-05 17:03:04 -0700 | [diff] [blame] | 44 | LUCID_SLEEP_BOARDS = ['samus', 'lulu'] |
| 45 | |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 46 | # A file to indicate provision failure and require Repair job to powerwash the |
| 47 | # dut. |
| 48 | PROVISION_FAILED = '/var/tmp/provision_failed' |
| 49 | |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 50 | class FactoryImageCheckerException(error.AutoservError): |
| 51 | """Exception raised when an image is a factory image.""" |
| 52 | pass |
| 53 | |
| 54 | |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 55 | class CrosHost(abstract_ssh.AbstractSSHHost): |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 56 | """Chromium OS specific subclass of Host.""" |
| 57 | |
| 58 | _parser = autoserv_parser.autoserv_parser |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 59 | _AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 60 | |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 61 | # Timeout values (in seconds) associated with various Chrome OS |
| 62 | # state changes. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 63 | # |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 64 | # In general, a good rule of thumb is that the timeout can be up |
| 65 | # to twice the typical measured value on the slowest platform. |
| 66 | # The times here have not necessarily been empirically tested to |
| 67 | # meet this criterion. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 68 | # |
| 69 | # SLEEP_TIMEOUT: Time to allow for suspend to memory. |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 70 | # RESUME_TIMEOUT: Time to allow for resume after suspend, plus |
| 71 | # time to restart the netwowrk. |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 72 | # SHUTDOWN_TIMEOUT: Time to allow for shut down. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 73 | # BOOT_TIMEOUT: Time to allow for boot from power off. Among |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 74 | # other things, this must account for the 30 second dev-mode |
J. Richard Barnette | 417cc79 | 2015-10-01 09:56:36 -0700 | [diff] [blame] | 75 | # screen delay, time to start the network on the DUT, and the |
| 76 | # ssh timeout of 120 seconds. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 77 | # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device, |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 78 | # including the 30 second dev-mode delay and time to start the |
J. Richard Barnette | d4649c6 | 2013-03-06 17:42:27 -0800 | [diff] [blame] | 79 | # network. |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 80 | # INSTALL_TIMEOUT: Time to allow for chromeos-install. |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 81 | # POWERWASH_BOOT_TIMEOUT: Time to allow for a reboot that |
| 82 | # includes powerwash. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 83 | |
| 84 | SLEEP_TIMEOUT = 2 |
J. Richard Barnette | d4649c6 | 2013-03-06 17:42:27 -0800 | [diff] [blame] | 85 | RESUME_TIMEOUT = 10 |
Tom Wai-Hong Tam | fe005c2 | 2014-12-03 09:25:44 +0800 | [diff] [blame] | 86 | SHUTDOWN_TIMEOUT = 10 |
J. Richard Barnette | 417cc79 | 2015-10-01 09:56:36 -0700 | [diff] [blame] | 87 | BOOT_TIMEOUT = 150 |
J. Richard Barnette | 5bab5f5 | 2015-08-03 13:14:38 -0700 | [diff] [blame] | 88 | USB_BOOT_TIMEOUT = 300 |
J. Richard Barnette | 7817b05 | 2014-08-28 09:47:29 -0700 | [diff] [blame] | 89 | INSTALL_TIMEOUT = 480 |
Dan Shi | 2c88eed | 2013-11-12 10:18:38 -0800 | [diff] [blame] | 90 | POWERWASH_BOOT_TIMEOUT = 60 |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 91 | |
Dan Shi | ca50348 | 2015-03-30 17:23:25 -0700 | [diff] [blame] | 92 | # Minimum OS version that supports server side packaging. Older builds may |
| 93 | # not have server side package built or with Autotest code change to support |
| 94 | # server-side packaging. |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 95 | MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value( |
Dan Shi | ced09e4 | 2015-04-17 16:09:34 -0700 | [diff] [blame] | 96 | 'AUTOSERV', 'min_version_support_ssp', type=int) |
Dan Shi | ca50348 | 2015-03-30 17:23:25 -0700 | [diff] [blame] | 97 | |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 98 | # REBOOT_TIMEOUT: How long to wait for a reboot. |
| 99 | # |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 100 | # We have a long timeout to ensure we don't flakily fail due to other |
| 101 | # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate. |
Simran Basi | 1160e2c | 2013-10-04 16:00:24 -0700 | [diff] [blame] | 102 | # TODO(sbasi - crbug.com/276094) Restore to 5 mins once the 'host did not |
| 103 | # return from reboot' bug is solved. |
| 104 | REBOOT_TIMEOUT = 480 |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 105 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 106 | # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF. |
| 107 | # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle. |
| 108 | _USB_POWER_TIMEOUT = 5 |
| 109 | _POWER_CYCLE_TIMEOUT = 10 |
| 110 | |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 111 | _RPM_RECOVERY_BOARDS = CONFIG.get_config_value('CROS', |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 112 | 'rpm_recovery_boards', type=str).split(',') |
| 113 | |
| 114 | _MAX_POWER_CYCLE_ATTEMPTS = 6 |
| 115 | _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine' |
Fang Deng | deba14f | 2014-11-14 11:54:09 -0800 | [diff] [blame] | 116 | _RPM_HOSTNAME_REGEX = ('chromeos(\d+)(-row(\d+))?-rack(\d+[a-z]*)' |
| 117 | '-host(\d+)') |
Katherine Threlkeld | ab83d39 | 2015-06-18 16:45:57 -0700 | [diff] [blame] | 118 | _LIGHTSENSOR_FILES = [ "in_illuminance0_input", |
| 119 | "in_illuminance_input", |
| 120 | "in_illuminance0_raw", |
| 121 | "in_illuminance_raw", |
| 122 | "illuminance0_input"] |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 123 | _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices' |
| 124 | _LABEL_FUNCTIONS = [] |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 125 | _DETECTABLE_LABELS = [] |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 126 | label_decorator = functools.partial(server_utils.add_label_detector, |
| 127 | _LABEL_FUNCTIONS, |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 128 | _DETECTABLE_LABELS) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 129 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 130 | # Constants used in ping_wait_up() and ping_wait_down(). |
| 131 | # |
| 132 | # _PING_WAIT_COUNT is the approximate number of polling |
| 133 | # cycles to use when waiting for a host state change. |
| 134 | # |
| 135 | # _PING_STATUS_DOWN and _PING_STATUS_UP are names used |
| 136 | # for arguments to the internal _ping_wait_for_status() |
| 137 | # method. |
| 138 | _PING_WAIT_COUNT = 40 |
| 139 | _PING_STATUS_DOWN = False |
| 140 | _PING_STATUS_UP = True |
| 141 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 142 | # Allowed values for the power_method argument. |
| 143 | |
| 144 | # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods. |
| 145 | # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods. |
| 146 | # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods. |
| 147 | POWER_CONTROL_RPM = 'RPM' |
| 148 | POWER_CONTROL_SERVO = 'servoj10' |
| 149 | POWER_CONTROL_MANUAL = 'manual' |
| 150 | |
| 151 | POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM, |
| 152 | POWER_CONTROL_SERVO, |
| 153 | POWER_CONTROL_MANUAL) |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 154 | |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 155 | _RPM_OUTLET_CHANGED = 'outlet_changed' |
| 156 | |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 157 | # URL pattern to download firmware image. |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 158 | _FW_IMAGE_URL_PATTERN = CONFIG.get_config_value( |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 159 | 'CROS', 'firmware_url_pattern', type=str) |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 160 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 161 | # File that has a list of directories to be collected |
| 162 | _LOGS_TO_COLLECT_FILE = os.path.join( |
| 163 | common.client_dir, 'common_lib', 'logs_to_collect') |
| 164 | |
| 165 | # Prefix of logging message w.r.t. crash collection |
| 166 | _CRASHLOGS_PREFIX = 'collect_crashlogs' |
| 167 | |
| 168 | # Time duration waiting for host up/down check |
| 169 | _CHECK_HOST_UP_TIMEOUT_SECS = 15 |
| 170 | |
| 171 | # A command that interacts with kernel and hardware (e.g., rm, mkdir, etc) |
| 172 | # might not be completely done deep through the hardware when the machine |
| 173 | # is powered down right after the command returns. |
| 174 | # We should wait for a few seconds to make them done. Finger crossed. |
| 175 | _SAFE_WAIT_SECS = 10 |
| 176 | |
| 177 | |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 178 | @staticmethod |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 179 | def check_host(host, timeout=10): |
| 180 | """ |
| 181 | Check if the given host is a chrome-os host. |
| 182 | |
| 183 | @param host: An ssh host representing a device. |
| 184 | @param timeout: The timeout for the run command. |
| 185 | |
| 186 | @return: True if the host device is chromeos. |
| 187 | |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 188 | """ |
| 189 | try: |
Simran Basi | 933c8af | 2015-04-29 14:05:07 -0700 | [diff] [blame] | 190 | result = host.run( |
| 191 | 'grep -q CHROMEOS /etc/lsb-release && ' |
| 192 | '! test -f /mnt/stateful_partition/.android_tester && ' |
| 193 | '! grep -q moblab /etc/lsb-release', |
| 194 | ignore_status=True, timeout=timeout) |
beeps | 46dadc9 | 2013-11-07 14:07:10 -0800 | [diff] [blame] | 195 | except (error.AutoservRunError, error.AutoservSSHTimeout): |
| 196 | return False |
| 197 | return result.exit_status == 0 |
| 198 | |
| 199 | |
| 200 | @staticmethod |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 201 | def _extract_arguments(args_dict, key_subset): |
| 202 | """Extract options from `args_dict` and return a subset result. |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 203 | |
| 204 | Take the provided dictionary of argument options and return |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 205 | a subset that represent standard arguments needed to construct |
| 206 | a test-assistant object (chameleon or servo) for a host. The |
| 207 | intent is to provide standard argument processing from |
Christopher Wiley | 644ef3e | 2015-05-15 13:14:14 -0700 | [diff] [blame] | 208 | CrosHost for tests that require a test-assistant board |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 209 | to operate. |
| 210 | |
| 211 | @param args_dict Dictionary from which to extract the arguments. |
| 212 | @param key_subset Tuple of keys to extract from the args_dict, e.g. |
| 213 | ('servo_host', 'servo_port'). |
| 214 | """ |
| 215 | result = {} |
| 216 | for arg in key_subset: |
| 217 | if arg in args_dict: |
| 218 | result[arg] = args_dict[arg] |
| 219 | return result |
| 220 | |
| 221 | |
| 222 | @staticmethod |
| 223 | def get_chameleon_arguments(args_dict): |
| 224 | """Extract chameleon options from `args_dict` and return the result. |
| 225 | |
| 226 | Recommended usage: |
| 227 | ~~~~~~~~ |
| 228 | args_dict = utils.args_to_dict(args) |
| 229 | chameleon_args = hosts.CrosHost.get_chameleon_arguments(args_dict) |
| 230 | host = hosts.create_host(machine, chameleon_args=chameleon_args) |
| 231 | ~~~~~~~~ |
| 232 | |
| 233 | @param args_dict Dictionary from which to extract the chameleon |
| 234 | arguments. |
| 235 | """ |
| 236 | return CrosHost._extract_arguments( |
| 237 | args_dict, ('chameleon_host', 'chameleon_port')) |
| 238 | |
| 239 | |
| 240 | @staticmethod |
| 241 | def get_servo_arguments(args_dict): |
| 242 | """Extract servo options from `args_dict` and return the result. |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 243 | |
| 244 | Recommended usage: |
| 245 | ~~~~~~~~ |
| 246 | args_dict = utils.args_to_dict(args) |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 247 | servo_args = hosts.CrosHost.get_servo_arguments(args_dict) |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 248 | host = hosts.create_host(machine, servo_args=servo_args) |
| 249 | ~~~~~~~~ |
| 250 | |
| 251 | @param args_dict Dictionary from which to extract the servo |
| 252 | arguments. |
| 253 | """ |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 254 | return CrosHost._extract_arguments( |
| 255 | args_dict, ('servo_host', 'servo_port')) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 256 | |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 257 | |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 258 | def _initialize(self, hostname, chameleon_args=None, servo_args=None, |
Fang Deng | e545abb | 2014-12-30 18:43:47 -0800 | [diff] [blame] | 259 | try_lab_servo=False, ssh_verbosity_flag='', ssh_options='', |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 260 | *args, **dargs): |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 261 | """Initialize superclasses, |self.chameleon|, and |self.servo|. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 262 | |
Fang Deng | e545abb | 2014-12-30 18:43:47 -0800 | [diff] [blame] | 263 | This method will attempt to create the test-assistant object |
| 264 | (chameleon/servo) when it is needed by the test. Check |
| 265 | the docstring of chameleon_host.create_chameleon_host and |
| 266 | servo_host.create_servo_host for how this is determined. |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 267 | |
Fang Deng | e545abb | 2014-12-30 18:43:47 -0800 | [diff] [blame] | 268 | @param hostname: Hostname of the dut. |
| 269 | @param chameleon_args: A dictionary that contains args for creating |
| 270 | a ChameleonHost. See chameleon_host for details. |
| 271 | @param servo_args: A dictionary that contains args for creating |
| 272 | a ServoHost object. See servo_host for details. |
| 273 | @param try_lab_servo: Boolean, False indicates that ServoHost should |
| 274 | not be created for a device in Cros test lab. |
| 275 | See servo_host for details. |
| 276 | @param ssh_verbosity_flag: String, to pass to the ssh command to control |
| 277 | verbosity. |
| 278 | @param ssh_options: String, other ssh options to pass to the ssh |
| 279 | command. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 280 | """ |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 281 | super(CrosHost, self)._initialize(hostname=hostname, |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 282 | *args, **dargs) |
J. Richard Barnette | f085985 | 2012-08-20 14:55:50 -0700 | [diff] [blame] | 283 | # self.env is a dictionary of environment variable settings |
| 284 | # to be exported for commands run on the host. |
| 285 | # LIBC_FATAL_STDERR_ can be useful for diagnosing certain |
| 286 | # errors that might happen. |
| 287 | self.env['LIBC_FATAL_STDERR_'] = '1' |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 288 | self._ssh_verbosity_flag = ssh_verbosity_flag |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 289 | self._ssh_options = ssh_options |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 290 | # TODO(fdeng): We need to simplify the |
| 291 | # process of servo and servo_host initialization. |
| 292 | # crbug.com/298432 |
Fang Deng | e545abb | 2014-12-30 18:43:47 -0800 | [diff] [blame] | 293 | self._servo_host = servo_host.create_servo_host( |
| 294 | dut=self.hostname, servo_args=servo_args, |
| 295 | try_lab_servo=try_lab_servo) |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 296 | # TODO(waihong): Do the simplication on Chameleon too. |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 297 | self._chameleon_host = chameleon_host.create_chameleon_host( |
| 298 | dut=self.hostname, chameleon_args=chameleon_args) |
| 299 | |
Dan Shi | 4d47852 | 2014-02-14 13:46:32 -0800 | [diff] [blame] | 300 | if self._servo_host is not None: |
| 301 | self.servo = self._servo_host.get_servo() |
| 302 | else: |
| 303 | self.servo = None |
| 304 | |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 305 | if self._chameleon_host: |
Tom Wai-Hong Tam | eaee340 | 2014-01-22 08:52:10 +0800 | [diff] [blame] | 306 | self.chameleon = self._chameleon_host.create_chameleon_board() |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 307 | else: |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 308 | self.chameleon = None |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 309 | |
| 310 | |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 311 | def get_repair_image_name(self, image_type='cros'): |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 312 | """Generate a image_name from variables in the global config. |
| 313 | |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 314 | image_type is used to differentiate different images. Default is CrOS, |
| 315 | in which case, repair image's name follows the naming convention defined |
| 316 | in global setting CROS/stable_build_pattern. |
| 317 | If the image_type is not `cros`, the repair image will be looked up |
| 318 | using key `board_name/image_type`, e.g., daisy_spring/firmware. |
| 319 | |
| 320 | @param image_type: Type of the image. Default is `cros`. |
| 321 | |
Dan Shi | 0817320 | 2015-11-12 13:08:45 -0800 | [diff] [blame] | 322 | @returns a str of $board-version/$BUILD. Returns None if stable version |
| 323 | for the board and the default are both not set, e.g., stable |
| 324 | firmware version for a new board. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 325 | |
| 326 | """ |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 327 | board = self._get_board_from_afe() |
| 328 | if board is None: |
| 329 | raise error.AutoservError('DUT has no board attribute, ' |
| 330 | 'cannot be repaired.') |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 331 | if image_type != 'cros': |
| 332 | board = '%s/%s' % (board, image_type) |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 333 | stable_version = self._AFE.run('get_stable_version', board=board) |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 334 | if image_type == 'cros': |
| 335 | build_pattern = CONFIG.get_config_value( |
| 336 | 'CROS', 'stable_build_pattern') |
| 337 | stable_version = build_pattern % (board, stable_version) |
Dan Shi | 0817320 | 2015-11-12 13:08:45 -0800 | [diff] [blame] | 338 | elif image_type == 'firmware': |
| 339 | # If firmware stable version is not specified, `stable_version` |
| 340 | # from the RPC is the default stable version for CrOS image. |
| 341 | # firmware stable version must be from firmware branch, thus its |
| 342 | # value must be like board-firmware/R31-1234.0.0. Check if |
| 343 | # firmware exists in the stable version, if not, return None. |
| 344 | if 'firmware' not in stable_version: |
| 345 | return None |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 346 | return stable_version |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 347 | |
| 348 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 349 | def _host_in_AFE(self): |
| 350 | """Check if the host is an object the AFE knows. |
| 351 | |
| 352 | @returns the host object. |
| 353 | """ |
| 354 | return self._AFE.get_hosts(hostname=self.hostname) |
| 355 | |
| 356 | |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 357 | def lookup_job_repo_url(self): |
| 358 | """Looks up the job_repo_url for the host. |
| 359 | |
| 360 | @returns job_repo_url from AFE or None if not found. |
| 361 | |
| 362 | @raises KeyError if the host does not have a job_repo_url |
| 363 | """ |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 364 | hosts = self._AFE.get_hosts(hostname=self.hostname) |
beeps | b5efc53 | 2013-06-04 11:29:34 -0700 | [diff] [blame] | 365 | if hosts and ds_constants.JOB_REPO_URL in hosts[0].attributes: |
| 366 | return hosts[0].attributes[ds_constants.JOB_REPO_URL] |
J. Richard Barnette | 85d0aac | 2015-08-20 10:34:39 -0700 | [diff] [blame] | 367 | else: |
| 368 | return None |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 369 | |
| 370 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 371 | def clear_cros_version_labels_and_job_repo_url(self): |
| 372 | """Clear cros_version labels and host attribute job_repo_url.""" |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 373 | if not self._host_in_AFE(): |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 374 | return |
| 375 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 376 | host_list = [self.hostname] |
| 377 | labels = self._AFE.get_labels( |
| 378 | name__startswith=ds_constants.VERSION_PREFIX, |
| 379 | host__hostname=self.hostname) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 380 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 381 | for label in labels: |
| 382 | label.remove_hosts(hosts=host_list) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 383 | |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 384 | self.update_job_repo_url(None, None) |
| 385 | |
| 386 | |
| 387 | def update_job_repo_url(self, devserver_url, image_name): |
| 388 | """ |
| 389 | Updates the job_repo_url host attribute and asserts it's value. |
| 390 | |
| 391 | @param devserver_url: The devserver to use in the job_repo_url. |
| 392 | @param image_name: The name of the image to use in the job_repo_url. |
| 393 | |
| 394 | @raises AutoservError: If we failed to update the job_repo_url. |
| 395 | """ |
| 396 | repo_url = None |
| 397 | if devserver_url and image_name: |
| 398 | repo_url = tools.get_package_url(devserver_url, image_name) |
| 399 | self._AFE.set_host_attribute(ds_constants.JOB_REPO_URL, repo_url, |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 400 | hostname=self.hostname) |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 401 | if self.lookup_job_repo_url() != repo_url: |
| 402 | raise error.AutoservError('Failed to update job_repo_url with %s, ' |
| 403 | 'host %s' % (repo_url, self.hostname)) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 404 | |
| 405 | |
Dan Shi | e930926 | 2013-06-19 22:50:21 -0700 | [diff] [blame] | 406 | def add_cros_version_labels_and_job_repo_url(self, image_name): |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 407 | """Add cros_version labels and host attribute job_repo_url. |
| 408 | |
| 409 | @param image_name: The name of the image e.g. |
| 410 | lumpy-release/R27-3837.0.0 |
Dan Shi | 7458bf6 | 2013-06-10 12:50:16 -0700 | [diff] [blame] | 411 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 412 | """ |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 413 | if not self._host_in_AFE(): |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 414 | return |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 415 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 416 | cros_label = '%s%s' % (ds_constants.VERSION_PREFIX, image_name) |
Dan Shi | e930926 | 2013-06-19 22:50:21 -0700 | [diff] [blame] | 417 | devserver_url = dev_server.ImageServer.resolve(image_name).url() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 418 | |
MK Ryu | fb5e3a8 | 2015-07-01 12:21:20 -0700 | [diff] [blame] | 419 | self._AFE.run('label_add_hosts', id=cros_label, hosts=[self.hostname]) |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 420 | self.update_job_repo_url(devserver_url, image_name) |
| 421 | |
| 422 | |
beeps | dae65fd | 2013-07-26 16:24:41 -0700 | [diff] [blame] | 423 | def verify_job_repo_url(self, tag=''): |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 424 | """ |
| 425 | Make sure job_repo_url of this host is valid. |
| 426 | |
joychen | 03eaad9 | 2013-06-26 09:55:21 -0700 | [diff] [blame] | 427 | Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/\ |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 428 | lumpy-release/R29-4279.0.0/autotest/packages" claims to have the |
| 429 | autotest package for lumpy-release/R29-4279.0.0. If this isn't the case, |
| 430 | download and extract it. If the devserver embedded in the url is |
| 431 | unresponsive, update the job_repo_url of the host after staging it on |
| 432 | another devserver. |
| 433 | |
| 434 | @param job_repo_url: A url pointing to the devserver where the autotest |
| 435 | package for this build should be staged. |
beeps | dae65fd | 2013-07-26 16:24:41 -0700 | [diff] [blame] | 436 | @param tag: The tag from the server job, in the format |
| 437 | <job_id>-<user>/<hostname>, or <hostless> for a server job. |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 438 | |
| 439 | @raises DevServerException: If we could not resolve a devserver. |
| 440 | @raises AutoservError: If we're unable to save the new job_repo_url as |
| 441 | a result of choosing a new devserver because the old one failed to |
| 442 | respond to a health check. |
beeps | 0c86503 | 2013-07-30 11:37:06 -0700 | [diff] [blame] | 443 | @raises urllib2.URLError: If the devserver embedded in job_repo_url |
| 444 | doesn't respond within the timeout. |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 445 | """ |
| 446 | job_repo_url = self.lookup_job_repo_url() |
| 447 | if not job_repo_url: |
| 448 | logging.warning('No job repo url set on host %s', self.hostname) |
| 449 | return |
| 450 | |
| 451 | logging.info('Verifying job repo url %s', job_repo_url) |
| 452 | devserver_url, image_name = tools.get_devserver_build_from_package_url( |
| 453 | job_repo_url) |
| 454 | |
beeps | 0c86503 | 2013-07-30 11:37:06 -0700 | [diff] [blame] | 455 | ds = dev_server.ImageServer(devserver_url) |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 456 | |
| 457 | logging.info('Staging autotest artifacts for %s on devserver %s', |
| 458 | image_name, ds.url()) |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 459 | |
| 460 | start_time = time.time() |
Simran Basi | 25e7a92 | 2014-10-31 11:56:10 -0700 | [diff] [blame] | 461 | ds.stage_artifacts(image_name, ['autotest_packages']) |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 462 | stage_time = time.time() - start_time |
| 463 | |
| 464 | # Record how much of the verification time comes from a devserver |
| 465 | # restage. If we're doing things right we should not see multiple |
| 466 | # devservers for a given board/build/branch path. |
| 467 | try: |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 468 | board, build_type, branch = server_utils.ParseBuildName( |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 469 | image_name)[:3] |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 470 | except server_utils.ParseBuildNameException: |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 471 | pass |
| 472 | else: |
beeps | 0c86503 | 2013-07-30 11:37:06 -0700 | [diff] [blame] | 473 | devserver = devserver_url[ |
Chris Sosa | 6542508 | 2013-10-16 13:26:22 -0700 | [diff] [blame] | 474 | devserver_url.find('/') + 2:devserver_url.rfind(':')] |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 475 | stats_key = { |
| 476 | 'board': board, |
| 477 | 'build_type': build_type, |
| 478 | 'branch': branch, |
beeps | 0c86503 | 2013-07-30 11:37:06 -0700 | [diff] [blame] | 479 | 'devserver': devserver.replace('.', '_'), |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 480 | } |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 481 | autotest_stats.Gauge('verify_job_repo_url').send( |
beeps | 687243d | 2013-07-18 15:29:27 -0700 | [diff] [blame] | 482 | '%(board)s.%(build_type)s.%(branch)s.%(devserver)s' % stats_key, |
| 483 | stage_time) |
beeps | cb6f1e2 | 2013-06-28 19:14:10 -0700 | [diff] [blame] | 484 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 485 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 486 | def stage_server_side_package(self, image=None): |
| 487 | """Stage autotest server-side package on devserver. |
| 488 | |
| 489 | @param image: Full path of an OS image to install or a build name. |
| 490 | |
| 491 | @return: A url to the autotest server-side package. |
| 492 | """ |
| 493 | if image: |
| 494 | image_name = tools.get_build_from_image(image) |
| 495 | if not image_name: |
| 496 | raise error.AutoservError( |
| 497 | 'Failed to parse build name from %s' % image) |
| 498 | ds = dev_server.ImageServer.resolve(image_name) |
| 499 | else: |
| 500 | job_repo_url = self.lookup_job_repo_url() |
| 501 | if job_repo_url: |
| 502 | devserver_url, image_name = ( |
| 503 | tools.get_devserver_build_from_package_url(job_repo_url)) |
| 504 | ds = dev_server.ImageServer(devserver_url) |
| 505 | else: |
| 506 | labels = self._AFE.get_labels( |
| 507 | name__startswith=ds_constants.VERSION_PREFIX, |
| 508 | host__hostname=self.hostname) |
| 509 | if not labels: |
| 510 | raise error.AutoservError( |
| 511 | 'Failed to stage server-side package. The host has ' |
| 512 | 'no job_report_url attribute or version label.') |
| 513 | image_name = labels[0].name[len(ds_constants.VERSION_PREFIX):] |
| 514 | ds = dev_server.ImageServer.resolve(image_name) |
Dan Shi | ca50348 | 2015-03-30 17:23:25 -0700 | [diff] [blame] | 515 | |
| 516 | # Get the OS version of the build, for any build older than |
| 517 | # MIN_VERSION_SUPPORT_SSP, server side packaging is not supported. |
| 518 | match = re.match('.*/R\d+-(\d+)\.', image_name) |
| 519 | if match and int(match.group(1)) < self.MIN_VERSION_SUPPORT_SSP: |
| 520 | logging.warn('Build %s is older than %s. Server side packaging is ' |
| 521 | 'disabled.', image_name, self.MIN_VERSION_SUPPORT_SSP) |
| 522 | return None |
| 523 | |
Dan Shi | cf4d203 | 2015-03-12 15:04:21 -0700 | [diff] [blame] | 524 | ds.stage_artifacts(image_name, ['autotest_server_package']) |
| 525 | return '%s/static/%s/%s' % (ds.url(), image_name, |
| 526 | 'autotest_server_package.tar.bz2') |
| 527 | |
| 528 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 529 | def _try_stateful_update(self, update_url, force_update, updater): |
| 530 | """Try to use stateful update to initialize DUT. |
| 531 | |
| 532 | When DUT is already running the same version that machine_install |
| 533 | tries to install, stateful update is a much faster way to clean up |
| 534 | the DUT for testing, compared to a full reimage. It is implemeted |
| 535 | by calling autoupdater.run_update, but skipping updating root, as |
| 536 | updating the kernel is time consuming and not necessary. |
| 537 | |
| 538 | @param update_url: url of the image. |
| 539 | @param force_update: Set to True to update the image even if the DUT |
| 540 | is running the same version. |
| 541 | @param updater: ChromiumOSUpdater instance used to update the DUT. |
| 542 | @returns: True if the DUT was updated with stateful update. |
| 543 | |
| 544 | """ |
J. Richard Barnette | 3f73103 | 2014-04-07 17:42:59 -0700 | [diff] [blame] | 545 | # TODO(jrbarnette): Yes, I hate this re.match() test case. |
| 546 | # It's better than the alternative: see crbug.com/360944. |
| 547 | image_name = autoupdater.url_to_image_name(update_url) |
| 548 | release_pattern = r'^.*-release/R[0-9]+-[0-9]+\.[0-9]+\.0$' |
| 549 | if not re.match(release_pattern, image_name): |
| 550 | return False |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 551 | if not updater.check_version(): |
| 552 | return False |
| 553 | if not force_update: |
| 554 | logging.info('Canceling stateful update because the new and ' |
| 555 | 'old versions are the same.') |
| 556 | return False |
| 557 | # Following folders should be rebuilt after stateful update. |
| 558 | # A test file is used to confirm each folder gets rebuilt after |
| 559 | # the stateful update. |
| 560 | folders_to_check = ['/var', '/home', '/mnt/stateful_partition'] |
| 561 | test_file = '.test_file_to_be_deleted' |
| 562 | for folder in folders_to_check: |
| 563 | touch_path = os.path.join(folder, test_file) |
| 564 | self.run('touch %s' % touch_path) |
| 565 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 566 | updater.run_update(update_root=False) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 567 | |
| 568 | # Reboot to complete stateful update. |
Chris Sosa | b76e0ee | 2013-05-22 16:55:41 -0700 | [diff] [blame] | 569 | self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 570 | check_file_cmd = 'test -f %s; echo $?' |
| 571 | for folder in folders_to_check: |
| 572 | test_file_path = os.path.join(folder, test_file) |
| 573 | result = self.run(check_file_cmd % test_file_path, |
| 574 | ignore_status=True) |
| 575 | if result.exit_status == 1: |
| 576 | return False |
| 577 | return True |
| 578 | |
| 579 | |
J. Richard Barnette | 7275b61 | 2013-06-04 18:13:11 -0700 | [diff] [blame] | 580 | def _post_update_processing(self, updater, expected_kernel=None): |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 581 | """After the DUT is updated, confirm machine_install succeeded. |
| 582 | |
| 583 | @param updater: ChromiumOSUpdater instance used to update the DUT. |
J. Richard Barnette | 7275b61 | 2013-06-04 18:13:11 -0700 | [diff] [blame] | 584 | @param expected_kernel: kernel expected to be active after reboot, |
| 585 | or `None` to skip rollback checking. |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 586 | |
| 587 | """ |
J. Richard Barnette | 7275b61 | 2013-06-04 18:13:11 -0700 | [diff] [blame] | 588 | # Touch the lab machine file to leave a marker that |
| 589 | # distinguishes this image from other test images. |
| 590 | # Afterwards, we must re-run the autoreboot script because |
| 591 | # it depends on the _LAB_MACHINE_FILE. |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 592 | self.run('touch %s' % self._LAB_MACHINE_FILE) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 593 | self.run('start autoreboot') |
Chris Sosa | 6542508 | 2013-10-16 13:26:22 -0700 | [diff] [blame] | 594 | updater.verify_boot_expectations( |
| 595 | expected_kernel, rollback_message= |
Gilad Arnold | c26ae1f | 2015-10-22 16:09:41 -0700 | [diff] [blame] | 596 | 'Build %s failed to boot on %s; system rolled back to previous ' |
Chris Sosa | 6542508 | 2013-10-16 13:26:22 -0700 | [diff] [blame] | 597 | 'build' % (updater.update_version, self.hostname)) |
J. Richard Barnette | 7275b61 | 2013-06-04 18:13:11 -0700 | [diff] [blame] | 598 | # Check that we've got the build we meant to install. |
| 599 | if not updater.check_version_to_confirm_install(): |
| 600 | raise autoupdater.ChromiumOSError( |
| 601 | 'Failed to update %s to build %s; found build ' |
| 602 | '%s instead' % (self.hostname, |
Chris Sosa | 6542508 | 2013-10-16 13:26:22 -0700 | [diff] [blame] | 603 | updater.update_version, |
Dan Shi | 0942b1d | 2015-03-31 11:07:00 -0700 | [diff] [blame] | 604 | self.get_release_version())) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 605 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 606 | logging.debug('Cleaning up old autotest directories.') |
| 607 | try: |
| 608 | installed_autodir = autotest.Autotest.get_installed_autodir(self) |
| 609 | self.run('rm -rf ' + installed_autodir) |
| 610 | except autotest.AutodirNotFoundError: |
| 611 | logging.debug('No autotest installed directory found.') |
| 612 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 613 | |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 614 | def _stage_image_for_update(self, image_name=None): |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 615 | """Stage a build on a devserver and return the update_url and devserver. |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 616 | |
| 617 | @param image_name: a name like lumpy-release/R27-3837.0.0 |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 618 | @returns a tuple with an update URL like: |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 619 | http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0 |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 620 | and the devserver instance. |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 621 | """ |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 622 | if not image_name: |
| 623 | image_name = self.get_repair_image_name() |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 624 | |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 625 | logging.info('Staging build for AU: %s', image_name) |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 626 | devserver = dev_server.ImageServer.resolve(image_name) |
| 627 | devserver.trigger_download(image_name, synchronous=False) |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 628 | return (tools.image_url_pattern() % (devserver.url(), image_name), |
| 629 | devserver) |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 630 | |
| 631 | |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 632 | def stage_image_for_servo(self, image_name=None): |
| 633 | """Stage a build on a devserver and return the update_url. |
| 634 | |
| 635 | @param image_name: a name like lumpy-release/R27-3837.0.0 |
| 636 | @returns an update URL like: |
| 637 | http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0 |
| 638 | """ |
| 639 | if not image_name: |
| 640 | image_name = self.get_repair_image_name() |
| 641 | logging.info('Staging build for servo install: %s', image_name) |
| 642 | devserver = dev_server.ImageServer.resolve(image_name) |
| 643 | devserver.stage_artifacts(image_name, ['test_image']) |
| 644 | return devserver.get_test_image_url(image_name) |
| 645 | |
| 646 | |
beeps | e539be0 | 2013-07-31 21:57:39 -0700 | [diff] [blame] | 647 | def stage_factory_image_for_servo(self, image_name): |
| 648 | """Stage a build on a devserver and return the update_url. |
| 649 | |
| 650 | @param image_name: a name like <baord>/4262.204.0 |
beeps | 12c0a3c | 2013-09-03 11:58:27 -0700 | [diff] [blame] | 651 | |
beeps | e539be0 | 2013-07-31 21:57:39 -0700 | [diff] [blame] | 652 | @return: An update URL, eg: |
| 653 | http://<devserver>/static/canary-channel/\ |
| 654 | <board>/4262.204.0/factory_test/chromiumos_factory_image.bin |
beeps | 12c0a3c | 2013-09-03 11:58:27 -0700 | [diff] [blame] | 655 | |
| 656 | @raises: ValueError if the factory artifact name is missing from |
| 657 | the config. |
| 658 | |
beeps | e539be0 | 2013-07-31 21:57:39 -0700 | [diff] [blame] | 659 | """ |
| 660 | if not image_name: |
| 661 | logging.error('Need an image_name to stage a factory image.') |
| 662 | return |
| 663 | |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 664 | factory_artifact = CONFIG.get_config_value( |
beeps | 12c0a3c | 2013-09-03 11:58:27 -0700 | [diff] [blame] | 665 | 'CROS', 'factory_artifact', type=str, default='') |
| 666 | if not factory_artifact: |
| 667 | raise ValueError('Cannot retrieve the factory artifact name from ' |
| 668 | 'autotest config, and hence cannot stage factory ' |
| 669 | 'artifacts.') |
| 670 | |
beeps | e539be0 | 2013-07-31 21:57:39 -0700 | [diff] [blame] | 671 | logging.info('Staging build for servo install: %s', image_name) |
| 672 | devserver = dev_server.ImageServer.resolve(image_name) |
| 673 | devserver.stage_artifacts( |
| 674 | image_name, |
beeps | 12c0a3c | 2013-09-03 11:58:27 -0700 | [diff] [blame] | 675 | [factory_artifact], |
| 676 | archive_url=None) |
beeps | e539be0 | 2013-07-31 21:57:39 -0700 | [diff] [blame] | 677 | |
| 678 | return tools.factory_image_url_pattern() % (devserver.url(), image_name) |
| 679 | |
| 680 | |
Chris Sosa | a3ac215 | 2012-05-23 22:23:13 -0700 | [diff] [blame] | 681 | def machine_install(self, update_url=None, force_update=False, |
Richard Barnette | 0b023a7 | 2015-04-24 16:07:30 +0000 | [diff] [blame] | 682 | local_devserver=False, repair=False, |
| 683 | force_full_update=False): |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 684 | """Install the DUT. |
| 685 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 686 | Use stateful update if the DUT is already running the same build. |
| 687 | Stateful update does not update kernel and tends to run much faster |
| 688 | than a full reimage. If the DUT is running a different build, or it |
| 689 | failed to do a stateful update, full update, including kernel update, |
| 690 | will be applied to the DUT. |
| 691 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 692 | Once a host enters machine_install its cros_version label will be |
| 693 | removed as well as its host attribute job_repo_url (used for |
| 694 | package install). |
| 695 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 696 | @param update_url: The url to use for the update |
| 697 | pattern: http://$devserver:###/update/$build |
| 698 | If update_url is None and repair is True we will install the |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 699 | stable image listed in afe_stable_versions table. If the table |
| 700 | is not setup, global_config value under CROS.stable_cros_version |
| 701 | will be used instead. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 702 | @param force_update: Force an update even if the version installed |
| 703 | is the same. Default:False |
Christopher Wiley | 6a4ff93 | 2015-05-15 14:00:47 -0700 | [diff] [blame] | 704 | @param local_devserver: Used by test_that to allow people to |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 705 | use their local devserver. Default: False |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 706 | @param repair: Forces update to repair image. Implies force_update. |
Fang Deng | 3d3b927 | 2014-12-22 12:20:28 -0800 | [diff] [blame] | 707 | @param force_full_update: If True, do not attempt to run stateful |
| 708 | update, force a full reimage. If False, try stateful update |
| 709 | first when the dut is already installed with the same version. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 710 | @raises autoupdater.ChromiumOSError |
| 711 | |
| 712 | """ |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 713 | devserver = None |
Richard Barnette | 0b023a7 | 2015-04-24 16:07:30 +0000 | [diff] [blame] | 714 | if repair: |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 715 | update_url, devserver = self._stage_image_for_update() |
Richard Barnette | 0b023a7 | 2015-04-24 16:07:30 +0000 | [diff] [blame] | 716 | force_update = True |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 717 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 718 | if not update_url and not self._parser.options.image: |
| 719 | raise error.AutoservError( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 720 | 'There is no update URL, nor a method to get one.') |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 721 | |
| 722 | if not update_url and self._parser.options.image: |
| 723 | # This is the base case where we have no given update URL i.e. |
| 724 | # dynamic suites logic etc. This is the most flexible case where we |
| 725 | # can serve an update from any of our fleet of devservers. |
| 726 | requested_build = self._parser.options.image |
| 727 | if not requested_build.startswith('http://'): |
| 728 | logging.debug('Update will be staged for this installation') |
| 729 | update_url, devserver = self._stage_image_for_update( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 730 | requested_build) |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 731 | else: |
| 732 | update_url = requested_build |
| 733 | |
| 734 | logging.debug('Update URL is %s', update_url) |
| 735 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 736 | # Remove cros-version and job_repo_url host attribute from host. |
| 737 | self.clear_cros_version_labels_and_job_repo_url() |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 738 | |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 739 | # Create a file to indicate if provision fails. The file will be removed |
| 740 | # by stateful update or full install. |
| 741 | self.run('touch %s' % PROVISION_FAILED) |
| 742 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 743 | update_complete = False |
| 744 | updater = autoupdater.ChromiumOSUpdater( |
| 745 | update_url, host=self, local_devserver=local_devserver) |
Fang Deng | 3d3b927 | 2014-12-22 12:20:28 -0800 | [diff] [blame] | 746 | if not force_full_update: |
| 747 | try: |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 748 | # If the DUT is already running the same build, try stateful |
| 749 | # update first as it's much quicker than a full re-image. |
| 750 | update_complete = self._try_stateful_update( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 751 | update_url, force_update, updater) |
Fang Deng | 3d3b927 | 2014-12-22 12:20:28 -0800 | [diff] [blame] | 752 | except Exception as e: |
| 753 | logging.exception(e) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 754 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 755 | inactive_kernel = None |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 756 | if update_complete or (not force_update and updater.check_version()): |
| 757 | logging.info('Install complete without full update') |
| 758 | else: |
| 759 | logging.info('DUT requires full update.') |
| 760 | self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True) |
| 761 | num_of_attempts = provision.FLAKY_DEVSERVER_ATTEMPTS |
Chris Sosa | b7612bc | 2013-03-21 10:32:37 -0700 | [diff] [blame] | 762 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 763 | while num_of_attempts > 0: |
| 764 | num_of_attempts -= 1 |
| 765 | try: |
| 766 | updater.run_update() |
| 767 | except Exception: |
| 768 | logging.warn('Autoupdate did not complete.') |
| 769 | # Do additional check for the devserver health. Ideally, |
| 770 | # the autoupdater.py could raise an exception when it |
| 771 | # detected network flake but that would require |
| 772 | # instrumenting the update engine and parsing it log. |
| 773 | if (num_of_attempts <= 0 or |
| 774 | devserver is None or |
| 775 | dev_server.DevServer.devserver_healthy( |
| 776 | devserver.url())): |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 777 | raise |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 778 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 779 | logging.warn('Devserver looks unhealthy. Trying another') |
| 780 | update_url, devserver = self._stage_image_for_update( |
| 781 | requested_build) |
| 782 | logging.debug('New Update URL is %s', update_url) |
| 783 | updater = autoupdater.ChromiumOSUpdater( |
| 784 | update_url, host=self, |
| 785 | local_devserver=local_devserver) |
| 786 | else: |
| 787 | break |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 788 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 789 | # Give it some time in case of IO issues. |
| 790 | time.sleep(10) |
Dan Shi | 5699ac2 | 2014-12-19 10:55:49 -0800 | [diff] [blame] | 791 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 792 | # Figure out active and inactive kernel. |
| 793 | active_kernel, inactive_kernel = updater.get_kernel_state() |
Simran Basi | 13fa1ba | 2013-03-04 10:56:47 -0800 | [diff] [blame] | 794 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 795 | # Ensure inactive kernel has higher priority than active. |
| 796 | if (updater.get_kernel_priority(inactive_kernel) |
| 797 | < updater.get_kernel_priority(active_kernel)): |
| 798 | raise autoupdater.ChromiumOSError( |
| 799 | 'Update failed. The priority of the inactive kernel' |
| 800 | ' partition is less than that of the active kernel' |
| 801 | ' partition.') |
| 802 | |
| 803 | # Updater has returned successfully; reboot the host. |
| 804 | self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True) |
| 805 | |
| 806 | self._post_update_processing(updater, inactive_kernel) |
| 807 | self.add_cros_version_labels_and_job_repo_url( |
| 808 | autoupdater.url_to_image_name(update_url)) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 809 | |
| 810 | |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 811 | def _clear_fw_version_labels(self): |
| 812 | """Clear firmware version labels from the machine.""" |
| 813 | labels = self._AFE.get_labels( |
Dan Shi | 0723bf5 | 2015-06-24 10:52:38 -0700 | [diff] [blame] | 814 | name__startswith=provision.FW_RW_VERSION_PREFIX, |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 815 | host__hostname=self.hostname) |
| 816 | for label in labels: |
| 817 | label.remove_hosts(hosts=[self.hostname]) |
| 818 | |
| 819 | |
| 820 | def _add_fw_version_label(self, build): |
| 821 | """Add firmware version label to the machine. |
| 822 | |
| 823 | @param build: Build of firmware. |
| 824 | |
| 825 | """ |
| 826 | fw_label = provision.fw_version_to_label(build) |
MK Ryu | 73be986 | 2015-07-06 12:25:00 -0700 | [diff] [blame] | 827 | self._AFE.run('label_add_hosts', id=fw_label, hosts=[self.hostname]) |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 828 | |
| 829 | |
| 830 | def firmware_install(self, build=None): |
| 831 | """Install firmware to the DUT. |
| 832 | |
| 833 | Use stateful update if the DUT is already running the same build. |
| 834 | Stateful update does not update kernel and tends to run much faster |
| 835 | than a full reimage. If the DUT is running a different build, or it |
| 836 | failed to do a stateful update, full update, including kernel update, |
| 837 | will be applied to the DUT. |
| 838 | |
| 839 | Once a host enters firmware_install its fw_version label will be |
| 840 | removed. After the firmware is updated successfully, a new fw_version |
| 841 | label will be added to the host. |
| 842 | |
| 843 | @param build: The build version to which we want to provision the |
| 844 | firmware of the machine, |
| 845 | e.g. 'link-firmware/R22-2695.1.144'. |
| 846 | |
| 847 | TODO(dshi): After bug 381718 is fixed, update here with corresponding |
| 848 | exceptions that could be raised. |
| 849 | |
| 850 | """ |
| 851 | if not self.servo: |
| 852 | raise error.TestError('Host %s does not have servo.' % |
| 853 | self.hostname) |
| 854 | |
| 855 | # TODO(fdeng): use host.get_board() after |
| 856 | # crbug.com/271834 is fixed. |
| 857 | board = self._get_board_from_afe() |
| 858 | |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 859 | # If build is not set, try to install firmware from stable CrOS. |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 860 | if not build: |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 861 | build = self.get_repair_image_name(image_type='firmware') |
| 862 | if not build: |
| 863 | raise error.TestError( |
| 864 | 'Failed to find stable firmware build for %s.', |
| 865 | self.hostname) |
| 866 | logging.info('Will install firmware from build %s.', build) |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 867 | |
| 868 | config = FAFTConfig(board) |
| 869 | if config.use_u_boot: |
| 870 | ap_image = 'image-%s.bin' % board |
| 871 | else: # Depthcharge platform |
| 872 | ap_image = 'image.bin' |
| 873 | ec_image = 'ec.bin' |
| 874 | ds = dev_server.ImageServer.resolve(build) |
| 875 | ds.stage_artifacts(build, ['firmware']) |
| 876 | |
| 877 | tmpd = autotemp.tempdir(unique_id='fwimage') |
| 878 | try: |
| 879 | fwurl = self._FW_IMAGE_URL_PATTERN % (ds.url(), build) |
| 880 | local_tarball = os.path.join(tmpd.name, os.path.basename(fwurl)) |
| 881 | server_utils.system('wget -O %s %s' % (local_tarball, fwurl), |
| 882 | timeout=60) |
| 883 | server_utils.system('tar xf %s -C %s %s %s' % |
| 884 | (local_tarball, tmpd.name, ap_image, ec_image), |
| 885 | timeout=60) |
| 886 | server_utils.system('tar xf %s --wildcards -C %s "dts/*"' % |
| 887 | (local_tarball, tmpd.name), |
| 888 | timeout=60, ignore_status=True) |
| 889 | |
| 890 | self._clear_fw_version_labels() |
| 891 | logging.info('Will re-program EC now') |
| 892 | self.servo.program_ec(os.path.join(tmpd.name, ec_image)) |
| 893 | logging.info('Will re-program BIOS now') |
| 894 | self.servo.program_bios(os.path.join(tmpd.name, ap_image)) |
| 895 | self.servo.get_power_state_controller().reset() |
| 896 | time.sleep(self.servo.BOOT_DELAY) |
Dan Shi | a5fef05 | 2015-05-18 23:28:47 -0700 | [diff] [blame] | 897 | self._add_fw_version_label(build) |
Dan Shi | 9cb0eec | 2014-06-03 09:04:50 -0700 | [diff] [blame] | 898 | finally: |
| 899 | tmpd.clean() |
| 900 | |
| 901 | |
Dan Shi | 10e992b | 2013-08-30 11:02:59 -0700 | [diff] [blame] | 902 | def show_update_engine_log(self): |
| 903 | """Output update engine log.""" |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 904 | logging.debug('Dumping %s', client_constants.UPDATE_ENGINE_LOG) |
| 905 | self.run('cat %s' % client_constants.UPDATE_ENGINE_LOG) |
Dan Shi | 10e992b | 2013-08-30 11:02:59 -0700 | [diff] [blame] | 906 | |
| 907 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 908 | def _get_board_from_afe(self): |
| 909 | """Retrieve this host's board from its labels in the AFE. |
| 910 | |
| 911 | Looks for a host label of the form "board:<board>", and |
| 912 | returns the "<board>" part of the label. `None` is returned |
| 913 | if there is not a single, unique label matching the pattern. |
| 914 | |
| 915 | @returns board from label, or `None`. |
| 916 | """ |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 917 | return server_utils.get_board_from_afe(self.hostname, self._AFE) |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 918 | |
| 919 | |
| 920 | def get_build(self): |
| 921 | """Retrieve the current build for this Host from the AFE. |
| 922 | |
| 923 | Looks through this host's labels in the AFE to determine its build. |
| 924 | |
| 925 | @returns The current build or None if it could not find it or if there |
| 926 | were multiple build labels assigned to this host. |
| 927 | """ |
Dan Shi | a1ecd5c | 2013-06-06 11:21:31 -0700 | [diff] [blame] | 928 | return server_utils.get_build_from_afe(self.hostname, self._AFE) |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 929 | |
| 930 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 931 | def _install_repair(self): |
Chris Sosa | e92399e | 2015-04-24 11:32:59 -0700 | [diff] [blame] | 932 | """Attempt to repair this host using the update-engine. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 933 | |
| 934 | If the host is up, try installing the DUT with a stable |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 935 | "repair" version of Chrome OS as defined in afe_stable_versions table. |
| 936 | If the table is not setup, global_config value under |
| 937 | CROS.stable_cros_version will be used instead. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 938 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 939 | @raises AutoservRepairMethodNA if the DUT is not reachable. |
| 940 | @raises ChromiumOSError if the install failed for some reason. |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 941 | |
| 942 | """ |
| 943 | if not self.is_up(): |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 944 | raise error.AutoservRepairMethodNA('DUT unreachable for install.') |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 945 | logging.info('Attempting to reimage machine to repair image.') |
| 946 | try: |
Richard Barnette | 0b023a7 | 2015-04-24 16:07:30 +0000 | [diff] [blame] | 947 | self.machine_install(repair=True) |
Fang Deng | d0672f3 | 2013-03-18 17:18:09 -0700 | [diff] [blame] | 948 | except autoupdater.ChromiumOSError as e: |
| 949 | logging.exception(e) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 950 | logging.info('Repair via install failed.') |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 951 | raise |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 952 | |
| 953 | |
Dan Shi | 2c88eed | 2013-11-12 10:18:38 -0800 | [diff] [blame] | 954 | def _install_repair_with_powerwash(self): |
Dan Shi | 9cc4845 | 2013-11-12 12:39:26 -0800 | [diff] [blame] | 955 | """Attempt to powerwash first then repair this host using update-engine. |
Dan Shi | 2c88eed | 2013-11-12 10:18:38 -0800 | [diff] [blame] | 956 | |
Dan Shi | 9cc4845 | 2013-11-12 12:39:26 -0800 | [diff] [blame] | 957 | update-engine may fail due to a bad image. In such case, powerwash |
| 958 | may help to cleanup the DUT for update-engine to work again. |
Dan Shi | 2c88eed | 2013-11-12 10:18:38 -0800 | [diff] [blame] | 959 | |
| 960 | @raises AutoservRepairMethodNA if the DUT is not reachable. |
| 961 | @raises ChromiumOSError if the install failed for some reason. |
| 962 | |
| 963 | """ |
| 964 | if not self.is_up(): |
| 965 | raise error.AutoservRepairMethodNA('DUT unreachable for install.') |
| 966 | |
| 967 | logging.info('Attempting to powerwash the DUT.') |
| 968 | self.run('echo "fast safe" > ' |
| 969 | '/mnt/stateful_partition/factory_install_reset') |
| 970 | self.reboot(timeout=self.POWERWASH_BOOT_TIMEOUT, wait=True) |
| 971 | if not self.is_up(): |
Dan Shi | 9cc4845 | 2013-11-12 12:39:26 -0800 | [diff] [blame] | 972 | logging.error('Powerwash failed. DUT did not come back after ' |
Dan Shi | 2c88eed | 2013-11-12 10:18:38 -0800 | [diff] [blame] | 973 | 'reboot.') |
| 974 | raise error.AutoservRepairFailure( |
| 975 | 'DUT failed to boot from powerwash after %d seconds' % |
| 976 | self.POWERWASH_BOOT_TIMEOUT) |
| 977 | |
| 978 | logging.info('Powerwash succeeded.') |
| 979 | self._install_repair() |
| 980 | |
| 981 | |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 982 | def servo_install(self, image_url=None, usb_boot_timeout=USB_BOOT_TIMEOUT, |
| 983 | install_timeout=INSTALL_TIMEOUT): |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 984 | """ |
| 985 | Re-install the OS on the DUT by: |
| 986 | 1) installing a test image on a USB storage device attached to the Servo |
| 987 | board, |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 988 | 2) booting that image in recovery mode, and then |
J. Richard Barnette | 31b2e31 | 2013-04-04 16:05:22 -0700 | [diff] [blame] | 989 | 3) installing the image with chromeos-install. |
| 990 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 991 | @param image_url: If specified use as the url to install on the DUT. |
| 992 | otherwise boot the currently staged image on the USB stick. |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 993 | @param usb_boot_timeout: The usb_boot_timeout to use during reimage. |
| 994 | Factory images need a longer usb_boot_timeout than regular |
| 995 | cros images. |
| 996 | @param install_timeout: The timeout to use when installing the chromeos |
| 997 | image. Factory images need a longer install_timeout. |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 998 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 999 | @raises AutoservError if the image fails to boot. |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1000 | |
J. Richard Barnette | 0199cc8 | 2014-12-05 17:08:40 -0800 | [diff] [blame] | 1001 | """ |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1002 | usb_boot_timer_key = ('servo_install.usb_boot_timeout_%s' |
| 1003 | % usb_boot_timeout) |
| 1004 | logging.info('Downloading image to USB, then booting from it. Usb boot ' |
| 1005 | 'timeout = %s', usb_boot_timeout) |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1006 | timer = autotest_stats.Timer(usb_boot_timer_key) |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1007 | timer.start() |
J. Richard Barnette | 31b2e31 | 2013-04-04 16:05:22 -0700 | [diff] [blame] | 1008 | self.servo.install_recovery_image(image_url) |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1009 | if not self.wait_up(timeout=usb_boot_timeout): |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1010 | raise error.AutoservRepairFailure( |
| 1011 | 'DUT failed to boot from USB after %d seconds' % |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1012 | usb_boot_timeout) |
| 1013 | timer.stop() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1014 | |
Tom Wai-Hong Tam | f6b4f81 | 2015-08-08 04:14:59 +0800 | [diff] [blame] | 1015 | # The new chromeos-tpm-recovery has been merged since R44-7073.0.0. |
| 1016 | # In old CrOS images, this command fails. Skip the error. |
Tom Wai-Hong Tam | 27af733 | 2015-07-25 06:09:39 +0800 | [diff] [blame] | 1017 | logging.info('Resetting the TPM status') |
Tom Wai-Hong Tam | f6b4f81 | 2015-08-08 04:14:59 +0800 | [diff] [blame] | 1018 | try: |
| 1019 | self.run('chromeos-tpm-recovery') |
| 1020 | except error.AutoservRunError: |
| 1021 | logging.warn('chromeos-tpm-recovery is too old.') |
| 1022 | |
Tom Wai-Hong Tam | 27af733 | 2015-07-25 06:09:39 +0800 | [diff] [blame] | 1023 | |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1024 | install_timer_key = ('servo_install.install_timeout_%s' |
| 1025 | % install_timeout) |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1026 | timer = autotest_stats.Timer(install_timer_key) |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1027 | timer.start() |
| 1028 | logging.info('Installing image through chromeos-install.') |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 1029 | self.run('chromeos-install --yes', timeout=install_timeout) |
| 1030 | self.halt() |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1031 | timer.stop() |
| 1032 | |
| 1033 | logging.info('Power cycling DUT through servo.') |
J. Richard Barnette | 0199cc8 | 2014-12-05 17:08:40 -0800 | [diff] [blame] | 1034 | self.servo.get_power_state_controller().power_off() |
Fang Deng | afb8814 | 2013-05-30 17:44:31 -0700 | [diff] [blame] | 1035 | self.servo.switch_usbkey('off') |
J. Richard Barnette | 0199cc8 | 2014-12-05 17:08:40 -0800 | [diff] [blame] | 1036 | # N.B. The Servo API requires that we use power_on() here |
| 1037 | # for two reasons: |
| 1038 | # 1) After turning on a DUT in recovery mode, you must turn |
| 1039 | # it off and then on with power_on() once more to |
| 1040 | # disable recovery mode (this is a Parrot specific |
| 1041 | # requirement). |
| 1042 | # 2) After power_off(), the only way to turn on is with |
| 1043 | # power_on() (this is a Storm specific requirement). |
J. Richard Barnette | fbcc712 | 2013-07-24 18:24:59 -0700 | [diff] [blame] | 1044 | self.servo.get_power_state_controller().power_on() |
beeps | f079cfb | 2013-09-18 17:49:51 -0700 | [diff] [blame] | 1045 | |
| 1046 | logging.info('Waiting for DUT to come back up.') |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 1047 | if not self.wait_up(timeout=self.BOOT_TIMEOUT): |
| 1048 | raise error.AutoservError('DUT failed to reboot installed ' |
| 1049 | 'test image after %d seconds' % |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1050 | self.BOOT_TIMEOUT) |
| 1051 | |
| 1052 | |
Dan Shi | c1b8bdd | 2015-09-14 23:11:24 -0700 | [diff] [blame] | 1053 | def _setup_servo(self): |
| 1054 | """Try to force to create servo object if it's not set up yet. |
| 1055 | """ |
| 1056 | if self.servo: |
| 1057 | return |
| 1058 | |
| 1059 | try: |
| 1060 | # Setting servo_args to {} will force it to create the servo_host |
| 1061 | # object if possible. |
| 1062 | self._servo_host = servo_host.create_servo_host( |
| 1063 | dut=self.hostname, servo_args={}) |
| 1064 | if self._servo_host: |
| 1065 | self.servo = self._servo_host.get_servo() |
| 1066 | else: |
| 1067 | logging.error('Failed to create servo_host object.') |
| 1068 | except Exception as e: |
| 1069 | logging.error('Failed to create servo object: %s', e) |
| 1070 | |
| 1071 | |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 1072 | def _servo_repair_reinstall(self): |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1073 | """Reinstall the DUT utilizing servo and a test image. |
| 1074 | |
| 1075 | Re-install the OS on the DUT by: |
| 1076 | 1) installing a test image on a USB storage device attached to the Servo |
| 1077 | board, |
Tom Wai-Hong Tam | 27af733 | 2015-07-25 06:09:39 +0800 | [diff] [blame] | 1078 | 2) booting that image in recovery mode, |
| 1079 | 3) resetting the TPM status, and then |
| 1080 | 4) installing the image with chromeos-install. |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1081 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1082 | @raises AutoservRepairMethodNA if the device does not have servo |
| 1083 | support. |
| 1084 | |
| 1085 | """ |
| 1086 | if not self.servo: |
| 1087 | raise error.AutoservRepairMethodNA('Repair Reinstall NA: ' |
| 1088 | 'DUT has no servo support.') |
| 1089 | |
| 1090 | logging.info('Attempting to recovery servo enabled device with ' |
| 1091 | 'servo_repair_reinstall') |
| 1092 | |
J. Richard Barnette | e4af8b9 | 2013-05-01 13:16:12 -0700 | [diff] [blame] | 1093 | image_url = self.stage_image_for_servo() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1094 | self.servo_install(image_url) |
| 1095 | |
| 1096 | |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 1097 | def _firmware_repair(self): |
| 1098 | """Reinstall the firmware image using servo. |
| 1099 | |
| 1100 | This repair function attempts to install the stable firmware specified |
| 1101 | by the stable firmware version. |
| 1102 | Then reset the DUT and try to verify it. If verify fails, it will try to |
| 1103 | install the CrOS image using servo. |
| 1104 | |
| 1105 | Note that the firmware repair is only applicable to DUTs in pools listed |
| 1106 | in global config CROS/pools_support_firmware_repair. |
| 1107 | """ |
| 1108 | logging.info('Checking if host %s can be repaired with firmware ' |
| 1109 | 'repair.', self.hostname) |
| 1110 | pools = server_utils.get_labels_from_afe(self.hostname, 'pool:', |
| 1111 | self._AFE) |
| 1112 | pools_support_firmware_repair = CONFIG.get_config_value('CROS', |
| 1113 | 'pools_support_firmware_repair', type=str).split(',') |
| 1114 | if (not pools or not pools_support_firmware_repair or |
| 1115 | not set(pools).intersection(set(pools_support_firmware_repair))): |
| 1116 | logging.info('Host %s is not in pools that support firmware repair.' |
| 1117 | ' pools supporting firmware repair are: %s.', |
| 1118 | self.hostname, pools_support_firmware_repair) |
| 1119 | raise error.AutoservRepairMethodNA( |
| 1120 | 'Firmware repair is not applicable to host %s.' % |
| 1121 | self.hostname) |
| 1122 | |
| 1123 | # To repair a DUT connected to a moblab, try to create a servo object if |
| 1124 | # it was failed to be created earlier as there may be a servo_host host |
| 1125 | # attribute for this host. |
| 1126 | if utils.is_moblab(): |
| 1127 | self._setup_servo() |
| 1128 | |
| 1129 | if not self.servo: |
| 1130 | raise error.AutoservRepairMethodNA('Repair Reinstall NA: ' |
| 1131 | 'DUT has no servo support.') |
| 1132 | |
| 1133 | logging.info('Attempting to recovery servo enabled device with ' |
| 1134 | 'firmware_repair.') |
| 1135 | self.firmware_install() |
| 1136 | |
| 1137 | logging.info('Firmware repaired. Check if the DUT can boot. If not, ' |
| 1138 | 'reinstall the CrOS using servo.') |
| 1139 | try: |
| 1140 | self.servo.reset() |
| 1141 | self.verify() |
| 1142 | except Exception as e: |
| 1143 | logging.warn('Failed to verify DUT, error: %s. Will try to repair ' |
| 1144 | 'the DUT with servo_repair_reinstall.', e) |
| 1145 | self._servo_repair_reinstall() |
| 1146 | |
| 1147 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1148 | def _servo_repair_power(self): |
| 1149 | """Attempt to repair DUT using an attached Servo. |
| 1150 | |
| 1151 | Attempt to power on the DUT via power_long_press. |
| 1152 | |
| 1153 | @raises AutoservRepairMethodNA if the device does not have servo |
| 1154 | support. |
| 1155 | @raises AutoservRepairFailure if the repair fails for any reason. |
| 1156 | """ |
| 1157 | if not self.servo: |
| 1158 | raise error.AutoservRepairMethodNA('Repair Power NA: ' |
| 1159 | 'DUT has no servo support.') |
| 1160 | |
| 1161 | logging.info('Attempting to recover servo enabled device by ' |
| 1162 | 'powering it off and on.') |
| 1163 | self.servo.get_power_state_controller().power_off() |
| 1164 | self.servo.get_power_state_controller().power_on() |
| 1165 | if self.wait_up(self.BOOT_TIMEOUT): |
| 1166 | return |
| 1167 | |
| 1168 | raise error.AutoservRepairFailure('DUT did not boot after long_press.') |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 1169 | |
| 1170 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1171 | def _powercycle_to_repair(self): |
| 1172 | """Utilize the RPM Infrastructure to bring the host back up. |
| 1173 | |
| 1174 | If the host is not up/repaired after the first powercycle we utilize |
| 1175 | auto fallback to the last good install by powercycling and rebooting the |
| 1176 | host 6 times. |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1177 | |
| 1178 | @raises AutoservRepairMethodNA if the device does not support remote |
| 1179 | power. |
| 1180 | @raises AutoservRepairFailure if the repair fails for any reason. |
| 1181 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1182 | """ |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1183 | if not self.has_power(): |
| 1184 | raise error.AutoservRepairMethodNA('Device does not support power.') |
| 1185 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1186 | logging.info('Attempting repair via RPM powercycle.') |
| 1187 | failed_cycles = 0 |
| 1188 | self.power_cycle() |
| 1189 | while not self.wait_up(timeout=self.BOOT_TIMEOUT): |
| 1190 | failed_cycles += 1 |
| 1191 | if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS: |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1192 | raise error.AutoservRepairFailure( |
| 1193 | 'Powercycled host %s %d times; device did not come back' |
| 1194 | ' online.' % (self.hostname, failed_cycles)) |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1195 | self.power_cycle() |
| 1196 | if failed_cycles == 0: |
| 1197 | logging.info('Powercycling was successful first time.') |
| 1198 | else: |
| 1199 | logging.info('Powercycling was successful after %d failures.', |
| 1200 | failed_cycles) |
| 1201 | |
| 1202 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1203 | def _reboot_repair(self): |
| 1204 | """SSH to this host and reboot.""" |
| 1205 | if not self.is_up(self._CHECK_HOST_UP_TIMEOUT_SECS): |
| 1206 | raise error.AutoservRepairMethodNA('DUT unreachable for reboot.') |
| 1207 | logging.info('Attempting repair via SSH reboot.') |
| 1208 | self.reboot(timeout=self.BOOT_TIMEOUT, wait=True) |
| 1209 | |
| 1210 | |
Prashanth B | 4d8184f | 2014-05-05 12:22:02 -0700 | [diff] [blame] | 1211 | def check_device(self): |
| 1212 | """Check if a device is ssh-able, and if so, clean and verify it. |
| 1213 | |
| 1214 | @raise AutoservSSHTimeout: If the ssh ping times out. |
| 1215 | @raise AutoservSshPermissionDeniedError: If ssh ping fails due to |
| 1216 | permissions. |
| 1217 | @raise AutoservSshPingHostError: For other AutoservRunErrors during |
| 1218 | ssh_ping. |
| 1219 | @raises AutoservError: As appropriate, during cleanup and verify. |
| 1220 | """ |
| 1221 | self.ssh_ping() |
| 1222 | self.cleanup() |
| 1223 | self.verify() |
| 1224 | |
| 1225 | |
Dan Shi | 9046635 | 2015-09-22 15:01:05 -0700 | [diff] [blame] | 1226 | def confirm_servo(self): |
| 1227 | """Confirm servo is initialized and verified. |
| 1228 | |
| 1229 | @raise AutoservError: If servo is not initialized and verified. |
| 1230 | """ |
| 1231 | if self._servo_host.required_by_test and self.servo: |
| 1232 | return |
| 1233 | |
| 1234 | # Force to re-create the servo object to make sure servo is verified. |
| 1235 | logging.debug('Rebuilding the servo object.') |
| 1236 | self.servo = None |
| 1237 | self._servo_host = None |
| 1238 | self._setup_servo() |
| 1239 | if not self.servo: |
| 1240 | raise error.AutoservError('Failed to create servo object.') |
| 1241 | |
| 1242 | |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1243 | def _is_last_provision_failed(self): |
| 1244 | """Checks if the last provision job failed. |
| 1245 | |
| 1246 | @return: True if there exists file /var/tmp/provision_failed, which |
| 1247 | indicates the last provision job failed. |
| 1248 | False if the file does not exist or the dut can't be reached. |
| 1249 | """ |
| 1250 | try: |
| 1251 | result = self.run('test -f %s' % PROVISION_FAILED, |
| 1252 | ignore_status=True, timeout=5) |
| 1253 | return result.exit_status == 0 |
| 1254 | except (error.AutoservRunError, error.AutoservSSHTimeout): |
| 1255 | # Default to False, for repair to try all repair method if the dut |
| 1256 | # can't be reached. |
| 1257 | return False |
| 1258 | |
| 1259 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1260 | def repair_full(self): |
| 1261 | """Repair a host for repair level NO_PROTECTION. |
| 1262 | |
| 1263 | This overrides the base class function for repair; it does |
| 1264 | not call back to the parent class, but instead offers a |
| 1265 | simplified implementation based on the capabilities in the |
| 1266 | Chrome OS test lab. |
| 1267 | |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1268 | It first verifies and repairs servo if it is a DUT in CrOS |
Fang Deng | 03590af | 2013-10-07 17:34:20 -0700 | [diff] [blame] | 1269 | lab and a servo is attached. |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1270 | |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 1271 | This escalates in order through the following procedures and verifies |
| 1272 | the status using `self.check_device()` after each of them. This is done |
| 1273 | until both the repair and the veryfing step succeed. |
| 1274 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1275 | Escalation order of repair procedures from less intrusive to |
| 1276 | more intrusive repairs: |
| 1277 | 1. SSH to the DUT and reboot. |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1278 | 2. If there's a servo for the DUT, try to power the DUT off and |
| 1279 | on. |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1280 | 3. If the DUT can be power-cycled via RPM, try to repair |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1281 | by power-cycling. |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1282 | 4. Try to re-install to a known stable image using |
| 1283 | auto-update. |
| 1284 | 5. If there's a servo for the DUT, try to re-install via |
| 1285 | the servo. |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1286 | |
| 1287 | As with the parent method, the last operation performed on |
Prashanth B | 4d8184f | 2014-05-05 12:22:02 -0700 | [diff] [blame] | 1288 | the DUT must be to call `self.check_device()`; If that call fails the |
| 1289 | exception it raises is passed back to the caller. |
J. Richard Barnette | fde55fc | 2013-03-15 17:47:01 -0700 | [diff] [blame] | 1290 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1291 | @raises AutoservRepairTotalFailure if the repair process fails to |
| 1292 | fix the DUT. |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1293 | @raises ServoHostRepairTotalFailure if the repair process fails to |
| 1294 | fix the servo host if one is attached to the DUT. |
| 1295 | @raises AutoservSshPermissionDeniedError if it is unable |
| 1296 | to ssh to the servo host due to permission error. |
| 1297 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1298 | """ |
Jakob Juelich | 82b7d1c | 2014-09-15 16:10:57 -0700 | [diff] [blame] | 1299 | # Caution: Deleting shards relies on repair to always reboot the DUT. |
| 1300 | |
Nicolas Boichat | e30c7e1 | 2015-11-05 11:12:50 +0800 | [diff] [blame] | 1301 | # To repair a DUT connected to a moblab, try to create a servo object if |
| 1302 | # it was failed to be created earlier as there may be a servo_host host |
| 1303 | # attribute for this host. |
| 1304 | if utils.is_moblab(): |
| 1305 | self._setup_servo() |
| 1306 | |
Dan Shi | 4d47852 | 2014-02-14 13:46:32 -0800 | [diff] [blame] | 1307 | if self._servo_host and not self.servo: |
Fang Deng | 03590af | 2013-10-07 17:34:20 -0700 | [diff] [blame] | 1308 | try: |
Dan Shi | 4d47852 | 2014-02-14 13:46:32 -0800 | [diff] [blame] | 1309 | self._servo_host.repair_full() |
Fang Deng | 03590af | 2013-10-07 17:34:20 -0700 | [diff] [blame] | 1310 | except Exception as e: |
Fang Deng | 03590af | 2013-10-07 17:34:20 -0700 | [diff] [blame] | 1311 | logging.error('Could not create a healthy servo: %s', e) |
Dan Shi | 4d47852 | 2014-02-14 13:46:32 -0800 | [diff] [blame] | 1312 | self.servo = self._servo_host.get_servo() |
Fang Deng | 5d518f4 | 2013-08-02 14:04:32 -0700 | [diff] [blame] | 1313 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1314 | self.try_collect_crashlogs() |
| 1315 | |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1316 | # TODO(scottz): This should use something similar to label_decorator, |
| 1317 | # but needs to be populated in order so DUTs are repaired with the |
| 1318 | # least amount of effort. |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1319 | force_powerwash = self._is_last_provision_failed() |
| 1320 | if force_powerwash: |
| 1321 | logging.info('Last provision failed, try powerwash first.') |
| 1322 | autotest_stats.Counter( |
| 1323 | 'repair_force_powerwash.TOTAL').increment() |
| 1324 | repair_funcs = [self._install_repair_with_powerwash, |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 1325 | self._servo_repair_reinstall, |
| 1326 | self._firmware_repair] |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1327 | else: |
| 1328 | repair_funcs = [self._reboot_repair, |
| 1329 | self._servo_repair_power, |
| 1330 | self._powercycle_to_repair, |
| 1331 | self._install_repair, |
| 1332 | self._install_repair_with_powerwash, |
Dan Shi | 3d7a0e1 | 2015-10-12 11:55:45 -0700 | [diff] [blame] | 1333 | self._servo_repair_reinstall, |
| 1334 | self._firmware_repair] |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1335 | errors = [] |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1336 | board = self._get_board_from_afe() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1337 | for repair_func in repair_funcs: |
| 1338 | try: |
| 1339 | repair_func() |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1340 | self.try_collect_crashlogs() |
Prashanth B | 4d8184f | 2014-05-05 12:22:02 -0700 | [diff] [blame] | 1341 | self.check_device() |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1342 | autotest_stats.Counter( |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1343 | '%s.SUCCEEDED' % repair_func.__name__).increment() |
| 1344 | if board: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1345 | autotest_stats.Counter( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1346 | '%s.%s.SUCCEEDED' % (repair_func.__name__, |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1347 | board)).increment() |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1348 | if force_powerwash: |
| 1349 | autotest_stats.Counter( |
| 1350 | 'repair_force_powerwash.SUCCEEDED').increment() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1351 | return |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1352 | except error.AutoservRepairMethodNA as e: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1353 | autotest_stats.Counter( |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1354 | '%s.RepairNA' % repair_func.__name__).increment() |
| 1355 | if board: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1356 | autotest_stats.Counter( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1357 | '%s.%s.RepairNA' % (repair_func.__name__, |
| 1358 | board)).increment() |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 1359 | logging.warning('Repair function NA: %s', e) |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1360 | errors.append(str(e)) |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1361 | except Exception as e: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1362 | autotest_stats.Counter( |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1363 | '%s.FAILED' % repair_func.__name__).increment() |
| 1364 | if board: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1365 | autotest_stats.Counter( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1366 | '%s.%s.FAILED' % (repair_func.__name__, |
| 1367 | board)).increment() |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 1368 | logging.warning('Failed to repair device: %s', e) |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1369 | errors.append(str(e)) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 1370 | |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1371 | if force_powerwash: |
| 1372 | autotest_stats.Counter( |
| 1373 | 'repair_force_powerwash.FAILED').increment() |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1374 | autotest_stats.Counter('Full_Repair_Failed').increment() |
Simran Basi | e613093 | 2013-10-01 14:07:52 -0700 | [diff] [blame] | 1375 | if board: |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 1376 | autotest_stats.Counter( |
Dan Shi | d07ee2e | 2015-09-24 14:49:25 -0700 | [diff] [blame] | 1377 | 'Full_Repair_Failed.%s' % board).increment() |
Scott Zawalski | 62bacae | 2013-03-05 10:40:32 -0500 | [diff] [blame] | 1378 | raise error.AutoservRepairTotalFailure( |
| 1379 | 'All attempts at repairing the device failed:\n%s' % |
| 1380 | '\n'.join(errors)) |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1381 | |
| 1382 | |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1383 | def try_collect_crashlogs(self, check_host_up=True): |
| 1384 | """ |
| 1385 | Check if a host is up and logs need to be collected from the host, |
| 1386 | if yes, collect them. |
| 1387 | |
| 1388 | @param check_host_up: Flag for checking host is up. Default is True. |
| 1389 | """ |
| 1390 | try: |
| 1391 | crash_job = self._need_crash_logs() |
| 1392 | if crash_job: |
| 1393 | logging.debug('%s: Job %s was crashed', self._CRASHLOGS_PREFIX, |
| 1394 | crash_job) |
| 1395 | if not check_host_up or self.is_up( |
| 1396 | self._CHECK_HOST_UP_TIMEOUT_SECS): |
| 1397 | self._collect_crashlogs(crash_job) |
| 1398 | logging.debug('%s: Completed collecting logs for the ' |
| 1399 | 'crashed job %s', self._CRASHLOGS_PREFIX, |
| 1400 | crash_job) |
| 1401 | except Exception as e: |
| 1402 | # Exception should not result in repair failure. |
| 1403 | # Therefore, suppress all exceptions here. |
| 1404 | logging.error('%s: Failed while trying to collect crash-logs: %s', |
| 1405 | self._CRASHLOGS_PREFIX, e) |
| 1406 | |
| 1407 | |
| 1408 | def _need_crash_logs(self): |
| 1409 | """Get the value of need_crash_logs attribute of this host. |
| 1410 | |
| 1411 | @return: Value string of need_crash_logs attribute |
| 1412 | None if there is no need_crash_logs attribute |
| 1413 | """ |
| 1414 | attrs = self._AFE.get_host_attribute(constants.CRASHLOGS_HOST_ATTRIBUTE, |
| 1415 | hostname=self.hostname) |
| 1416 | assert len(attrs) < 2 |
| 1417 | return attrs[0].value if attrs else None |
| 1418 | |
| 1419 | |
| 1420 | def _collect_crashlogs(self, job_id): |
| 1421 | """Grab logs from the host where a job was crashed. |
| 1422 | |
| 1423 | First, check if PRIOR_LOGS_DIR exists in the host. |
| 1424 | If yes, collect them. |
| 1425 | Otherwise, check if a lab-machine marker (_LAB_MACHINE_FILE) exists |
| 1426 | in the host. |
| 1427 | If yes, the host was repaired automatically, and we collect normal |
| 1428 | system logs. |
| 1429 | |
| 1430 | @param job_id: Id of the job that was crashed. |
| 1431 | """ |
| 1432 | crashlogs_dir = crashcollect.get_crashinfo_dir(self, |
| 1433 | constants.CRASHLOGS_DEST_DIR_PREFIX) |
| 1434 | flag_prior_logs = False |
| 1435 | |
| 1436 | if self.path_exists(client_constants.PRIOR_LOGS_DIR): |
| 1437 | flag_prior_logs = True |
| 1438 | self._collect_prior_logs(crashlogs_dir) |
| 1439 | elif self.path_exists(self._LAB_MACHINE_FILE): |
| 1440 | self._collect_system_logs(crashlogs_dir) |
| 1441 | else: |
| 1442 | logging.warning('%s: Host was manually re-installed without ' |
| 1443 | '--lab_preserve_log option. Skip collecting ' |
| 1444 | 'crash-logs.', self._CRASHLOGS_PREFIX) |
| 1445 | |
| 1446 | # We make crash collection be one-time effort. |
| 1447 | # _collect_prior_logs() and _collect_system_logs() will not throw |
| 1448 | # any exception, and following codes will be executed even when |
| 1449 | # those methods fail. |
| 1450 | # _collect_crashlogs() is called only when the host is up (refer |
| 1451 | # to try_collect_crashlogs()). We assume _collect_prior_logs() and |
| 1452 | # _collect_system_logs() fail rarely when the host is up. |
| 1453 | # In addition, it is not clear how many times we should try crash |
| 1454 | # collection again while not triggering next repair unnecessarily. |
| 1455 | # Threfore, we try crash collection one time. |
| 1456 | |
| 1457 | # Create a marker file as soon as log collection is done. |
| 1458 | # Leave the job id to this marker for gs_offloader to consume. |
| 1459 | marker_file = os.path.join(crashlogs_dir, constants.CRASHLOGS_MARKER) |
| 1460 | with open(marker_file, 'a') as f: |
| 1461 | f.write('%s\n' % job_id) |
| 1462 | |
| 1463 | # Remove need_crash_logs attribute |
| 1464 | logging.debug('%s: Remove attribute need_crash_logs from host %s', |
| 1465 | self._CRASHLOGS_PREFIX, self.hostname) |
| 1466 | self._AFE.set_host_attribute(constants.CRASHLOGS_HOST_ATTRIBUTE, |
| 1467 | None, hostname=self.hostname) |
| 1468 | |
| 1469 | if flag_prior_logs: |
| 1470 | logging.debug('%s: Remove %s from host %s', self._CRASHLOGS_PREFIX, |
| 1471 | client_constants.PRIOR_LOGS_DIR, self.hostname) |
| 1472 | self.run('rm -rf %s; sync' % client_constants.PRIOR_LOGS_DIR) |
| 1473 | # Wait for a few seconds to make sure the prior command is |
| 1474 | # done deep through storage. |
| 1475 | time.sleep(self._SAFE_WAIT_SECS) |
| 1476 | |
| 1477 | |
| 1478 | def _collect_prior_logs(self, crashlogs_dir): |
| 1479 | """Grab prior logs that were stashed before re-installing a host. |
| 1480 | |
| 1481 | @param crashlogs_dir: Directory path where crash-logs are stored. |
| 1482 | """ |
| 1483 | logging.debug('%s: Found %s, collecting them...', |
| 1484 | self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR) |
| 1485 | try: |
| 1486 | self.collect_logs(client_constants.PRIOR_LOGS_DIR, |
| 1487 | crashlogs_dir, False) |
| 1488 | logging.debug('%s: %s is collected', |
| 1489 | self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR) |
| 1490 | except Exception as e: |
| 1491 | logging.error('%s: Failed to collect %s: %s', |
| 1492 | self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR, |
| 1493 | e) |
| 1494 | |
| 1495 | |
| 1496 | def _collect_system_logs(self, crashlogs_dir): |
| 1497 | """Grab normal system logs from a host. |
| 1498 | |
| 1499 | @param crashlogs_dir: Directory path where crash-logs are stored. |
| 1500 | """ |
| 1501 | logging.debug('%s: Found %s, collecting system logs...', |
| 1502 | self._CRASHLOGS_PREFIX, self._LAB_MACHINE_FILE) |
| 1503 | sources = server_utils.parse_simple_config(self._LOGS_TO_COLLECT_FILE) |
| 1504 | for src in sources: |
| 1505 | try: |
| 1506 | if self.path_exists(src): |
| 1507 | logging.debug('%s: Collecting %s...', |
| 1508 | self._CRASHLOGS_PREFIX, src) |
| 1509 | dest = server_utils.concat_path_except_last( |
| 1510 | crashlogs_dir, src) |
| 1511 | self.collect_logs(src, dest, False) |
| 1512 | logging.debug('%s: %s is collected', |
| 1513 | self._CRASHLOGS_PREFIX, src) |
| 1514 | except Exception as e: |
| 1515 | logging.error('%s: Failed to collect %s: %s', |
| 1516 | self._CRASHLOGS_PREFIX, src, e) |
| 1517 | |
| 1518 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 1519 | def close(self): |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 1520 | super(CrosHost, self).close() |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 1521 | |
| 1522 | |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1523 | def get_power_supply_info(self): |
| 1524 | """Get the output of power_supply_info. |
| 1525 | |
| 1526 | power_supply_info outputs the info of each power supply, e.g., |
| 1527 | Device: Line Power |
| 1528 | online: no |
| 1529 | type: Mains |
| 1530 | voltage (V): 0 |
| 1531 | current (A): 0 |
| 1532 | Device: Battery |
| 1533 | state: Discharging |
| 1534 | percentage: 95.9276 |
| 1535 | technology: Li-ion |
| 1536 | |
| 1537 | Above output shows two devices, Line Power and Battery, with details of |
| 1538 | each device listed. This function parses the output into a dictionary, |
| 1539 | with key being the device name, and value being a dictionary of details |
| 1540 | of the device info. |
| 1541 | |
| 1542 | @return: The dictionary of power_supply_info, e.g., |
| 1543 | {'Line Power': {'online': 'yes', 'type': 'main'}, |
| 1544 | 'Battery': {'vendor': 'xyz', 'percentage': '100'}} |
Dan Shi | e9b765d | 2014-12-29 16:59:49 -0800 | [diff] [blame] | 1545 | @raise error.AutoservRunError if power_supply_info tool is not found in |
| 1546 | the DUT. Caller should handle this error to avoid false failure |
| 1547 | on verification. |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1548 | """ |
| 1549 | result = self.run('power_supply_info').stdout.strip() |
| 1550 | info = {} |
| 1551 | device_name = None |
| 1552 | device_info = {} |
| 1553 | for line in result.split('\n'): |
| 1554 | pair = [v.strip() for v in line.split(':')] |
| 1555 | if len(pair) != 2: |
| 1556 | continue |
| 1557 | if pair[0] == 'Device': |
| 1558 | if device_name: |
| 1559 | info[device_name] = device_info |
| 1560 | device_name = pair[1] |
| 1561 | device_info = {} |
| 1562 | else: |
| 1563 | device_info[pair[0]] = pair[1] |
| 1564 | if device_name and not device_name in info: |
| 1565 | info[device_name] = device_info |
| 1566 | return info |
| 1567 | |
| 1568 | |
| 1569 | def get_battery_percentage(self): |
| 1570 | """Get the battery percentage. |
| 1571 | |
| 1572 | @return: The percentage of battery level, value range from 0-100. Return |
| 1573 | None if the battery info cannot be retrieved. |
| 1574 | """ |
| 1575 | try: |
| 1576 | info = self.get_power_supply_info() |
| 1577 | logging.info(info) |
| 1578 | return float(info['Battery']['percentage']) |
Dan Shi | e9b765d | 2014-12-29 16:59:49 -0800 | [diff] [blame] | 1579 | except (KeyError, ValueError, error.AutoservRunError): |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1580 | return None |
| 1581 | |
| 1582 | |
| 1583 | def is_ac_connected(self): |
| 1584 | """Check if the dut has power adapter connected and charging. |
| 1585 | |
| 1586 | @return: True if power adapter is connected and charging. |
| 1587 | """ |
| 1588 | try: |
| 1589 | info = self.get_power_supply_info() |
| 1590 | return info['Line Power']['online'] == 'yes' |
Dan Shi | e9b765d | 2014-12-29 16:59:49 -0800 | [diff] [blame] | 1591 | except (KeyError, error.AutoservRunError): |
| 1592 | return None |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1593 | |
| 1594 | |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1595 | def _cleanup_poweron(self): |
| 1596 | """Special cleanup method to make sure hosts always get power back.""" |
| 1597 | afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) |
| 1598 | hosts = afe.get_hosts(hostname=self.hostname) |
| 1599 | if not hosts or not (self._RPM_OUTLET_CHANGED in |
| 1600 | hosts[0].attributes): |
| 1601 | return |
| 1602 | logging.debug('This host has recently interacted with the RPM' |
| 1603 | ' Infrastructure. Ensuring power is on.') |
| 1604 | try: |
| 1605 | self.power_on() |
Dan Shi | 7dca56e | 2014-11-11 17:07:56 -0800 | [diff] [blame] | 1606 | afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None, |
| 1607 | hostname=self.hostname) |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1608 | except rpm_client.RemotePowerException: |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1609 | logging.error('Failed to turn Power On for this host after ' |
| 1610 | 'cleanup through the RPM Infrastructure.') |
Gabe Black | b72f4fb | 2015-01-20 16:47:13 -0800 | [diff] [blame] | 1611 | autotest_es.post( |
Dan Shi | 7dca56e | 2014-11-11 17:07:56 -0800 | [diff] [blame] | 1612 | type_str='RPM_poweron_failure', |
| 1613 | metadata={'hostname': self.hostname}) |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1614 | |
| 1615 | battery_percentage = self.get_battery_percentage() |
Dan Shi | f01ebe2 | 2014-12-05 13:10:57 -0800 | [diff] [blame] | 1616 | if battery_percentage and battery_percentage < 50: |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1617 | raise |
| 1618 | elif self.is_ac_connected(): |
| 1619 | logging.info('The device has power adapter connected and ' |
| 1620 | 'charging. No need to try to turn RPM on ' |
| 1621 | 'again.') |
| 1622 | afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None, |
| 1623 | hostname=self.hostname) |
| 1624 | logging.info('Battery level is now at %s%%. The device may ' |
| 1625 | 'still have enough power to run test, so no ' |
| 1626 | 'exception will be raised.', battery_percentage) |
| 1627 | |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1628 | |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1629 | def _is_factory_image(self): |
| 1630 | """Checks if the image on the DUT is a factory image. |
| 1631 | |
| 1632 | @return: True if the image on the DUT is a factory image. |
| 1633 | False otherwise. |
| 1634 | """ |
| 1635 | result = self.run('[ -f /root/.factory_test ]', ignore_status=True) |
| 1636 | return result.exit_status == 0 |
| 1637 | |
| 1638 | |
| 1639 | def _restart_ui(self): |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 1640 | """Restart the Chrome UI. |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1641 | |
| 1642 | @raises: FactoryImageCheckerException for factory images, since |
| 1643 | we cannot attempt to restart ui on them. |
| 1644 | error.AutoservRunError for any other type of error that |
| 1645 | occurs while restarting ui. |
| 1646 | """ |
| 1647 | if self._is_factory_image(): |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 1648 | raise FactoryImageCheckerException('Cannot restart ui on factory ' |
| 1649 | 'images') |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1650 | |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 1651 | # TODO(jrbarnette): The command to stop/start the ui job |
| 1652 | # should live inside cros_ui, too. However that would seem |
| 1653 | # to imply interface changes to the existing start()/restart() |
| 1654 | # functions, which is a bridge too far (for now). |
J. Richard Barnette | 6069aa1 | 2015-06-08 09:10:24 -0700 | [diff] [blame] | 1655 | prompt = cros_ui.get_chrome_session_ident(self) |
J. Richard Barnette | 84890bd | 2014-02-21 11:05:47 -0800 | [diff] [blame] | 1656 | self.run('stop ui; start ui') |
| 1657 | cros_ui.wait_for_chrome_ready(prompt, self) |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1658 | |
| 1659 | |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 1660 | def get_release_version(self): |
| 1661 | """Get the value of attribute CHROMEOS_RELEASE_VERSION from lsb-release. |
| 1662 | |
| 1663 | @returns The version string in lsb-release, under attribute |
| 1664 | CHROMEOS_RELEASE_VERSION. |
| 1665 | """ |
| 1666 | lsb_release_content = self.run( |
| 1667 | 'cat "%s"' % client_constants.LSB_RELEASE).stdout.strip() |
| 1668 | return lsbrelease_utils.get_chromeos_release_version( |
| 1669 | lsb_release_content=lsb_release_content) |
| 1670 | |
| 1671 | |
| 1672 | def verify_cros_version_label(self): |
| 1673 | """ Make sure host's cros-version label match the actual image in dut. |
| 1674 | |
| 1675 | Remove any cros-version: label that doesn't match that installed in |
| 1676 | the dut. |
| 1677 | |
| 1678 | @param raise_error: Set to True to raise exception if any mismatch found |
| 1679 | |
| 1680 | @raise error.AutoservError: If any mismatch between cros-version label |
| 1681 | and the build installed in dut is found. |
| 1682 | """ |
| 1683 | labels = self._AFE.get_labels( |
| 1684 | name__startswith=ds_constants.VERSION_PREFIX, |
| 1685 | host__hostname=self.hostname) |
| 1686 | mismatch_found = False |
| 1687 | if labels: |
| 1688 | # Get CHROMEOS_RELEASE_VERSION from lsb-release, e.g., 6908.0.0. |
| 1689 | # Note that it's different from cros-version label, which has |
| 1690 | # builder and branch info, e.g., |
| 1691 | # cros-version:peppy-release/R43-6908.0.0 |
| 1692 | release_version = self.get_release_version() |
| 1693 | host_list = [self.hostname] |
| 1694 | for label in labels: |
| 1695 | # Remove any cros-version label that does not match |
| 1696 | # release_version. |
| 1697 | build_version = label.name[len(ds_constants.VERSION_PREFIX):] |
| 1698 | if not utils.version_match(build_version, release_version): |
| 1699 | logging.warn('cros-version label "%s" does not match ' |
| 1700 | 'release version %s. Removing the label.', |
| 1701 | label.name, release_version) |
| 1702 | label.remove_hosts(hosts=host_list) |
| 1703 | mismatch_found = True |
| 1704 | if mismatch_found: |
Dan Shi | 1057bae | 2015-03-30 11:35:09 -0700 | [diff] [blame] | 1705 | autotest_es.post(use_http=True, |
| 1706 | type_str='cros_version_label_mismatch', |
| 1707 | metadata={'hostname': self.hostname}) |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 1708 | raise error.AutoservError('The host has wrong cros-version label.') |
| 1709 | |
| 1710 | |
Darren Krahn | 0bd18e8 | 2015-10-28 23:30:46 +0000 | [diff] [blame] | 1711 | def verify_tpm_status(self): |
| 1712 | """ Verify the host's TPM is in a good state. |
| 1713 | |
| 1714 | @raise error.AutoservError: If state is not good. |
| 1715 | """ |
| 1716 | # This cryptohome command emits status information in JSON format. It |
| 1717 | # looks something like this: |
| 1718 | # { |
| 1719 | # "installattrs": { |
| 1720 | # "first_install": false, |
| 1721 | # "initialized": true, |
| 1722 | # "invalid": false, |
| 1723 | # "lockbox_index": 536870916, |
| 1724 | # "lockbox_nvram_version": 2, |
| 1725 | # "secure": true, |
| 1726 | # "size": 0, |
| 1727 | # "version": 1 |
| 1728 | # }, |
| 1729 | # "mounts": [ { |
| 1730 | # "enterprise": false, |
| 1731 | # "keysets": [ { |
| 1732 | # "current": true, |
| 1733 | # "index": 0, |
| 1734 | # "last_activity": 1330111359, |
| 1735 | # "ok": true, |
| 1736 | # "scrypt": true, |
| 1737 | # "tpm": false |
| 1738 | # } ], |
| 1739 | # "mounted": true, |
| 1740 | # "owner": "dbb3dd34edb181245130e136be51fa08478d3909" |
| 1741 | # } ], |
| 1742 | # "tpm": { |
| 1743 | # "being_owned": false, |
| 1744 | # "can_connect": true, |
| 1745 | # "can_decrypt": false, |
| 1746 | # "can_encrypt": false, |
| 1747 | # "can_load_srk": true, |
| 1748 | # "can_load_srk_pubkey": true, |
| 1749 | # "enabled": true, |
| 1750 | # "has_context": true, |
| 1751 | # "has_cryptohome_key": false, |
| 1752 | # "has_key_handle": false, |
| 1753 | # "last_error": 0, |
| 1754 | # "owned": true |
| 1755 | # } |
| 1756 | # } |
| 1757 | output = self.run('cryptohome --action=status').stdout.strip() |
| 1758 | try: |
| 1759 | status = json.loads(output) |
| 1760 | except ValueError: |
| 1761 | logging.error('TPM_VERIFY: Cryptohome did not return valid status.') |
| 1762 | return |
| 1763 | try: |
| 1764 | tpm = status['tpm'] |
| 1765 | if (not tpm['enabled'] or not tpm['can_connect'] or |
| 1766 | (tpm['owned'] and not tpm['can_load_srk']) or |
| 1767 | (tpm['can_load_srk'] and not tpm['can_load_srk_pubkey'])): |
| 1768 | logging.error('TPM_VERIFY: The host TPM is in a bad state.') |
| 1769 | raise error.AutoservError('The host TPM is in a bad state.') |
| 1770 | else: |
| 1771 | logging.debug('TPM_VERIFY: The host TPM is in a good state.') |
| 1772 | except KeyError: |
| 1773 | logging.error('TPM_VERIFY: Cryptohome did not return valid status.') |
| 1774 | |
| 1775 | |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1776 | def cleanup(self): |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1777 | self.run('rm -f %s' % client_constants.CLEANUP_LOGS_PAUSED_FILE) |
Scott Zawalski | ddbc31e | 2012-11-15 11:29:01 -0500 | [diff] [blame] | 1778 | try: |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1779 | self._restart_ui() |
| 1780 | except (error.AutotestRunError, error.AutoservRunError, |
| 1781 | FactoryImageCheckerException): |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 1782 | logging.warning('Unable to restart ui, rebooting device.') |
Scott Zawalski | ddbc31e | 2012-11-15 11:29:01 -0500 | [diff] [blame] | 1783 | # Since restarting the UI fails fall back to normal Autotest |
| 1784 | # cleanup routines, i.e. reboot the machine. |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 1785 | super(CrosHost, self).cleanup() |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1786 | # Check if the rpm outlet was manipulated. |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 1787 | if self.has_power(): |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1788 | self._cleanup_poweron() |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 1789 | self.verify_cros_version_label() |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1790 | |
| 1791 | |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 1792 | def reboot(self, **dargs): |
| 1793 | """ |
| 1794 | This function reboots the site host. The more generic |
| 1795 | RemoteHost.reboot() performs sync and sleeps for 5 |
| 1796 | seconds. This is not necessary for Chrome OS devices as the |
| 1797 | sync should be finished in a short time during the reboot |
| 1798 | command. |
| 1799 | """ |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 1800 | if 'reboot_cmd' not in dargs: |
Doug Anderson | 7d5aeb2 | 2014-02-27 15:12:17 -0800 | [diff] [blame] | 1801 | reboot_timeout = dargs.get('reboot_timeout', 10) |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 1802 | dargs['reboot_cmd'] = ('sleep 1; ' |
| 1803 | 'reboot & sleep %d; ' |
| 1804 | 'reboot -f' % reboot_timeout) |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 1805 | # Enable fastsync to avoid running extra sync commands before reboot. |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 1806 | if 'fastsync' not in dargs: |
| 1807 | dargs['fastsync'] = True |
Michael Liang | da8c60a | 2014-06-03 13:24:51 -0700 | [diff] [blame] | 1808 | |
Charlie Mooney | a8e6dab | 2014-05-29 14:37:55 -0700 | [diff] [blame] | 1809 | # For purposes of logging reboot times: |
| 1810 | # Get the board name i.e. 'daisy_spring' |
Michael Liang | ca4f5a6 | 2014-07-10 15:45:13 -0700 | [diff] [blame] | 1811 | board_fullname = self.get_board() |
| 1812 | |
| 1813 | # Strip the prefix and add it to dargs. |
| 1814 | dargs['board'] = board_fullname[board_fullname.find(':')+1:] |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 1815 | super(CrosHost, self).reboot(**dargs) |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 1816 | |
| 1817 | |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 1818 | def suspend(self, **dargs): |
| 1819 | """ |
| 1820 | This function suspends the site host. |
| 1821 | """ |
| 1822 | suspend_time = dargs.get('suspend_time', 60) |
| 1823 | dargs['timeout'] = suspend_time |
| 1824 | if 'suspend_cmd' not in dargs: |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 1825 | dargs['suspend_cmd'] = ' && '.join([ |
| 1826 | 'echo 0 > /sys/class/rtc/rtc0/wakealarm', |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 1827 | 'echo +%d > /sys/class/rtc/rtc0/wakealarm' % suspend_time, |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 1828 | 'powerd_dbus_suspend --delay=0']) |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 1829 | super(CrosHost, self).suspend(**dargs) |
| 1830 | |
| 1831 | |
Simran Basi | ec56439 | 2014-08-25 16:48:09 -0700 | [diff] [blame] | 1832 | def upstart_status(self, service_name): |
| 1833 | """Check the status of an upstart init script. |
| 1834 | |
| 1835 | @param service_name: Service to look up. |
| 1836 | |
| 1837 | @returns True if the service is running, False otherwise. |
| 1838 | """ |
| 1839 | return self.run('status %s | grep start/running' % |
| 1840 | service_name).stdout.strip() != '' |
| 1841 | |
| 1842 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1843 | def verify_software(self): |
Richard Barnette | b2bc13c | 2013-01-08 17:32:51 -0800 | [diff] [blame] | 1844 | """Verify working software on a Chrome OS system. |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1845 | |
Richard Barnette | b2bc13c | 2013-01-08 17:32:51 -0800 | [diff] [blame] | 1846 | Tests for the following conditions: |
| 1847 | 1. All conditions tested by the parent version of this |
| 1848 | function. |
| 1849 | 2. Sufficient space in /mnt/stateful_partition. |
Fang Deng | 6b05f5b | 2013-03-20 13:42:11 -0700 | [diff] [blame] | 1850 | 3. Sufficient space in /mnt/stateful_partition/encrypted. |
| 1851 | 4. update_engine answers a simple status request over DBus. |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1852 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1853 | """ |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 1854 | # Check if a job was crashed on this host. |
| 1855 | # If yes, avoid verification until crash-logs are collected. |
| 1856 | if self._need_crash_logs(): |
| 1857 | raise error.AutoservCrashLogCollectRequired( |
| 1858 | 'Need to collect crash-logs before verification') |
| 1859 | |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 1860 | super(CrosHost, self).verify_software() |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 1861 | default_kilo_inodes_required = CONFIG.get_config_value( |
| 1862 | 'SERVER', 'kilo_inodes_required', type=int, default=100) |
| 1863 | board = self.get_board().replace(ds_constants.BOARD_PREFIX, '') |
| 1864 | kilo_inodes_required = CONFIG.get_config_value( |
| 1865 | 'SERVER', 'kilo_inodes_required_%s' % board, |
| 1866 | type=int, default=default_kilo_inodes_required) |
| 1867 | self.check_inodes('/mnt/stateful_partition', kilo_inodes_required) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 1868 | self.check_diskspace( |
| 1869 | '/mnt/stateful_partition', |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 1870 | CONFIG.get_config_value( |
Fang Deng | 6b05f5b | 2013-03-20 13:42:11 -0700 | [diff] [blame] | 1871 | 'SERVER', 'gb_diskspace_required', type=float, |
| 1872 | default=20.0)) |
Gaurav Shah | e448af8 | 2014-06-19 15:18:59 -0700 | [diff] [blame] | 1873 | encrypted_stateful_path = '/mnt/stateful_partition/encrypted' |
| 1874 | # Not all targets build with encrypted stateful support. |
| 1875 | if self.path_exists(encrypted_stateful_path): |
| 1876 | self.check_diskspace( |
| 1877 | encrypted_stateful_path, |
Dan Shi | b8540a5 | 2015-07-16 14:18:23 -0700 | [diff] [blame] | 1878 | CONFIG.get_config_value( |
Gaurav Shah | e448af8 | 2014-06-19 15:18:59 -0700 | [diff] [blame] | 1879 | 'SERVER', 'gb_encrypted_diskspace_required', type=float, |
| 1880 | default=0.1)) |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1881 | |
Simran Basi | ec56439 | 2014-08-25 16:48:09 -0700 | [diff] [blame] | 1882 | if not self.upstart_status('system-services'): |
Prashanth B | 5d0a051 | 2014-04-25 12:26:08 -0700 | [diff] [blame] | 1883 | raise error.AutoservError('Chrome failed to reach login. ' |
| 1884 | 'System services not running.') |
| 1885 | |
beeps | c87ff60 | 2013-07-31 21:53:00 -0700 | [diff] [blame] | 1886 | # Factory images don't run update engine, |
| 1887 | # goofy controls dbus on these DUTs. |
| 1888 | if not self._is_factory_image(): |
| 1889 | self.run('update_engine_client --status') |
Scott Zawalski | fbca4a9 | 2013-03-04 15:56:42 -0500 | [diff] [blame] | 1890 | # Makes sure python is present, loads and can use built in functions. |
| 1891 | # We have seen cases where importing cPickle fails with undefined |
| 1892 | # symbols in cPickle.so. |
| 1893 | self.run('python -c "import cPickle"') |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1894 | |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 1895 | self.verify_cros_version_label() |
| 1896 | |
Darren Krahn | 0bd18e8 | 2015-10-28 23:30:46 +0000 | [diff] [blame] | 1897 | self.verify_tpm_status() |
| 1898 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1899 | |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1900 | def verify_hardware(self): |
| 1901 | """Verify hardware system of a Chrome OS system. |
| 1902 | |
| 1903 | Check following hardware conditions: |
| 1904 | 1. Battery level. |
| 1905 | 2. Is power adapter connected. |
| 1906 | """ |
| 1907 | logging.info('Battery percentage: %s', self.get_battery_percentage()) |
Dan Shi | e9b765d | 2014-12-29 16:59:49 -0800 | [diff] [blame] | 1908 | if self.is_ac_connected() is None: |
| 1909 | logging.info('Can not determine if the device has power adapter ' |
| 1910 | 'connected.') |
| 1911 | else: |
| 1912 | logging.info('Device %s power adapter connected and charging.', |
| 1913 | 'has' if self.is_ac_connected() else 'does not have') |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 1914 | |
| 1915 | |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 1916 | def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None, |
| 1917 | connect_timeout=None, alive_interval=None): |
| 1918 | """Override default make_ssh_command to use options tuned for Chrome OS. |
| 1919 | |
| 1920 | Tuning changes: |
| 1921 | - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH |
| 1922 | connection failure. Consistency with remote_access.sh. |
| 1923 | |
Samuel Tan | 2ce155b | 2015-06-23 18:24:38 -0700 | [diff] [blame] | 1924 | - ServerAliveInterval=900; which causes SSH to ping connection every |
| 1925 | 900 seconds. In conjunction with ServerAliveCountMax ensures |
| 1926 | that if the connection dies, Autotest will bail out. |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 1927 | Originally tried 60 secs, but saw frequent job ABORTS where |
Samuel Tan | 2ce155b | 2015-06-23 18:24:38 -0700 | [diff] [blame] | 1928 | the test completed successfully. Later increased from 180 seconds to |
| 1929 | 900 seconds to account for tests where the DUT is suspended for |
| 1930 | longer periods of time. |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 1931 | |
| 1932 | - ServerAliveCountMax=3; consistency with remote_access.sh. |
| 1933 | |
| 1934 | - ConnectAttempts=4; reduce flakiness in connection errors; |
| 1935 | consistency with remote_access.sh. |
| 1936 | |
| 1937 | - UserKnownHostsFile=/dev/null; we don't care about the keys. |
| 1938 | Host keys change with every new installation, don't waste |
| 1939 | memory/space saving them. |
| 1940 | |
| 1941 | - SSH protocol forced to 2; needed for ServerAliveInterval. |
| 1942 | |
| 1943 | @param user User name to use for the ssh connection. |
| 1944 | @param port Port on the target host to use for ssh connection. |
| 1945 | @param opts Additional options to the ssh command. |
| 1946 | @param hosts_file Ignored. |
| 1947 | @param connect_timeout Ignored. |
| 1948 | @param alive_interval Ignored. |
| 1949 | """ |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 1950 | base_command = ('/usr/bin/ssh -a -x %s %s %s' |
| 1951 | ' -o StrictHostKeyChecking=no' |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 1952 | ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes' |
Samuel Tan | 2ce155b | 2015-06-23 18:24:38 -0700 | [diff] [blame] | 1953 | ' -o ConnectTimeout=30 -o ServerAliveInterval=900' |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 1954 | ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4' |
| 1955 | ' -o Protocol=2 -l %s -p %d') |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 1956 | return base_command % (self._ssh_verbosity_flag, self._ssh_options, |
| 1957 | opts, user, port) |
Jason Abele | b6f924f | 2013-11-13 16:01:54 -0800 | [diff] [blame] | 1958 | def syslog(self, message, tag='autotest'): |
| 1959 | """Logs a message to syslog on host. |
| 1960 | |
| 1961 | @param message String message to log into syslog |
| 1962 | @param tag String tag prefix for syslog |
| 1963 | |
| 1964 | """ |
| 1965 | self.run('logger -t "%s" "%s"' % (tag, message)) |
| 1966 | |
| 1967 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 1968 | def _ping_check_status(self, status): |
| 1969 | """Ping the host once, and return whether it has a given status. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1970 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 1971 | @param status Check the ping status against this value. |
| 1972 | @return True iff `status` and the result of ping are the same |
| 1973 | (i.e. both True or both False). |
| 1974 | |
| 1975 | """ |
| 1976 | ping_val = utils.ping(self.hostname, tries=1, deadline=1) |
| 1977 | return not (status ^ (ping_val == 0)) |
| 1978 | |
| 1979 | def _ping_wait_for_status(self, status, timeout): |
| 1980 | """Wait for the host to have a given status (UP or DOWN). |
| 1981 | |
| 1982 | Status is checked by polling. Polling will not last longer |
| 1983 | than the number of seconds in `timeout`. The polling |
| 1984 | interval will be long enough that only approximately |
| 1985 | _PING_WAIT_COUNT polling cycles will be executed, subject |
| 1986 | to a maximum interval of about one minute. |
| 1987 | |
| 1988 | @param status Waiting will stop immediately if `ping` of the |
| 1989 | host returns this status. |
| 1990 | @param timeout Poll for at most this many seconds. |
| 1991 | @return True iff the host status from `ping` matched the |
| 1992 | requested status at the time of return. |
| 1993 | |
| 1994 | """ |
| 1995 | # _ping_check_status() takes about 1 second, hence the |
| 1996 | # "- 1" in the formula below. |
| 1997 | poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1 |
| 1998 | end_time = time.time() + timeout |
| 1999 | while time.time() <= end_time: |
| 2000 | if self._ping_check_status(status): |
| 2001 | return True |
| 2002 | if poll_interval > 0: |
| 2003 | time.sleep(poll_interval) |
| 2004 | |
| 2005 | # The last thing we did was sleep(poll_interval), so it may |
| 2006 | # have been too long since the last `ping`. Check one more |
| 2007 | # time, just to be sure. |
| 2008 | return self._ping_check_status(status) |
| 2009 | |
| 2010 | def ping_wait_up(self, timeout): |
| 2011 | """Wait for the host to respond to `ping`. |
| 2012 | |
| 2013 | N.B. This method is not a reliable substitute for |
| 2014 | `wait_up()`, because a host that responds to ping will not |
| 2015 | necessarily respond to ssh. This method should only be used |
| 2016 | if the target DUT can be considered functional even if it |
| 2017 | can't be reached via ssh. |
| 2018 | |
| 2019 | @param timeout Minimum time to allow before declaring the |
| 2020 | host to be non-responsive. |
| 2021 | @return True iff the host answered to ping before the timeout. |
| 2022 | |
| 2023 | """ |
| 2024 | return self._ping_wait_for_status(self._PING_STATUS_UP, timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2025 | |
Andrew Bresticker | 678c0c7 | 2013-01-22 10:44:09 -0800 | [diff] [blame] | 2026 | def ping_wait_down(self, timeout): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2027 | """Wait until the host no longer responds to `ping`. |
| 2028 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 2029 | This function can be used as a slightly faster version of |
| 2030 | `wait_down()`, by avoiding potentially long ssh timeouts. |
| 2031 | |
| 2032 | @param timeout Minimum time to allow for the host to become |
| 2033 | non-responsive. |
| 2034 | @return True iff the host quit answering ping before the |
| 2035 | timeout. |
| 2036 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2037 | """ |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 2038 | return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2039 | |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2040 | def test_wait_for_sleep(self, sleep_timeout=None): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2041 | """Wait for the client to enter low-power sleep mode. |
| 2042 | |
| 2043 | The test for "is asleep" can't distinguish a system that is |
| 2044 | powered off; to confirm that the unit was asleep, it is |
| 2045 | necessary to force resume, and then call |
| 2046 | `test_wait_for_resume()`. |
| 2047 | |
| 2048 | This function is expected to be called from a test as part |
| 2049 | of a sequence like the following: |
| 2050 | |
| 2051 | ~~~~~~~~ |
| 2052 | boot_id = host.get_boot_id() |
| 2053 | # trigger sleep on the host |
| 2054 | host.test_wait_for_sleep() |
| 2055 | # trigger resume on the host |
| 2056 | host.test_wait_for_resume(boot_id) |
| 2057 | ~~~~~~~~ |
| 2058 | |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2059 | @param sleep_timeout time limit in seconds to allow the host sleep. |
| 2060 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2061 | @exception TestFail The host did not go to sleep within |
| 2062 | the allowed time. |
| 2063 | """ |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2064 | if sleep_timeout is None: |
| 2065 | sleep_timeout = self.SLEEP_TIMEOUT |
| 2066 | |
| 2067 | if not self.ping_wait_down(timeout=sleep_timeout): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2068 | raise error.TestFail( |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2069 | 'client failed to sleep after %d seconds' % sleep_timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2070 | |
| 2071 | |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2072 | def test_wait_for_resume(self, old_boot_id, resume_timeout=None): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2073 | """Wait for the client to resume from low-power sleep mode. |
| 2074 | |
| 2075 | The `old_boot_id` parameter should be the value from |
| 2076 | `get_boot_id()` obtained prior to entering sleep mode. A |
| 2077 | `TestFail` exception is raised if the boot id changes. |
| 2078 | |
| 2079 | See @ref test_wait_for_sleep for more on this function's |
| 2080 | usage. |
| 2081 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 2082 | @param old_boot_id A boot id value obtained before the |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2083 | target host went to sleep. |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2084 | @param resume_timeout time limit in seconds to allow the host up. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2085 | |
| 2086 | @exception TestFail The host did not respond within the |
| 2087 | allowed time. |
| 2088 | @exception TestFail The host responded, but the boot id test |
| 2089 | indicated a reboot rather than a sleep |
| 2090 | cycle. |
| 2091 | """ |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2092 | if resume_timeout is None: |
| 2093 | resume_timeout = self.RESUME_TIMEOUT |
| 2094 | |
| 2095 | if not self.wait_up(timeout=resume_timeout): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2096 | raise error.TestFail( |
| 2097 | 'client failed to resume from sleep after %d seconds' % |
Tom Wai-Hong Tam | fced4f6 | 2014-04-17 10:56:30 +0800 | [diff] [blame] | 2098 | resume_timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2099 | else: |
| 2100 | new_boot_id = self.get_boot_id() |
| 2101 | if new_boot_id != old_boot_id: |
Tom Wai-Hong Tam | 0179268 | 2015-01-06 08:00:46 +0800 | [diff] [blame] | 2102 | logging.error('client rebooted (old boot %s, new boot %s)', |
| 2103 | old_boot_id, new_boot_id) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2104 | raise error.TestFail( |
Tom Wai-Hong Tam | 0179268 | 2015-01-06 08:00:46 +0800 | [diff] [blame] | 2105 | 'client rebooted, but sleep was expected') |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2106 | |
| 2107 | |
Tom Wai-Hong Tam | fe005c2 | 2014-12-03 09:25:44 +0800 | [diff] [blame] | 2108 | def test_wait_for_shutdown(self, shutdown_timeout=None): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2109 | """Wait for the client to shut down. |
| 2110 | |
| 2111 | The test for "has shut down" can't distinguish a system that |
| 2112 | is merely asleep; to confirm that the unit was down, it is |
| 2113 | necessary to force boot, and then call test_wait_for_boot(). |
| 2114 | |
| 2115 | This function is expected to be called from a test as part |
| 2116 | of a sequence like the following: |
| 2117 | |
| 2118 | ~~~~~~~~ |
| 2119 | boot_id = host.get_boot_id() |
| 2120 | # trigger shutdown on the host |
| 2121 | host.test_wait_for_shutdown() |
| 2122 | # trigger boot on the host |
| 2123 | host.test_wait_for_boot(boot_id) |
| 2124 | ~~~~~~~~ |
| 2125 | |
Tom Wai-Hong Tam | fe005c2 | 2014-12-03 09:25:44 +0800 | [diff] [blame] | 2126 | @param shutdown_timeout time limit in seconds to allow the host down. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2127 | @exception TestFail The host did not shut down within the |
| 2128 | allowed time. |
| 2129 | """ |
Tom Wai-Hong Tam | fe005c2 | 2014-12-03 09:25:44 +0800 | [diff] [blame] | 2130 | if shutdown_timeout is None: |
| 2131 | shutdown_timeout = self.SHUTDOWN_TIMEOUT |
| 2132 | |
| 2133 | if not self.ping_wait_down(timeout=shutdown_timeout): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2134 | raise error.TestFail( |
| 2135 | 'client failed to shut down after %d seconds' % |
Tom Wai-Hong Tam | fe005c2 | 2014-12-03 09:25:44 +0800 | [diff] [blame] | 2136 | shutdown_timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2137 | |
| 2138 | |
| 2139 | def test_wait_for_boot(self, old_boot_id=None): |
| 2140 | """Wait for the client to boot from cold power. |
| 2141 | |
| 2142 | The `old_boot_id` parameter should be the value from |
| 2143 | `get_boot_id()` obtained prior to shutting down. A |
| 2144 | `TestFail` exception is raised if the boot id does not |
| 2145 | change. The boot id test is omitted if `old_boot_id` is not |
| 2146 | specified. |
| 2147 | |
| 2148 | See @ref test_wait_for_shutdown for more on this function's |
| 2149 | usage. |
| 2150 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 2151 | @param old_boot_id A boot id value obtained before the |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2152 | shut down. |
| 2153 | |
| 2154 | @exception TestFail The host did not respond within the |
| 2155 | allowed time. |
| 2156 | @exception TestFail The host responded, but the boot id test |
| 2157 | indicated that there was no reboot. |
| 2158 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 2159 | if not self.wait_up(timeout=self.REBOOT_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2160 | raise error.TestFail( |
| 2161 | 'client failed to reboot after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 2162 | self.REBOOT_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2163 | elif old_boot_id: |
| 2164 | if self.get_boot_id() == old_boot_id: |
Tom Wai-Hong Tam | 0179268 | 2015-01-06 08:00:46 +0800 | [diff] [blame] | 2165 | logging.error('client not rebooted (boot %s)', |
| 2166 | old_boot_id) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 2167 | raise error.TestFail( |
Tom Wai-Hong Tam | 0179268 | 2015-01-06 08:00:46 +0800 | [diff] [blame] | 2168 | 'client is back up, but did not reboot') |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2169 | |
| 2170 | |
| 2171 | @staticmethod |
| 2172 | def check_for_rpm_support(hostname): |
| 2173 | """For a given hostname, return whether or not it is powered by an RPM. |
| 2174 | |
Simran Basi | 1df5511 | 2013-09-06 11:25:09 -0700 | [diff] [blame] | 2175 | @param hostname: hostname to check for rpm support. |
| 2176 | |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2177 | @return None if this host does not follows the defined naming format |
| 2178 | for RPM powered DUT's in the lab. If it does follow the format, |
| 2179 | it returns a regular expression MatchObject instead. |
| 2180 | """ |
Fang Deng | baff908 | 2015-01-06 13:46:15 -0800 | [diff] [blame] | 2181 | return re.match(CrosHost._RPM_HOSTNAME_REGEX, hostname) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2182 | |
| 2183 | |
| 2184 | def has_power(self): |
| 2185 | """For this host, return whether or not it is powered by an RPM. |
| 2186 | |
| 2187 | @return True if this host is in the CROS lab and follows the defined |
| 2188 | naming format. |
| 2189 | """ |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 2190 | return CrosHost.check_for_rpm_support(self.hostname) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2191 | |
| 2192 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 2193 | def _set_power(self, state, power_method): |
| 2194 | """Sets the power to the host via RPM, Servo or manual. |
| 2195 | |
| 2196 | @param state Specifies which power state to set to DUT |
| 2197 | @param power_method Specifies which method of power control to |
| 2198 | use. By default "RPM" will be used. Valid values |
| 2199 | are the strings "RPM", "manual", "servoj10". |
| 2200 | |
| 2201 | """ |
| 2202 | ACCEPTABLE_STATES = ['ON', 'OFF'] |
| 2203 | |
| 2204 | if state.upper() not in ACCEPTABLE_STATES: |
| 2205 | raise error.TestError('State must be one of: %s.' |
| 2206 | % (ACCEPTABLE_STATES,)) |
| 2207 | |
| 2208 | if power_method == self.POWER_CONTROL_SERVO: |
| 2209 | logging.info('Setting servo port J10 to %s', state) |
| 2210 | self.servo.set('prtctl3_pwren', state.lower()) |
| 2211 | time.sleep(self._USB_POWER_TIMEOUT) |
| 2212 | elif power_method == self.POWER_CONTROL_MANUAL: |
| 2213 | logging.info('You have %d seconds to set the AC power to %s.', |
| 2214 | self._POWER_CYCLE_TIMEOUT, state) |
| 2215 | time.sleep(self._POWER_CYCLE_TIMEOUT) |
| 2216 | else: |
| 2217 | if not self.has_power(): |
| 2218 | raise error.TestFail('DUT does not have RPM connected.') |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 2219 | afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) |
| 2220 | afe.set_host_attribute(self._RPM_OUTLET_CHANGED, True, |
| 2221 | hostname=self.hostname) |
Simran Basi | 1df5511 | 2013-09-06 11:25:09 -0700 | [diff] [blame] | 2222 | rpm_client.set_power(self.hostname, state.upper(), timeout_mins=5) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2223 | |
| 2224 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 2225 | def power_off(self, power_method=POWER_CONTROL_RPM): |
| 2226 | """Turn off power to this host via RPM, Servo or manual. |
| 2227 | |
| 2228 | @param power_method Specifies which method of power control to |
| 2229 | use. By default "RPM" will be used. Valid values |
| 2230 | are the strings "RPM", "manual", "servoj10". |
| 2231 | |
| 2232 | """ |
| 2233 | self._set_power('OFF', power_method) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 2234 | |
| 2235 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 2236 | def power_on(self, power_method=POWER_CONTROL_RPM): |
| 2237 | """Turn on power to this host via RPM, Servo or manual. |
| 2238 | |
| 2239 | @param power_method Specifies which method of power control to |
| 2240 | use. By default "RPM" will be used. Valid values |
| 2241 | are the strings "RPM", "manual", "servoj10". |
| 2242 | |
| 2243 | """ |
| 2244 | self._set_power('ON', power_method) |
| 2245 | |
| 2246 | |
| 2247 | def power_cycle(self, power_method=POWER_CONTROL_RPM): |
| 2248 | """Cycle power to this host by turning it OFF, then ON. |
| 2249 | |
| 2250 | @param power_method Specifies which method of power control to |
| 2251 | use. By default "RPM" will be used. Valid values |
| 2252 | are the strings "RPM", "manual", "servoj10". |
| 2253 | |
| 2254 | """ |
| 2255 | if power_method in (self.POWER_CONTROL_SERVO, |
| 2256 | self.POWER_CONTROL_MANUAL): |
| 2257 | self.power_off(power_method=power_method) |
| 2258 | time.sleep(self._POWER_CYCLE_TIMEOUT) |
| 2259 | self.power_on(power_method=power_method) |
| 2260 | else: |
| 2261 | rpm_client.set_power(self.hostname, 'CYCLE') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2262 | |
| 2263 | |
| 2264 | def get_platform(self): |
| 2265 | """Determine the correct platform label for this host. |
| 2266 | |
| 2267 | @returns a string representing this host's platform. |
| 2268 | """ |
| 2269 | crossystem = utils.Crossystem(self) |
| 2270 | crossystem.init() |
| 2271 | # Extract fwid value and use the leading part as the platform id. |
| 2272 | # fwid generally follow the format of {platform}.{firmware version} |
| 2273 | # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z |
| 2274 | platform = crossystem.fwid().split('.')[0].lower() |
| 2275 | # Newer platforms start with 'Google_' while the older ones do not. |
| 2276 | return platform.replace('google_', '') |
| 2277 | |
| 2278 | |
Hung-ying Tyan | b132803 | 2014-04-01 14:18:54 +0800 | [diff] [blame] | 2279 | def get_architecture(self): |
| 2280 | """Determine the correct architecture label for this host. |
| 2281 | |
| 2282 | @returns a string representing this host's architecture. |
| 2283 | """ |
| 2284 | crossystem = utils.Crossystem(self) |
| 2285 | crossystem.init() |
| 2286 | return crossystem.arch() |
| 2287 | |
| 2288 | |
Luis Lozano | 40b7d0d | 2014-01-17 15:12:06 -0800 | [diff] [blame] | 2289 | def get_chrome_version(self): |
| 2290 | """Gets the Chrome version number and milestone as strings. |
| 2291 | |
| 2292 | Invokes "chrome --version" to get the version number and milestone. |
| 2293 | |
| 2294 | @return A tuple (chrome_ver, milestone) where "chrome_ver" is the |
| 2295 | current Chrome version number as a string (in the form "W.X.Y.Z") |
| 2296 | and "milestone" is the first component of the version number |
| 2297 | (the "W" from "W.X.Y.Z"). If the version number cannot be parsed |
| 2298 | in the "W.X.Y.Z" format, the "chrome_ver" will be the full output |
| 2299 | of "chrome --version" and the milestone will be the empty string. |
| 2300 | |
| 2301 | """ |
MK Ryu | 35d661e | 2014-09-25 17:44:10 -0700 | [diff] [blame] | 2302 | version_string = self.run(client_constants.CHROME_VERSION_COMMAND).stdout |
Luis Lozano | 40b7d0d | 2014-01-17 15:12:06 -0800 | [diff] [blame] | 2303 | return utils.parse_chrome_version(version_string) |
| 2304 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 2305 | @label_decorator() |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2306 | def get_board(self): |
| 2307 | """Determine the correct board label for this host. |
| 2308 | |
| 2309 | @returns a string representing this host's board. |
| 2310 | """ |
| 2311 | release_info = utils.parse_cmd_output('cat /etc/lsb-release', |
| 2312 | run_method=self.run) |
| 2313 | board = release_info['CHROMEOS_RELEASE_BOARD'] |
| 2314 | # Devices in the lab generally have the correct board name but our own |
| 2315 | # development devices have {board_name}-signed-{key_type}. The board |
| 2316 | # name may also begin with 'x86-' which we need to keep. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 2317 | board_format_string = ds_constants.BOARD_PREFIX + '%s' |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2318 | if 'x86' not in board: |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 2319 | return board_format_string % board.split('-')[0] |
| 2320 | return board_format_string % '-'.join(board.split('-')[0:2]) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2321 | |
| 2322 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 2323 | @label_decorator('lightsensor') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2324 | def has_lightsensor(self): |
| 2325 | """Determine the correct board label for this host. |
| 2326 | |
| 2327 | @returns the string 'lightsensor' if this host has a lightsensor or |
| 2328 | None if it does not. |
| 2329 | """ |
| 2330 | search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % ( |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 2331 | self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES)) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2332 | try: |
| 2333 | # Run the search cmd following the symlinks. Stderr_tee is set to |
| 2334 | # None as there can be a symlink loop, but the command will still |
| 2335 | # execute correctly with a few messages printed to stderr. |
| 2336 | self.run(search_cmd, stdout_tee=None, stderr_tee=None) |
| 2337 | return 'lightsensor' |
| 2338 | except error.AutoservRunError: |
| 2339 | # egrep exited with a return code of 1 meaning none of the possible |
| 2340 | # lightsensor files existed. |
| 2341 | return None |
| 2342 | |
| 2343 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 2344 | @label_decorator('bluetooth') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 2345 | def has_bluetooth(self): |
| 2346 | """Determine the correct board label for this host. |
| 2347 | |
| 2348 | @returns the string 'bluetooth' if this host has bluetooth or |
| 2349 | None if it does not. |
| 2350 | """ |
| 2351 | try: |
| 2352 | self.run('test -d /sys/class/bluetooth/hci0') |
| 2353 | # test exited with a return code of 0. |
| 2354 | return 'bluetooth' |
| 2355 | except error.AutoservRunError: |
| 2356 | # test exited with a return code 1 meaning the directory did not |
| 2357 | # exist. |
| 2358 | return None |
| 2359 | |
| 2360 | |
Bill Richardson | 4f595f5 | 2014-02-13 16:20:26 -0800 | [diff] [blame] | 2361 | @label_decorator('ec') |
| 2362 | def get_ec(self): |
| 2363 | """ |
| 2364 | Determine the type of EC on this host. |
| 2365 | |
| 2366 | @returns a string representing this host's embedded controller type. |
| 2367 | At present, it only returns "ec:cros", for Chrome OS ECs. Other types |
| 2368 | of EC (or none) don't return any strings, since no tests depend on |
| 2369 | those. |
| 2370 | """ |
| 2371 | cmd = 'mosys ec info' |
| 2372 | # The output should look like these, so that the last field should |
| 2373 | # match our EC version scheme: |
| 2374 | # |
| 2375 | # stm | stm32f100 | snow_v1.3.139-375eb9f |
| 2376 | # ti | Unknown-10de | peppy_v1.5.114-5d52788 |
| 2377 | # |
| 2378 | # Non-Chrome OS ECs will look like these: |
| 2379 | # |
| 2380 | # ENE | KB932 | 00BE107A00 |
| 2381 | # ite | it8518 | 3.08 |
| 2382 | # |
| 2383 | # And some systems don't have ECs at all (Lumpy, for example). |
| 2384 | regexp = r'^.*\|\s*(\S+_v\d+\.\d+\.\d+-[0-9a-f]+)\s*$' |
| 2385 | |
| 2386 | ecinfo = self.run(command=cmd, ignore_status=True) |
| 2387 | if ecinfo.exit_status == 0: |
| 2388 | res = re.search(regexp, ecinfo.stdout) |
| 2389 | if res: |
| 2390 | logging.info("EC version is %s", res.groups()[0]) |
| 2391 | return 'ec:cros' |
| 2392 | logging.info("%s got: %s", cmd, ecinfo.stdout) |
| 2393 | # Has an EC, but it's not a Chrome OS EC |
| 2394 | return None |
| 2395 | logging.info("%s exited with status %d", cmd, ecinfo.exit_status) |
| 2396 | # No EC present |
| 2397 | return None |
| 2398 | |
| 2399 | |
Alec Berg | 31b932b | 2014-04-04 16:09:11 -0700 | [diff] [blame] | 2400 | @label_decorator('accels') |
| 2401 | def get_accels(self): |
| 2402 | """ |
| 2403 | Determine the type of accelerometers on this host. |
| 2404 | |
| 2405 | @returns a string representing this host's accelerometer type. |
| 2406 | At present, it only returns "accel:cros-ec", for accelerometers |
| 2407 | attached to a Chrome OS EC, or none, if no accelerometers. |
| 2408 | """ |
| 2409 | # Check to make sure we have ectool |
| 2410 | rv = self.run('which ectool', ignore_status=True) |
| 2411 | if rv.exit_status: |
| 2412 | logging.info("No ectool cmd found, assuming no EC accelerometers") |
| 2413 | return None |
| 2414 | |
| 2415 | # Check that the EC supports the motionsense command |
| 2416 | rv = self.run('ectool motionsense', ignore_status=True) |
| 2417 | if rv.exit_status: |
| 2418 | logging.info("EC does not support motionsense command " |
| 2419 | "assuming no EC accelerometers") |
| 2420 | return None |
| 2421 | |
| 2422 | # Check that EC motion sensors are active |
| 2423 | active = self.run('ectool motionsense active').stdout.split('\n') |
| 2424 | if active[0] == "0": |
| 2425 | logging.info("Motion sense inactive, assuming no EC accelerometers") |
| 2426 | return None |
| 2427 | |
| 2428 | logging.info("EC accelerometers found") |
| 2429 | return 'accel:cros-ec' |
| 2430 | |
| 2431 | |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 2432 | @label_decorator('chameleon') |
| 2433 | def has_chameleon(self): |
| 2434 | """Determine if a Chameleon connected to this host. |
| 2435 | |
Tom Wai-Hong Tam | badbb33 | 2014-10-10 02:59:41 +0800 | [diff] [blame] | 2436 | @returns a list containing two strings ('chameleon' and |
| 2437 | 'chameleon:' + label, e.g. 'chameleon:hdmi') if this host |
| 2438 | has a Chameleon or None if it has not. |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 2439 | """ |
| 2440 | if self._chameleon_host: |
Tom Wai-Hong Tam | badbb33 | 2014-10-10 02:59:41 +0800 | [diff] [blame] | 2441 | return ['chameleon', 'chameleon:' + self.chameleon.get_label()] |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 2442 | else: |
| 2443 | return None |
| 2444 | |
| 2445 | |
Cheng-Yi Chiang | f4104ff | 2014-12-23 19:39:01 +0800 | [diff] [blame] | 2446 | @label_decorator('audio_loopback_dongle') |
| 2447 | def has_loopback_dongle(self): |
| 2448 | """Determine if an audio loopback dongle is plugged to this host. |
| 2449 | |
| 2450 | @returns 'audio_loopback_dongle' when there is an audio loopback dongle |
| 2451 | plugged to this host. |
| 2452 | None when there is no audio loopback dongle |
| 2453 | plugged to this host. |
| 2454 | """ |
Cheng-Yi Chiang | 8de7811 | 2015-05-27 14:47:08 +0800 | [diff] [blame] | 2455 | nodes_info = self.run(command=cras_utils.get_cras_nodes_cmd(), |
| 2456 | ignore_status=True).stdout |
| 2457 | if (cras_utils.node_type_is_plugged('HEADPHONE', nodes_info) and |
| 2458 | cras_utils.node_type_is_plugged('MIC', nodes_info)): |
Cheng-Yi Chiang | f4104ff | 2014-12-23 19:39:01 +0800 | [diff] [blame] | 2459 | return 'audio_loopback_dongle' |
| 2460 | else: |
| 2461 | return None |
| 2462 | |
| 2463 | |
Derek Basehore | c71ff62 | 2014-07-07 15:18:40 -0700 | [diff] [blame] | 2464 | @label_decorator('power_supply') |
| 2465 | def get_power_supply(self): |
| 2466 | """ |
| 2467 | Determine what type of power supply the host has |
| 2468 | |
| 2469 | @returns a string representing this host's power supply. |
| 2470 | 'power:battery' when the device has a battery intended for |
| 2471 | extended use |
| 2472 | 'power:AC_primary' when the device has a battery not intended |
| 2473 | for extended use (for moving the machine, etc) |
| 2474 | 'power:AC_only' when the device has no battery at all. |
| 2475 | """ |
| 2476 | psu = self.run(command='mosys psu type', ignore_status=True) |
| 2477 | if psu.exit_status: |
| 2478 | # The psu command for mosys is not included for all platforms. The |
| 2479 | # assumption is that the device will have a battery if the command |
| 2480 | # is not found. |
| 2481 | return 'power:battery' |
| 2482 | |
| 2483 | psu_str = psu.stdout.strip() |
| 2484 | if psu_str == 'unknown': |
| 2485 | return None |
| 2486 | |
| 2487 | return 'power:%s' % psu_str |
| 2488 | |
| 2489 | |
Puthikorn Voravootivat | fa01124 | 2014-03-14 18:45:11 -0700 | [diff] [blame] | 2490 | @label_decorator('storage') |
| 2491 | def get_storage(self): |
| 2492 | """ |
| 2493 | Determine the type of boot device for this host. |
| 2494 | |
| 2495 | Determine if the internal device is SCSI or dw_mmc device. |
| 2496 | Then check that it is SSD or HDD or eMMC or something else. |
| 2497 | |
| 2498 | @returns a string representing this host's internal device type. |
| 2499 | 'storage:ssd' when internal device is solid state drive |
| 2500 | 'storage:hdd' when internal device is hard disk drive |
| 2501 | 'storage:mmc' when internal device is mmc drive |
| 2502 | None When internal device is something else or |
| 2503 | when we are unable to determine the type |
| 2504 | """ |
| 2505 | # The output should be /dev/mmcblk* for SD/eMMC or /dev/sd* for scsi |
| 2506 | rootdev_cmd = ' '.join(['. /usr/sbin/write_gpt.sh;', |
| 2507 | '. /usr/share/misc/chromeos-common.sh;', |
| 2508 | 'load_base_vars;', |
| 2509 | 'get_fixed_dst_drive']) |
Puthikorn Voravootivat | 03c5168 | 2014-04-24 13:52:12 -0700 | [diff] [blame] | 2510 | rootdev = self.run(command=rootdev_cmd, ignore_status=True) |
| 2511 | if rootdev.exit_status: |
| 2512 | logging.info("Fail to run %s", rootdev_cmd) |
| 2513 | return None |
Puthikorn Voravootivat | fa01124 | 2014-03-14 18:45:11 -0700 | [diff] [blame] | 2514 | rootdev_str = rootdev.stdout.strip() |
| 2515 | |
| 2516 | if not rootdev_str: |
| 2517 | return None |
| 2518 | |
| 2519 | rootdev_base = os.path.basename(rootdev_str) |
| 2520 | |
| 2521 | mmc_pattern = '/dev/mmcblk[0-9]' |
| 2522 | if re.match(mmc_pattern, rootdev_str): |
| 2523 | # Use type to determine if the internal device is eMMC or somthing |
| 2524 | # else. We can assume that MMC is always an internal device. |
| 2525 | type_cmd = 'cat /sys/block/%s/device/type' % rootdev_base |
Puthikorn Voravootivat | 03c5168 | 2014-04-24 13:52:12 -0700 | [diff] [blame] | 2526 | type = self.run(command=type_cmd, ignore_status=True) |
| 2527 | if type.exit_status: |
| 2528 | logging.info("Fail to run %s", type_cmd) |
| 2529 | return None |
Puthikorn Voravootivat | fa01124 | 2014-03-14 18:45:11 -0700 | [diff] [blame] | 2530 | type_str = type.stdout.strip() |
| 2531 | |
| 2532 | if type_str == 'MMC': |
| 2533 | return 'storage:mmc' |
| 2534 | |
| 2535 | scsi_pattern = '/dev/sd[a-z]+' |
| 2536 | if re.match(scsi_pattern, rootdev.stdout): |
| 2537 | # Read symlink for /sys/block/sd* to determine if the internal |
| 2538 | # device is connected via ata or usb. |
| 2539 | link_cmd = 'readlink /sys/block/%s' % rootdev_base |
Puthikorn Voravootivat | 03c5168 | 2014-04-24 13:52:12 -0700 | [diff] [blame] | 2540 | link = self.run(command=link_cmd, ignore_status=True) |
| 2541 | if link.exit_status: |
| 2542 | logging.info("Fail to run %s", link_cmd) |
| 2543 | return None |
Puthikorn Voravootivat | fa01124 | 2014-03-14 18:45:11 -0700 | [diff] [blame] | 2544 | link_str = link.stdout.strip() |
| 2545 | if 'usb' in link_str: |
| 2546 | return None |
| 2547 | |
| 2548 | # Read rotation to determine if the internal device is ssd or hdd. |
| 2549 | rotate_cmd = str('cat /sys/block/%s/queue/rotational' |
| 2550 | % rootdev_base) |
Puthikorn Voravootivat | 03c5168 | 2014-04-24 13:52:12 -0700 | [diff] [blame] | 2551 | rotate = self.run(command=rotate_cmd, ignore_status=True) |
| 2552 | if rotate.exit_status: |
| 2553 | logging.info("Fail to run %s", rotate_cmd) |
| 2554 | return None |
Puthikorn Voravootivat | fa01124 | 2014-03-14 18:45:11 -0700 | [diff] [blame] | 2555 | rotate_str = rotate.stdout.strip() |
| 2556 | |
| 2557 | rotate_dict = {'0':'storage:ssd', '1':'storage:hdd'} |
| 2558 | return rotate_dict.get(rotate_str) |
| 2559 | |
| 2560 | # All other internal device / error case will always fall here |
| 2561 | return None |
| 2562 | |
| 2563 | |
Dan Shi | 4e9a2aa | 2014-03-24 14:28:42 -0700 | [diff] [blame] | 2564 | @label_decorator('servo') |
| 2565 | def get_servo(self): |
| 2566 | """Determine if the host has a servo attached. |
| 2567 | |
| 2568 | If the host has a working servo attached, it should have a servo label. |
| 2569 | |
| 2570 | @return: string 'servo' if the host has servo attached. Otherwise, |
| 2571 | returns None. |
| 2572 | """ |
| 2573 | return 'servo' if self._servo_host else None |
| 2574 | |
| 2575 | |
Dan Shi | 5beba47 | 2014-05-28 22:46:07 -0700 | [diff] [blame] | 2576 | @label_decorator('video_labels') |
| 2577 | def get_video_labels(self): |
| 2578 | """Run /usr/local/bin/avtest_label_detect to get a list of video labels. |
| 2579 | |
| 2580 | Sample output of avtest_label_detect: |
| 2581 | Detected label: hw_video_acc_vp8 |
| 2582 | Detected label: webcam |
| 2583 | |
| 2584 | @return: A list of labels detected by tool avtest_label_detect. |
| 2585 | """ |
| 2586 | try: |
| 2587 | result = self.run('/usr/local/bin/avtest_label_detect').stdout |
| 2588 | return re.findall('^Detected label: (\w+)$', result, re.M) |
| 2589 | except error.AutoservRunError: |
| 2590 | # The tool is not installed. |
| 2591 | return [] |
| 2592 | |
| 2593 | |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 2594 | @label_decorator('video_glitch_detection') |
| 2595 | def is_video_glitch_detection_supported(self): |
| 2596 | """ Determine if a board under test is supported for video glitch |
| 2597 | detection tests. |
| 2598 | |
| 2599 | @return: 'video_glitch_detection' if board is supported, None otherwise. |
| 2600 | """ |
Mussa | 5b58905 | 2015-10-26 17:55:26 -0700 | [diff] [blame] | 2601 | board = self.get_board().replace(ds_constants.BOARD_PREFIX, '') |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 2602 | |
Mussa | 5b58905 | 2015-10-26 17:55:26 -0700 | [diff] [blame] | 2603 | if board in video_test_constants.SUPPORTED_BOARDS: |
| 2604 | return 'video_glitch_detection' |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 2605 | |
Mussa | 5b58905 | 2015-10-26 17:55:26 -0700 | [diff] [blame] | 2606 | return None |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 2607 | |
mussa | 584b446 | 2014-06-20 15:13:28 -0700 | [diff] [blame] | 2608 | |
Katherine Threlkeld | 7b97a9f | 2014-06-24 13:47:14 -0700 | [diff] [blame] | 2609 | @label_decorator('touch_labels') |
| 2610 | def get_touch(self): |
| 2611 | """ |
| 2612 | Determine whether board under test has a touchpad or touchscreen. |
| 2613 | |
| 2614 | @return: A list of some combination of 'touchscreen' and 'touchpad', |
| 2615 | depending on what is present on the device. |
Katherine Threlkeld | ab83d39 | 2015-06-18 16:45:57 -0700 | [diff] [blame] | 2616 | |
Katherine Threlkeld | 7b97a9f | 2014-06-24 13:47:14 -0700 | [diff] [blame] | 2617 | """ |
| 2618 | labels = [] |
Katherine Threlkeld | ab83d39 | 2015-06-18 16:45:57 -0700 | [diff] [blame] | 2619 | looking_for = ['touchpad', 'touchscreen'] |
| 2620 | player = input_playback.InputPlayback() |
| 2621 | input_events = self.run('ls /dev/input/event*').stdout.strip().split() |
| 2622 | filename = '/tmp/touch_labels' |
| 2623 | for event in input_events: |
| 2624 | self.run('evtest %s > %s' % (event, filename), timeout=1, |
| 2625 | ignore_timeout=True) |
| 2626 | properties = self.run('cat %s' % filename).stdout |
| 2627 | input_type = player._determine_input_type(properties) |
| 2628 | if input_type in looking_for: |
| 2629 | labels.append(input_type) |
| 2630 | looking_for.remove(input_type) |
| 2631 | if len(looking_for) == 0: |
| 2632 | break |
| 2633 | self.run('rm %s' % filename) |
| 2634 | |
Katherine Threlkeld | 7b97a9f | 2014-06-24 13:47:14 -0700 | [diff] [blame] | 2635 | return labels |
| 2636 | |
Hung-ying Tyan | a39b054 | 2015-06-30 10:36:42 +0800 | [diff] [blame] | 2637 | |
| 2638 | @label_decorator('internal_display') |
| 2639 | def has_internal_display(self): |
| 2640 | """Determine if the device under test is equipped with an internal |
| 2641 | display. |
| 2642 | |
| 2643 | @return: 'internal_display' if one is present; None otherwise. |
| 2644 | """ |
| 2645 | from autotest_lib.client.cros.graphics import graphics_utils |
| 2646 | from autotest_lib.client.common_lib import utils as common_utils |
| 2647 | |
| 2648 | def __system_output(cmd): |
| 2649 | return self.run(cmd).stdout |
| 2650 | |
| 2651 | def __read_file(remote_path): |
| 2652 | return self.run('cat %s' % remote_path).stdout |
| 2653 | |
| 2654 | # Hijack the necessary client functions so that we can take advantage |
| 2655 | # of the client lib here. |
| 2656 | # FIXME: find a less hacky way than this |
| 2657 | original_system_output = utils.system_output |
| 2658 | original_read_file = common_utils.read_file |
| 2659 | utils.system_output = __system_output |
| 2660 | common_utils.read_file = __read_file |
| 2661 | try: |
| 2662 | return ('internal_display' if graphics_utils.has_internal_display() |
| 2663 | else None) |
| 2664 | finally: |
| 2665 | utils.system_output = original_system_output |
| 2666 | common_utils.read_file = original_read_file |
| 2667 | |
| 2668 | |
Eric Caruso | ee673ac | 2015-08-05 17:03:04 -0700 | [diff] [blame] | 2669 | @label_decorator('lucidsleep') |
| 2670 | def has_lucid_sleep_support(self): |
| 2671 | """Determine if the device under test has support for lucid sleep. |
| 2672 | |
| 2673 | @return 'lucidsleep' if this board supports lucid sleep; None otherwise |
| 2674 | """ |
| 2675 | board = self.get_board().replace(ds_constants.BOARD_PREFIX, '') |
| 2676 | return 'lucidsleep' if board in LUCID_SLEEP_BOARDS else None |
| 2677 | |
| 2678 | |
Dan Shi | 85276d4 | 2014-04-08 22:11:45 -0700 | [diff] [blame] | 2679 | def is_boot_from_usb(self): |
| 2680 | """Check if DUT is boot from USB. |
| 2681 | |
| 2682 | @return: True if DUT is boot from usb. |
| 2683 | """ |
| 2684 | device = self.run('rootdev -s -d').stdout.strip() |
| 2685 | removable = int(self.run('cat /sys/block/%s/removable' % |
| 2686 | os.path.basename(device)).stdout.strip()) |
| 2687 | return removable == 1 |
Helen Zhang | 17dae2b | 2014-11-11 09:25:52 -0800 | [diff] [blame] | 2688 | |
| 2689 | |
| 2690 | def read_from_meminfo(self, key): |
Dan Shi | 49ca093 | 2014-11-14 11:22:27 -0800 | [diff] [blame] | 2691 | """Return the memory info from /proc/meminfo |
Helen Zhang | 17dae2b | 2014-11-11 09:25:52 -0800 | [diff] [blame] | 2692 | |
| 2693 | @param key: meminfo requested |
| 2694 | |
| 2695 | @return the memory value as a string |
| 2696 | |
| 2697 | """ |
Helen Zhang | 17dae2b | 2014-11-11 09:25:52 -0800 | [diff] [blame] | 2698 | meminfo = self.run('grep %s /proc/meminfo' % key).stdout.strip() |
| 2699 | logging.debug('%s', meminfo) |
| 2700 | return int(re.search(r'\d+', meminfo).group(0)) |
Rohit Makasana | 8a4923c | 2015-08-13 17:04:26 -0700 | [diff] [blame] | 2701 | |
| 2702 | |
| 2703 | def get_board_type(self): |
| 2704 | """ |
| 2705 | Get the DUT's device type from /etc/lsb-release. |
Danny Chan | 471a8d1 | 2015-08-18 14:57:41 -0700 | [diff] [blame] | 2706 | DEVICETYPE can be one of CHROMEBOX, CHROMEBASE, CHROMEBOOK or more. |
| 2707 | |
| 2708 | @return value of DEVICETYPE param from lsb-release. |
Rohit Makasana | 8a4923c | 2015-08-13 17:04:26 -0700 | [diff] [blame] | 2709 | """ |
Danny Chan | 471a8d1 | 2015-08-18 14:57:41 -0700 | [diff] [blame] | 2710 | device_type = self.run('grep DEVICETYPE /etc/lsb-release', |
| 2711 | ignore_status=True).stdout |
| 2712 | if device_type: |
Kalin Stoyanov | 524310b | 2015-08-21 16:24:04 -0700 | [diff] [blame] | 2713 | return device_type.split('=')[-1].strip() |
Danny Chan | 471a8d1 | 2015-08-18 14:57:41 -0700 | [diff] [blame] | 2714 | return '' |
Gilad Arnold | a76bef0 | 2015-09-29 13:55:15 -0700 | [diff] [blame] | 2715 | |
| 2716 | |
| 2717 | def get_os_type(self): |
| 2718 | return 'cros' |
Simran Basi | a5522a3 | 2015-10-06 11:01:24 -0700 | [diff] [blame] | 2719 | |
| 2720 | |
| 2721 | def enable_adb_testing(self): |
| 2722 | """Mark this host as an adb tester.""" |
Dan Shi | a287217 | 2015-10-31 01:16:51 -0700 | [diff] [blame] | 2723 | self.run('touch %s' % constants.ANDROID_TESTER_FILEFLAG) |