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