blob: 43ad2fc31e0291fbf12c5dca20ee1e4861983c2f [file] [log] [blame]
J. Richard Barnette24adbf42012-04-11 15:04:53 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtisaa5eedb2011-08-23 16:18:52 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
mussa584b4462014-06-20 15:13:28 -07005import ConfigParser
Aviv Keshet74c89a92013-02-04 15:18:30 -08006import functools
Christopher Wiley0ed712b2013-04-09 15:25:12 -07007import httplib
J. Richard Barnette1d78b012012-05-15 13:56:30 -07008import logging
Dan Shi0f466e82013-02-22 15:44:58 -08009import os
Simran Basid5e5e272012-09-24 15:23:59 -070010import re
Christopher Wileyd78249a2013-03-01 13:05:31 -080011import socket
J. Richard Barnette1d78b012012-05-15 13:56:30 -070012import subprocess
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070013import time
J. Richard Barnette1d78b012012-05-15 13:56:30 -070014import xmlrpclib
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070015
mussa584b4462014-06-20 15:13:28 -070016import common
J. Richard Barnette45e93de2012-04-11 17:24:15 -070017from autotest_lib.client.bin import utils
Richard Barnette0c73ffc2012-11-19 15:21:18 -080018from autotest_lib.client.common_lib import error
19from autotest_lib.client.common_lib import global_config
J. Richard Barnette45e93de2012-04-11 17:24:15 -070020from autotest_lib.client.common_lib.cros import autoupdater
Richard Barnette03a0c132012-11-05 12:40:35 -080021from autotest_lib.client.common_lib.cros import dev_server
Christopher Wileyd78249a2013-03-01 13:05:31 -080022from autotest_lib.client.common_lib.cros import retry
Michael Liangda8c60a2014-06-03 13:24:51 -070023from autotest_lib.client.common_lib.cros.graphite import stats
Richard Barnette82c35912012-11-20 10:09:10 -080024from autotest_lib.client.cros import constants
J. Richard Barnette84890bd2014-02-21 11:05:47 -080025from autotest_lib.client.cros import cros_ui
J. Richard Barnette45e93de2012-04-11 17:24:15 -070026from autotest_lib.server import autoserv_parser
Dan Shia1ecd5c2013-06-06 11:21:31 -070027from autotest_lib.server import utils as server_utils
Scott Zawalski89c44dd2013-02-26 09:28:02 -050028from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
Simran Basi5e6339a2013-03-21 11:34:32 -070029from autotest_lib.server.cros.dynamic_suite import tools, frontend_wrappers
Fang Deng96667ca2013-08-01 17:46:18 -070030from autotest_lib.server.hosts import abstract_ssh
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +080031from autotest_lib.server.hosts import chameleon_host
Fang Deng5d518f42013-08-02 14:04:32 -070032from autotest_lib.server.hosts import servo_host
Simran Basidcff4252012-11-20 16:13:20 -080033from autotest_lib.site_utils.rpm_control_system import rpm_client
Simran Basid5e5e272012-09-24 15:23:59 -070034
35
beeps32a63082013-08-22 14:02:29 -070036try:
37 import jsonrpclib
38except ImportError:
39 jsonrpclib = None
Fang Deng96667ca2013-08-01 17:46:18 -070040
Fang Dengd1c2b732013-08-20 12:59:46 -070041
beepsc87ff602013-07-31 21:53:00 -070042class FactoryImageCheckerException(error.AutoservError):
43 """Exception raised when an image is a factory image."""
44 pass
45
46
Aviv Keshet74c89a92013-02-04 15:18:30 -080047def add_label_detector(label_function_list, label_list=None, label=None):
48 """Decorator used to group functions together into the provided list.
49 @param label_function_list: List of label detecting functions to add
50 decorated function to.
51 @param label_list: List of detectable labels to add detectable labels to.
52 (Default: None)
53 @param label: Label string that is detectable by this detection function
54 (Default: None)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -080055 """
Simran Basic6f1f7a2012-10-16 10:47:46 -070056 def add_func(func):
Aviv Keshet74c89a92013-02-04 15:18:30 -080057 """
58 @param func: The function to be added as a detector.
59 """
60 label_function_list.append(func)
61 if label and label_list is not None:
62 label_list.append(label)
Simran Basic6f1f7a2012-10-16 10:47:46 -070063 return func
64 return add_func
65
66
Fang Deng0ca40e22013-08-27 17:47:44 -070067class CrosHost(abstract_ssh.AbstractSSHHost):
J. Richard Barnette45e93de2012-04-11 17:24:15 -070068 """Chromium OS specific subclass of Host."""
69
70 _parser = autoserv_parser.autoserv_parser
Scott Zawalski62bacae2013-03-05 10:40:32 -050071 _AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
J. Richard Barnette45e93de2012-04-11 17:24:15 -070072
Richard Barnette03a0c132012-11-05 12:40:35 -080073 # Timeout values (in seconds) associated with various Chrome OS
74 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070075 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -080076 # In general, a good rule of thumb is that the timeout can be up
77 # to twice the typical measured value on the slowest platform.
78 # The times here have not necessarily been empirically tested to
79 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070080 #
81 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -080082 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
83 # time to restart the netwowrk.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080084 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070085 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -080086 # other things, this must account for the 30 second dev-mode
J. Richard Barnetted4649c62013-03-06 17:42:27 -080087 # screen delay and time to start the network.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070088 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -080089 # including the 30 second dev-mode delay and time to start the
J. Richard Barnetted4649c62013-03-06 17:42:27 -080090 # network.
beepsf079cfb2013-09-18 17:49:51 -070091 # INSTALL_TIMEOUT: Time to allow for chromeos-install.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080092 # POWERWASH_BOOT_TIMEOUT: Time to allow for a reboot that
93 # includes powerwash.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070094
95 SLEEP_TIMEOUT = 2
J. Richard Barnetted4649c62013-03-06 17:42:27 -080096 RESUME_TIMEOUT = 10
Tom Wai-Hong Tam4d169ed2014-02-14 11:05:40 +080097 SHUTDOWN_TIMEOUT = 5
J. Richard Barnettefbcc7122013-07-24 18:24:59 -070098 BOOT_TIMEOUT = 60
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070099 USB_BOOT_TIMEOUT = 150
J. Richard Barnette84890bd2014-02-21 11:05:47 -0800100 INSTALL_TIMEOUT = 240
Dan Shi2c88eed2013-11-12 10:18:38 -0800101 POWERWASH_BOOT_TIMEOUT = 60
Chris Sosab76e0ee2013-05-22 16:55:41 -0700102
J. Richard Barnette84890bd2014-02-21 11:05:47 -0800103 # REBOOT_TIMEOUT: How long to wait for a reboot.
104 #
Chris Sosab76e0ee2013-05-22 16:55:41 -0700105 # We have a long timeout to ensure we don't flakily fail due to other
106 # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate.
Simran Basi1160e2c2013-10-04 16:00:24 -0700107 # TODO(sbasi - crbug.com/276094) Restore to 5 mins once the 'host did not
108 # return from reboot' bug is solved.
109 REBOOT_TIMEOUT = 480
Chris Sosab76e0ee2013-05-22 16:55:41 -0700110
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800111 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
112 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
113 _USB_POWER_TIMEOUT = 5
114 _POWER_CYCLE_TIMEOUT = 10
115
beeps32a63082013-08-22 14:02:29 -0700116 _RPC_PROXY_URL = 'http://localhost:%d'
Christopher Wileydd181852013-10-10 19:56:58 -0700117 _RPC_SHUTDOWN_POLLING_PERIOD_SECONDS = 2
Peter Qiu4410db72014-06-05 10:32:41 -0700118 # Set shutdown timeout to account for the time for restarting the UI.
119 _RPC_SHUTDOWN_TIMEOUT_SECONDS = cros_ui.RESTART_UI_TIMEOUT
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800120
Richard Barnette82c35912012-11-20 10:09:10 -0800121 _RPM_RECOVERY_BOARDS = global_config.global_config.get_config_value('CROS',
122 'rpm_recovery_boards', type=str).split(',')
123
124 _MAX_POWER_CYCLE_ATTEMPTS = 6
125 _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
126 _RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-'
127 'host[0-9]+')
128 _LIGHTSENSOR_FILES = ['in_illuminance0_input',
129 'in_illuminance0_raw',
130 'illuminance0_input']
131 _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices'
132 _LABEL_FUNCTIONS = []
Aviv Keshet74c89a92013-02-04 15:18:30 -0800133 _DETECTABLE_LABELS = []
134 label_decorator = functools.partial(add_label_detector, _LABEL_FUNCTIONS,
135 _DETECTABLE_LABELS)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700136
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800137 # Constants used in ping_wait_up() and ping_wait_down().
138 #
139 # _PING_WAIT_COUNT is the approximate number of polling
140 # cycles to use when waiting for a host state change.
141 #
142 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
143 # for arguments to the internal _ping_wait_for_status()
144 # method.
145 _PING_WAIT_COUNT = 40
146 _PING_STATUS_DOWN = False
147 _PING_STATUS_UP = True
148
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800149 # Allowed values for the power_method argument.
150
151 # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods.
152 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
153 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
154 POWER_CONTROL_RPM = 'RPM'
155 POWER_CONTROL_SERVO = 'servoj10'
156 POWER_CONTROL_MANUAL = 'manual'
157
158 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
159 POWER_CONTROL_SERVO,
160 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800161
Simran Basi5e6339a2013-03-21 11:34:32 -0700162 _RPM_OUTLET_CHANGED = 'outlet_changed'
163
beeps687243d2013-07-18 15:29:27 -0700164
J. Richard Barnette964fba02012-10-24 17:34:29 -0700165 @staticmethod
beeps46dadc92013-11-07 14:07:10 -0800166 def check_host(host, timeout=10):
167 """
168 Check if the given host is a chrome-os host.
169
170 @param host: An ssh host representing a device.
171 @param timeout: The timeout for the run command.
172
173 @return: True if the host device is chromeos.
174
beeps46dadc92013-11-07 14:07:10 -0800175 """
176 try:
Christopher Wiley1ea80942014-02-26 16:45:08 -0800177 result = host.run('grep -q CHROMEOS /etc/lsb-release && '
Simran Basie5f7ae42014-06-26 15:44:06 -0700178 '! which adb >/dev/null 2>&1 && '
179 '! grep -q moblab /etc/lsb-release',
Christopher Wileyfc3eac02013-11-21 16:24:57 -0800180 ignore_status=True, timeout=timeout)
beeps46dadc92013-11-07 14:07:10 -0800181 except (error.AutoservRunError, error.AutoservSSHTimeout):
182 return False
183 return result.exit_status == 0
184
185
186 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800187 def _extract_arguments(args_dict, key_subset):
188 """Extract options from `args_dict` and return a subset result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800189
190 Take the provided dictionary of argument options and return
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800191 a subset that represent standard arguments needed to construct
192 a test-assistant object (chameleon or servo) for a host. The
193 intent is to provide standard argument processing from
194 run_remote_tests for tests that require a test-assistant board
195 to operate.
196
197 @param args_dict Dictionary from which to extract the arguments.
198 @param key_subset Tuple of keys to extract from the args_dict, e.g.
199 ('servo_host', 'servo_port').
200 """
201 result = {}
202 for arg in key_subset:
203 if arg in args_dict:
204 result[arg] = args_dict[arg]
205 return result
206
207
208 @staticmethod
209 def get_chameleon_arguments(args_dict):
210 """Extract chameleon options from `args_dict` and return the result.
211
212 Recommended usage:
213 ~~~~~~~~
214 args_dict = utils.args_to_dict(args)
215 chameleon_args = hosts.CrosHost.get_chameleon_arguments(args_dict)
216 host = hosts.create_host(machine, chameleon_args=chameleon_args)
217 ~~~~~~~~
218
219 @param args_dict Dictionary from which to extract the chameleon
220 arguments.
221 """
222 return CrosHost._extract_arguments(
223 args_dict, ('chameleon_host', 'chameleon_port'))
224
225
226 @staticmethod
227 def get_servo_arguments(args_dict):
228 """Extract servo options from `args_dict` and return the result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800229
230 Recommended usage:
231 ~~~~~~~~
232 args_dict = utils.args_to_dict(args)
Fang Deng0ca40e22013-08-27 17:47:44 -0700233 servo_args = hosts.CrosHost.get_servo_arguments(args_dict)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800234 host = hosts.create_host(machine, servo_args=servo_args)
235 ~~~~~~~~
236
237 @param args_dict Dictionary from which to extract the servo
238 arguments.
239 """
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800240 return CrosHost._extract_arguments(
241 args_dict, ('servo_host', 'servo_port'))
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700242
J. Richard Barnette964fba02012-10-24 17:34:29 -0700243
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800244 def _initialize(self, hostname, chameleon_args=None, servo_args=None,
245 ssh_verbosity_flag='', ssh_options='',
Fang Dengd1c2b732013-08-20 12:59:46 -0700246 *args, **dargs):
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800247 """Initialize superclasses, |self.chameleon|, and |self.servo|.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700248
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800249 This method checks whether a chameleon/servo (aka
250 test-assistant objects) is required by checking whether
251 chameleon_args/servo_args is None. This method will only
252 attempt to create the test-assistant object when it is
253 required by the test.
Fang Deng5d518f42013-08-02 14:04:32 -0700254
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800255 For creating the test-assistant object, there are three
256 possibilities: First, if the host is a lab system known to have
257 a test-assistant board, we connect to that board unconditionally.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700258 Second, if we're called from a control file that requires
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800259 test-assistant features for testing, it will pass settings from
260 the arguments, like `servo_host`, `servo_port`. If neither of
261 these cases apply, the test-assistant object will be `None`.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700262
263 """
Fang Deng0ca40e22013-08-27 17:47:44 -0700264 super(CrosHost, self)._initialize(hostname=hostname,
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700265 *args, **dargs)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700266 # self.env is a dictionary of environment variable settings
267 # to be exported for commands run on the host.
268 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
269 # errors that might happen.
270 self.env['LIBC_FATAL_STDERR_'] = '1'
beeps32a63082013-08-22 14:02:29 -0700271 self._rpc_proxy_map = {}
Fang Dengd1c2b732013-08-20 12:59:46 -0700272 self._ssh_verbosity_flag = ssh_verbosity_flag
Aviv Keshetc5947fa2013-09-04 14:06:29 -0700273 self._ssh_options = ssh_options
Fang Deng5d518f42013-08-02 14:04:32 -0700274 # TODO(fdeng): We need to simplify the
275 # process of servo and servo_host initialization.
276 # crbug.com/298432
Dan Shi4d478522014-02-14 13:46:32 -0800277 self._servo_host = servo_host.create_servo_host(dut=self.hostname,
278 servo_args=servo_args)
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800279 # TODO(waihong): Do the simplication on Chameleon too.
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800280 self._chameleon_host = chameleon_host.create_chameleon_host(
281 dut=self.hostname, chameleon_args=chameleon_args)
282
Dan Shi4d478522014-02-14 13:46:32 -0800283 if self._servo_host is not None:
284 self.servo = self._servo_host.get_servo()
285 else:
286 self.servo = None
287
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800288 if self._chameleon_host:
Tom Wai-Hong Tameaee3402014-01-22 08:52:10 +0800289 self.chameleon = self._chameleon_host.create_chameleon_board()
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800290 else:
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800291 self.chameleon = None
Fang Deng5d518f42013-08-02 14:04:32 -0700292
293
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500294 def get_repair_image_name(self):
295 """Generate a image_name from variables in the global config.
296
297 @returns a str of $board-version/$BUILD.
298
299 """
300 stable_version = global_config.global_config.get_config_value(
301 'CROS', 'stable_cros_version')
302 build_pattern = global_config.global_config.get_config_value(
303 'CROS', 'stable_build_pattern')
304 board = self._get_board_from_afe()
305 if board is None:
306 raise error.AutoservError('DUT has no board attribute, '
307 'cannot be repaired.')
308 return build_pattern % (board, stable_version)
309
310
Scott Zawalski62bacae2013-03-05 10:40:32 -0500311 def _host_in_AFE(self):
312 """Check if the host is an object the AFE knows.
313
314 @returns the host object.
315 """
316 return self._AFE.get_hosts(hostname=self.hostname)
317
318
Chris Sosab76e0ee2013-05-22 16:55:41 -0700319 def lookup_job_repo_url(self):
320 """Looks up the job_repo_url for the host.
321
322 @returns job_repo_url from AFE or None if not found.
323
324 @raises KeyError if the host does not have a job_repo_url
325 """
326 if not self._host_in_AFE():
327 return None
328
329 hosts = self._AFE.get_hosts(hostname=self.hostname)
beepsb5efc532013-06-04 11:29:34 -0700330 if hosts and ds_constants.JOB_REPO_URL in hosts[0].attributes:
331 return hosts[0].attributes[ds_constants.JOB_REPO_URL]
Chris Sosab76e0ee2013-05-22 16:55:41 -0700332
333
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500334 def clear_cros_version_labels_and_job_repo_url(self):
335 """Clear cros_version labels and host attribute job_repo_url."""
Scott Zawalski62bacae2013-03-05 10:40:32 -0500336 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400337 return
338
Scott Zawalski62bacae2013-03-05 10:40:32 -0500339 host_list = [self.hostname]
340 labels = self._AFE.get_labels(
341 name__startswith=ds_constants.VERSION_PREFIX,
342 host__hostname=self.hostname)
Dan Shi0f466e82013-02-22 15:44:58 -0800343
Scott Zawalski62bacae2013-03-05 10:40:32 -0500344 for label in labels:
345 label.remove_hosts(hosts=host_list)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500346
beepscb6f1e22013-06-28 19:14:10 -0700347 self.update_job_repo_url(None, None)
348
349
350 def update_job_repo_url(self, devserver_url, image_name):
351 """
352 Updates the job_repo_url host attribute and asserts it's value.
353
354 @param devserver_url: The devserver to use in the job_repo_url.
355 @param image_name: The name of the image to use in the job_repo_url.
356
357 @raises AutoservError: If we failed to update the job_repo_url.
358 """
359 repo_url = None
360 if devserver_url and image_name:
361 repo_url = tools.get_package_url(devserver_url, image_name)
362 self._AFE.set_host_attribute(ds_constants.JOB_REPO_URL, repo_url,
Scott Zawalski62bacae2013-03-05 10:40:32 -0500363 hostname=self.hostname)
beepscb6f1e22013-06-28 19:14:10 -0700364 if self.lookup_job_repo_url() != repo_url:
365 raise error.AutoservError('Failed to update job_repo_url with %s, '
366 'host %s' % (repo_url, self.hostname))
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500367
368
Dan Shie9309262013-06-19 22:50:21 -0700369 def add_cros_version_labels_and_job_repo_url(self, image_name):
Scott Zawalskieadbf702013-03-14 09:23:06 -0400370 """Add cros_version labels and host attribute job_repo_url.
371
372 @param image_name: The name of the image e.g.
373 lumpy-release/R27-3837.0.0
Dan Shi7458bf62013-06-10 12:50:16 -0700374
Scott Zawalskieadbf702013-03-14 09:23:06 -0400375 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500376 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400377 return
Scott Zawalski62bacae2013-03-05 10:40:32 -0500378
Scott Zawalskieadbf702013-03-14 09:23:06 -0400379 cros_label = '%s%s' % (ds_constants.VERSION_PREFIX, image_name)
Dan Shie9309262013-06-19 22:50:21 -0700380 devserver_url = dev_server.ImageServer.resolve(image_name).url()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500381
382 labels = self._AFE.get_labels(name=cros_label)
383 if labels:
384 label = labels[0]
385 else:
386 label = self._AFE.create_label(name=cros_label)
387
388 label.add_hosts([self.hostname])
beepscb6f1e22013-06-28 19:14:10 -0700389 self.update_job_repo_url(devserver_url, image_name)
390
391
beepsdae65fd2013-07-26 16:24:41 -0700392 def verify_job_repo_url(self, tag=''):
beepscb6f1e22013-06-28 19:14:10 -0700393 """
394 Make sure job_repo_url of this host is valid.
395
joychen03eaad92013-06-26 09:55:21 -0700396 Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/\
beepscb6f1e22013-06-28 19:14:10 -0700397 lumpy-release/R29-4279.0.0/autotest/packages" claims to have the
398 autotest package for lumpy-release/R29-4279.0.0. If this isn't the case,
399 download and extract it. If the devserver embedded in the url is
400 unresponsive, update the job_repo_url of the host after staging it on
401 another devserver.
402
403 @param job_repo_url: A url pointing to the devserver where the autotest
404 package for this build should be staged.
beepsdae65fd2013-07-26 16:24:41 -0700405 @param tag: The tag from the server job, in the format
406 <job_id>-<user>/<hostname>, or <hostless> for a server job.
beepscb6f1e22013-06-28 19:14:10 -0700407
408 @raises DevServerException: If we could not resolve a devserver.
409 @raises AutoservError: If we're unable to save the new job_repo_url as
410 a result of choosing a new devserver because the old one failed to
411 respond to a health check.
beeps0c865032013-07-30 11:37:06 -0700412 @raises urllib2.URLError: If the devserver embedded in job_repo_url
413 doesn't respond within the timeout.
beepscb6f1e22013-06-28 19:14:10 -0700414 """
415 job_repo_url = self.lookup_job_repo_url()
416 if not job_repo_url:
417 logging.warning('No job repo url set on host %s', self.hostname)
418 return
419
420 logging.info('Verifying job repo url %s', job_repo_url)
421 devserver_url, image_name = tools.get_devserver_build_from_package_url(
422 job_repo_url)
423
beeps0c865032013-07-30 11:37:06 -0700424 ds = dev_server.ImageServer(devserver_url)
beepscb6f1e22013-06-28 19:14:10 -0700425
426 logging.info('Staging autotest artifacts for %s on devserver %s',
427 image_name, ds.url())
beeps687243d2013-07-18 15:29:27 -0700428
429 start_time = time.time()
beepscb6f1e22013-06-28 19:14:10 -0700430 ds.stage_artifacts(image_name, ['autotest'])
beeps687243d2013-07-18 15:29:27 -0700431 stage_time = time.time() - start_time
432
433 # Record how much of the verification time comes from a devserver
434 # restage. If we're doing things right we should not see multiple
435 # devservers for a given board/build/branch path.
436 try:
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800437 board, build_type, branch = server_utils.ParseBuildName(
beeps687243d2013-07-18 15:29:27 -0700438 image_name)[:3]
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800439 except server_utils.ParseBuildNameException:
beeps687243d2013-07-18 15:29:27 -0700440 pass
441 else:
beeps0c865032013-07-30 11:37:06 -0700442 devserver = devserver_url[
Chris Sosa65425082013-10-16 13:26:22 -0700443 devserver_url.find('/') + 2:devserver_url.rfind(':')]
beeps687243d2013-07-18 15:29:27 -0700444 stats_key = {
445 'board': board,
446 'build_type': build_type,
447 'branch': branch,
beeps0c865032013-07-30 11:37:06 -0700448 'devserver': devserver.replace('.', '_'),
beeps687243d2013-07-18 15:29:27 -0700449 }
450 stats.Gauge('verify_job_repo_url').send(
451 '%(board)s.%(build_type)s.%(branch)s.%(devserver)s' % stats_key,
452 stage_time)
beepscb6f1e22013-06-28 19:14:10 -0700453
Scott Zawalskieadbf702013-03-14 09:23:06 -0400454
Dan Shi0f466e82013-02-22 15:44:58 -0800455 def _try_stateful_update(self, update_url, force_update, updater):
456 """Try to use stateful update to initialize DUT.
457
458 When DUT is already running the same version that machine_install
459 tries to install, stateful update is a much faster way to clean up
460 the DUT for testing, compared to a full reimage. It is implemeted
461 by calling autoupdater.run_update, but skipping updating root, as
462 updating the kernel is time consuming and not necessary.
463
464 @param update_url: url of the image.
465 @param force_update: Set to True to update the image even if the DUT
466 is running the same version.
467 @param updater: ChromiumOSUpdater instance used to update the DUT.
468 @returns: True if the DUT was updated with stateful update.
469
470 """
J. Richard Barnette3f731032014-04-07 17:42:59 -0700471 # TODO(jrbarnette): Yes, I hate this re.match() test case.
472 # It's better than the alternative: see crbug.com/360944.
473 image_name = autoupdater.url_to_image_name(update_url)
474 release_pattern = r'^.*-release/R[0-9]+-[0-9]+\.[0-9]+\.0$'
475 if not re.match(release_pattern, image_name):
476 return False
Dan Shi0f466e82013-02-22 15:44:58 -0800477 if not updater.check_version():
478 return False
479 if not force_update:
480 logging.info('Canceling stateful update because the new and '
481 'old versions are the same.')
482 return False
483 # Following folders should be rebuilt after stateful update.
484 # A test file is used to confirm each folder gets rebuilt after
485 # the stateful update.
486 folders_to_check = ['/var', '/home', '/mnt/stateful_partition']
487 test_file = '.test_file_to_be_deleted'
488 for folder in folders_to_check:
489 touch_path = os.path.join(folder, test_file)
490 self.run('touch %s' % touch_path)
491
492 if not updater.run_update(force_update=True, update_root=False):
493 return False
494
495 # Reboot to complete stateful update.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700496 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Dan Shi0f466e82013-02-22 15:44:58 -0800497 check_file_cmd = 'test -f %s; echo $?'
498 for folder in folders_to_check:
499 test_file_path = os.path.join(folder, test_file)
500 result = self.run(check_file_cmd % test_file_path,
501 ignore_status=True)
502 if result.exit_status == 1:
503 return False
504 return True
505
506
J. Richard Barnette7275b612013-06-04 18:13:11 -0700507 def _post_update_processing(self, updater, expected_kernel=None):
Dan Shi0f466e82013-02-22 15:44:58 -0800508 """After the DUT is updated, confirm machine_install succeeded.
509
510 @param updater: ChromiumOSUpdater instance used to update the DUT.
J. Richard Barnette7275b612013-06-04 18:13:11 -0700511 @param expected_kernel: kernel expected to be active after reboot,
512 or `None` to skip rollback checking.
Dan Shi0f466e82013-02-22 15:44:58 -0800513
514 """
J. Richard Barnette7275b612013-06-04 18:13:11 -0700515 # Touch the lab machine file to leave a marker that
516 # distinguishes this image from other test images.
517 # Afterwards, we must re-run the autoreboot script because
518 # it depends on the _LAB_MACHINE_FILE.
Dan Shi0f466e82013-02-22 15:44:58 -0800519 self.run('touch %s' % self._LAB_MACHINE_FILE)
Dan Shi0f466e82013-02-22 15:44:58 -0800520 self.run('start autoreboot')
Chris Sosa65425082013-10-16 13:26:22 -0700521 updater.verify_boot_expectations(
522 expected_kernel, rollback_message=
523 'Build %s failed to boot on %s; system rolled back to previous'
524 'build' % (updater.update_version, self.hostname))
J. Richard Barnette7275b612013-06-04 18:13:11 -0700525 # Check that we've got the build we meant to install.
526 if not updater.check_version_to_confirm_install():
527 raise autoupdater.ChromiumOSError(
528 'Failed to update %s to build %s; found build '
529 '%s instead' % (self.hostname,
Chris Sosa65425082013-10-16 13:26:22 -0700530 updater.update_version,
531 updater.get_build_id()))
Dan Shi0f466e82013-02-22 15:44:58 -0800532
533
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700534 def _stage_image_for_update(self, image_name=None):
Scott Zawalskieadbf702013-03-14 09:23:06 -0400535 """Stage a build on a devserver and return the update_url.
536
537 @param image_name: a name like lumpy-release/R27-3837.0.0
538 @returns an update URL like:
539 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
540 """
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700541 if not image_name:
542 image_name = self.get_repair_image_name()
543 logging.info('Staging build for AU: %s', image_name)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400544 devserver = dev_server.ImageServer.resolve(image_name)
545 devserver.trigger_download(image_name, synchronous=False)
546 return tools.image_url_pattern() % (devserver.url(), image_name)
547
548
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700549 def stage_image_for_servo(self, image_name=None):
550 """Stage a build on a devserver and return the update_url.
551
552 @param image_name: a name like lumpy-release/R27-3837.0.0
553 @returns an update URL like:
554 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
555 """
556 if not image_name:
557 image_name = self.get_repair_image_name()
558 logging.info('Staging build for servo install: %s', image_name)
559 devserver = dev_server.ImageServer.resolve(image_name)
560 devserver.stage_artifacts(image_name, ['test_image'])
561 return devserver.get_test_image_url(image_name)
562
563
beepse539be02013-07-31 21:57:39 -0700564 def stage_factory_image_for_servo(self, image_name):
565 """Stage a build on a devserver and return the update_url.
566
567 @param image_name: a name like <baord>/4262.204.0
beeps12c0a3c2013-09-03 11:58:27 -0700568
beepse539be02013-07-31 21:57:39 -0700569 @return: An update URL, eg:
570 http://<devserver>/static/canary-channel/\
571 <board>/4262.204.0/factory_test/chromiumos_factory_image.bin
beeps12c0a3c2013-09-03 11:58:27 -0700572
573 @raises: ValueError if the factory artifact name is missing from
574 the config.
575
beepse539be02013-07-31 21:57:39 -0700576 """
577 if not image_name:
578 logging.error('Need an image_name to stage a factory image.')
579 return
580
beeps12c0a3c2013-09-03 11:58:27 -0700581 factory_artifact = global_config.global_config.get_config_value(
582 'CROS', 'factory_artifact', type=str, default='')
583 if not factory_artifact:
584 raise ValueError('Cannot retrieve the factory artifact name from '
585 'autotest config, and hence cannot stage factory '
586 'artifacts.')
587
beepse539be02013-07-31 21:57:39 -0700588 logging.info('Staging build for servo install: %s', image_name)
589 devserver = dev_server.ImageServer.resolve(image_name)
590 devserver.stage_artifacts(
591 image_name,
beeps12c0a3c2013-09-03 11:58:27 -0700592 [factory_artifact],
593 archive_url=None)
beepse539be02013-07-31 21:57:39 -0700594
595 return tools.factory_image_url_pattern() % (devserver.url(), image_name)
596
597
Chris Sosaa3ac2152012-05-23 22:23:13 -0700598 def machine_install(self, update_url=None, force_update=False,
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500599 local_devserver=False, repair=False):
600 """Install the DUT.
601
Dan Shi0f466e82013-02-22 15:44:58 -0800602 Use stateful update if the DUT is already running the same build.
603 Stateful update does not update kernel and tends to run much faster
604 than a full reimage. If the DUT is running a different build, or it
605 failed to do a stateful update, full update, including kernel update,
606 will be applied to the DUT.
607
Scott Zawalskieadbf702013-03-14 09:23:06 -0400608 Once a host enters machine_install its cros_version label will be
609 removed as well as its host attribute job_repo_url (used for
610 package install).
611
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500612 @param update_url: The url to use for the update
613 pattern: http://$devserver:###/update/$build
614 If update_url is None and repair is True we will install the
615 stable image listed in global_config under
616 CROS.stable_cros_version.
617 @param force_update: Force an update even if the version installed
618 is the same. Default:False
619 @param local_devserver: Used by run_remote_test to allow people to
620 use their local devserver. Default: False
621 @param repair: Whether or not we are in repair mode. This adds special
622 cases for repairing a machine like starting update_engine.
623 Setting repair to True sets force_update to True as well.
624 default: False
625 @raises autoupdater.ChromiumOSError
626
627 """
Dan Shi7458bf62013-06-10 12:50:16 -0700628 if update_url:
629 logging.debug('update url is set to %s', update_url)
630 else:
631 logging.debug('update url is not set, resolving...')
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700632 if self._parser.options.image:
633 requested_build = self._parser.options.image
634 if requested_build.startswith('http://'):
635 update_url = requested_build
Dan Shi7458bf62013-06-10 12:50:16 -0700636 logging.debug('update url is retrieved from requested_build'
637 ': %s', update_url)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700638 else:
639 # Try to stage any build that does not start with
640 # http:// on the devservers defined in
641 # global_config.ini.
Dan Shi7458bf62013-06-10 12:50:16 -0700642 update_url = self._stage_image_for_update(requested_build)
643 logging.debug('Build staged, and update_url is set to: %s',
644 update_url)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700645 elif repair:
646 update_url = self._stage_image_for_update()
Dan Shi7458bf62013-06-10 12:50:16 -0700647 logging.debug('Build staged, and update_url is set to: %s',
648 update_url)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400649 else:
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700650 raise autoupdater.ChromiumOSError(
651 'Update failed. No update URL provided.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500652
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500653 if repair:
Dan Shi0f466e82013-02-22 15:44:58 -0800654 # In case the system is in a bad state, we always reboot the machine
655 # before machine_install.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700656 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500657 self.run('stop update-engine; start update-engine')
658 force_update = True
Dan Shi0f466e82013-02-22 15:44:58 -0800659
Chris Sosaa3ac2152012-05-23 22:23:13 -0700660 updater = autoupdater.ChromiumOSUpdater(update_url, host=self,
Chris Sosa72312602013-04-16 15:01:56 -0700661 local_devserver=local_devserver)
Dan Shi0f466e82013-02-22 15:44:58 -0800662 updated = False
Scott Zawalskieadbf702013-03-14 09:23:06 -0400663 # Remove cros-version and job_repo_url host attribute from host.
664 self.clear_cros_version_labels_and_job_repo_url()
Dan Shi0f466e82013-02-22 15:44:58 -0800665 # If the DUT is already running the same build, try stateful update
666 # first. Stateful update does not update kernel and tends to run much
667 # faster than a full reimage.
668 try:
Chris Sosab76e0ee2013-05-22 16:55:41 -0700669 updated = self._try_stateful_update(
670 update_url, force_update, updater)
Dan Shi0f466e82013-02-22 15:44:58 -0800671 if updated:
672 logging.info('DUT is updated with stateful update.')
673 except Exception as e:
674 logging.exception(e)
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -0700675 logging.warning('Failed to stateful update DUT, force to update.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700676
Dan Shi0f466e82013-02-22 15:44:58 -0800677 inactive_kernel = None
678 # Do a full update if stateful update is not applicable or failed.
679 if not updated:
680 # In case the system is in a bad state, we always reboot the
681 # machine before machine_install.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700682 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Chris Sosab7612bc2013-03-21 10:32:37 -0700683
684 # TODO(sosa): Remove temporary hack to get rid of bricked machines
685 # that can't update due to a corrupted policy.
686 self.run('rm -rf /var/lib/whitelist')
687 self.run('touch /var/lib/whitelist')
688 self.run('chmod -w /var/lib/whitelist')
Scott Zawalskib550d5a2013-03-22 09:23:59 -0400689 self.run('stop update-engine; start update-engine')
Chris Sosab7612bc2013-03-21 10:32:37 -0700690
Dan Shi0f466e82013-02-22 15:44:58 -0800691 if updater.run_update(force_update):
692 updated = True
693 # Figure out active and inactive kernel.
694 active_kernel, inactive_kernel = updater.get_kernel_state()
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700695
Dan Shi0f466e82013-02-22 15:44:58 -0800696 # Ensure inactive kernel has higher priority than active.
697 if (updater.get_kernel_priority(inactive_kernel)
698 < updater.get_kernel_priority(active_kernel)):
699 raise autoupdater.ChromiumOSError(
700 'Update failed. The priority of the inactive kernel'
701 ' partition is less than that of the active kernel'
702 ' partition.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700703
Dan Shi0f466e82013-02-22 15:44:58 -0800704 # Updater has returned successfully; reboot the host.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700705 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700706
Dan Shi0f466e82013-02-22 15:44:58 -0800707 if updated:
708 self._post_update_processing(updater, inactive_kernel)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400709 image_name = autoupdater.url_to_image_name(update_url)
Dan Shie9309262013-06-19 22:50:21 -0700710 self.add_cros_version_labels_and_job_repo_url(image_name)
Simran Basi13fa1ba2013-03-04 10:56:47 -0800711
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700712 # Clean up any old autotest directories which may be lying around.
713 for path in global_config.global_config.get_config_value(
714 'AUTOSERV', 'client_autodir_paths', type=list):
715 self.run('rm -rf ' + path)
716
717
Dan Shi10e992b2013-08-30 11:02:59 -0700718 def show_update_engine_log(self):
719 """Output update engine log."""
720 logging.debug('Dumping %s', constants.UPDATE_ENGINE_LOG)
721 self.run('cat %s' % constants.UPDATE_ENGINE_LOG)
722
723
Richard Barnette82c35912012-11-20 10:09:10 -0800724 def _get_board_from_afe(self):
725 """Retrieve this host's board from its labels in the AFE.
726
727 Looks for a host label of the form "board:<board>", and
728 returns the "<board>" part of the label. `None` is returned
729 if there is not a single, unique label matching the pattern.
730
731 @returns board from label, or `None`.
732 """
Dan Shia1ecd5c2013-06-06 11:21:31 -0700733 return server_utils.get_board_from_afe(self.hostname, self._AFE)
Simran Basi833814b2013-01-29 13:13:43 -0800734
735
736 def get_build(self):
737 """Retrieve the current build for this Host from the AFE.
738
739 Looks through this host's labels in the AFE to determine its build.
740
741 @returns The current build or None if it could not find it or if there
742 were multiple build labels assigned to this host.
743 """
Dan Shia1ecd5c2013-06-06 11:21:31 -0700744 return server_utils.get_build_from_afe(self.hostname, self._AFE)
Richard Barnette82c35912012-11-20 10:09:10 -0800745
746
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500747 def _install_repair(self):
748 """Attempt to repair this host using upate-engine.
749
750 If the host is up, try installing the DUT with a stable
751 "repair" version of Chrome OS as defined in the global_config
752 under CROS.stable_cros_version.
753
Scott Zawalski62bacae2013-03-05 10:40:32 -0500754 @raises AutoservRepairMethodNA if the DUT is not reachable.
755 @raises ChromiumOSError if the install failed for some reason.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500756
757 """
758 if not self.is_up():
Scott Zawalski62bacae2013-03-05 10:40:32 -0500759 raise error.AutoservRepairMethodNA('DUT unreachable for install.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500760 logging.info('Attempting to reimage machine to repair image.')
761 try:
762 self.machine_install(repair=True)
Fang Dengd0672f32013-03-18 17:18:09 -0700763 except autoupdater.ChromiumOSError as e:
764 logging.exception(e)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500765 logging.info('Repair via install failed.')
Scott Zawalski62bacae2013-03-05 10:40:32 -0500766 raise
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500767
768
Dan Shi2c88eed2013-11-12 10:18:38 -0800769 def _install_repair_with_powerwash(self):
Dan Shi9cc48452013-11-12 12:39:26 -0800770 """Attempt to powerwash first then repair this host using update-engine.
Dan Shi2c88eed2013-11-12 10:18:38 -0800771
Dan Shi9cc48452013-11-12 12:39:26 -0800772 update-engine may fail due to a bad image. In such case, powerwash
773 may help to cleanup the DUT for update-engine to work again.
Dan Shi2c88eed2013-11-12 10:18:38 -0800774
775 @raises AutoservRepairMethodNA if the DUT is not reachable.
776 @raises ChromiumOSError if the install failed for some reason.
777
778 """
779 if not self.is_up():
780 raise error.AutoservRepairMethodNA('DUT unreachable for install.')
781
782 logging.info('Attempting to powerwash the DUT.')
783 self.run('echo "fast safe" > '
784 '/mnt/stateful_partition/factory_install_reset')
785 self.reboot(timeout=self.POWERWASH_BOOT_TIMEOUT, wait=True)
786 if not self.is_up():
Dan Shi9cc48452013-11-12 12:39:26 -0800787 logging.error('Powerwash failed. DUT did not come back after '
Dan Shi2c88eed2013-11-12 10:18:38 -0800788 'reboot.')
789 raise error.AutoservRepairFailure(
790 'DUT failed to boot from powerwash after %d seconds' %
791 self.POWERWASH_BOOT_TIMEOUT)
792
793 logging.info('Powerwash succeeded.')
794 self._install_repair()
795
796
beepsf079cfb2013-09-18 17:49:51 -0700797 def servo_install(self, image_url=None, usb_boot_timeout=USB_BOOT_TIMEOUT,
798 install_timeout=INSTALL_TIMEOUT):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500799 """
800 Re-install the OS on the DUT by:
801 1) installing a test image on a USB storage device attached to the Servo
802 board,
Richard Barnette03a0c132012-11-05 12:40:35 -0800803 2) booting that image in recovery mode, and then
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700804 3) installing the image with chromeos-install.
805
Scott Zawalski62bacae2013-03-05 10:40:32 -0500806 @param image_url: If specified use as the url to install on the DUT.
807 otherwise boot the currently staged image on the USB stick.
beepsf079cfb2013-09-18 17:49:51 -0700808 @param usb_boot_timeout: The usb_boot_timeout to use during reimage.
809 Factory images need a longer usb_boot_timeout than regular
810 cros images.
811 @param install_timeout: The timeout to use when installing the chromeos
812 image. Factory images need a longer install_timeout.
Richard Barnette03a0c132012-11-05 12:40:35 -0800813
Scott Zawalski62bacae2013-03-05 10:40:32 -0500814 @raises AutoservError if the image fails to boot.
Richard Barnette03a0c132012-11-05 12:40:35 -0800815 """
beepsf079cfb2013-09-18 17:49:51 -0700816
817 usb_boot_timer_key = ('servo_install.usb_boot_timeout_%s'
818 % usb_boot_timeout)
819 logging.info('Downloading image to USB, then booting from it. Usb boot '
820 'timeout = %s', usb_boot_timeout)
821 timer = stats.Timer(usb_boot_timer_key)
822 timer.start()
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700823 self.servo.install_recovery_image(image_url)
beepsf079cfb2013-09-18 17:49:51 -0700824 if not self.wait_up(timeout=usb_boot_timeout):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500825 raise error.AutoservRepairFailure(
826 'DUT failed to boot from USB after %d seconds' %
beepsf079cfb2013-09-18 17:49:51 -0700827 usb_boot_timeout)
828 timer.stop()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500829
beepsf079cfb2013-09-18 17:49:51 -0700830 install_timer_key = ('servo_install.install_timeout_%s'
831 % install_timeout)
832 timer = stats.Timer(install_timer_key)
833 timer.start()
834 logging.info('Installing image through chromeos-install.')
835 self.run('chromeos-install --yes', timeout=install_timeout)
836 timer.stop()
837
838 logging.info('Power cycling DUT through servo.')
Richard Barnette03a0c132012-11-05 12:40:35 -0800839 self.servo.power_long_press()
Fang Dengafb88142013-05-30 17:44:31 -0700840 self.servo.switch_usbkey('off')
J. Richard Barnettefbcc7122013-07-24 18:24:59 -0700841 # We *must* use power_on() here; on Parrot it's how we get
842 # out of recovery mode.
843 self.servo.get_power_state_controller().power_on()
beepsf079cfb2013-09-18 17:49:51 -0700844
845 logging.info('Waiting for DUT to come back up.')
Richard Barnette03a0c132012-11-05 12:40:35 -0800846 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
847 raise error.AutoservError('DUT failed to reboot installed '
848 'test image after %d seconds' %
Scott Zawalski62bacae2013-03-05 10:40:32 -0500849 self.BOOT_TIMEOUT)
850
851
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700852 def _servo_repair_reinstall(self):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500853 """Reinstall the DUT utilizing servo and a test image.
854
855 Re-install the OS on the DUT by:
856 1) installing a test image on a USB storage device attached to the Servo
857 board,
858 2) booting that image in recovery mode, and then
859 3) installing the image with chromeos-install.
860
Scott Zawalski62bacae2013-03-05 10:40:32 -0500861 @raises AutoservRepairMethodNA if the device does not have servo
862 support.
863
864 """
865 if not self.servo:
866 raise error.AutoservRepairMethodNA('Repair Reinstall NA: '
867 'DUT has no servo support.')
868
869 logging.info('Attempting to recovery servo enabled device with '
870 'servo_repair_reinstall')
871
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700872 image_url = self.stage_image_for_servo()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500873 self.servo_install(image_url)
874
875
876 def _servo_repair_power(self):
877 """Attempt to repair DUT using an attached Servo.
878
879 Attempt to power on the DUT via power_long_press.
880
881 @raises AutoservRepairMethodNA if the device does not have servo
882 support.
883 @raises AutoservRepairFailure if the repair fails for any reason.
884 """
885 if not self.servo:
886 raise error.AutoservRepairMethodNA('Repair Power NA: '
887 'DUT has no servo support.')
888
889 logging.info('Attempting to recover servo enabled device by '
890 'powering it off and on.')
891 self.servo.get_power_state_controller().power_off()
892 self.servo.get_power_state_controller().power_on()
893 if self.wait_up(self.BOOT_TIMEOUT):
894 return
895
896 raise error.AutoservRepairFailure('DUT did not boot after long_press.')
Richard Barnette03a0c132012-11-05 12:40:35 -0800897
898
Richard Barnette82c35912012-11-20 10:09:10 -0800899 def _powercycle_to_repair(self):
900 """Utilize the RPM Infrastructure to bring the host back up.
901
902 If the host is not up/repaired after the first powercycle we utilize
903 auto fallback to the last good install by powercycling and rebooting the
904 host 6 times.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500905
906 @raises AutoservRepairMethodNA if the device does not support remote
907 power.
908 @raises AutoservRepairFailure if the repair fails for any reason.
909
Richard Barnette82c35912012-11-20 10:09:10 -0800910 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500911 if not self.has_power():
912 raise error.AutoservRepairMethodNA('Device does not support power.')
913
Richard Barnette82c35912012-11-20 10:09:10 -0800914 logging.info('Attempting repair via RPM powercycle.')
915 failed_cycles = 0
916 self.power_cycle()
917 while not self.wait_up(timeout=self.BOOT_TIMEOUT):
918 failed_cycles += 1
919 if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS:
Scott Zawalski62bacae2013-03-05 10:40:32 -0500920 raise error.AutoservRepairFailure(
921 'Powercycled host %s %d times; device did not come back'
922 ' online.' % (self.hostname, failed_cycles))
Richard Barnette82c35912012-11-20 10:09:10 -0800923 self.power_cycle()
924 if failed_cycles == 0:
925 logging.info('Powercycling was successful first time.')
926 else:
927 logging.info('Powercycling was successful after %d failures.',
928 failed_cycles)
929
930
Prashanth B4d8184f2014-05-05 12:22:02 -0700931 def check_device(self):
932 """Check if a device is ssh-able, and if so, clean and verify it.
933
934 @raise AutoservSSHTimeout: If the ssh ping times out.
935 @raise AutoservSshPermissionDeniedError: If ssh ping fails due to
936 permissions.
937 @raise AutoservSshPingHostError: For other AutoservRunErrors during
938 ssh_ping.
939 @raises AutoservError: As appropriate, during cleanup and verify.
940 """
941 self.ssh_ping()
942 self.cleanup()
943 self.verify()
944
945
Richard Barnette82c35912012-11-20 10:09:10 -0800946 def repair_full(self):
947 """Repair a host for repair level NO_PROTECTION.
948
949 This overrides the base class function for repair; it does
950 not call back to the parent class, but instead offers a
951 simplified implementation based on the capabilities in the
952 Chrome OS test lab.
953
Fang Deng5d518f42013-08-02 14:04:32 -0700954 It first verifies and repairs servo if it is a DUT in CrOS
Fang Deng03590af2013-10-07 17:34:20 -0700955 lab and a servo is attached.
Fang Deng5d518f42013-08-02 14:04:32 -0700956
Prashanth B4d8184f2014-05-05 12:22:02 -0700957 If `self.check_device()` fails, the following procedures are
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700958 attempted:
959 1. Try to re-install to a known stable image using
960 auto-update.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500961 2. If there's a servo for the DUT, try to power the DUT off and
962 on.
963 3. If there's a servo for the DUT, try to re-install via
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700964 the servo.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500965 4. If the DUT can be power-cycled via RPM, try to repair
Richard Barnette82c35912012-11-20 10:09:10 -0800966 by power-cycling.
967
968 As with the parent method, the last operation performed on
Prashanth B4d8184f2014-05-05 12:22:02 -0700969 the DUT must be to call `self.check_device()`; If that call fails the
970 exception it raises is passed back to the caller.
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700971
Scott Zawalski62bacae2013-03-05 10:40:32 -0500972 @raises AutoservRepairTotalFailure if the repair process fails to
973 fix the DUT.
Fang Deng5d518f42013-08-02 14:04:32 -0700974 @raises ServoHostRepairTotalFailure if the repair process fails to
975 fix the servo host if one is attached to the DUT.
976 @raises AutoservSshPermissionDeniedError if it is unable
977 to ssh to the servo host due to permission error.
978
Richard Barnette82c35912012-11-20 10:09:10 -0800979 """
Dan Shi4d478522014-02-14 13:46:32 -0800980 if self._servo_host and not self.servo:
Fang Deng03590af2013-10-07 17:34:20 -0700981 try:
Dan Shi4d478522014-02-14 13:46:32 -0800982 self._servo_host.repair_full()
Fang Deng03590af2013-10-07 17:34:20 -0700983 except Exception as e:
Fang Deng03590af2013-10-07 17:34:20 -0700984 logging.error('Could not create a healthy servo: %s', e)
Dan Shi4d478522014-02-14 13:46:32 -0800985 self.servo = self._servo_host.get_servo()
Fang Deng5d518f42013-08-02 14:04:32 -0700986
Scott Zawalski62bacae2013-03-05 10:40:32 -0500987 # TODO(scottz): This should use something similar to label_decorator,
988 # but needs to be populated in order so DUTs are repaired with the
989 # least amount of effort.
Dan Shi849a1c42014-03-05 11:10:43 -0800990 repair_funcs = [self._servo_repair_power,
991 self._install_repair,
Dan Shi2c88eed2013-11-12 10:18:38 -0800992 self._install_repair_with_powerwash,
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700993 self._servo_repair_reinstall,
Scott Zawalski62bacae2013-03-05 10:40:32 -0500994 self._powercycle_to_repair]
995 errors = []
Simran Basie6130932013-10-01 14:07:52 -0700996 board = self._get_board_from_afe()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500997 for repair_func in repair_funcs:
998 try:
999 repair_func()
Prashanth B4d8184f2014-05-05 12:22:02 -07001000 self.check_device()
Simran Basie6130932013-10-01 14:07:52 -07001001 stats.Counter(
1002 '%s.SUCCEEDED' % repair_func.__name__).increment()
1003 if board:
1004 stats.Counter(
Dan Shib87c3aa2014-02-12 15:40:31 -08001005 '%s.%s.SUCCEEDED' % (repair_func.__name__,
Simran Basie6130932013-10-01 14:07:52 -07001006 board)).increment()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001007 return
Simran Basie6130932013-10-01 14:07:52 -07001008 except error.AutoservRepairMethodNA as e:
1009 stats.Counter(
1010 '%s.RepairNA' % repair_func.__name__).increment()
1011 if board:
1012 stats.Counter(
Dan Shib87c3aa2014-02-12 15:40:31 -08001013 '%s.%s.RepairNA' % (repair_func.__name__,
Simran Basie6130932013-10-01 14:07:52 -07001014 board)).increment()
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001015 logging.warning('Repair function NA: %s', e)
Simran Basie6130932013-10-01 14:07:52 -07001016 errors.append(str(e))
Scott Zawalski62bacae2013-03-05 10:40:32 -05001017 except Exception as e:
Simran Basie6130932013-10-01 14:07:52 -07001018 stats.Counter(
1019 '%s.FAILED' % repair_func.__name__).increment()
1020 if board:
1021 stats.Counter(
Dan Shib87c3aa2014-02-12 15:40:31 -08001022 '%s.%s.FAILED' % (repair_func.__name__,
Simran Basie6130932013-10-01 14:07:52 -07001023 board)).increment()
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001024 logging.warning('Failed to repair device: %s', e)
Scott Zawalski62bacae2013-03-05 10:40:32 -05001025 errors.append(str(e))
Scott Zawalski89c44dd2013-02-26 09:28:02 -05001026
Simran Basie6130932013-10-01 14:07:52 -07001027 stats.Counter('Full_Repair_Failed').increment()
1028 if board:
1029 stats.Counter(
1030 'Full_Repair_Failed.%s' % board).increment()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001031 raise error.AutoservRepairTotalFailure(
1032 'All attempts at repairing the device failed:\n%s' %
1033 '\n'.join(errors))
Richard Barnette82c35912012-11-20 10:09:10 -08001034
1035
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001036 def close(self):
beeps32a63082013-08-22 14:02:29 -07001037 self.rpc_disconnect_all()
Fang Deng0ca40e22013-08-27 17:47:44 -07001038 super(CrosHost, self).close()
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001039
1040
Simran Basi5e6339a2013-03-21 11:34:32 -07001041 def _cleanup_poweron(self):
1042 """Special cleanup method to make sure hosts always get power back."""
1043 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
1044 hosts = afe.get_hosts(hostname=self.hostname)
1045 if not hosts or not (self._RPM_OUTLET_CHANGED in
1046 hosts[0].attributes):
1047 return
1048 logging.debug('This host has recently interacted with the RPM'
1049 ' Infrastructure. Ensuring power is on.')
1050 try:
1051 self.power_on()
1052 except rpm_client.RemotePowerException:
1053 # If cleanup has completed but there was an issue with the RPM
1054 # Infrastructure, log an error message rather than fail cleanup
1055 logging.error('Failed to turn Power On for this host after '
1056 'cleanup through the RPM Infrastructure.')
1057 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None,
1058 hostname=self.hostname)
1059
1060
beepsc87ff602013-07-31 21:53:00 -07001061 def _is_factory_image(self):
1062 """Checks if the image on the DUT is a factory image.
1063
1064 @return: True if the image on the DUT is a factory image.
1065 False otherwise.
1066 """
1067 result = self.run('[ -f /root/.factory_test ]', ignore_status=True)
1068 return result.exit_status == 0
1069
1070
1071 def _restart_ui(self):
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001072 """Restart the Chrome UI.
beepsc87ff602013-07-31 21:53:00 -07001073
1074 @raises: FactoryImageCheckerException for factory images, since
1075 we cannot attempt to restart ui on them.
1076 error.AutoservRunError for any other type of error that
1077 occurs while restarting ui.
1078 """
1079 if self._is_factory_image():
1080 raise FactoryImageCheckerException('Cannot restart ui on factory '
1081 'images')
1082
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001083 # TODO(jrbarnette): The command to stop/start the ui job
1084 # should live inside cros_ui, too. However that would seem
1085 # to imply interface changes to the existing start()/restart()
1086 # functions, which is a bridge too far (for now).
1087 prompt = cros_ui.get_login_prompt_state(self)
1088 self.run('stop ui; start ui')
1089 cros_ui.wait_for_chrome_ready(prompt, self)
beepsc87ff602013-07-31 21:53:00 -07001090
1091
1092 def cleanup(self):
Richard Barnette82c35912012-11-20 10:09:10 -08001093 self.run('rm -f %s' % constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001094 try:
beepsc87ff602013-07-31 21:53:00 -07001095 self._restart_ui()
1096 except (error.AutotestRunError, error.AutoservRunError,
1097 FactoryImageCheckerException):
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001098 logging.warning('Unable to restart ui, rebooting device.')
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001099 # Since restarting the UI fails fall back to normal Autotest
1100 # cleanup routines, i.e. reboot the machine.
Fang Deng0ca40e22013-08-27 17:47:44 -07001101 super(CrosHost, self).cleanup()
Simran Basi5e6339a2013-03-21 11:34:32 -07001102 # Check if the rpm outlet was manipulated.
Simran Basid5e5e272012-09-24 15:23:59 -07001103 if self.has_power():
Simran Basi5e6339a2013-03-21 11:34:32 -07001104 self._cleanup_poweron()
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001105
1106
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001107 def reboot(self, **dargs):
1108 """
1109 This function reboots the site host. The more generic
1110 RemoteHost.reboot() performs sync and sleeps for 5
1111 seconds. This is not necessary for Chrome OS devices as the
1112 sync should be finished in a short time during the reboot
1113 command.
1114 """
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001115 if 'reboot_cmd' not in dargs:
Doug Anderson7d5aeb22014-02-27 15:12:17 -08001116 reboot_timeout = dargs.get('reboot_timeout', 10)
1117 dargs['reboot_cmd'] = ('((reboot & sleep %d; reboot -f &)'
1118 ' </dev/null >/dev/null 2>&1 &)' %
1119 reboot_timeout)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001120 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001121 if 'fastsync' not in dargs:
1122 dargs['fastsync'] = True
Michael Liangda8c60a2014-06-03 13:24:51 -07001123
Charlie Mooneya8e6dab2014-05-29 14:37:55 -07001124 # For purposes of logging reboot times:
1125 # Get the board name i.e. 'daisy_spring'
1126 dargs['board'] = self._get_board_from_afe()
Fang Deng0ca40e22013-08-27 17:47:44 -07001127 super(CrosHost, self).reboot(**dargs)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001128
1129
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001130 def suspend(self, **dargs):
1131 """
1132 This function suspends the site host.
1133 """
1134 suspend_time = dargs.get('suspend_time', 60)
1135 dargs['timeout'] = suspend_time
1136 if 'suspend_cmd' not in dargs:
1137 cmd = ' && '.join(['echo 0 > /sys/class/rtc/rtc0/wakealarm',
1138 'echo +%d > /sys/class/rtc/rtc0/wakealarm' % suspend_time,
1139 'powerd_dbus_suspend --delay=0 &'])
1140 dargs['suspend_cmd'] = ('(( %s )'
1141 '< /dev/null >/dev/null 2>&1 &)' % cmd)
1142 super(CrosHost, self).suspend(**dargs)
1143
1144
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001145 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001146 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001147
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001148 Tests for the following conditions:
1149 1. All conditions tested by the parent version of this
1150 function.
1151 2. Sufficient space in /mnt/stateful_partition.
Fang Deng6b05f5b2013-03-20 13:42:11 -07001152 3. Sufficient space in /mnt/stateful_partition/encrypted.
1153 4. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001154
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001155 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001156 super(CrosHost, self).verify_software()
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001157 self.check_diskspace(
1158 '/mnt/stateful_partition',
1159 global_config.global_config.get_config_value(
Fang Deng6b05f5b2013-03-20 13:42:11 -07001160 'SERVER', 'gb_diskspace_required', type=float,
1161 default=20.0))
Gaurav Shahe448af82014-06-19 15:18:59 -07001162 encrypted_stateful_path = '/mnt/stateful_partition/encrypted'
1163 # Not all targets build with encrypted stateful support.
1164 if self.path_exists(encrypted_stateful_path):
1165 self.check_diskspace(
1166 encrypted_stateful_path,
1167 global_config.global_config.get_config_value(
1168 'SERVER', 'gb_encrypted_diskspace_required', type=float,
1169 default=0.1))
beepsc87ff602013-07-31 21:53:00 -07001170
Prashanth B5d0a0512014-04-25 12:26:08 -07001171 services_status = self.run('status system-services').stdout
1172 if services_status != 'system-services start/running\n':
1173 raise error.AutoservError('Chrome failed to reach login. '
1174 'System services not running.')
1175
beepsc87ff602013-07-31 21:53:00 -07001176 # Factory images don't run update engine,
1177 # goofy controls dbus on these DUTs.
1178 if not self._is_factory_image():
1179 self.run('update_engine_client --status')
Scott Zawalskifbca4a92013-03-04 15:56:42 -05001180 # Makes sure python is present, loads and can use built in functions.
1181 # We have seen cases where importing cPickle fails with undefined
1182 # symbols in cPickle.so.
1183 self.run('python -c "import cPickle"')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001184
1185
Fang Deng96667ca2013-08-01 17:46:18 -07001186 def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None,
1187 connect_timeout=None, alive_interval=None):
1188 """Override default make_ssh_command to use options tuned for Chrome OS.
1189
1190 Tuning changes:
1191 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH
1192 connection failure. Consistency with remote_access.sh.
1193
1194 - ServerAliveInterval=180; which causes SSH to ping connection every
1195 180 seconds. In conjunction with ServerAliveCountMax ensures
1196 that if the connection dies, Autotest will bail out quickly.
1197 Originally tried 60 secs, but saw frequent job ABORTS where
1198 the test completed successfully.
1199
1200 - ServerAliveCountMax=3; consistency with remote_access.sh.
1201
1202 - ConnectAttempts=4; reduce flakiness in connection errors;
1203 consistency with remote_access.sh.
1204
1205 - UserKnownHostsFile=/dev/null; we don't care about the keys.
1206 Host keys change with every new installation, don't waste
1207 memory/space saving them.
1208
1209 - SSH protocol forced to 2; needed for ServerAliveInterval.
1210
1211 @param user User name to use for the ssh connection.
1212 @param port Port on the target host to use for ssh connection.
1213 @param opts Additional options to the ssh command.
1214 @param hosts_file Ignored.
1215 @param connect_timeout Ignored.
1216 @param alive_interval Ignored.
1217 """
Aviv Keshetc5947fa2013-09-04 14:06:29 -07001218 base_command = ('/usr/bin/ssh -a -x %s %s %s'
1219 ' -o StrictHostKeyChecking=no'
Fang Deng96667ca2013-08-01 17:46:18 -07001220 ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes'
1221 ' -o ConnectTimeout=30 -o ServerAliveInterval=180'
1222 ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4'
1223 ' -o Protocol=2 -l %s -p %d')
Aviv Keshetc5947fa2013-09-04 14:06:29 -07001224 return base_command % (self._ssh_verbosity_flag, self._ssh_options,
1225 opts, user, port)
Fang Deng96667ca2013-08-01 17:46:18 -07001226
1227
beeps32a63082013-08-22 14:02:29 -07001228 def _create_ssh_tunnel(self, port, local_port):
1229 """Create an ssh tunnel from local_port to port.
1230
1231 @param port: remote port on the host.
1232 @param local_port: local forwarding port.
1233
1234 @return: the tunnel process.
1235 """
1236 # Chrome OS on the target closes down most external ports
1237 # for security. We could open the port, but doing that
1238 # would conflict with security tests that check that only
1239 # expected ports are open. So, to get to the port on the
1240 # target we use an ssh tunnel.
1241 tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port)
1242 ssh_cmd = self.make_ssh_command(opts=tunnel_options)
1243 tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname)
1244 logging.debug('Full tunnel command: %s', tunnel_cmd)
1245 tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True)
1246 logging.debug('Started ssh tunnel, local = %d'
1247 ' remote = %d, pid = %d',
1248 local_port, port, tunnel_proc.pid)
1249 return tunnel_proc
1250
1251
Christopher Wileydd181852013-10-10 19:56:58 -07001252 def _setup_rpc(self, port, command_name, remote_pid=None):
beeps32a63082013-08-22 14:02:29 -07001253 """Sets up a tunnel process and performs rpc connection book keeping.
1254
1255 This method assumes that xmlrpc and jsonrpc never conflict, since
1256 we can only either have an xmlrpc or a jsonrpc server listening on
1257 a remote port. As such, it enforces a single proxy->remote port
1258 policy, i.e if one starts a jsonrpc proxy/server from port A->B,
1259 and then tries to start an xmlrpc proxy forwarded to the same port,
1260 the xmlrpc proxy will override the jsonrpc tunnel process, however:
1261
1262 1. None of the methods on the xmlrpc proxy will work because
1263 the server listening on B is jsonrpc.
1264
1265 2. The xmlrpc client cannot initiate a termination of the JsonRPC
1266 server, as the only use case currently is goofy, which is tied to
1267 the factory image. It is much easier to handle a failed xmlrpc
1268 call on the client than it is to terminate goofy in this scenario,
1269 as doing the latter might leave the DUT in a hard to recover state.
1270
1271 With the current implementation newer rpc proxy connections will
1272 terminate the tunnel processes of older rpc connections tunneling
1273 to the same remote port. If methods are invoked on the client
1274 after this has happened they will fail with connection closed errors.
1275
1276 @param port: The remote forwarding port.
1277 @param command_name: The name of the remote process, to terminate
1278 using pkill.
1279
1280 @return A url that we can use to initiate the rpc connection.
1281 """
1282 self.rpc_disconnect(port)
1283 local_port = utils.get_unused_port()
1284 tunnel_proc = self._create_ssh_tunnel(port, local_port)
Christopher Wileydd181852013-10-10 19:56:58 -07001285 self._rpc_proxy_map[port] = (command_name, tunnel_proc, remote_pid)
beeps32a63082013-08-22 14:02:29 -07001286 return self._RPC_PROXY_URL % local_port
1287
1288
Christopher Wileyd78249a2013-03-01 13:05:31 -08001289 def xmlrpc_connect(self, command, port, command_name=None,
Yusuf Mohsinallyfff89d62013-11-18 16:34:07 -08001290 ready_test_name=None, timeout_seconds=10,
1291 logfile='/dev/null'):
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001292 """Connect to an XMLRPC server on the host.
1293
1294 The `command` argument should be a simple shell command that
1295 starts an XMLRPC server on the given `port`. The command
1296 must not daemonize, and must terminate cleanly on SIGTERM.
1297 The command is started in the background on the host, and a
1298 local XMLRPC client for the server is created and returned
1299 to the caller.
1300
1301 Note that the process of creating an XMLRPC client makes no
1302 attempt to connect to the remote server; the caller is
1303 responsible for determining whether the server is running
1304 correctly, and is ready to serve requests.
1305
Christopher Wileyd78249a2013-03-01 13:05:31 -08001306 Optionally, the caller can pass ready_test_name, a string
1307 containing the name of a method to call on the proxy. This
1308 method should take no parameters and return successfully only
1309 when the server is ready to process client requests. When
1310 ready_test_name is set, xmlrpc_connect will block until the
1311 proxy is ready, and throw a TestError if the server isn't
1312 ready by timeout_seconds.
1313
beeps32a63082013-08-22 14:02:29 -07001314 If a server is already running on the remote port, this
1315 method will kill it and disconnect the tunnel process
1316 associated with the connection before establishing a new one,
1317 by consulting the rpc_proxy_map in rpc_disconnect.
1318
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001319 @param command Shell command to start the server.
1320 @param port Port number on which the server is expected to
1321 be serving.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001322 @param command_name String to use as input to `pkill` to
1323 terminate the XMLRPC server on the host.
Christopher Wileyd78249a2013-03-01 13:05:31 -08001324 @param ready_test_name String containing the name of a
1325 method defined on the XMLRPC server.
1326 @param timeout_seconds Number of seconds to wait
1327 for the server to become 'ready.' Will throw a
1328 TestFail error if server is not ready in time.
Yusuf Mohsinallyfff89d62013-11-18 16:34:07 -08001329 @param logfile Logfile to send output when running
1330 'command' argument.
Yusuf Mohsinally8d19e3c2013-11-21 14:25:45 -08001331
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001332 """
Christopher Wileyc14f06a2013-10-16 13:55:39 -07001333 # Clean up any existing state. If the caller is willing
1334 # to believe their server is down, we ought to clean up
1335 # any tunnels we might have sitting around.
1336 self.rpc_disconnect(port)
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001337 # Start the server on the host. Redirection in the command
1338 # below is necessary, because 'ssh' won't terminate until
1339 # background child processes close stdin, stdout, and
1340 # stderr.
Yusuf Mohsinallyfff89d62013-11-18 16:34:07 -08001341 remote_cmd = '%s </dev/null >%s 2>&1 & echo $!' % (command, logfile)
Christopher Wileydd181852013-10-10 19:56:58 -07001342 remote_pid = self.run(remote_cmd).stdout.rstrip('\n')
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001343 logging.debug('Started XMLRPC server on host %s, pid = %s',
1344 self.hostname, remote_pid)
1345
Christopher Wileydd181852013-10-10 19:56:58 -07001346 # Tunnel through SSH to be able to reach that remote port.
1347 rpc_url = self._setup_rpc(port, command_name, remote_pid=remote_pid)
Christopher Wileyd78249a2013-03-01 13:05:31 -08001348 proxy = xmlrpclib.ServerProxy(rpc_url, allow_none=True)
Christopher Wileydd181852013-10-10 19:56:58 -07001349
Christopher Wileyd78249a2013-03-01 13:05:31 -08001350 if ready_test_name is not None:
J. Richard Barnette13eb7c02013-03-07 12:06:29 -08001351 # retry.retry logs each attempt; calculate delay_sec to
1352 # keep log spam to a dull roar.
Christopher Wiley0ed712b2013-04-09 15:25:12 -07001353 @retry.retry((socket.error,
1354 xmlrpclib.ProtocolError,
1355 httplib.BadStatusLine),
Chris Sosa65425082013-10-16 13:26:22 -07001356 timeout_min=timeout_seconds / 60.0,
1357 delay_sec=min(max(timeout_seconds / 20.0, 0.1), 1))
Christopher Wileyd78249a2013-03-01 13:05:31 -08001358 def ready_test():
1359 """ Call proxy.ready_test_name(). """
1360 getattr(proxy, ready_test_name)()
1361 successful = False
1362 try:
1363 logging.info('Waiting %d seconds for XMLRPC server '
1364 'to start.', timeout_seconds)
1365 ready_test()
1366 successful = True
Christopher Wileyd78249a2013-03-01 13:05:31 -08001367 finally:
1368 if not successful:
1369 logging.error('Failed to start XMLRPC server.')
beeps32a63082013-08-22 14:02:29 -07001370 self.rpc_disconnect(port)
Christopher Wileyd78249a2013-03-01 13:05:31 -08001371 logging.info('XMLRPC server started successfully.')
1372 return proxy
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001373
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001374
Jason Abeleb6f924f2013-11-13 16:01:54 -08001375 def syslog(self, message, tag='autotest'):
1376 """Logs a message to syslog on host.
1377
1378 @param message String message to log into syslog
1379 @param tag String tag prefix for syslog
1380
1381 """
1382 self.run('logger -t "%s" "%s"' % (tag, message))
1383
1384
beeps32a63082013-08-22 14:02:29 -07001385 def jsonrpc_connect(self, port):
1386 """Creates a jsonrpc proxy connection through an ssh tunnel.
1387
1388 This method exists to facilitate communication with goofy (which is
1389 the default system manager on all factory images) and as such, leaves
1390 most of the rpc server sanity checking to the caller. Unlike
1391 xmlrpc_connect, this method does not facilitate the creation of a remote
1392 jsonrpc server, as the only clients of this code are factory tests,
1393 for which the goofy system manager is built in to the image and starts
1394 when the target boots.
1395
1396 One can theoretically create multiple jsonrpc proxies all forwarded
1397 to the same remote port, provided the remote port has an rpc server
1398 listening. However, in doing so we stand the risk of leaking an
1399 existing tunnel process, so we always disconnect any older tunnels
1400 we might have through rpc_disconnect.
1401
1402 @param port: port on the remote host that is serving this proxy.
1403
1404 @return: The client proxy.
1405 """
1406 if not jsonrpclib:
1407 logging.warning('Jsonrpclib could not be imported. Check that '
1408 'site-packages contains jsonrpclib.')
1409 return None
1410
1411 proxy = jsonrpclib.jsonrpc.ServerProxy(self._setup_rpc(port, None))
1412
1413 logging.info('Established a jsonrpc connection through port %s.', port)
1414 return proxy
1415
1416
1417 def rpc_disconnect(self, port):
1418 """Disconnect from an RPC server on the host.
1419
1420 Terminates the remote RPC server previously started for
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001421 the given `port`. Also closes the local ssh tunnel created
1422 for the connection to the host. This function does not
beeps32a63082013-08-22 14:02:29 -07001423 directly alter the state of a previously returned RPC
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001424 client object; however disconnection will cause all
1425 subsequent calls to methods on the object to fail.
1426
1427 This function does nothing if requested to disconnect a port
beeps32a63082013-08-22 14:02:29 -07001428 that was not previously connected via _setup_rpc.
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001429
1430 @param port Port number passed to a previous call to
beeps32a63082013-08-22 14:02:29 -07001431 `_setup_rpc()`.
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001432 """
beeps32a63082013-08-22 14:02:29 -07001433 if port not in self._rpc_proxy_map:
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001434 return
Christopher Wileydd181852013-10-10 19:56:58 -07001435 remote_name, tunnel_proc, remote_pid = self._rpc_proxy_map[port]
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001436 if remote_name:
1437 # We use 'pkill' to find our target process rather than
1438 # a PID, because the host may have rebooted since
1439 # connecting, and we don't want to kill an innocent
1440 # process with the same PID.
1441 #
1442 # 'pkill' helpfully exits with status 1 if no target
1443 # process is found, for which run() will throw an
Simran Basid5e5e272012-09-24 15:23:59 -07001444 # exception. We don't want that, so we the ignore
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001445 # status.
1446 self.run("pkill -f '%s'" % remote_name, ignore_status=True)
Christopher Wileydd181852013-10-10 19:56:58 -07001447 if remote_pid:
1448 logging.info('Waiting for RPC server "%s" shutdown',
1449 remote_name)
1450 start_time = time.time()
1451 while (time.time() - start_time <
1452 self._RPC_SHUTDOWN_TIMEOUT_SECONDS):
1453 running_processes = self.run(
1454 "pgrep -f '%s'" % remote_name,
1455 ignore_status=True).stdout.split()
1456 if not remote_pid in running_processes:
1457 logging.info('Shut down RPC server.')
1458 break
1459 time.sleep(self._RPC_SHUTDOWN_POLLING_PERIOD_SECONDS)
1460 else:
1461 raise error.TestError('Failed to shutdown RPC server %s' %
1462 remote_name)
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001463
1464 if tunnel_proc.poll() is None:
1465 tunnel_proc.terminate()
1466 logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid)
1467 else:
1468 logging.debug('Tunnel pid %d terminated early, status %d',
1469 tunnel_proc.pid, tunnel_proc.returncode)
beeps32a63082013-08-22 14:02:29 -07001470 del self._rpc_proxy_map[port]
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001471
1472
beeps32a63082013-08-22 14:02:29 -07001473 def rpc_disconnect_all(self):
1474 """Disconnect all known RPC proxy ports."""
1475 for port in self._rpc_proxy_map.keys():
1476 self.rpc_disconnect(port)
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001477
1478
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001479 def _ping_check_status(self, status):
1480 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001481
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001482 @param status Check the ping status against this value.
1483 @return True iff `status` and the result of ping are the same
1484 (i.e. both True or both False).
1485
1486 """
1487 ping_val = utils.ping(self.hostname, tries=1, deadline=1)
1488 return not (status ^ (ping_val == 0))
1489
1490 def _ping_wait_for_status(self, status, timeout):
1491 """Wait for the host to have a given status (UP or DOWN).
1492
1493 Status is checked by polling. Polling will not last longer
1494 than the number of seconds in `timeout`. The polling
1495 interval will be long enough that only approximately
1496 _PING_WAIT_COUNT polling cycles will be executed, subject
1497 to a maximum interval of about one minute.
1498
1499 @param status Waiting will stop immediately if `ping` of the
1500 host returns this status.
1501 @param timeout Poll for at most this many seconds.
1502 @return True iff the host status from `ping` matched the
1503 requested status at the time of return.
1504
1505 """
1506 # _ping_check_status() takes about 1 second, hence the
1507 # "- 1" in the formula below.
1508 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
1509 end_time = time.time() + timeout
1510 while time.time() <= end_time:
1511 if self._ping_check_status(status):
1512 return True
1513 if poll_interval > 0:
1514 time.sleep(poll_interval)
1515
1516 # The last thing we did was sleep(poll_interval), so it may
1517 # have been too long since the last `ping`. Check one more
1518 # time, just to be sure.
1519 return self._ping_check_status(status)
1520
1521 def ping_wait_up(self, timeout):
1522 """Wait for the host to respond to `ping`.
1523
1524 N.B. This method is not a reliable substitute for
1525 `wait_up()`, because a host that responds to ping will not
1526 necessarily respond to ssh. This method should only be used
1527 if the target DUT can be considered functional even if it
1528 can't be reached via ssh.
1529
1530 @param timeout Minimum time to allow before declaring the
1531 host to be non-responsive.
1532 @return True iff the host answered to ping before the timeout.
1533
1534 """
1535 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001536
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001537 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001538 """Wait until the host no longer responds to `ping`.
1539
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001540 This function can be used as a slightly faster version of
1541 `wait_down()`, by avoiding potentially long ssh timeouts.
1542
1543 @param timeout Minimum time to allow for the host to become
1544 non-responsive.
1545 @return True iff the host quit answering ping before the
1546 timeout.
1547
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001548 """
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001549 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001550
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001551 def test_wait_for_sleep(self, sleep_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001552 """Wait for the client to enter low-power sleep mode.
1553
1554 The test for "is asleep" can't distinguish a system that is
1555 powered off; to confirm that the unit was asleep, it is
1556 necessary to force resume, and then call
1557 `test_wait_for_resume()`.
1558
1559 This function is expected to be called from a test as part
1560 of a sequence like the following:
1561
1562 ~~~~~~~~
1563 boot_id = host.get_boot_id()
1564 # trigger sleep on the host
1565 host.test_wait_for_sleep()
1566 # trigger resume on the host
1567 host.test_wait_for_resume(boot_id)
1568 ~~~~~~~~
1569
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001570 @param sleep_timeout time limit in seconds to allow the host sleep.
1571
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001572 @exception TestFail The host did not go to sleep within
1573 the allowed time.
1574 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001575 if sleep_timeout is None:
1576 sleep_timeout = self.SLEEP_TIMEOUT
1577
1578 if not self.ping_wait_down(timeout=sleep_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001579 raise error.TestFail(
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001580 'client failed to sleep after %d seconds' % sleep_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001581
1582
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001583 def test_wait_for_resume(self, old_boot_id, resume_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001584 """Wait for the client to resume from low-power sleep mode.
1585
1586 The `old_boot_id` parameter should be the value from
1587 `get_boot_id()` obtained prior to entering sleep mode. A
1588 `TestFail` exception is raised if the boot id changes.
1589
1590 See @ref test_wait_for_sleep for more on this function's
1591 usage.
1592
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001593 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001594 target host went to sleep.
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001595 @param resume_timeout time limit in seconds to allow the host up.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001596
1597 @exception TestFail The host did not respond within the
1598 allowed time.
1599 @exception TestFail The host responded, but the boot id test
1600 indicated a reboot rather than a sleep
1601 cycle.
1602 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001603 if resume_timeout is None:
1604 resume_timeout = self.RESUME_TIMEOUT
1605
1606 if not self.wait_up(timeout=resume_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001607 raise error.TestFail(
1608 'client failed to resume from sleep after %d seconds' %
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001609 resume_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001610 else:
1611 new_boot_id = self.get_boot_id()
1612 if new_boot_id != old_boot_id:
1613 raise error.TestFail(
1614 'client rebooted, but sleep was expected'
1615 ' (old boot %s, new boot %s)'
1616 % (old_boot_id, new_boot_id))
1617
1618
1619 def test_wait_for_shutdown(self):
1620 """Wait for the client to shut down.
1621
1622 The test for "has shut down" can't distinguish a system that
1623 is merely asleep; to confirm that the unit was down, it is
1624 necessary to force boot, and then call test_wait_for_boot().
1625
1626 This function is expected to be called from a test as part
1627 of a sequence like the following:
1628
1629 ~~~~~~~~
1630 boot_id = host.get_boot_id()
1631 # trigger shutdown on the host
1632 host.test_wait_for_shutdown()
1633 # trigger boot on the host
1634 host.test_wait_for_boot(boot_id)
1635 ~~~~~~~~
1636
1637 @exception TestFail The host did not shut down within the
1638 allowed time.
1639 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001640 if not self.ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001641 raise error.TestFail(
1642 'client failed to shut down after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001643 self.SHUTDOWN_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001644
1645
1646 def test_wait_for_boot(self, old_boot_id=None):
1647 """Wait for the client to boot from cold power.
1648
1649 The `old_boot_id` parameter should be the value from
1650 `get_boot_id()` obtained prior to shutting down. A
1651 `TestFail` exception is raised if the boot id does not
1652 change. The boot id test is omitted if `old_boot_id` is not
1653 specified.
1654
1655 See @ref test_wait_for_shutdown for more on this function's
1656 usage.
1657
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001658 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001659 shut down.
1660
1661 @exception TestFail The host did not respond within the
1662 allowed time.
1663 @exception TestFail The host responded, but the boot id test
1664 indicated that there was no reboot.
1665 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001666 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001667 raise error.TestFail(
1668 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001669 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001670 elif old_boot_id:
1671 if self.get_boot_id() == old_boot_id:
1672 raise error.TestFail(
1673 'client is back up, but did not reboot'
1674 ' (boot %s)' % old_boot_id)
Simran Basid5e5e272012-09-24 15:23:59 -07001675
1676
1677 @staticmethod
1678 def check_for_rpm_support(hostname):
1679 """For a given hostname, return whether or not it is powered by an RPM.
1680
Simran Basi1df55112013-09-06 11:25:09 -07001681 @param hostname: hostname to check for rpm support.
1682
Simran Basid5e5e272012-09-24 15:23:59 -07001683 @return None if this host does not follows the defined naming format
1684 for RPM powered DUT's in the lab. If it does follow the format,
1685 it returns a regular expression MatchObject instead.
1686 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001687 return re.match(CrosHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001688
1689
1690 def has_power(self):
1691 """For this host, return whether or not it is powered by an RPM.
1692
1693 @return True if this host is in the CROS lab and follows the defined
1694 naming format.
1695 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001696 return CrosHost.check_for_rpm_support(self.hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001697
1698
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001699 def _set_power(self, state, power_method):
1700 """Sets the power to the host via RPM, Servo or manual.
1701
1702 @param state Specifies which power state to set to DUT
1703 @param power_method Specifies which method of power control to
1704 use. By default "RPM" will be used. Valid values
1705 are the strings "RPM", "manual", "servoj10".
1706
1707 """
1708 ACCEPTABLE_STATES = ['ON', 'OFF']
1709
1710 if state.upper() not in ACCEPTABLE_STATES:
1711 raise error.TestError('State must be one of: %s.'
1712 % (ACCEPTABLE_STATES,))
1713
1714 if power_method == self.POWER_CONTROL_SERVO:
1715 logging.info('Setting servo port J10 to %s', state)
1716 self.servo.set('prtctl3_pwren', state.lower())
1717 time.sleep(self._USB_POWER_TIMEOUT)
1718 elif power_method == self.POWER_CONTROL_MANUAL:
1719 logging.info('You have %d seconds to set the AC power to %s.',
1720 self._POWER_CYCLE_TIMEOUT, state)
1721 time.sleep(self._POWER_CYCLE_TIMEOUT)
1722 else:
1723 if not self.has_power():
1724 raise error.TestFail('DUT does not have RPM connected.')
Simran Basi5e6339a2013-03-21 11:34:32 -07001725 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
1726 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, True,
1727 hostname=self.hostname)
Simran Basi1df55112013-09-06 11:25:09 -07001728 rpm_client.set_power(self.hostname, state.upper(), timeout_mins=5)
Simran Basid5e5e272012-09-24 15:23:59 -07001729
1730
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001731 def power_off(self, power_method=POWER_CONTROL_RPM):
1732 """Turn off power to this host via RPM, Servo or manual.
1733
1734 @param power_method Specifies which method of power control to
1735 use. By default "RPM" will be used. Valid values
1736 are the strings "RPM", "manual", "servoj10".
1737
1738 """
1739 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07001740
1741
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001742 def power_on(self, power_method=POWER_CONTROL_RPM):
1743 """Turn on power to this host via RPM, Servo or manual.
1744
1745 @param power_method Specifies which method of power control to
1746 use. By default "RPM" will be used. Valid values
1747 are the strings "RPM", "manual", "servoj10".
1748
1749 """
1750 self._set_power('ON', power_method)
1751
1752
1753 def power_cycle(self, power_method=POWER_CONTROL_RPM):
1754 """Cycle power to this host by turning it OFF, then ON.
1755
1756 @param power_method Specifies which method of power control to
1757 use. By default "RPM" will be used. Valid values
1758 are the strings "RPM", "manual", "servoj10".
1759
1760 """
1761 if power_method in (self.POWER_CONTROL_SERVO,
1762 self.POWER_CONTROL_MANUAL):
1763 self.power_off(power_method=power_method)
1764 time.sleep(self._POWER_CYCLE_TIMEOUT)
1765 self.power_on(power_method=power_method)
1766 else:
1767 rpm_client.set_power(self.hostname, 'CYCLE')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001768
1769
1770 def get_platform(self):
1771 """Determine the correct platform label for this host.
1772
1773 @returns a string representing this host's platform.
1774 """
1775 crossystem = utils.Crossystem(self)
1776 crossystem.init()
1777 # Extract fwid value and use the leading part as the platform id.
1778 # fwid generally follow the format of {platform}.{firmware version}
1779 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
1780 platform = crossystem.fwid().split('.')[0].lower()
1781 # Newer platforms start with 'Google_' while the older ones do not.
1782 return platform.replace('google_', '')
1783
1784
Hung-ying Tyanb1328032014-04-01 14:18:54 +08001785 def get_architecture(self):
1786 """Determine the correct architecture label for this host.
1787
1788 @returns a string representing this host's architecture.
1789 """
1790 crossystem = utils.Crossystem(self)
1791 crossystem.init()
1792 return crossystem.arch()
1793
1794
Luis Lozano40b7d0d2014-01-17 15:12:06 -08001795 def get_chrome_version(self):
1796 """Gets the Chrome version number and milestone as strings.
1797
1798 Invokes "chrome --version" to get the version number and milestone.
1799
1800 @return A tuple (chrome_ver, milestone) where "chrome_ver" is the
1801 current Chrome version number as a string (in the form "W.X.Y.Z")
1802 and "milestone" is the first component of the version number
1803 (the "W" from "W.X.Y.Z"). If the version number cannot be parsed
1804 in the "W.X.Y.Z" format, the "chrome_ver" will be the full output
1805 of "chrome --version" and the milestone will be the empty string.
1806
1807 """
1808 version_string = self.run(constants.CHROME_VERSION_COMMAND).stdout
1809 return utils.parse_chrome_version(version_string)
1810
Aviv Keshet74c89a92013-02-04 15:18:30 -08001811 @label_decorator()
Simran Basic6f1f7a2012-10-16 10:47:46 -07001812 def get_board(self):
1813 """Determine the correct board label for this host.
1814
1815 @returns a string representing this host's board.
1816 """
1817 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
1818 run_method=self.run)
1819 board = release_info['CHROMEOS_RELEASE_BOARD']
1820 # Devices in the lab generally have the correct board name but our own
1821 # development devices have {board_name}-signed-{key_type}. The board
1822 # name may also begin with 'x86-' which we need to keep.
Simran Basi833814b2013-01-29 13:13:43 -08001823 board_format_string = ds_constants.BOARD_PREFIX + '%s'
Simran Basic6f1f7a2012-10-16 10:47:46 -07001824 if 'x86' not in board:
Simran Basi833814b2013-01-29 13:13:43 -08001825 return board_format_string % board.split('-')[0]
1826 return board_format_string % '-'.join(board.split('-')[0:2])
Simran Basic6f1f7a2012-10-16 10:47:46 -07001827
1828
Aviv Keshet74c89a92013-02-04 15:18:30 -08001829 @label_decorator('lightsensor')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001830 def has_lightsensor(self):
1831 """Determine the correct board label for this host.
1832
1833 @returns the string 'lightsensor' if this host has a lightsensor or
1834 None if it does not.
1835 """
1836 search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % (
Richard Barnette82c35912012-11-20 10:09:10 -08001837 self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES))
Simran Basic6f1f7a2012-10-16 10:47:46 -07001838 try:
1839 # Run the search cmd following the symlinks. Stderr_tee is set to
1840 # None as there can be a symlink loop, but the command will still
1841 # execute correctly with a few messages printed to stderr.
1842 self.run(search_cmd, stdout_tee=None, stderr_tee=None)
1843 return 'lightsensor'
1844 except error.AutoservRunError:
1845 # egrep exited with a return code of 1 meaning none of the possible
1846 # lightsensor files existed.
1847 return None
1848
1849
Aviv Keshet74c89a92013-02-04 15:18:30 -08001850 @label_decorator('bluetooth')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001851 def has_bluetooth(self):
1852 """Determine the correct board label for this host.
1853
1854 @returns the string 'bluetooth' if this host has bluetooth or
1855 None if it does not.
1856 """
1857 try:
1858 self.run('test -d /sys/class/bluetooth/hci0')
1859 # test exited with a return code of 0.
1860 return 'bluetooth'
1861 except error.AutoservRunError:
1862 # test exited with a return code 1 meaning the directory did not
1863 # exist.
1864 return None
1865
1866
Ilja Friedel0ce0b602013-08-15 18:45:27 -07001867 @label_decorator('graphics')
1868 def get_graphics(self):
1869 """
1870 Determine the correct board label for this host.
1871
1872 @returns a string representing this host's graphics. For now ARM boards
1873 return graphics:gles while all other boards return graphics:gl. This
1874 may change over time, but for robustness reasons this should avoid
1875 executing code in actual graphics libraries (which may not be ready and
1876 is tested by graphics_GLAPICheck).
1877 """
1878 uname = self.run('uname -a').stdout.lower()
1879 if 'arm' in uname:
1880 return 'graphics:gles'
1881 return 'graphics:gl'
1882
1883
Bill Richardson4f595f52014-02-13 16:20:26 -08001884 @label_decorator('ec')
1885 def get_ec(self):
1886 """
1887 Determine the type of EC on this host.
1888
1889 @returns a string representing this host's embedded controller type.
1890 At present, it only returns "ec:cros", for Chrome OS ECs. Other types
1891 of EC (or none) don't return any strings, since no tests depend on
1892 those.
1893 """
1894 cmd = 'mosys ec info'
1895 # The output should look like these, so that the last field should
1896 # match our EC version scheme:
1897 #
1898 # stm | stm32f100 | snow_v1.3.139-375eb9f
1899 # ti | Unknown-10de | peppy_v1.5.114-5d52788
1900 #
1901 # Non-Chrome OS ECs will look like these:
1902 #
1903 # ENE | KB932 | 00BE107A00
1904 # ite | it8518 | 3.08
1905 #
1906 # And some systems don't have ECs at all (Lumpy, for example).
1907 regexp = r'^.*\|\s*(\S+_v\d+\.\d+\.\d+-[0-9a-f]+)\s*$'
1908
1909 ecinfo = self.run(command=cmd, ignore_status=True)
1910 if ecinfo.exit_status == 0:
1911 res = re.search(regexp, ecinfo.stdout)
1912 if res:
1913 logging.info("EC version is %s", res.groups()[0])
1914 return 'ec:cros'
1915 logging.info("%s got: %s", cmd, ecinfo.stdout)
1916 # Has an EC, but it's not a Chrome OS EC
1917 return None
1918 logging.info("%s exited with status %d", cmd, ecinfo.exit_status)
1919 # No EC present
1920 return None
1921
1922
Alec Berg31b932b2014-04-04 16:09:11 -07001923 @label_decorator('accels')
1924 def get_accels(self):
1925 """
1926 Determine the type of accelerometers on this host.
1927
1928 @returns a string representing this host's accelerometer type.
1929 At present, it only returns "accel:cros-ec", for accelerometers
1930 attached to a Chrome OS EC, or none, if no accelerometers.
1931 """
1932 # Check to make sure we have ectool
1933 rv = self.run('which ectool', ignore_status=True)
1934 if rv.exit_status:
1935 logging.info("No ectool cmd found, assuming no EC accelerometers")
1936 return None
1937
1938 # Check that the EC supports the motionsense command
1939 rv = self.run('ectool motionsense', ignore_status=True)
1940 if rv.exit_status:
1941 logging.info("EC does not support motionsense command "
1942 "assuming no EC accelerometers")
1943 return None
1944
1945 # Check that EC motion sensors are active
1946 active = self.run('ectool motionsense active').stdout.split('\n')
1947 if active[0] == "0":
1948 logging.info("Motion sense inactive, assuming no EC accelerometers")
1949 return None
1950
1951 logging.info("EC accelerometers found")
1952 return 'accel:cros-ec'
1953
1954
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +08001955 @label_decorator('chameleon')
1956 def has_chameleon(self):
1957 """Determine if a Chameleon connected to this host.
1958
1959 @returns the string 'chameleon' if this host has a Chameleon or
1960 None if it has not.
1961 """
1962 if self._chameleon_host:
1963 return 'chameleon'
1964 else:
1965 return None
1966
1967
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07001968 @label_decorator('storage')
1969 def get_storage(self):
1970 """
1971 Determine the type of boot device for this host.
1972
1973 Determine if the internal device is SCSI or dw_mmc device.
1974 Then check that it is SSD or HDD or eMMC or something else.
1975
1976 @returns a string representing this host's internal device type.
1977 'storage:ssd' when internal device is solid state drive
1978 'storage:hdd' when internal device is hard disk drive
1979 'storage:mmc' when internal device is mmc drive
1980 None When internal device is something else or
1981 when we are unable to determine the type
1982 """
1983 # The output should be /dev/mmcblk* for SD/eMMC or /dev/sd* for scsi
1984 rootdev_cmd = ' '.join(['. /usr/sbin/write_gpt.sh;',
1985 '. /usr/share/misc/chromeos-common.sh;',
1986 'load_base_vars;',
1987 'get_fixed_dst_drive'])
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07001988 rootdev = self.run(command=rootdev_cmd, ignore_status=True)
1989 if rootdev.exit_status:
1990 logging.info("Fail to run %s", rootdev_cmd)
1991 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07001992 rootdev_str = rootdev.stdout.strip()
1993
1994 if not rootdev_str:
1995 return None
1996
1997 rootdev_base = os.path.basename(rootdev_str)
1998
1999 mmc_pattern = '/dev/mmcblk[0-9]'
2000 if re.match(mmc_pattern, rootdev_str):
2001 # Use type to determine if the internal device is eMMC or somthing
2002 # else. We can assume that MMC is always an internal device.
2003 type_cmd = 'cat /sys/block/%s/device/type' % rootdev_base
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002004 type = self.run(command=type_cmd, ignore_status=True)
2005 if type.exit_status:
2006 logging.info("Fail to run %s", type_cmd)
2007 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002008 type_str = type.stdout.strip()
2009
2010 if type_str == 'MMC':
2011 return 'storage:mmc'
2012
2013 scsi_pattern = '/dev/sd[a-z]+'
2014 if re.match(scsi_pattern, rootdev.stdout):
2015 # Read symlink for /sys/block/sd* to determine if the internal
2016 # device is connected via ata or usb.
2017 link_cmd = 'readlink /sys/block/%s' % rootdev_base
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002018 link = self.run(command=link_cmd, ignore_status=True)
2019 if link.exit_status:
2020 logging.info("Fail to run %s", link_cmd)
2021 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002022 link_str = link.stdout.strip()
2023 if 'usb' in link_str:
2024 return None
2025
2026 # Read rotation to determine if the internal device is ssd or hdd.
2027 rotate_cmd = str('cat /sys/block/%s/queue/rotational'
2028 % rootdev_base)
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002029 rotate = self.run(command=rotate_cmd, ignore_status=True)
2030 if rotate.exit_status:
2031 logging.info("Fail to run %s", rotate_cmd)
2032 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002033 rotate_str = rotate.stdout.strip()
2034
2035 rotate_dict = {'0':'storage:ssd', '1':'storage:hdd'}
2036 return rotate_dict.get(rotate_str)
2037
2038 # All other internal device / error case will always fall here
2039 return None
2040
2041
Dan Shi4e9a2aa2014-03-24 14:28:42 -07002042 @label_decorator('servo')
2043 def get_servo(self):
2044 """Determine if the host has a servo attached.
2045
2046 If the host has a working servo attached, it should have a servo label.
2047
2048 @return: string 'servo' if the host has servo attached. Otherwise,
2049 returns None.
2050 """
2051 return 'servo' if self._servo_host else None
2052
2053
Dan Shi5beba472014-05-28 22:46:07 -07002054 @label_decorator('video_labels')
2055 def get_video_labels(self):
2056 """Run /usr/local/bin/avtest_label_detect to get a list of video labels.
2057
2058 Sample output of avtest_label_detect:
2059 Detected label: hw_video_acc_vp8
2060 Detected label: webcam
2061
2062 @return: A list of labels detected by tool avtest_label_detect.
2063 """
2064 try:
2065 result = self.run('/usr/local/bin/avtest_label_detect').stdout
2066 return re.findall('^Detected label: (\w+)$', result, re.M)
2067 except error.AutoservRunError:
2068 # The tool is not installed.
2069 return []
2070
2071
mussa584b4462014-06-20 15:13:28 -07002072 @label_decorator('video_glitch_detection')
2073 def is_video_glitch_detection_supported(self):
2074 """ Determine if a board under test is supported for video glitch
2075 detection tests.
2076
2077 @return: 'video_glitch_detection' if board is supported, None otherwise.
2078 """
2079 parser = ConfigParser.SafeConfigParser()
2080 filename = os.path.join(
2081 common.autotest_dir, 'client/cros/video/device_spec.conf')
2082
2083 dut = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
2084
2085 try:
2086 parser.read(filename)
2087
2088 supported_boards = parser.sections()
2089
2090 if dut in supported_boards:
2091 return 'video_glitch_detection'
2092 else:
2093 return None
2094
2095 except ConfigParser.error:
2096 # something went wrong while parsing the conf file
2097 return None
2098
Katherine Threlkeld7b97a9f2014-06-24 13:47:14 -07002099 @label_decorator('touch_labels')
2100 def get_touch(self):
2101 """
2102 Determine whether board under test has a touchpad or touchscreen.
2103
2104 @return: A list of some combination of 'touchscreen' and 'touchpad',
2105 depending on what is present on the device.
2106 """
2107 labels = []
2108 input_cmd = '/opt/google/input/inputcontrol --names -t %s'
2109 for elt in ['touchpad', 'touchscreen']:
2110 if self.run(input_cmd % elt).stdout:
2111 labels.append(elt)
2112 return labels
2113
2114
mussa584b4462014-06-20 15:13:28 -07002115
Simran Basic6f1f7a2012-10-16 10:47:46 -07002116 def get_labels(self):
2117 """Return a list of labels for this given host.
2118
2119 This is the main way to retrieve all the automatic labels for a host
2120 as it will run through all the currently implemented label functions.
2121 """
2122 labels = []
Richard Barnette82c35912012-11-20 10:09:10 -08002123 for label_function in self._LABEL_FUNCTIONS:
Simran Basic6f1f7a2012-10-16 10:47:46 -07002124 label = label_function(self)
2125 if label:
Dan Shi5beba472014-05-28 22:46:07 -07002126 if type(label) is str:
2127 labels.append(label)
2128 elif type(label) is list:
2129 labels.extend(label)
Simran Basic6f1f7a2012-10-16 10:47:46 -07002130 return labels
Dan Shi85276d42014-04-08 22:11:45 -07002131
2132
2133 def is_boot_from_usb(self):
2134 """Check if DUT is boot from USB.
2135
2136 @return: True if DUT is boot from usb.
2137 """
2138 device = self.run('rootdev -s -d').stdout.strip()
2139 removable = int(self.run('cat /sys/block/%s/removable' %
2140 os.path.basename(device)).stdout.strip())
2141 return removable == 1