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