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 |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 6 | import logging |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 7 | import os |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 8 | import re |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 9 | import socket |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 10 | import subprocess |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 11 | import time |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 12 | import xmlrpclib |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 13 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 14 | from autotest_lib.client.bin import utils |
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 |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib.cros import autoupdater |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 18 | from autotest_lib.client.common_lib.cros import dev_server |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 19 | from autotest_lib.client.common_lib.cros import retry |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 20 | from autotest_lib.client.cros import constants |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 21 | from autotest_lib.server import autoserv_parser |
Chris Sosa | f4d43ff | 2012-10-30 11:21:05 -0700 | [diff] [blame] | 22 | from autotest_lib.server import autotest |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 23 | from autotest_lib.server import site_host_attributes |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 24 | from autotest_lib.server.cros.dynamic_suite import constants as ds_constants |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 25 | from autotest_lib.server.cros.dynamic_suite import tools, frontend_wrappers |
J. Richard Barnette | 7548757 | 2013-03-08 12:47:50 -0800 | [diff] [blame] | 26 | from autotest_lib.server.cros.servo import servo |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 27 | from autotest_lib.server.hosts import remote |
Simran Basi | dcff425 | 2012-11-20 16:13:20 -0800 | [diff] [blame] | 28 | from autotest_lib.site_utils.rpm_control_system import rpm_client |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 29 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 30 | # Importing frontend.afe.models requires a full Autotest |
| 31 | # installation (with the Django modules), not just the source |
| 32 | # repository. Most developers won't have the full installation, so |
| 33 | # the imports below will fail for them. |
| 34 | # |
| 35 | # The fix is to catch import exceptions, and set `models` to `None` |
| 36 | # on failure. This has the side effect that |
| 37 | # SiteHost._get_board_from_afe() will fail: That will manifest as |
| 38 | # failures during Repair jobs leaving the DUT as "Repair Failed". |
| 39 | # In practice, you can't test Repair jobs without a full |
| 40 | # installation, so that kind of failure isn't expected. |
| 41 | try: |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 42 | # pylint: disable=W0611 |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 43 | from autotest_lib.frontend import setup_django_environment |
| 44 | from autotest_lib.frontend.afe import models |
| 45 | except: |
| 46 | models = None |
| 47 | |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 48 | |
J. Richard Barnette | be5ebcc | 2013-02-11 16:03:15 -0800 | [diff] [blame] | 49 | def _make_servo_hostname(hostname): |
| 50 | host_parts = hostname.split('.') |
| 51 | host_parts[0] = host_parts[0] + '-servo' |
| 52 | return '.'.join(host_parts) |
| 53 | |
| 54 | |
| 55 | def _get_lab_servo(target_hostname): |
| 56 | """Instantiate a Servo for |target_hostname| in the lab. |
| 57 | |
| 58 | Assuming that |target_hostname| is a device in the CrOS test |
| 59 | lab, create and return a Servo object pointed at the servo |
| 60 | attached to that DUT. The servo in the test lab is assumed |
| 61 | to already have servod up and running on it. |
| 62 | |
| 63 | @param target_hostname: device whose servo we want to target. |
| 64 | @return an appropriately configured Servo instance. |
| 65 | """ |
| 66 | servo_host = _make_servo_hostname(target_hostname) |
| 67 | if utils.host_is_in_lab_zone(servo_host): |
| 68 | try: |
J. Richard Barnette | d5f807a | 2013-02-11 16:51:00 -0800 | [diff] [blame] | 69 | return servo.Servo(servo_host=servo_host) |
J. Richard Barnette | be5ebcc | 2013-02-11 16:03:15 -0800 | [diff] [blame] | 70 | except: # pylint: disable=W0702 |
| 71 | # TODO(jrbarnette): Long-term, if we can't get to |
| 72 | # a servo in the lab, we want to fail, so we should |
| 73 | # pass any exceptions along. Short-term, we're not |
| 74 | # ready to rely on servo, so we ignore failures. |
| 75 | pass |
| 76 | return None |
| 77 | |
| 78 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 79 | def make_ssh_command(user='root', port=22, opts='', hosts_file=None, |
| 80 | connect_timeout=None, alive_interval=None): |
| 81 | """Override default make_ssh_command to use options tuned for Chrome OS. |
| 82 | |
| 83 | Tuning changes: |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 84 | - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH connection |
| 85 | failure. Consistency with remote_access.sh. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 86 | |
Dale Curtis | aa5eedb | 2011-08-23 16:18:52 -0700 | [diff] [blame] | 87 | - ServerAliveInterval=180; which causes SSH to ping connection every |
| 88 | 180 seconds. In conjunction with ServerAliveCountMax ensures that if the |
| 89 | connection dies, Autotest will bail out quickly. Originally tried 60 secs, |
| 90 | but saw frequent job ABORTS where the test completed successfully. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 91 | |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 92 | - ServerAliveCountMax=3; consistency with remote_access.sh. |
| 93 | |
| 94 | - ConnectAttempts=4; reduce flakiness in connection errors; consistency |
| 95 | with remote_access.sh. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 96 | |
| 97 | - UserKnownHostsFile=/dev/null; we don't care about the keys. Host keys |
| 98 | change with every new installation, don't waste memory/space saving them. |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 99 | |
| 100 | - SSH protocol forced to 2; needed for ServerAliveInterval. |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 101 | |
| 102 | @param user User name to use for the ssh connection. |
| 103 | @param port Port on the target host to use for ssh connection. |
| 104 | @param opts Additional options to the ssh command. |
| 105 | @param hosts_file Ignored. |
| 106 | @param connect_timeout Ignored. |
| 107 | @param alive_interval Ignored. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 108 | """ |
| 109 | base_command = ('/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no' |
| 110 | ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes' |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 111 | ' -o ConnectTimeout=30 -o ServerAliveInterval=180' |
| 112 | ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4' |
| 113 | ' -o Protocol=2 -l %s -p %d') |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 114 | return base_command % (opts, user, port) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 115 | |
| 116 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 117 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 118 | def add_label_detector(label_function_list, label_list=None, label=None): |
| 119 | """Decorator used to group functions together into the provided list. |
| 120 | @param label_function_list: List of label detecting functions to add |
| 121 | decorated function to. |
| 122 | @param label_list: List of detectable labels to add detectable labels to. |
| 123 | (Default: None) |
| 124 | @param label: Label string that is detectable by this detection function |
| 125 | (Default: None) |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 126 | """ |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 127 | def add_func(func): |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 128 | """ |
| 129 | @param func: The function to be added as a detector. |
| 130 | """ |
| 131 | label_function_list.append(func) |
| 132 | if label and label_list is not None: |
| 133 | label_list.append(label) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 134 | return func |
| 135 | return add_func |
| 136 | |
| 137 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 138 | class SiteHost(remote.RemoteHost): |
| 139 | """Chromium OS specific subclass of Host.""" |
| 140 | |
| 141 | _parser = autoserv_parser.autoserv_parser |
| 142 | |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 143 | # Time to wait for new kernel to be marked successful after |
| 144 | # auto update. |
Chris Masone | 163cead | 2012-05-16 11:49:48 -0700 | [diff] [blame] | 145 | _KERNEL_UPDATE_TIMEOUT = 120 |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 146 | |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 147 | # Timeout values (in seconds) associated with various Chrome OS |
| 148 | # state changes. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 149 | # |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 150 | # In general, a good rule of thumb is that the timeout can be up |
| 151 | # to twice the typical measured value on the slowest platform. |
| 152 | # The times here have not necessarily been empirically tested to |
| 153 | # meet this criterion. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 154 | # |
| 155 | # SLEEP_TIMEOUT: Time to allow for suspend to memory. |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 156 | # RESUME_TIMEOUT: Time to allow for resume after suspend, plus |
| 157 | # time to restart the netwowrk. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 158 | # BOOT_TIMEOUT: Time to allow for boot from power off. Among |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 159 | # other things, this must account for the 30 second dev-mode |
J. Richard Barnette | d4649c6 | 2013-03-06 17:42:27 -0800 | [diff] [blame] | 160 | # screen delay and time to start the network. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 161 | # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device, |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 162 | # 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] | 163 | # network. |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 164 | # SHUTDOWN_TIMEOUT: Time to allow for shut down. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 165 | # REBOOT_TIMEOUT: Combination of shutdown and reboot times. |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 166 | # _INSTALL_TIMEOUT: Time to allow for chromeos-install. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 167 | |
| 168 | SLEEP_TIMEOUT = 2 |
J. Richard Barnette | d4649c6 | 2013-03-06 17:42:27 -0800 | [diff] [blame] | 169 | RESUME_TIMEOUT = 10 |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 170 | BOOT_TIMEOUT = 45 |
| 171 | USB_BOOT_TIMEOUT = 150 |
| 172 | SHUTDOWN_TIMEOUT = 5 |
| 173 | REBOOT_TIMEOUT = SHUTDOWN_TIMEOUT + BOOT_TIMEOUT |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 174 | _INSTALL_TIMEOUT = 240 |
| 175 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 176 | # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF. |
| 177 | # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle. |
| 178 | _USB_POWER_TIMEOUT = 5 |
| 179 | _POWER_CYCLE_TIMEOUT = 10 |
| 180 | |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 181 | _DEFAULT_SERVO_URL_FORMAT = ('/static/servo-images/' |
| 182 | '%(board)s_test_image.bin') |
| 183 | |
J. Richard Barnette | c14897e | 2013-03-06 15:56:55 -0800 | [diff] [blame] | 184 | # TODO(jrbarnette): Servo repair is restricted to specific |
| 185 | # boards, because the existing servo client code doesn't account |
| 186 | # for board-specific differences in handling for 'cold_reset'. |
| 187 | # http://crosbug.com/36973 |
| 188 | _SERVO_REPAIR_WHITELIST = ('x86-alex', 'lumpy') |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 189 | |
| 190 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 191 | _RPM_RECOVERY_BOARDS = global_config.global_config.get_config_value('CROS', |
| 192 | 'rpm_recovery_boards', type=str).split(',') |
| 193 | |
| 194 | _MAX_POWER_CYCLE_ATTEMPTS = 6 |
| 195 | _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine' |
| 196 | _RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-' |
| 197 | 'host[0-9]+') |
| 198 | _LIGHTSENSOR_FILES = ['in_illuminance0_input', |
| 199 | 'in_illuminance0_raw', |
| 200 | 'illuminance0_input'] |
| 201 | _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices' |
| 202 | _LABEL_FUNCTIONS = [] |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 203 | _DETECTABLE_LABELS = [] |
| 204 | label_decorator = functools.partial(add_label_detector, _LABEL_FUNCTIONS, |
| 205 | _DETECTABLE_LABELS) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 206 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 207 | # Constants used in ping_wait_up() and ping_wait_down(). |
| 208 | # |
| 209 | # _PING_WAIT_COUNT is the approximate number of polling |
| 210 | # cycles to use when waiting for a host state change. |
| 211 | # |
| 212 | # _PING_STATUS_DOWN and _PING_STATUS_UP are names used |
| 213 | # for arguments to the internal _ping_wait_for_status() |
| 214 | # method. |
| 215 | _PING_WAIT_COUNT = 40 |
| 216 | _PING_STATUS_DOWN = False |
| 217 | _PING_STATUS_UP = True |
| 218 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 219 | # Allowed values for the power_method argument. |
| 220 | |
| 221 | # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods. |
| 222 | # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods. |
| 223 | # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods. |
| 224 | POWER_CONTROL_RPM = 'RPM' |
| 225 | POWER_CONTROL_SERVO = 'servoj10' |
| 226 | POWER_CONTROL_MANUAL = 'manual' |
| 227 | |
| 228 | POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM, |
| 229 | POWER_CONTROL_SERVO, |
| 230 | POWER_CONTROL_MANUAL) |
Richard Barnette | 0c73ffc | 2012-11-19 15:21:18 -0800 | [diff] [blame] | 231 | |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 232 | _RPM_OUTLET_CHANGED = 'outlet_changed' |
| 233 | |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 234 | @staticmethod |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 235 | def get_servo_arguments(args_dict): |
| 236 | """Extract servo options from `args_dict` and return the result. |
| 237 | |
| 238 | Take the provided dictionary of argument options and return |
| 239 | a subset that represent standard arguments needed to |
| 240 | construct a servo object for a host. The intent is to |
| 241 | provide standard argument processing from run_remote_tests |
| 242 | for tests that require a servo to operate. |
| 243 | |
| 244 | Recommended usage: |
| 245 | ~~~~~~~~ |
| 246 | args_dict = utils.args_to_dict(args) |
| 247 | servo_args = hosts.SiteHost.get_servo_arguments(args_dict) |
| 248 | host = hosts.create_host(machine, servo_args=servo_args) |
| 249 | ~~~~~~~~ |
| 250 | |
| 251 | @param args_dict Dictionary from which to extract the servo |
| 252 | arguments. |
| 253 | """ |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 254 | servo_args = {} |
| 255 | for arg in ('servo_host', 'servo_port'): |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 256 | if arg in args_dict: |
| 257 | servo_args[arg] = args_dict[arg] |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 258 | return servo_args |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 259 | |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 260 | |
| 261 | def _initialize(self, hostname, servo_args=None, *args, **dargs): |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 262 | """Initialize superclasses, and |self.servo|. |
| 263 | |
| 264 | For creating the host servo object, there are three |
| 265 | possibilities: First, if the host is a lab system known to |
| 266 | have a servo board, we connect to that servo unconditionally. |
| 267 | Second, if we're called from a control file that requires |
J. Richard Barnette | 55fb806 | 2012-05-23 10:29:31 -0700 | [diff] [blame] | 268 | servo features for testing, it will pass settings for |
| 269 | `servo_host`, `servo_port`, or both. If neither of these |
| 270 | cases apply, `self.servo` will be `None`. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 271 | |
| 272 | """ |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 273 | super(SiteHost, self)._initialize(hostname=hostname, |
| 274 | *args, **dargs) |
J. Richard Barnette | f085985 | 2012-08-20 14:55:50 -0700 | [diff] [blame] | 275 | # self.env is a dictionary of environment variable settings |
| 276 | # to be exported for commands run on the host. |
| 277 | # LIBC_FATAL_STDERR_ can be useful for diagnosing certain |
| 278 | # errors that might happen. |
| 279 | self.env['LIBC_FATAL_STDERR_'] = '1' |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 280 | self._xmlrpc_proxy_map = {} |
J. Richard Barnette | be5ebcc | 2013-02-11 16:03:15 -0800 | [diff] [blame] | 281 | self.servo = _get_lab_servo(hostname) |
J. Richard Barnette | ad7da48 | 2012-10-30 16:46:52 -0700 | [diff] [blame] | 282 | if not self.servo and servo_args is not None: |
J. Richard Barnette | 964fba0 | 2012-10-24 17:34:29 -0700 | [diff] [blame] | 283 | self.servo = servo.Servo(**servo_args) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 284 | |
| 285 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 286 | def get_repair_image_name(self): |
| 287 | """Generate a image_name from variables in the global config. |
| 288 | |
| 289 | @returns a str of $board-version/$BUILD. |
| 290 | |
| 291 | """ |
| 292 | stable_version = global_config.global_config.get_config_value( |
| 293 | 'CROS', 'stable_cros_version') |
| 294 | build_pattern = global_config.global_config.get_config_value( |
| 295 | 'CROS', 'stable_build_pattern') |
| 296 | board = self._get_board_from_afe() |
| 297 | if board is None: |
| 298 | raise error.AutoservError('DUT has no board attribute, ' |
| 299 | 'cannot be repaired.') |
| 300 | return build_pattern % (board, stable_version) |
| 301 | |
| 302 | |
| 303 | def clear_cros_version_labels_and_job_repo_url(self): |
| 304 | """Clear cros_version labels and host attribute job_repo_url.""" |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 305 | try: |
| 306 | host_model = models.Host.objects.get(hostname=self.hostname) |
| 307 | except models.Host.DoesNotExist: |
| 308 | return |
| 309 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 310 | for label in host_model.labels.iterator(): |
| 311 | if not label.name.startswith(ds_constants.VERSION_PREFIX): |
| 312 | continue |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 313 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 314 | label.host_set.remove(host_model) |
| 315 | |
| 316 | host_model.set_or_delete_attribute('job_repo_url', None) |
| 317 | |
| 318 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 319 | def add_cros_version_labels_and_job_repo_url(self, image_name): |
| 320 | """Add cros_version labels and host attribute job_repo_url. |
| 321 | |
| 322 | @param image_name: The name of the image e.g. |
| 323 | lumpy-release/R27-3837.0.0 |
| 324 | """ |
| 325 | try: |
| 326 | host_model = models.Host.objects.get(hostname=self.hostname) |
| 327 | except models.Host.DoesNotExist: |
| 328 | return |
| 329 | cros_label = '%s%s' % (ds_constants.VERSION_PREFIX, image_name) |
| 330 | devserver_url = dev_server.ImageServer.resolve(image_name).url() |
| 331 | try: |
| 332 | label_model = models.Label.objects.get(name=cros_label) |
| 333 | except models.Label.DoesNotExist: |
| 334 | label_model = models.Label.objects.create(name=cros_label) |
| 335 | host_model.labels.add(label_model) |
| 336 | repo_url = tools.get_package_url(devserver_url, image_name) |
| 337 | host_model.set_or_delete_attribute('job_repo_url', repo_url) |
| 338 | |
| 339 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 340 | def _try_stateful_update(self, update_url, force_update, updater): |
| 341 | """Try to use stateful update to initialize DUT. |
| 342 | |
| 343 | When DUT is already running the same version that machine_install |
| 344 | tries to install, stateful update is a much faster way to clean up |
| 345 | the DUT for testing, compared to a full reimage. It is implemeted |
| 346 | by calling autoupdater.run_update, but skipping updating root, as |
| 347 | updating the kernel is time consuming and not necessary. |
| 348 | |
| 349 | @param update_url: url of the image. |
| 350 | @param force_update: Set to True to update the image even if the DUT |
| 351 | is running the same version. |
| 352 | @param updater: ChromiumOSUpdater instance used to update the DUT. |
| 353 | @returns: True if the DUT was updated with stateful update. |
| 354 | |
| 355 | """ |
Dan Shi | 7b7379d | 2013-03-19 16:26:33 -0700 | [diff] [blame] | 356 | # Stateful update is disabled until lsb-release has rc build info. |
| 357 | logging.info('Stateful update only is disabled.') |
| 358 | return False |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 359 | if not updater.check_version(): |
| 360 | return False |
| 361 | if not force_update: |
| 362 | logging.info('Canceling stateful update because the new and ' |
| 363 | 'old versions are the same.') |
| 364 | return False |
| 365 | # Following folders should be rebuilt after stateful update. |
| 366 | # A test file is used to confirm each folder gets rebuilt after |
| 367 | # the stateful update. |
| 368 | folders_to_check = ['/var', '/home', '/mnt/stateful_partition'] |
| 369 | test_file = '.test_file_to_be_deleted' |
| 370 | for folder in folders_to_check: |
| 371 | touch_path = os.path.join(folder, test_file) |
| 372 | self.run('touch %s' % touch_path) |
| 373 | |
| 374 | if not updater.run_update(force_update=True, update_root=False): |
| 375 | return False |
| 376 | |
| 377 | # Reboot to complete stateful update. |
| 378 | self.reboot(timeout=60, wait=True) |
| 379 | check_file_cmd = 'test -f %s; echo $?' |
| 380 | for folder in folders_to_check: |
| 381 | test_file_path = os.path.join(folder, test_file) |
| 382 | result = self.run(check_file_cmd % test_file_path, |
| 383 | ignore_status=True) |
| 384 | if result.exit_status == 1: |
| 385 | return False |
| 386 | return True |
| 387 | |
| 388 | |
| 389 | def _post_update_processing(self, updater, inactive_kernel=None): |
| 390 | """After the DUT is updated, confirm machine_install succeeded. |
| 391 | |
| 392 | @param updater: ChromiumOSUpdater instance used to update the DUT. |
| 393 | @param inactive_kernel: kernel state of inactive kernel before reboot. |
| 394 | |
| 395 | """ |
| 396 | |
| 397 | # Touch the lab machine file to leave a marker that distinguishes |
| 398 | # this image from other test images. |
| 399 | self.run('touch %s' % self._LAB_MACHINE_FILE) |
| 400 | |
| 401 | # Kick off the autoreboot script as the _LAB_MACHINE_FILE was |
| 402 | # missing on the first boot. |
| 403 | self.run('start autoreboot') |
| 404 | |
| 405 | # Following the reboot, verify the correct version. |
| 406 | if not updater.check_version(): |
| 407 | # Print out crossystem to make it easier to debug the rollback. |
| 408 | logging.debug('Dumping partition table.') |
Dan Shi | 346725f | 2013-03-20 15:22:38 -0700 | [diff] [blame] | 409 | self.run('cgpt show $(rootdev -s -d)') |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 410 | logging.debug('Dumping crossystem for firmware debugging.') |
Dan Shi | 346725f | 2013-03-20 15:22:38 -0700 | [diff] [blame] | 411 | self.run('crossystem --all') |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 412 | logging.error('Expected Chromium OS version: %s. ' |
| 413 | 'Found Chromium OS %s', |
Dan Shi | 346725f | 2013-03-20 15:22:38 -0700 | [diff] [blame] | 414 | updater.update_version, updater.get_build_id()) |
| 415 | raise autoupdater.ChromiumOSError('Updater failed on host %s' % |
| 416 | self.hostname) |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 417 | |
| 418 | # Figure out newly active kernel. |
| 419 | new_active_kernel, _ = updater.get_kernel_state() |
| 420 | |
| 421 | # Ensure that previously inactive kernel is now the active kernel. |
| 422 | if inactive_kernel and new_active_kernel != inactive_kernel: |
| 423 | raise autoupdater.ChromiumOSError( |
| 424 | 'Update failed. New kernel partition is not active after' |
| 425 | ' boot.') |
| 426 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 427 | try: |
| 428 | host_attributes = site_host_attributes.HostAttributes(self.hostname) |
| 429 | except models.Host.DoesNotExist: |
| 430 | host_attributes = None |
| 431 | if host_attributes and host_attributes.has_chromeos_firmware: |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 432 | # Wait until tries == 0 and success, or until timeout. |
| 433 | utils.poll_for_condition( |
| 434 | lambda: (updater.get_kernel_tries(new_active_kernel) == 0 |
| 435 | and updater.get_kernel_success(new_active_kernel)), |
| 436 | exception=autoupdater.ChromiumOSError( |
| 437 | 'Update failed. Timed out waiting for system to mark' |
| 438 | ' new kernel as successful.'), |
| 439 | timeout=self._KERNEL_UPDATE_TIMEOUT, sleep_interval=5) |
| 440 | |
| 441 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 442 | def _stage_build_and_return_update_url(self, image_name): |
| 443 | """Stage a build on a devserver and return the update_url. |
| 444 | |
| 445 | @param image_name: a name like lumpy-release/R27-3837.0.0 |
| 446 | @returns an update URL like: |
| 447 | http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0 |
| 448 | """ |
| 449 | logging.info('Staging requested build: %s', image_name) |
| 450 | devserver = dev_server.ImageServer.resolve(image_name) |
| 451 | devserver.trigger_download(image_name, synchronous=False) |
| 452 | return tools.image_url_pattern() % (devserver.url(), image_name) |
| 453 | |
| 454 | |
Chris Sosa | a3ac215 | 2012-05-23 22:23:13 -0700 | [diff] [blame] | 455 | def machine_install(self, update_url=None, force_update=False, |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 456 | local_devserver=False, repair=False): |
| 457 | """Install the DUT. |
| 458 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 459 | Use stateful update if the DUT is already running the same build. |
| 460 | Stateful update does not update kernel and tends to run much faster |
| 461 | than a full reimage. If the DUT is running a different build, or it |
| 462 | failed to do a stateful update, full update, including kernel update, |
| 463 | will be applied to the DUT. |
| 464 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 465 | Once a host enters machine_install its cros_version label will be |
| 466 | removed as well as its host attribute job_repo_url (used for |
| 467 | package install). |
| 468 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 469 | @param update_url: The url to use for the update |
| 470 | pattern: http://$devserver:###/update/$build |
| 471 | If update_url is None and repair is True we will install the |
| 472 | stable image listed in global_config under |
| 473 | CROS.stable_cros_version. |
| 474 | @param force_update: Force an update even if the version installed |
| 475 | is the same. Default:False |
| 476 | @param local_devserver: Used by run_remote_test to allow people to |
| 477 | use their local devserver. Default: False |
| 478 | @param repair: Whether or not we are in repair mode. This adds special |
| 479 | cases for repairing a machine like starting update_engine. |
| 480 | Setting repair to True sets force_update to True as well. |
| 481 | default: False |
| 482 | @raises autoupdater.ChromiumOSError |
| 483 | |
| 484 | """ |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 485 | if not update_url and self._parser.options.image: |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 486 | requested_build = self._parser.options.image |
| 487 | if requested_build.startswith('http://'): |
| 488 | update_url = requested_build |
| 489 | else: |
| 490 | # Try to stage any build that does not start with http:// on |
| 491 | # the devservers defined in global_config.ini. |
| 492 | update_url = self._stage_build_and_return_update_url( |
| 493 | requested_build) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 494 | elif not update_url and not repair: |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 495 | raise autoupdater.ChromiumOSError( |
| 496 | 'Update failed. No update URL provided.') |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 497 | elif not update_url and repair: |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 498 | update_url = self._stage_build_and_return_update_url( |
| 499 | self.get_repair_image_name()) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 500 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 501 | if repair: |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 502 | # In case the system is in a bad state, we always reboot the machine |
| 503 | # before machine_install. |
| 504 | self.reboot(timeout=60, wait=True) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 505 | self.run('stop update-engine; start update-engine') |
| 506 | force_update = True |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 507 | |
Chris Sosa | a3ac215 | 2012-05-23 22:23:13 -0700 | [diff] [blame] | 508 | updater = autoupdater.ChromiumOSUpdater(update_url, host=self, |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 509 | local_devserver=local_devserver) |
| 510 | updated = False |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 511 | # Remove cros-version and job_repo_url host attribute from host. |
| 512 | self.clear_cros_version_labels_and_job_repo_url() |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 513 | # If the DUT is already running the same build, try stateful update |
| 514 | # first. Stateful update does not update kernel and tends to run much |
| 515 | # faster than a full reimage. |
| 516 | try: |
| 517 | updated = self._try_stateful_update(update_url, force_update, |
| 518 | updater) |
| 519 | if updated: |
| 520 | logging.info('DUT is updated with stateful update.') |
| 521 | except Exception as e: |
| 522 | logging.exception(e) |
| 523 | logging.warn('Failed to stateful update DUT, force to update.') |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 524 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 525 | inactive_kernel = None |
| 526 | # Do a full update if stateful update is not applicable or failed. |
| 527 | if not updated: |
| 528 | # In case the system is in a bad state, we always reboot the |
| 529 | # machine before machine_install. |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 530 | self.reboot(timeout=60, wait=True) |
Chris Sosa | b7612bc | 2013-03-21 10:32:37 -0700 | [diff] [blame] | 531 | |
| 532 | # TODO(sosa): Remove temporary hack to get rid of bricked machines |
| 533 | # that can't update due to a corrupted policy. |
| 534 | self.run('rm -rf /var/lib/whitelist') |
| 535 | self.run('touch /var/lib/whitelist') |
| 536 | self.run('chmod -w /var/lib/whitelist') |
Scott Zawalski | b550d5a | 2013-03-22 09:23:59 -0400 | [diff] [blame] | 537 | self.run('stop update-engine; start update-engine') |
Chris Sosa | b7612bc | 2013-03-21 10:32:37 -0700 | [diff] [blame] | 538 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 539 | if updater.run_update(force_update): |
| 540 | updated = True |
| 541 | # Figure out active and inactive kernel. |
| 542 | active_kernel, inactive_kernel = updater.get_kernel_state() |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 543 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 544 | # Ensure inactive kernel has higher priority than active. |
| 545 | if (updater.get_kernel_priority(inactive_kernel) |
| 546 | < updater.get_kernel_priority(active_kernel)): |
| 547 | raise autoupdater.ChromiumOSError( |
| 548 | 'Update failed. The priority of the inactive kernel' |
| 549 | ' partition is less than that of the active kernel' |
| 550 | ' partition.') |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 551 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 552 | update_engine_log = '/var/log/update_engine.log' |
| 553 | logging.info('Dumping %s', update_engine_log) |
| 554 | self.run('cat %s' % update_engine_log) |
| 555 | # Updater has returned successfully; reboot the host. |
| 556 | self.reboot(timeout=60, wait=True) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 557 | |
Dan Shi | 0f466e8 | 2013-02-22 15:44:58 -0800 | [diff] [blame] | 558 | if updated: |
| 559 | self._post_update_processing(updater, inactive_kernel) |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 560 | image_name = autoupdater.url_to_image_name(update_url) |
| 561 | self.add_cros_version_labels_and_job_repo_url(image_name) |
Simran Basi | 13fa1ba | 2013-03-04 10:56:47 -0800 | [diff] [blame] | 562 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 563 | # Clean up any old autotest directories which may be lying around. |
| 564 | for path in global_config.global_config.get_config_value( |
| 565 | 'AUTOSERV', 'client_autodir_paths', type=list): |
| 566 | self.run('rm -rf ' + path) |
| 567 | |
| 568 | |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 569 | def _get_label_from_afe(self, label_prefix): |
| 570 | """Retrieve a host's specific label from the AFE. |
| 571 | |
| 572 | Looks for a host label that has the form <label_prefix>:<value> |
| 573 | and returns the "<value>" part of the label. None is returned |
| 574 | if there is not a label matching the pattern |
| 575 | |
| 576 | @returns the label that matches the prefix or 'None' |
| 577 | """ |
| 578 | host_model = models.Host.objects.get(hostname=self.hostname) |
| 579 | host_label = host_model.labels.get(name__startswith=label_prefix) |
| 580 | if not host_label: |
| 581 | return None |
| 582 | return host_label.name.split(label_prefix, 1)[1] |
| 583 | |
| 584 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 585 | def _get_board_from_afe(self): |
| 586 | """Retrieve this host's board from its labels in the AFE. |
| 587 | |
| 588 | Looks for a host label of the form "board:<board>", and |
| 589 | returns the "<board>" part of the label. `None` is returned |
| 590 | if there is not a single, unique label matching the pattern. |
| 591 | |
| 592 | @returns board from label, or `None`. |
| 593 | """ |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 594 | return self._get_label_from_afe(ds_constants.BOARD_PREFIX) |
| 595 | |
| 596 | |
| 597 | def get_build(self): |
| 598 | """Retrieve the current build for this Host from the AFE. |
| 599 | |
| 600 | Looks through this host's labels in the AFE to determine its build. |
| 601 | |
| 602 | @returns The current build or None if it could not find it or if there |
| 603 | were multiple build labels assigned to this host. |
| 604 | """ |
| 605 | return self._get_label_from_afe(ds_constants.VERSION_PREFIX) |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 606 | |
| 607 | |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 608 | def _install_repair(self): |
| 609 | """Attempt to repair this host using upate-engine. |
| 610 | |
| 611 | If the host is up, try installing the DUT with a stable |
| 612 | "repair" version of Chrome OS as defined in the global_config |
| 613 | under CROS.stable_cros_version. |
| 614 | |
| 615 | @returns True if successful, False if update_engine failed. |
| 616 | |
| 617 | """ |
| 618 | if not self.is_up(): |
| 619 | return False |
| 620 | |
| 621 | logging.info('Attempting to reimage machine to repair image.') |
| 622 | try: |
| 623 | self.machine_install(repair=True) |
Fang Deng | d0672f3 | 2013-03-18 17:18:09 -0700 | [diff] [blame] | 624 | except autoupdater.ChromiumOSError as e: |
| 625 | logging.exception(e) |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 626 | logging.info('Repair via install failed.') |
| 627 | return False |
| 628 | |
| 629 | return True |
| 630 | |
| 631 | |
Richard Barnette | 03a0c13 | 2012-11-05 12:40:35 -0800 | [diff] [blame] | 632 | def _servo_repair(self, board): |
| 633 | """Attempt to repair this host using an attached Servo. |
| 634 | |
| 635 | Re-install the OS on the DUT by 1) installing a test image |
| 636 | on a USB storage device attached to the Servo board, |
| 637 | 2) booting that image in recovery mode, and then |
| 638 | 3) installing the image. |
| 639 | |
| 640 | """ |
| 641 | server = dev_server.ImageServer.devserver_url_for_servo(board) |
| 642 | image = server + (self._DEFAULT_SERVO_URL_FORMAT % |
| 643 | { 'board': board }) |
| 644 | self.servo.install_recovery_image(image) |
| 645 | if not self.wait_up(timeout=self.USB_BOOT_TIMEOUT): |
| 646 | raise error.AutoservError('DUT failed to boot from USB' |
| 647 | ' after %d seconds' % |
| 648 | self.USB_BOOT_TIMEOUT) |
| 649 | self.run('chromeos-install --yes', |
| 650 | timeout=self._INSTALL_TIMEOUT) |
| 651 | self.servo.power_long_press() |
| 652 | self.servo.set('usb_mux_sel1', 'servo_sees_usbkey') |
| 653 | self.servo.power_short_press() |
| 654 | if not self.wait_up(timeout=self.BOOT_TIMEOUT): |
| 655 | raise error.AutoservError('DUT failed to reboot installed ' |
| 656 | 'test image after %d seconds' % |
| 657 | self.BOOT_TIMEOUT) |
| 658 | |
| 659 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 660 | def _powercycle_to_repair(self): |
| 661 | """Utilize the RPM Infrastructure to bring the host back up. |
| 662 | |
| 663 | If the host is not up/repaired after the first powercycle we utilize |
| 664 | auto fallback to the last good install by powercycling and rebooting the |
| 665 | host 6 times. |
| 666 | """ |
| 667 | logging.info('Attempting repair via RPM powercycle.') |
| 668 | failed_cycles = 0 |
| 669 | self.power_cycle() |
| 670 | while not self.wait_up(timeout=self.BOOT_TIMEOUT): |
| 671 | failed_cycles += 1 |
| 672 | if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS: |
| 673 | raise error.AutoservError('Powercycled host %s %d times; ' |
| 674 | 'device did not come back online.' % |
| 675 | (self.hostname, failed_cycles)) |
| 676 | self.power_cycle() |
| 677 | if failed_cycles == 0: |
| 678 | logging.info('Powercycling was successful first time.') |
| 679 | else: |
| 680 | logging.info('Powercycling was successful after %d failures.', |
| 681 | failed_cycles) |
| 682 | |
| 683 | |
| 684 | def repair_full(self): |
| 685 | """Repair a host for repair level NO_PROTECTION. |
| 686 | |
| 687 | This overrides the base class function for repair; it does |
| 688 | not call back to the parent class, but instead offers a |
| 689 | simplified implementation based on the capabilities in the |
| 690 | Chrome OS test lab. |
| 691 | |
J. Richard Barnette | fde55fc | 2013-03-15 17:47:01 -0700 | [diff] [blame] | 692 | If `self.verify()` fails, the following procedures are |
| 693 | attempted: |
| 694 | 1. Try to re-install to a known stable image using |
| 695 | auto-update. |
| 696 | 2. If there's a servo for the DUT, try to re-install via |
| 697 | the servo. |
| 698 | 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] | 699 | by power-cycling. |
| 700 | |
| 701 | As with the parent method, the last operation performed on |
| 702 | the DUT must be to call `self.verify()`; if that call fails, |
| 703 | the exception it raises is passed back to the caller. |
J. Richard Barnette | fde55fc | 2013-03-15 17:47:01 -0700 | [diff] [blame] | 704 | |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 705 | """ |
Scott Zawalski | b550d5a | 2013-03-22 09:23:59 -0400 | [diff] [blame] | 706 | host_board = self._get_board_from_afe() |
| 707 | if host_board is None: |
| 708 | logging.error('host %s has no board; failing repair', |
| 709 | self.hostname) |
| 710 | raise |
Scott Zawalski | 89c44dd | 2013-02-26 09:28:02 -0500 | [diff] [blame] | 711 | |
Scott Zawalski | b550d5a | 2013-03-22 09:23:59 -0400 | [diff] [blame] | 712 | if not self._install_repair(): |
| 713 | # TODO(scottz): All repair pathways should be |
| 714 | # executed until we've exhausted all options. Below |
| 715 | # we favor servo over powercycle when we really |
| 716 | # should be falling back to power if servo fails. |
| 717 | if (self.servo and |
| 718 | host_board in self._SERVO_REPAIR_WHITELIST): |
| 719 | self._servo_repair(host_board) |
| 720 | elif (self.has_power() and |
| 721 | host_board in self._RPM_RECOVERY_BOARDS): |
| 722 | self._powercycle_to_repair() |
| 723 | else: |
| 724 | logging.error('host %s has no servo and no RPM control; ' |
| 725 | 'failing repair', self.hostname) |
| 726 | raise |
| 727 | self.verify() |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 728 | |
| 729 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 730 | def close(self): |
| 731 | super(SiteHost, self).close() |
| 732 | self.xmlrpc_disconnect_all() |
| 733 | |
| 734 | |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 735 | def _cleanup_poweron(self): |
| 736 | """Special cleanup method to make sure hosts always get power back.""" |
| 737 | afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) |
| 738 | hosts = afe.get_hosts(hostname=self.hostname) |
| 739 | if not hosts or not (self._RPM_OUTLET_CHANGED in |
| 740 | hosts[0].attributes): |
| 741 | return |
| 742 | logging.debug('This host has recently interacted with the RPM' |
| 743 | ' Infrastructure. Ensuring power is on.') |
| 744 | try: |
| 745 | self.power_on() |
| 746 | except rpm_client.RemotePowerException: |
| 747 | # If cleanup has completed but there was an issue with the RPM |
| 748 | # Infrastructure, log an error message rather than fail cleanup |
| 749 | logging.error('Failed to turn Power On for this host after ' |
| 750 | 'cleanup through the RPM Infrastructure.') |
| 751 | afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None, |
| 752 | hostname=self.hostname) |
| 753 | |
| 754 | |
Chris Sosa | f4d43ff | 2012-10-30 11:21:05 -0700 | [diff] [blame] | 755 | def cleanup(self): |
Chris Sosa | f4d43ff | 2012-10-30 11:21:05 -0700 | [diff] [blame] | 756 | client_at = autotest.Autotest(self) |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 757 | self.run('rm -f %s' % constants.CLEANUP_LOGS_PAUSED_FILE) |
Scott Zawalski | ddbc31e | 2012-11-15 11:29:01 -0500 | [diff] [blame] | 758 | try: |
| 759 | client_at.run_static_method('autotest_lib.client.cros.cros_ui', |
| 760 | '_clear_login_prompt_state') |
| 761 | self.run('restart ui') |
| 762 | client_at.run_static_method('autotest_lib.client.cros.cros_ui', |
| 763 | '_wait_for_login_prompt') |
Alex Miller | f451796 | 2013-02-25 15:03:02 -0800 | [diff] [blame] | 764 | except (error.AutotestRunError, error.AutoservRunError): |
Scott Zawalski | ddbc31e | 2012-11-15 11:29:01 -0500 | [diff] [blame] | 765 | logging.warn('Unable to restart ui, rebooting device.') |
| 766 | # Since restarting the UI fails fall back to normal Autotest |
| 767 | # cleanup routines, i.e. reboot the machine. |
| 768 | super(SiteHost, self).cleanup() |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 769 | # Check if the rpm outlet was manipulated. |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 770 | if self.has_power(): |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 771 | self._cleanup_poweron() |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 772 | |
| 773 | |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 774 | def reboot(self, **dargs): |
| 775 | """ |
| 776 | This function reboots the site host. The more generic |
| 777 | RemoteHost.reboot() performs sync and sleeps for 5 |
| 778 | seconds. This is not necessary for Chrome OS devices as the |
| 779 | sync should be finished in a short time during the reboot |
| 780 | command. |
| 781 | """ |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 782 | if 'reboot_cmd' not in dargs: |
| 783 | dargs['reboot_cmd'] = ('((reboot & sleep 10; reboot -f &)' |
| 784 | ' </dev/null >/dev/null 2>&1 &)') |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 785 | # Enable fastsync to avoid running extra sync commands before reboot. |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 786 | if 'fastsync' not in dargs: |
| 787 | dargs['fastsync'] = True |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 788 | super(SiteHost, self).reboot(**dargs) |
| 789 | |
| 790 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 791 | def verify_software(self): |
Richard Barnette | b2bc13c | 2013-01-08 17:32:51 -0800 | [diff] [blame] | 792 | """Verify working software on a Chrome OS system. |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 793 | |
Richard Barnette | b2bc13c | 2013-01-08 17:32:51 -0800 | [diff] [blame] | 794 | Tests for the following conditions: |
| 795 | 1. All conditions tested by the parent version of this |
| 796 | function. |
| 797 | 2. Sufficient space in /mnt/stateful_partition. |
| 798 | 3. update_engine answers a simple status request over DBus. |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 799 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 800 | """ |
| 801 | super(SiteHost, self).verify_software() |
| 802 | self.check_diskspace( |
| 803 | '/mnt/stateful_partition', |
| 804 | global_config.global_config.get_config_value( |
| 805 | 'SERVER', 'gb_diskspace_required', type=int, |
| 806 | default=20)) |
Richard Barnette | b2bc13c | 2013-01-08 17:32:51 -0800 | [diff] [blame] | 807 | self.run('update_engine_client --status') |
Scott Zawalski | fbca4a9 | 2013-03-04 15:56:42 -0500 | [diff] [blame] | 808 | # Makes sure python is present, loads and can use built in functions. |
| 809 | # We have seen cases where importing cPickle fails with undefined |
| 810 | # symbols in cPickle.so. |
| 811 | self.run('python -c "import cPickle"') |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 812 | |
| 813 | |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 814 | def xmlrpc_connect(self, command, port, command_name=None, |
| 815 | ready_test_name=None, timeout_seconds=10): |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 816 | """Connect to an XMLRPC server on the host. |
| 817 | |
| 818 | The `command` argument should be a simple shell command that |
| 819 | starts an XMLRPC server on the given `port`. The command |
| 820 | must not daemonize, and must terminate cleanly on SIGTERM. |
| 821 | The command is started in the background on the host, and a |
| 822 | local XMLRPC client for the server is created and returned |
| 823 | to the caller. |
| 824 | |
| 825 | Note that the process of creating an XMLRPC client makes no |
| 826 | attempt to connect to the remote server; the caller is |
| 827 | responsible for determining whether the server is running |
| 828 | correctly, and is ready to serve requests. |
| 829 | |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 830 | Optionally, the caller can pass ready_test_name, a string |
| 831 | containing the name of a method to call on the proxy. This |
| 832 | method should take no parameters and return successfully only |
| 833 | when the server is ready to process client requests. When |
| 834 | ready_test_name is set, xmlrpc_connect will block until the |
| 835 | proxy is ready, and throw a TestError if the server isn't |
| 836 | ready by timeout_seconds. |
| 837 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 838 | @param command Shell command to start the server. |
| 839 | @param port Port number on which the server is expected to |
| 840 | be serving. |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 841 | @param command_name String to use as input to `pkill` to |
| 842 | terminate the XMLRPC server on the host. |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 843 | @param ready_test_name String containing the name of a |
| 844 | method defined on the XMLRPC server. |
| 845 | @param timeout_seconds Number of seconds to wait |
| 846 | for the server to become 'ready.' Will throw a |
| 847 | TestFail error if server is not ready in time. |
| 848 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 849 | """ |
| 850 | self.xmlrpc_disconnect(port) |
| 851 | |
| 852 | # Chrome OS on the target closes down most external ports |
| 853 | # for security. We could open the port, but doing that |
| 854 | # would conflict with security tests that check that only |
| 855 | # expected ports are open. So, to get to the port on the |
| 856 | # target we use an ssh tunnel. |
| 857 | local_port = utils.get_unused_port() |
| 858 | tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port) |
| 859 | ssh_cmd = make_ssh_command(opts=tunnel_options) |
| 860 | tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname) |
| 861 | logging.debug('Full tunnel command: %s', tunnel_cmd) |
| 862 | tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True) |
| 863 | logging.debug('Started XMLRPC tunnel, local = %d' |
| 864 | ' remote = %d, pid = %d', |
| 865 | local_port, port, tunnel_proc.pid) |
| 866 | |
| 867 | # Start the server on the host. Redirection in the command |
| 868 | # below is necessary, because 'ssh' won't terminate until |
| 869 | # background child processes close stdin, stdout, and |
| 870 | # stderr. |
| 871 | remote_cmd = '( %s ) </dev/null >/dev/null 2>&1 & echo $!' % command |
| 872 | remote_pid = self.run(remote_cmd).stdout.rstrip('\n') |
| 873 | logging.debug('Started XMLRPC server on host %s, pid = %s', |
| 874 | self.hostname, remote_pid) |
| 875 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 876 | self._xmlrpc_proxy_map[port] = (command_name, tunnel_proc) |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 877 | rpc_url = 'http://localhost:%d' % local_port |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 878 | proxy = xmlrpclib.ServerProxy(rpc_url, allow_none=True) |
| 879 | if ready_test_name is not None: |
J. Richard Barnette | 13eb7c0 | 2013-03-07 12:06:29 -0800 | [diff] [blame] | 880 | # retry.retry logs each attempt; calculate delay_sec to |
| 881 | # keep log spam to a dull roar. |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 882 | @retry.retry((socket.error, xmlrpclib.ProtocolError), |
| 883 | timeout_min=timeout_seconds/60.0, |
J. Richard Barnette | 13eb7c0 | 2013-03-07 12:06:29 -0800 | [diff] [blame] | 884 | delay_sec=min(max(timeout_seconds/20.0, 0.1), 1)) |
Christopher Wiley | d78249a | 2013-03-01 13:05:31 -0800 | [diff] [blame] | 885 | def ready_test(): |
| 886 | """ Call proxy.ready_test_name(). """ |
| 887 | getattr(proxy, ready_test_name)() |
| 888 | successful = False |
| 889 | try: |
| 890 | logging.info('Waiting %d seconds for XMLRPC server ' |
| 891 | 'to start.', timeout_seconds) |
| 892 | ready_test() |
| 893 | successful = True |
| 894 | except retry.TimeoutException: |
| 895 | raise error.TestError('Unable to start XMLRPC server after ' |
| 896 | '%d seconds.' % timeout_seconds) |
| 897 | finally: |
| 898 | if not successful: |
| 899 | logging.error('Failed to start XMLRPC server.') |
| 900 | self.xmlrpc_disconnect(port) |
| 901 | logging.info('XMLRPC server started successfully.') |
| 902 | return proxy |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 903 | |
| 904 | def xmlrpc_disconnect(self, port): |
| 905 | """Disconnect from an XMLRPC server on the host. |
| 906 | |
| 907 | Terminates the remote XMLRPC server previously started for |
| 908 | the given `port`. Also closes the local ssh tunnel created |
| 909 | for the connection to the host. This function does not |
| 910 | directly alter the state of a previously returned XMLRPC |
| 911 | client object; however disconnection will cause all |
| 912 | subsequent calls to methods on the object to fail. |
| 913 | |
| 914 | This function does nothing if requested to disconnect a port |
| 915 | that was not previously connected via `self.xmlrpc_connect()` |
| 916 | |
| 917 | @param port Port number passed to a previous call to |
| 918 | `xmlrpc_connect()` |
| 919 | """ |
| 920 | if port not in self._xmlrpc_proxy_map: |
| 921 | return |
| 922 | entry = self._xmlrpc_proxy_map[port] |
| 923 | remote_name = entry[0] |
| 924 | tunnel_proc = entry[1] |
| 925 | if remote_name: |
| 926 | # We use 'pkill' to find our target process rather than |
| 927 | # a PID, because the host may have rebooted since |
| 928 | # connecting, and we don't want to kill an innocent |
| 929 | # process with the same PID. |
| 930 | # |
| 931 | # 'pkill' helpfully exits with status 1 if no target |
| 932 | # process is found, for which run() will throw an |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 933 | # exception. We don't want that, so we the ignore |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 934 | # status. |
| 935 | self.run("pkill -f '%s'" % remote_name, ignore_status=True) |
| 936 | |
| 937 | if tunnel_proc.poll() is None: |
| 938 | tunnel_proc.terminate() |
| 939 | logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid) |
| 940 | else: |
| 941 | logging.debug('Tunnel pid %d terminated early, status %d', |
| 942 | tunnel_proc.pid, tunnel_proc.returncode) |
| 943 | del self._xmlrpc_proxy_map[port] |
| 944 | |
| 945 | |
| 946 | def xmlrpc_disconnect_all(self): |
| 947 | """Disconnect all known XMLRPC proxy ports.""" |
| 948 | for port in self._xmlrpc_proxy_map.keys(): |
| 949 | self.xmlrpc_disconnect(port) |
| 950 | |
| 951 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 952 | def _ping_check_status(self, status): |
| 953 | """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] | 954 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 955 | @param status Check the ping status against this value. |
| 956 | @return True iff `status` and the result of ping are the same |
| 957 | (i.e. both True or both False). |
| 958 | |
| 959 | """ |
| 960 | ping_val = utils.ping(self.hostname, tries=1, deadline=1) |
| 961 | return not (status ^ (ping_val == 0)) |
| 962 | |
| 963 | def _ping_wait_for_status(self, status, timeout): |
| 964 | """Wait for the host to have a given status (UP or DOWN). |
| 965 | |
| 966 | Status is checked by polling. Polling will not last longer |
| 967 | than the number of seconds in `timeout`. The polling |
| 968 | interval will be long enough that only approximately |
| 969 | _PING_WAIT_COUNT polling cycles will be executed, subject |
| 970 | to a maximum interval of about one minute. |
| 971 | |
| 972 | @param status Waiting will stop immediately if `ping` of the |
| 973 | host returns this status. |
| 974 | @param timeout Poll for at most this many seconds. |
| 975 | @return True iff the host status from `ping` matched the |
| 976 | requested status at the time of return. |
| 977 | |
| 978 | """ |
| 979 | # _ping_check_status() takes about 1 second, hence the |
| 980 | # "- 1" in the formula below. |
| 981 | poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1 |
| 982 | end_time = time.time() + timeout |
| 983 | while time.time() <= end_time: |
| 984 | if self._ping_check_status(status): |
| 985 | return True |
| 986 | if poll_interval > 0: |
| 987 | time.sleep(poll_interval) |
| 988 | |
| 989 | # The last thing we did was sleep(poll_interval), so it may |
| 990 | # have been too long since the last `ping`. Check one more |
| 991 | # time, just to be sure. |
| 992 | return self._ping_check_status(status) |
| 993 | |
| 994 | def ping_wait_up(self, timeout): |
| 995 | """Wait for the host to respond to `ping`. |
| 996 | |
| 997 | N.B. This method is not a reliable substitute for |
| 998 | `wait_up()`, because a host that responds to ping will not |
| 999 | necessarily respond to ssh. This method should only be used |
| 1000 | if the target DUT can be considered functional even if it |
| 1001 | can't be reached via ssh. |
| 1002 | |
| 1003 | @param timeout Minimum time to allow before declaring the |
| 1004 | host to be non-responsive. |
| 1005 | @return True iff the host answered to ping before the timeout. |
| 1006 | |
| 1007 | """ |
| 1008 | return self._ping_wait_for_status(self._PING_STATUS_UP, timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1009 | |
Andrew Bresticker | 678c0c7 | 2013-01-22 10:44:09 -0800 | [diff] [blame] | 1010 | def ping_wait_down(self, timeout): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1011 | """Wait until the host no longer responds to `ping`. |
| 1012 | |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 1013 | This function can be used as a slightly faster version of |
| 1014 | `wait_down()`, by avoiding potentially long ssh timeouts. |
| 1015 | |
| 1016 | @param timeout Minimum time to allow for the host to become |
| 1017 | non-responsive. |
| 1018 | @return True iff the host quit answering ping before the |
| 1019 | timeout. |
| 1020 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1021 | """ |
J. Richard Barnette | b6de7e3 | 2013-02-14 13:28:04 -0800 | [diff] [blame] | 1022 | return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1023 | |
| 1024 | def test_wait_for_sleep(self): |
| 1025 | """Wait for the client to enter low-power sleep mode. |
| 1026 | |
| 1027 | The test for "is asleep" can't distinguish a system that is |
| 1028 | powered off; to confirm that the unit was asleep, it is |
| 1029 | necessary to force resume, and then call |
| 1030 | `test_wait_for_resume()`. |
| 1031 | |
| 1032 | This function is expected to be called from a test as part |
| 1033 | of a sequence like the following: |
| 1034 | |
| 1035 | ~~~~~~~~ |
| 1036 | boot_id = host.get_boot_id() |
| 1037 | # trigger sleep on the host |
| 1038 | host.test_wait_for_sleep() |
| 1039 | # trigger resume on the host |
| 1040 | host.test_wait_for_resume(boot_id) |
| 1041 | ~~~~~~~~ |
| 1042 | |
| 1043 | @exception TestFail The host did not go to sleep within |
| 1044 | the allowed time. |
| 1045 | """ |
Andrew Bresticker | 678c0c7 | 2013-01-22 10:44:09 -0800 | [diff] [blame] | 1046 | if not self.ping_wait_down(timeout=self.SLEEP_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1047 | raise error.TestFail( |
| 1048 | 'client failed to sleep after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1049 | self.SLEEP_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1050 | |
| 1051 | |
| 1052 | def test_wait_for_resume(self, old_boot_id): |
| 1053 | """Wait for the client to resume from low-power sleep mode. |
| 1054 | |
| 1055 | The `old_boot_id` parameter should be the value from |
| 1056 | `get_boot_id()` obtained prior to entering sleep mode. A |
| 1057 | `TestFail` exception is raised if the boot id changes. |
| 1058 | |
| 1059 | See @ref test_wait_for_sleep for more on this function's |
| 1060 | usage. |
| 1061 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 1062 | @param old_boot_id A boot id value obtained before the |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1063 | target host went to sleep. |
| 1064 | |
| 1065 | @exception TestFail The host did not respond within the |
| 1066 | allowed time. |
| 1067 | @exception TestFail The host responded, but the boot id test |
| 1068 | indicated a reboot rather than a sleep |
| 1069 | cycle. |
| 1070 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1071 | if not self.wait_up(timeout=self.RESUME_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1072 | raise error.TestFail( |
| 1073 | 'client failed to resume from sleep after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1074 | self.RESUME_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1075 | else: |
| 1076 | new_boot_id = self.get_boot_id() |
| 1077 | if new_boot_id != old_boot_id: |
| 1078 | raise error.TestFail( |
| 1079 | 'client rebooted, but sleep was expected' |
| 1080 | ' (old boot %s, new boot %s)' |
| 1081 | % (old_boot_id, new_boot_id)) |
| 1082 | |
| 1083 | |
| 1084 | def test_wait_for_shutdown(self): |
| 1085 | """Wait for the client to shut down. |
| 1086 | |
| 1087 | The test for "has shut down" can't distinguish a system that |
| 1088 | is merely asleep; to confirm that the unit was down, it is |
| 1089 | necessary to force boot, and then call test_wait_for_boot(). |
| 1090 | |
| 1091 | This function is expected to be called from a test as part |
| 1092 | of a sequence like the following: |
| 1093 | |
| 1094 | ~~~~~~~~ |
| 1095 | boot_id = host.get_boot_id() |
| 1096 | # trigger shutdown on the host |
| 1097 | host.test_wait_for_shutdown() |
| 1098 | # trigger boot on the host |
| 1099 | host.test_wait_for_boot(boot_id) |
| 1100 | ~~~~~~~~ |
| 1101 | |
| 1102 | @exception TestFail The host did not shut down within the |
| 1103 | allowed time. |
| 1104 | """ |
Andrew Bresticker | 678c0c7 | 2013-01-22 10:44:09 -0800 | [diff] [blame] | 1105 | if not self.ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1106 | raise error.TestFail( |
| 1107 | 'client failed to shut down after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1108 | self.SHUTDOWN_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1109 | |
| 1110 | |
| 1111 | def test_wait_for_boot(self, old_boot_id=None): |
| 1112 | """Wait for the client to boot from cold power. |
| 1113 | |
| 1114 | The `old_boot_id` parameter should be the value from |
| 1115 | `get_boot_id()` obtained prior to shutting down. A |
| 1116 | `TestFail` exception is raised if the boot id does not |
| 1117 | change. The boot id test is omitted if `old_boot_id` is not |
| 1118 | specified. |
| 1119 | |
| 1120 | See @ref test_wait_for_shutdown for more on this function's |
| 1121 | usage. |
| 1122 | |
J. Richard Barnette | 7214e0b | 2013-02-06 15:20:49 -0800 | [diff] [blame] | 1123 | @param old_boot_id A boot id value obtained before the |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1124 | shut down. |
| 1125 | |
| 1126 | @exception TestFail The host did not respond within the |
| 1127 | allowed time. |
| 1128 | @exception TestFail The host responded, but the boot id test |
| 1129 | indicated that there was no reboot. |
| 1130 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1131 | if not self.wait_up(timeout=self.REBOOT_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1132 | raise error.TestFail( |
| 1133 | 'client failed to reboot after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 1134 | self.REBOOT_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 1135 | elif old_boot_id: |
| 1136 | if self.get_boot_id() == old_boot_id: |
| 1137 | raise error.TestFail( |
| 1138 | 'client is back up, but did not reboot' |
| 1139 | ' (boot %s)' % old_boot_id) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 1140 | |
| 1141 | |
| 1142 | @staticmethod |
| 1143 | def check_for_rpm_support(hostname): |
| 1144 | """For a given hostname, return whether or not it is powered by an RPM. |
| 1145 | |
| 1146 | @return None if this host does not follows the defined naming format |
| 1147 | for RPM powered DUT's in the lab. If it does follow the format, |
| 1148 | it returns a regular expression MatchObject instead. |
| 1149 | """ |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1150 | return re.match(SiteHost._RPM_HOSTNAME_REGEX, hostname) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 1151 | |
| 1152 | |
| 1153 | def has_power(self): |
| 1154 | """For this host, return whether or not it is powered by an RPM. |
| 1155 | |
| 1156 | @return True if this host is in the CROS lab and follows the defined |
| 1157 | naming format. |
| 1158 | """ |
| 1159 | return SiteHost.check_for_rpm_support(self.hostname) |
| 1160 | |
| 1161 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 1162 | def _set_power(self, state, power_method): |
| 1163 | """Sets the power to the host via RPM, Servo or manual. |
| 1164 | |
| 1165 | @param state Specifies which power state to set to DUT |
| 1166 | @param power_method Specifies which method of power control to |
| 1167 | use. By default "RPM" will be used. Valid values |
| 1168 | are the strings "RPM", "manual", "servoj10". |
| 1169 | |
| 1170 | """ |
| 1171 | ACCEPTABLE_STATES = ['ON', 'OFF'] |
| 1172 | |
| 1173 | if state.upper() not in ACCEPTABLE_STATES: |
| 1174 | raise error.TestError('State must be one of: %s.' |
| 1175 | % (ACCEPTABLE_STATES,)) |
| 1176 | |
| 1177 | if power_method == self.POWER_CONTROL_SERVO: |
| 1178 | logging.info('Setting servo port J10 to %s', state) |
| 1179 | self.servo.set('prtctl3_pwren', state.lower()) |
| 1180 | time.sleep(self._USB_POWER_TIMEOUT) |
| 1181 | elif power_method == self.POWER_CONTROL_MANUAL: |
| 1182 | logging.info('You have %d seconds to set the AC power to %s.', |
| 1183 | self._POWER_CYCLE_TIMEOUT, state) |
| 1184 | time.sleep(self._POWER_CYCLE_TIMEOUT) |
| 1185 | else: |
| 1186 | if not self.has_power(): |
| 1187 | raise error.TestFail('DUT does not have RPM connected.') |
Simran Basi | 5e6339a | 2013-03-21 11:34:32 -0700 | [diff] [blame] | 1188 | afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10) |
| 1189 | afe.set_host_attribute(self._RPM_OUTLET_CHANGED, True, |
| 1190 | hostname=self.hostname) |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 1191 | rpm_client.set_power(self.hostname, state.upper()) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 1192 | |
| 1193 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 1194 | def power_off(self, power_method=POWER_CONTROL_RPM): |
| 1195 | """Turn off power to this host via RPM, Servo or manual. |
| 1196 | |
| 1197 | @param power_method Specifies which method of power control to |
| 1198 | use. By default "RPM" will be used. Valid values |
| 1199 | are the strings "RPM", "manual", "servoj10". |
| 1200 | |
| 1201 | """ |
| 1202 | self._set_power('OFF', power_method) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame] | 1203 | |
| 1204 | |
Ismail Noorbasha | 07fdb61 | 2013-02-14 14:13:31 -0800 | [diff] [blame] | 1205 | def power_on(self, power_method=POWER_CONTROL_RPM): |
| 1206 | """Turn on power to this host via RPM, Servo or manual. |
| 1207 | |
| 1208 | @param power_method Specifies which method of power control to |
| 1209 | use. By default "RPM" will be used. Valid values |
| 1210 | are the strings "RPM", "manual", "servoj10". |
| 1211 | |
| 1212 | """ |
| 1213 | self._set_power('ON', power_method) |
| 1214 | |
| 1215 | |
| 1216 | def power_cycle(self, power_method=POWER_CONTROL_RPM): |
| 1217 | """Cycle power to this host by turning it OFF, then ON. |
| 1218 | |
| 1219 | @param power_method Specifies which method of power control to |
| 1220 | use. By default "RPM" will be used. Valid values |
| 1221 | are the strings "RPM", "manual", "servoj10". |
| 1222 | |
| 1223 | """ |
| 1224 | if power_method in (self.POWER_CONTROL_SERVO, |
| 1225 | self.POWER_CONTROL_MANUAL): |
| 1226 | self.power_off(power_method=power_method) |
| 1227 | time.sleep(self._POWER_CYCLE_TIMEOUT) |
| 1228 | self.power_on(power_method=power_method) |
| 1229 | else: |
| 1230 | rpm_client.set_power(self.hostname, 'CYCLE') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1231 | |
| 1232 | |
| 1233 | def get_platform(self): |
| 1234 | """Determine the correct platform label for this host. |
| 1235 | |
| 1236 | @returns a string representing this host's platform. |
| 1237 | """ |
| 1238 | crossystem = utils.Crossystem(self) |
| 1239 | crossystem.init() |
| 1240 | # Extract fwid value and use the leading part as the platform id. |
| 1241 | # fwid generally follow the format of {platform}.{firmware version} |
| 1242 | # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z |
| 1243 | platform = crossystem.fwid().split('.')[0].lower() |
| 1244 | # Newer platforms start with 'Google_' while the older ones do not. |
| 1245 | return platform.replace('google_', '') |
| 1246 | |
| 1247 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 1248 | @label_decorator() |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1249 | def get_board(self): |
| 1250 | """Determine the correct board label for this host. |
| 1251 | |
| 1252 | @returns a string representing this host's board. |
| 1253 | """ |
| 1254 | release_info = utils.parse_cmd_output('cat /etc/lsb-release', |
| 1255 | run_method=self.run) |
| 1256 | board = release_info['CHROMEOS_RELEASE_BOARD'] |
| 1257 | # Devices in the lab generally have the correct board name but our own |
| 1258 | # development devices have {board_name}-signed-{key_type}. The board |
| 1259 | # name may also begin with 'x86-' which we need to keep. |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 1260 | board_format_string = ds_constants.BOARD_PREFIX + '%s' |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1261 | if 'x86' not in board: |
Simran Basi | 833814b | 2013-01-29 13:13:43 -0800 | [diff] [blame] | 1262 | return board_format_string % board.split('-')[0] |
| 1263 | return board_format_string % '-'.join(board.split('-')[0:2]) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1264 | |
| 1265 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 1266 | @label_decorator('lightsensor') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1267 | def has_lightsensor(self): |
| 1268 | """Determine the correct board label for this host. |
| 1269 | |
| 1270 | @returns the string 'lightsensor' if this host has a lightsensor or |
| 1271 | None if it does not. |
| 1272 | """ |
| 1273 | search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % ( |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1274 | self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES)) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1275 | try: |
| 1276 | # Run the search cmd following the symlinks. Stderr_tee is set to |
| 1277 | # None as there can be a symlink loop, but the command will still |
| 1278 | # execute correctly with a few messages printed to stderr. |
| 1279 | self.run(search_cmd, stdout_tee=None, stderr_tee=None) |
| 1280 | return 'lightsensor' |
| 1281 | except error.AutoservRunError: |
| 1282 | # egrep exited with a return code of 1 meaning none of the possible |
| 1283 | # lightsensor files existed. |
| 1284 | return None |
| 1285 | |
| 1286 | |
Aviv Keshet | 74c89a9 | 2013-02-04 15:18:30 -0800 | [diff] [blame] | 1287 | @label_decorator('bluetooth') |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1288 | def has_bluetooth(self): |
| 1289 | """Determine the correct board label for this host. |
| 1290 | |
| 1291 | @returns the string 'bluetooth' if this host has bluetooth or |
| 1292 | None if it does not. |
| 1293 | """ |
| 1294 | try: |
| 1295 | self.run('test -d /sys/class/bluetooth/hci0') |
| 1296 | # test exited with a return code of 0. |
| 1297 | return 'bluetooth' |
| 1298 | except error.AutoservRunError: |
| 1299 | # test exited with a return code 1 meaning the directory did not |
| 1300 | # exist. |
| 1301 | return None |
| 1302 | |
| 1303 | |
| 1304 | def get_labels(self): |
| 1305 | """Return a list of labels for this given host. |
| 1306 | |
| 1307 | This is the main way to retrieve all the automatic labels for a host |
| 1308 | as it will run through all the currently implemented label functions. |
| 1309 | """ |
| 1310 | labels = [] |
Richard Barnette | 82c3591 | 2012-11-20 10:09:10 -0800 | [diff] [blame] | 1311 | for label_function in self._LABEL_FUNCTIONS: |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1312 | label = label_function(self) |
| 1313 | if label: |
| 1314 | labels.append(label) |
| 1315 | return labels |