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