blob: c6610075c5bd7973fea34f37ffae0f2ee30446c2 [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
Aviv Keshet74c89a92013-02-04 15:18:30 -08005import functools
J. Richard Barnette1d78b012012-05-15 13:56:30 -07006import logging
Dan Shi0f466e82013-02-22 15:44:58 -08007import os
Simran Basid5e5e272012-09-24 15:23:59 -07008import re
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07009import time
10
mussa584b4462014-06-20 15:13:28 -070011import common
J. Richard Barnette45e93de2012-04-11 17:24:15 -070012from autotest_lib.client.bin import utils
Dan Shi9cb0eec2014-06-03 09:04:50 -070013from autotest_lib.client.common_lib import autotemp
Richard Barnette0c73ffc2012-11-19 15:21:18 -080014from autotest_lib.client.common_lib import error
15from autotest_lib.client.common_lib import global_config
Dan Shi549fb822015-03-24 18:01:11 -070016from autotest_lib.client.common_lib import lsbrelease_utils
J. Richard Barnette45e93de2012-04-11 17:24:15 -070017from autotest_lib.client.common_lib.cros import autoupdater
Richard Barnette03a0c132012-11-05 12:40:35 -080018from autotest_lib.client.common_lib.cros import dev_server
Gabe Blackb72f4fb2015-01-20 16:47:13 -080019from autotest_lib.client.common_lib.cros.graphite import autotest_es
Gabe Black1e1c41b2015-02-04 23:55:15 -080020from autotest_lib.client.common_lib.cros.graphite import autotest_stats
Hsinyu Chaoe0b08e62015-08-11 10:50:37 +000021from autotest_lib.client.cros import constants as client_constants
J. Richard Barnette84890bd2014-02-21 11:05:47 -080022from autotest_lib.client.cros import cros_ui
Cheng-Yi Chiangf4104ff2014-12-23 19:39:01 +080023from autotest_lib.client.cros.audio import cras_utils
Katherine Threlkeldab83d392015-06-18 16:45:57 -070024from autotest_lib.client.cros.input_playback import input_playback
Mussa5b589052015-10-26 17:55:26 -070025from autotest_lib.client.cros.video import constants as video_test_constants
MK Ryu35d661e2014-09-25 17:44:10 -070026from autotest_lib.server import autoserv_parser
27from autotest_lib.server import autotest
28from autotest_lib.server import constants
29from autotest_lib.server import crashcollect
Dan Shia1ecd5c2013-06-06 11:21:31 -070030from autotest_lib.server import utils as server_utils
Dan Shi9cb0eec2014-06-03 09:04:50 -070031from autotest_lib.server.cros import provision
Scott Zawalski89c44dd2013-02-26 09:28:02 -050032from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
Simran Basi5e6339a2013-03-21 11:34:32 -070033from autotest_lib.server.cros.dynamic_suite import tools, frontend_wrappers
Dan Shi9cb0eec2014-06-03 09:04:50 -070034from autotest_lib.server.cros.faft.config.config import Config as FAFTConfig
Fang Deng96667ca2013-08-01 17:46:18 -070035from autotest_lib.server.hosts import abstract_ssh
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +080036from autotest_lib.server.hosts import chameleon_host
Fang Deng5d518f42013-08-02 14:04:32 -070037from autotest_lib.server.hosts import servo_host
Simran Basidcff4252012-11-20 16:13:20 -080038from autotest_lib.site_utils.rpm_control_system import rpm_client
Simran Basid5e5e272012-09-24 15:23:59 -070039
40
Dan Shib8540a52015-07-16 14:18:23 -070041CONFIG = global_config.global_config
42
Eric Carusoee673ac2015-08-05 17:03:04 -070043LUCID_SLEEP_BOARDS = ['samus', 'lulu']
44
Dan Shid07ee2e2015-09-24 14:49:25 -070045# A file to indicate provision failure and require Repair job to powerwash the
46# dut.
47PROVISION_FAILED = '/var/tmp/provision_failed'
48
beepsc87ff602013-07-31 21:53:00 -070049class FactoryImageCheckerException(error.AutoservError):
50 """Exception raised when an image is a factory image."""
51 pass
52
53
Fang Deng0ca40e22013-08-27 17:47:44 -070054class CrosHost(abstract_ssh.AbstractSSHHost):
J. Richard Barnette45e93de2012-04-11 17:24:15 -070055 """Chromium OS specific subclass of Host."""
56
57 _parser = autoserv_parser.autoserv_parser
Scott Zawalski62bacae2013-03-05 10:40:32 -050058 _AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
J. Richard Barnette45e93de2012-04-11 17:24:15 -070059
Richard Barnette03a0c132012-11-05 12:40:35 -080060 # Timeout values (in seconds) associated with various Chrome OS
61 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070062 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -080063 # In general, a good rule of thumb is that the timeout can be up
64 # to twice the typical measured value on the slowest platform.
65 # The times here have not necessarily been empirically tested to
66 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070067 #
68 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -080069 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
70 # time to restart the netwowrk.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080071 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070072 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -080073 # other things, this must account for the 30 second dev-mode
J. Richard Barnette417cc792015-10-01 09:56:36 -070074 # screen delay, time to start the network on the DUT, and the
75 # ssh timeout of 120 seconds.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070076 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -080077 # including the 30 second dev-mode delay and time to start the
J. Richard Barnetted4649c62013-03-06 17:42:27 -080078 # network.
beepsf079cfb2013-09-18 17:49:51 -070079 # INSTALL_TIMEOUT: Time to allow for chromeos-install.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080080 # POWERWASH_BOOT_TIMEOUT: Time to allow for a reboot that
81 # includes powerwash.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070082
83 SLEEP_TIMEOUT = 2
J. Richard Barnetted4649c62013-03-06 17:42:27 -080084 RESUME_TIMEOUT = 10
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +080085 SHUTDOWN_TIMEOUT = 10
J. Richard Barnette417cc792015-10-01 09:56:36 -070086 BOOT_TIMEOUT = 150
J. Richard Barnette5bab5f52015-08-03 13:14:38 -070087 USB_BOOT_TIMEOUT = 300
J. Richard Barnette7817b052014-08-28 09:47:29 -070088 INSTALL_TIMEOUT = 480
Dan Shi2c88eed2013-11-12 10:18:38 -080089 POWERWASH_BOOT_TIMEOUT = 60
Chris Sosab76e0ee2013-05-22 16:55:41 -070090
Dan Shica503482015-03-30 17:23:25 -070091 # Minimum OS version that supports server side packaging. Older builds may
92 # not have server side package built or with Autotest code change to support
93 # server-side packaging.
Dan Shib8540a52015-07-16 14:18:23 -070094 MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value(
Dan Shiced09e42015-04-17 16:09:34 -070095 'AUTOSERV', 'min_version_support_ssp', type=int)
Dan Shica503482015-03-30 17:23:25 -070096
J. Richard Barnette84890bd2014-02-21 11:05:47 -080097 # REBOOT_TIMEOUT: How long to wait for a reboot.
98 #
Chris Sosab76e0ee2013-05-22 16:55:41 -070099 # We have a long timeout to ensure we don't flakily fail due to other
100 # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate.
Simran Basi1160e2c2013-10-04 16:00:24 -0700101 # TODO(sbasi - crbug.com/276094) Restore to 5 mins once the 'host did not
102 # return from reboot' bug is solved.
103 REBOOT_TIMEOUT = 480
Chris Sosab76e0ee2013-05-22 16:55:41 -0700104
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800105 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
106 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
107 _USB_POWER_TIMEOUT = 5
108 _POWER_CYCLE_TIMEOUT = 10
109
Dan Shib8540a52015-07-16 14:18:23 -0700110 _RPM_RECOVERY_BOARDS = CONFIG.get_config_value('CROS',
Richard Barnette82c35912012-11-20 10:09:10 -0800111 'rpm_recovery_boards', type=str).split(',')
112
113 _MAX_POWER_CYCLE_ATTEMPTS = 6
114 _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
Fang Dengdeba14f2014-11-14 11:54:09 -0800115 _RPM_HOSTNAME_REGEX = ('chromeos(\d+)(-row(\d+))?-rack(\d+[a-z]*)'
116 '-host(\d+)')
Katherine Threlkeldab83d392015-06-18 16:45:57 -0700117 _LIGHTSENSOR_FILES = [ "in_illuminance0_input",
118 "in_illuminance_input",
119 "in_illuminance0_raw",
120 "in_illuminance_raw",
121 "illuminance0_input"]
Richard Barnette82c35912012-11-20 10:09:10 -0800122 _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices'
123 _LABEL_FUNCTIONS = []
Aviv Keshet74c89a92013-02-04 15:18:30 -0800124 _DETECTABLE_LABELS = []
Kevin Cheng3a4a57a2015-09-30 12:09:50 -0700125 label_decorator = functools.partial(server_utils.add_label_detector,
126 _LABEL_FUNCTIONS,
Aviv Keshet74c89a92013-02-04 15:18:30 -0800127 _DETECTABLE_LABELS)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700128
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800129 # Constants used in ping_wait_up() and ping_wait_down().
130 #
131 # _PING_WAIT_COUNT is the approximate number of polling
132 # cycles to use when waiting for a host state change.
133 #
134 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
135 # for arguments to the internal _ping_wait_for_status()
136 # method.
137 _PING_WAIT_COUNT = 40
138 _PING_STATUS_DOWN = False
139 _PING_STATUS_UP = True
140
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800141 # Allowed values for the power_method argument.
142
143 # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods.
144 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
145 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
146 POWER_CONTROL_RPM = 'RPM'
147 POWER_CONTROL_SERVO = 'servoj10'
148 POWER_CONTROL_MANUAL = 'manual'
149
150 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
151 POWER_CONTROL_SERVO,
152 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800153
Simran Basi5e6339a2013-03-21 11:34:32 -0700154 _RPM_OUTLET_CHANGED = 'outlet_changed'
155
Dan Shi9cb0eec2014-06-03 09:04:50 -0700156 # URL pattern to download firmware image.
Dan Shib8540a52015-07-16 14:18:23 -0700157 _FW_IMAGE_URL_PATTERN = CONFIG.get_config_value(
Dan Shi9cb0eec2014-06-03 09:04:50 -0700158 'CROS', 'firmware_url_pattern', type=str)
beeps687243d2013-07-18 15:29:27 -0700159
MK Ryu35d661e2014-09-25 17:44:10 -0700160 # File that has a list of directories to be collected
161 _LOGS_TO_COLLECT_FILE = os.path.join(
162 common.client_dir, 'common_lib', 'logs_to_collect')
163
164 # Prefix of logging message w.r.t. crash collection
165 _CRASHLOGS_PREFIX = 'collect_crashlogs'
166
167 # Time duration waiting for host up/down check
168 _CHECK_HOST_UP_TIMEOUT_SECS = 15
169
170 # A command that interacts with kernel and hardware (e.g., rm, mkdir, etc)
171 # might not be completely done deep through the hardware when the machine
172 # is powered down right after the command returns.
173 # We should wait for a few seconds to make them done. Finger crossed.
174 _SAFE_WAIT_SECS = 10
175
176
J. Richard Barnette964fba02012-10-24 17:34:29 -0700177 @staticmethod
beeps46dadc92013-11-07 14:07:10 -0800178 def check_host(host, timeout=10):
179 """
180 Check if the given host is a chrome-os host.
181
182 @param host: An ssh host representing a device.
183 @param timeout: The timeout for the run command.
184
185 @return: True if the host device is chromeos.
186
beeps46dadc92013-11-07 14:07:10 -0800187 """
188 try:
Simran Basi933c8af2015-04-29 14:05:07 -0700189 result = host.run(
190 'grep -q CHROMEOS /etc/lsb-release && '
191 '! test -f /mnt/stateful_partition/.android_tester && '
192 '! grep -q moblab /etc/lsb-release',
193 ignore_status=True, timeout=timeout)
beeps46dadc92013-11-07 14:07:10 -0800194 except (error.AutoservRunError, error.AutoservSSHTimeout):
195 return False
196 return result.exit_status == 0
197
198
199 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800200 def _extract_arguments(args_dict, key_subset):
201 """Extract options from `args_dict` and return a subset result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800202
203 Take the provided dictionary of argument options and return
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800204 a subset that represent standard arguments needed to construct
205 a test-assistant object (chameleon or servo) for a host. The
206 intent is to provide standard argument processing from
Christopher Wiley644ef3e2015-05-15 13:14:14 -0700207 CrosHost for tests that require a test-assistant board
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800208 to operate.
209
210 @param args_dict Dictionary from which to extract the arguments.
211 @param key_subset Tuple of keys to extract from the args_dict, e.g.
212 ('servo_host', 'servo_port').
213 """
214 result = {}
215 for arg in key_subset:
216 if arg in args_dict:
217 result[arg] = args_dict[arg]
218 return result
219
220
221 @staticmethod
222 def get_chameleon_arguments(args_dict):
223 """Extract chameleon options from `args_dict` and return the result.
224
225 Recommended usage:
226 ~~~~~~~~
227 args_dict = utils.args_to_dict(args)
228 chameleon_args = hosts.CrosHost.get_chameleon_arguments(args_dict)
229 host = hosts.create_host(machine, chameleon_args=chameleon_args)
230 ~~~~~~~~
231
232 @param args_dict Dictionary from which to extract the chameleon
233 arguments.
234 """
235 return CrosHost._extract_arguments(
236 args_dict, ('chameleon_host', 'chameleon_port'))
237
238
239 @staticmethod
240 def get_servo_arguments(args_dict):
241 """Extract servo options from `args_dict` and return the result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800242
243 Recommended usage:
244 ~~~~~~~~
245 args_dict = utils.args_to_dict(args)
Fang Deng0ca40e22013-08-27 17:47:44 -0700246 servo_args = hosts.CrosHost.get_servo_arguments(args_dict)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800247 host = hosts.create_host(machine, servo_args=servo_args)
248 ~~~~~~~~
249
250 @param args_dict Dictionary from which to extract the servo
251 arguments.
252 """
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800253 return CrosHost._extract_arguments(
254 args_dict, ('servo_host', 'servo_port'))
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700255
J. Richard Barnette964fba02012-10-24 17:34:29 -0700256
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800257 def _initialize(self, hostname, chameleon_args=None, servo_args=None,
Fang Denge545abb2014-12-30 18:43:47 -0800258 try_lab_servo=False, ssh_verbosity_flag='', ssh_options='',
Fang Dengd1c2b732013-08-20 12:59:46 -0700259 *args, **dargs):
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800260 """Initialize superclasses, |self.chameleon|, and |self.servo|.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700261
Fang Denge545abb2014-12-30 18:43:47 -0800262 This method will attempt to create the test-assistant object
263 (chameleon/servo) when it is needed by the test. Check
264 the docstring of chameleon_host.create_chameleon_host and
265 servo_host.create_servo_host for how this is determined.
Fang Deng5d518f42013-08-02 14:04:32 -0700266
Fang Denge545abb2014-12-30 18:43:47 -0800267 @param hostname: Hostname of the dut.
268 @param chameleon_args: A dictionary that contains args for creating
269 a ChameleonHost. See chameleon_host for details.
270 @param servo_args: A dictionary that contains args for creating
271 a ServoHost object. See servo_host for details.
272 @param try_lab_servo: Boolean, False indicates that ServoHost should
273 not be created for a device in Cros test lab.
274 See servo_host for details.
275 @param ssh_verbosity_flag: String, to pass to the ssh command to control
276 verbosity.
277 @param ssh_options: String, other ssh options to pass to the ssh
278 command.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700279 """
Fang Deng0ca40e22013-08-27 17:47:44 -0700280 super(CrosHost, self)._initialize(hostname=hostname,
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700281 *args, **dargs)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700282 # self.env is a dictionary of environment variable settings
283 # to be exported for commands run on the host.
284 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
285 # errors that might happen.
286 self.env['LIBC_FATAL_STDERR_'] = '1'
Fang Dengd1c2b732013-08-20 12:59:46 -0700287 self._ssh_verbosity_flag = ssh_verbosity_flag
Aviv Keshetc5947fa2013-09-04 14:06:29 -0700288 self._ssh_options = ssh_options
Fang Deng5d518f42013-08-02 14:04:32 -0700289 # TODO(fdeng): We need to simplify the
290 # process of servo and servo_host initialization.
291 # crbug.com/298432
Fang Denge545abb2014-12-30 18:43:47 -0800292 self._servo_host = servo_host.create_servo_host(
293 dut=self.hostname, servo_args=servo_args,
294 try_lab_servo=try_lab_servo)
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800295 # TODO(waihong): Do the simplication on Chameleon too.
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800296 self._chameleon_host = chameleon_host.create_chameleon_host(
297 dut=self.hostname, chameleon_args=chameleon_args)
298
Dan Shi4d478522014-02-14 13:46:32 -0800299 if self._servo_host is not None:
300 self.servo = self._servo_host.get_servo()
301 else:
302 self.servo = None
303
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800304 if self._chameleon_host:
Tom Wai-Hong Tameaee3402014-01-22 08:52:10 +0800305 self.chameleon = self._chameleon_host.create_chameleon_board()
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800306 else:
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800307 self.chameleon = None
Fang Deng5d518f42013-08-02 14:04:32 -0700308
309
Dan Shi3d7a0e12015-10-12 11:55:45 -0700310 def get_repair_image_name(self, image_type='cros'):
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500311 """Generate a image_name from variables in the global config.
312
Dan Shi3d7a0e12015-10-12 11:55:45 -0700313 image_type is used to differentiate different images. Default is CrOS,
314 in which case, repair image's name follows the naming convention defined
315 in global setting CROS/stable_build_pattern.
316 If the image_type is not `cros`, the repair image will be looked up
317 using key `board_name/image_type`, e.g., daisy_spring/firmware.
318
319 @param image_type: Type of the image. Default is `cros`.
320
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500321 @returns a str of $board-version/$BUILD.
322
323 """
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500324 board = self._get_board_from_afe()
325 if board is None:
326 raise error.AutoservError('DUT has no board attribute, '
327 'cannot be repaired.')
Dan Shi3d7a0e12015-10-12 11:55:45 -0700328 if image_type != 'cros':
329 board = '%s/%s' % (board, image_type)
Dan Shi6964fa52014-12-18 11:04:27 -0800330 stable_version = self._AFE.run('get_stable_version', board=board)
Dan Shi3d7a0e12015-10-12 11:55:45 -0700331 if image_type == 'cros':
332 build_pattern = CONFIG.get_config_value(
333 'CROS', 'stable_build_pattern')
334 stable_version = build_pattern % (board, stable_version)
335 return stable_version
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500336
337
Scott Zawalski62bacae2013-03-05 10:40:32 -0500338 def _host_in_AFE(self):
339 """Check if the host is an object the AFE knows.
340
341 @returns the host object.
342 """
343 return self._AFE.get_hosts(hostname=self.hostname)
344
345
Chris Sosab76e0ee2013-05-22 16:55:41 -0700346 def lookup_job_repo_url(self):
347 """Looks up the job_repo_url for the host.
348
349 @returns job_repo_url from AFE or None if not found.
350
351 @raises KeyError if the host does not have a job_repo_url
352 """
Chris Sosab76e0ee2013-05-22 16:55:41 -0700353 hosts = self._AFE.get_hosts(hostname=self.hostname)
beepsb5efc532013-06-04 11:29:34 -0700354 if hosts and ds_constants.JOB_REPO_URL in hosts[0].attributes:
355 return hosts[0].attributes[ds_constants.JOB_REPO_URL]
J. Richard Barnette85d0aac2015-08-20 10:34:39 -0700356 else:
357 return None
Chris Sosab76e0ee2013-05-22 16:55:41 -0700358
359
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500360 def clear_cros_version_labels_and_job_repo_url(self):
361 """Clear cros_version labels and host attribute job_repo_url."""
Scott Zawalski62bacae2013-03-05 10:40:32 -0500362 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400363 return
364
Scott Zawalski62bacae2013-03-05 10:40:32 -0500365 host_list = [self.hostname]
366 labels = self._AFE.get_labels(
367 name__startswith=ds_constants.VERSION_PREFIX,
368 host__hostname=self.hostname)
Dan Shi0f466e82013-02-22 15:44:58 -0800369
Scott Zawalski62bacae2013-03-05 10:40:32 -0500370 for label in labels:
371 label.remove_hosts(hosts=host_list)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500372
beepscb6f1e22013-06-28 19:14:10 -0700373 self.update_job_repo_url(None, None)
374
375
376 def update_job_repo_url(self, devserver_url, image_name):
377 """
378 Updates the job_repo_url host attribute and asserts it's value.
379
380 @param devserver_url: The devserver to use in the job_repo_url.
381 @param image_name: The name of the image to use in the job_repo_url.
382
383 @raises AutoservError: If we failed to update the job_repo_url.
384 """
385 repo_url = None
386 if devserver_url and image_name:
387 repo_url = tools.get_package_url(devserver_url, image_name)
388 self._AFE.set_host_attribute(ds_constants.JOB_REPO_URL, repo_url,
Scott Zawalski62bacae2013-03-05 10:40:32 -0500389 hostname=self.hostname)
beepscb6f1e22013-06-28 19:14:10 -0700390 if self.lookup_job_repo_url() != repo_url:
391 raise error.AutoservError('Failed to update job_repo_url with %s, '
392 'host %s' % (repo_url, self.hostname))
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500393
394
Dan Shie9309262013-06-19 22:50:21 -0700395 def add_cros_version_labels_and_job_repo_url(self, image_name):
Scott Zawalskieadbf702013-03-14 09:23:06 -0400396 """Add cros_version labels and host attribute job_repo_url.
397
398 @param image_name: The name of the image e.g.
399 lumpy-release/R27-3837.0.0
Dan Shi7458bf62013-06-10 12:50:16 -0700400
Scott Zawalskieadbf702013-03-14 09:23:06 -0400401 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500402 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400403 return
Scott Zawalski62bacae2013-03-05 10:40:32 -0500404
Scott Zawalskieadbf702013-03-14 09:23:06 -0400405 cros_label = '%s%s' % (ds_constants.VERSION_PREFIX, image_name)
Dan Shie9309262013-06-19 22:50:21 -0700406 devserver_url = dev_server.ImageServer.resolve(image_name).url()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500407
MK Ryufb5e3a82015-07-01 12:21:20 -0700408 self._AFE.run('label_add_hosts', id=cros_label, hosts=[self.hostname])
beepscb6f1e22013-06-28 19:14:10 -0700409 self.update_job_repo_url(devserver_url, image_name)
410
411
beepsdae65fd2013-07-26 16:24:41 -0700412 def verify_job_repo_url(self, tag=''):
beepscb6f1e22013-06-28 19:14:10 -0700413 """
414 Make sure job_repo_url of this host is valid.
415
joychen03eaad92013-06-26 09:55:21 -0700416 Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/\
beepscb6f1e22013-06-28 19:14:10 -0700417 lumpy-release/R29-4279.0.0/autotest/packages" claims to have the
418 autotest package for lumpy-release/R29-4279.0.0. If this isn't the case,
419 download and extract it. If the devserver embedded in the url is
420 unresponsive, update the job_repo_url of the host after staging it on
421 another devserver.
422
423 @param job_repo_url: A url pointing to the devserver where the autotest
424 package for this build should be staged.
beepsdae65fd2013-07-26 16:24:41 -0700425 @param tag: The tag from the server job, in the format
426 <job_id>-<user>/<hostname>, or <hostless> for a server job.
beepscb6f1e22013-06-28 19:14:10 -0700427
428 @raises DevServerException: If we could not resolve a devserver.
429 @raises AutoservError: If we're unable to save the new job_repo_url as
430 a result of choosing a new devserver because the old one failed to
431 respond to a health check.
beeps0c865032013-07-30 11:37:06 -0700432 @raises urllib2.URLError: If the devserver embedded in job_repo_url
433 doesn't respond within the timeout.
beepscb6f1e22013-06-28 19:14:10 -0700434 """
435 job_repo_url = self.lookup_job_repo_url()
436 if not job_repo_url:
437 logging.warning('No job repo url set on host %s', self.hostname)
438 return
439
440 logging.info('Verifying job repo url %s', job_repo_url)
441 devserver_url, image_name = tools.get_devserver_build_from_package_url(
442 job_repo_url)
443
beeps0c865032013-07-30 11:37:06 -0700444 ds = dev_server.ImageServer(devserver_url)
beepscb6f1e22013-06-28 19:14:10 -0700445
446 logging.info('Staging autotest artifacts for %s on devserver %s',
447 image_name, ds.url())
beeps687243d2013-07-18 15:29:27 -0700448
449 start_time = time.time()
Simran Basi25e7a922014-10-31 11:56:10 -0700450 ds.stage_artifacts(image_name, ['autotest_packages'])
beeps687243d2013-07-18 15:29:27 -0700451 stage_time = time.time() - start_time
452
453 # Record how much of the verification time comes from a devserver
454 # restage. If we're doing things right we should not see multiple
455 # devservers for a given board/build/branch path.
456 try:
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800457 board, build_type, branch = server_utils.ParseBuildName(
beeps687243d2013-07-18 15:29:27 -0700458 image_name)[:3]
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800459 except server_utils.ParseBuildNameException:
beeps687243d2013-07-18 15:29:27 -0700460 pass
461 else:
beeps0c865032013-07-30 11:37:06 -0700462 devserver = devserver_url[
Chris Sosa65425082013-10-16 13:26:22 -0700463 devserver_url.find('/') + 2:devserver_url.rfind(':')]
beeps687243d2013-07-18 15:29:27 -0700464 stats_key = {
465 'board': board,
466 'build_type': build_type,
467 'branch': branch,
beeps0c865032013-07-30 11:37:06 -0700468 'devserver': devserver.replace('.', '_'),
beeps687243d2013-07-18 15:29:27 -0700469 }
Gabe Black1e1c41b2015-02-04 23:55:15 -0800470 autotest_stats.Gauge('verify_job_repo_url').send(
beeps687243d2013-07-18 15:29:27 -0700471 '%(board)s.%(build_type)s.%(branch)s.%(devserver)s' % stats_key,
472 stage_time)
beepscb6f1e22013-06-28 19:14:10 -0700473
Scott Zawalskieadbf702013-03-14 09:23:06 -0400474
Dan Shicf4d2032015-03-12 15:04:21 -0700475 def stage_server_side_package(self, image=None):
476 """Stage autotest server-side package on devserver.
477
478 @param image: Full path of an OS image to install or a build name.
479
480 @return: A url to the autotest server-side package.
481 """
482 if image:
483 image_name = tools.get_build_from_image(image)
484 if not image_name:
485 raise error.AutoservError(
486 'Failed to parse build name from %s' % image)
487 ds = dev_server.ImageServer.resolve(image_name)
488 else:
489 job_repo_url = self.lookup_job_repo_url()
490 if job_repo_url:
491 devserver_url, image_name = (
492 tools.get_devserver_build_from_package_url(job_repo_url))
493 ds = dev_server.ImageServer(devserver_url)
494 else:
495 labels = self._AFE.get_labels(
496 name__startswith=ds_constants.VERSION_PREFIX,
497 host__hostname=self.hostname)
498 if not labels:
499 raise error.AutoservError(
500 'Failed to stage server-side package. The host has '
501 'no job_report_url attribute or version label.')
502 image_name = labels[0].name[len(ds_constants.VERSION_PREFIX):]
503 ds = dev_server.ImageServer.resolve(image_name)
Dan Shica503482015-03-30 17:23:25 -0700504
505 # Get the OS version of the build, for any build older than
506 # MIN_VERSION_SUPPORT_SSP, server side packaging is not supported.
507 match = re.match('.*/R\d+-(\d+)\.', image_name)
508 if match and int(match.group(1)) < self.MIN_VERSION_SUPPORT_SSP:
509 logging.warn('Build %s is older than %s. Server side packaging is '
510 'disabled.', image_name, self.MIN_VERSION_SUPPORT_SSP)
511 return None
512
Dan Shicf4d2032015-03-12 15:04:21 -0700513 ds.stage_artifacts(image_name, ['autotest_server_package'])
514 return '%s/static/%s/%s' % (ds.url(), image_name,
515 'autotest_server_package.tar.bz2')
516
517
Dan Shi0f466e82013-02-22 15:44:58 -0800518 def _try_stateful_update(self, update_url, force_update, updater):
519 """Try to use stateful update to initialize DUT.
520
521 When DUT is already running the same version that machine_install
522 tries to install, stateful update is a much faster way to clean up
523 the DUT for testing, compared to a full reimage. It is implemeted
524 by calling autoupdater.run_update, but skipping updating root, as
525 updating the kernel is time consuming and not necessary.
526
527 @param update_url: url of the image.
528 @param force_update: Set to True to update the image even if the DUT
529 is running the same version.
530 @param updater: ChromiumOSUpdater instance used to update the DUT.
531 @returns: True if the DUT was updated with stateful update.
532
533 """
J. Richard Barnette3f731032014-04-07 17:42:59 -0700534 # TODO(jrbarnette): Yes, I hate this re.match() test case.
535 # It's better than the alternative: see crbug.com/360944.
536 image_name = autoupdater.url_to_image_name(update_url)
537 release_pattern = r'^.*-release/R[0-9]+-[0-9]+\.[0-9]+\.0$'
538 if not re.match(release_pattern, image_name):
539 return False
Dan Shi0f466e82013-02-22 15:44:58 -0800540 if not updater.check_version():
541 return False
542 if not force_update:
543 logging.info('Canceling stateful update because the new and '
544 'old versions are the same.')
545 return False
546 # Following folders should be rebuilt after stateful update.
547 # A test file is used to confirm each folder gets rebuilt after
548 # the stateful update.
549 folders_to_check = ['/var', '/home', '/mnt/stateful_partition']
550 test_file = '.test_file_to_be_deleted'
551 for folder in folders_to_check:
552 touch_path = os.path.join(folder, test_file)
553 self.run('touch %s' % touch_path)
554
Chris Sosae92399e2015-04-24 11:32:59 -0700555 updater.run_update(update_root=False)
Dan Shi0f466e82013-02-22 15:44:58 -0800556
557 # Reboot to complete stateful update.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700558 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Dan Shi0f466e82013-02-22 15:44:58 -0800559 check_file_cmd = 'test -f %s; echo $?'
560 for folder in folders_to_check:
561 test_file_path = os.path.join(folder, test_file)
562 result = self.run(check_file_cmd % test_file_path,
563 ignore_status=True)
564 if result.exit_status == 1:
565 return False
566 return True
567
568
J. Richard Barnette7275b612013-06-04 18:13:11 -0700569 def _post_update_processing(self, updater, expected_kernel=None):
Dan Shi0f466e82013-02-22 15:44:58 -0800570 """After the DUT is updated, confirm machine_install succeeded.
571
572 @param updater: ChromiumOSUpdater instance used to update the DUT.
J. Richard Barnette7275b612013-06-04 18:13:11 -0700573 @param expected_kernel: kernel expected to be active after reboot,
574 or `None` to skip rollback checking.
Dan Shi0f466e82013-02-22 15:44:58 -0800575
576 """
J. Richard Barnette7275b612013-06-04 18:13:11 -0700577 # Touch the lab machine file to leave a marker that
578 # distinguishes this image from other test images.
579 # Afterwards, we must re-run the autoreboot script because
580 # it depends on the _LAB_MACHINE_FILE.
Dan Shi0f466e82013-02-22 15:44:58 -0800581 self.run('touch %s' % self._LAB_MACHINE_FILE)
Dan Shi0f466e82013-02-22 15:44:58 -0800582 self.run('start autoreboot')
Chris Sosa65425082013-10-16 13:26:22 -0700583 updater.verify_boot_expectations(
584 expected_kernel, rollback_message=
Gilad Arnoldc26ae1f2015-10-22 16:09:41 -0700585 'Build %s failed to boot on %s; system rolled back to previous '
Chris Sosa65425082013-10-16 13:26:22 -0700586 'build' % (updater.update_version, self.hostname))
J. Richard Barnette7275b612013-06-04 18:13:11 -0700587 # Check that we've got the build we meant to install.
588 if not updater.check_version_to_confirm_install():
589 raise autoupdater.ChromiumOSError(
590 'Failed to update %s to build %s; found build '
591 '%s instead' % (self.hostname,
Chris Sosa65425082013-10-16 13:26:22 -0700592 updater.update_version,
Dan Shi0942b1d2015-03-31 11:07:00 -0700593 self.get_release_version()))
Dan Shi0f466e82013-02-22 15:44:58 -0800594
Chris Sosae92399e2015-04-24 11:32:59 -0700595 logging.debug('Cleaning up old autotest directories.')
596 try:
597 installed_autodir = autotest.Autotest.get_installed_autodir(self)
598 self.run('rm -rf ' + installed_autodir)
599 except autotest.AutodirNotFoundError:
600 logging.debug('No autotest installed directory found.')
601
Dan Shi0f466e82013-02-22 15:44:58 -0800602
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700603 def _stage_image_for_update(self, image_name=None):
Chris Sosae92399e2015-04-24 11:32:59 -0700604 """Stage a build on a devserver and return the update_url and devserver.
Scott Zawalskieadbf702013-03-14 09:23:06 -0400605
606 @param image_name: a name like lumpy-release/R27-3837.0.0
Chris Sosae92399e2015-04-24 11:32:59 -0700607 @returns a tuple with an update URL like:
Scott Zawalskieadbf702013-03-14 09:23:06 -0400608 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
Chris Sosae92399e2015-04-24 11:32:59 -0700609 and the devserver instance.
Scott Zawalskieadbf702013-03-14 09:23:06 -0400610 """
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700611 if not image_name:
612 image_name = self.get_repair_image_name()
Chris Sosae92399e2015-04-24 11:32:59 -0700613
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700614 logging.info('Staging build for AU: %s', image_name)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400615 devserver = dev_server.ImageServer.resolve(image_name)
616 devserver.trigger_download(image_name, synchronous=False)
Chris Sosae92399e2015-04-24 11:32:59 -0700617 return (tools.image_url_pattern() % (devserver.url(), image_name),
618 devserver)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400619
620
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700621 def stage_image_for_servo(self, image_name=None):
622 """Stage a build on a devserver and return the update_url.
623
624 @param image_name: a name like lumpy-release/R27-3837.0.0
625 @returns an update URL like:
626 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
627 """
628 if not image_name:
629 image_name = self.get_repair_image_name()
630 logging.info('Staging build for servo install: %s', image_name)
631 devserver = dev_server.ImageServer.resolve(image_name)
632 devserver.stage_artifacts(image_name, ['test_image'])
633 return devserver.get_test_image_url(image_name)
634
635
beepse539be02013-07-31 21:57:39 -0700636 def stage_factory_image_for_servo(self, image_name):
637 """Stage a build on a devserver and return the update_url.
638
639 @param image_name: a name like <baord>/4262.204.0
beeps12c0a3c2013-09-03 11:58:27 -0700640
beepse539be02013-07-31 21:57:39 -0700641 @return: An update URL, eg:
642 http://<devserver>/static/canary-channel/\
643 <board>/4262.204.0/factory_test/chromiumos_factory_image.bin
beeps12c0a3c2013-09-03 11:58:27 -0700644
645 @raises: ValueError if the factory artifact name is missing from
646 the config.
647
beepse539be02013-07-31 21:57:39 -0700648 """
649 if not image_name:
650 logging.error('Need an image_name to stage a factory image.')
651 return
652
Dan Shib8540a52015-07-16 14:18:23 -0700653 factory_artifact = CONFIG.get_config_value(
beeps12c0a3c2013-09-03 11:58:27 -0700654 'CROS', 'factory_artifact', type=str, default='')
655 if not factory_artifact:
656 raise ValueError('Cannot retrieve the factory artifact name from '
657 'autotest config, and hence cannot stage factory '
658 'artifacts.')
659
beepse539be02013-07-31 21:57:39 -0700660 logging.info('Staging build for servo install: %s', image_name)
661 devserver = dev_server.ImageServer.resolve(image_name)
662 devserver.stage_artifacts(
663 image_name,
beeps12c0a3c2013-09-03 11:58:27 -0700664 [factory_artifact],
665 archive_url=None)
beepse539be02013-07-31 21:57:39 -0700666
667 return tools.factory_image_url_pattern() % (devserver.url(), image_name)
668
669
Chris Sosaa3ac2152012-05-23 22:23:13 -0700670 def machine_install(self, update_url=None, force_update=False,
Richard Barnette0b023a72015-04-24 16:07:30 +0000671 local_devserver=False, repair=False,
672 force_full_update=False):
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500673 """Install the DUT.
674
Dan Shi0f466e82013-02-22 15:44:58 -0800675 Use stateful update if the DUT is already running the same build.
676 Stateful update does not update kernel and tends to run much faster
677 than a full reimage. If the DUT is running a different build, or it
678 failed to do a stateful update, full update, including kernel update,
679 will be applied to the DUT.
680
Scott Zawalskieadbf702013-03-14 09:23:06 -0400681 Once a host enters machine_install its cros_version label will be
682 removed as well as its host attribute job_repo_url (used for
683 package install).
684
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500685 @param update_url: The url to use for the update
686 pattern: http://$devserver:###/update/$build
687 If update_url is None and repair is True we will install the
Dan Shi6964fa52014-12-18 11:04:27 -0800688 stable image listed in afe_stable_versions table. If the table
689 is not setup, global_config value under CROS.stable_cros_version
690 will be used instead.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500691 @param force_update: Force an update even if the version installed
692 is the same. Default:False
Christopher Wiley6a4ff932015-05-15 14:00:47 -0700693 @param local_devserver: Used by test_that to allow people to
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500694 use their local devserver. Default: False
Chris Sosae92399e2015-04-24 11:32:59 -0700695 @param repair: Forces update to repair image. Implies force_update.
Fang Deng3d3b9272014-12-22 12:20:28 -0800696 @param force_full_update: If True, do not attempt to run stateful
697 update, force a full reimage. If False, try stateful update
698 first when the dut is already installed with the same version.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500699 @raises autoupdater.ChromiumOSError
700
701 """
Chris Sosae92399e2015-04-24 11:32:59 -0700702 devserver = None
Richard Barnette0b023a72015-04-24 16:07:30 +0000703 if repair:
Chris Sosae92399e2015-04-24 11:32:59 -0700704 update_url, devserver = self._stage_image_for_update()
Richard Barnette0b023a72015-04-24 16:07:30 +0000705 force_update = True
Dan Shi0f466e82013-02-22 15:44:58 -0800706
Chris Sosae92399e2015-04-24 11:32:59 -0700707 if not update_url and not self._parser.options.image:
708 raise error.AutoservError(
Dan Shid07ee2e2015-09-24 14:49:25 -0700709 'There is no update URL, nor a method to get one.')
Chris Sosae92399e2015-04-24 11:32:59 -0700710
711 if not update_url and self._parser.options.image:
712 # This is the base case where we have no given update URL i.e.
713 # dynamic suites logic etc. This is the most flexible case where we
714 # can serve an update from any of our fleet of devservers.
715 requested_build = self._parser.options.image
716 if not requested_build.startswith('http://'):
717 logging.debug('Update will be staged for this installation')
718 update_url, devserver = self._stage_image_for_update(
Dan Shid07ee2e2015-09-24 14:49:25 -0700719 requested_build)
Chris Sosae92399e2015-04-24 11:32:59 -0700720 else:
721 update_url = requested_build
722
723 logging.debug('Update URL is %s', update_url)
724
Scott Zawalskieadbf702013-03-14 09:23:06 -0400725 # Remove cros-version and job_repo_url host attribute from host.
726 self.clear_cros_version_labels_and_job_repo_url()
Chris Sosae92399e2015-04-24 11:32:59 -0700727
Dan Shid07ee2e2015-09-24 14:49:25 -0700728 # Create a file to indicate if provision fails. The file will be removed
729 # by stateful update or full install.
730 self.run('touch %s' % PROVISION_FAILED)
731
Chris Sosae92399e2015-04-24 11:32:59 -0700732 update_complete = False
733 updater = autoupdater.ChromiumOSUpdater(
734 update_url, host=self, local_devserver=local_devserver)
Fang Deng3d3b9272014-12-22 12:20:28 -0800735 if not force_full_update:
736 try:
Chris Sosae92399e2015-04-24 11:32:59 -0700737 # If the DUT is already running the same build, try stateful
738 # update first as it's much quicker than a full re-image.
739 update_complete = self._try_stateful_update(
Dan Shid07ee2e2015-09-24 14:49:25 -0700740 update_url, force_update, updater)
Fang Deng3d3b9272014-12-22 12:20:28 -0800741 except Exception as e:
742 logging.exception(e)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700743
Dan Shi0f466e82013-02-22 15:44:58 -0800744 inactive_kernel = None
Chris Sosae92399e2015-04-24 11:32:59 -0700745 if update_complete or (not force_update and updater.check_version()):
746 logging.info('Install complete without full update')
747 else:
748 logging.info('DUT requires full update.')
749 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
750 num_of_attempts = provision.FLAKY_DEVSERVER_ATTEMPTS
Chris Sosab7612bc2013-03-21 10:32:37 -0700751
Chris Sosae92399e2015-04-24 11:32:59 -0700752 while num_of_attempts > 0:
753 num_of_attempts -= 1
754 try:
755 updater.run_update()
756 except Exception:
757 logging.warn('Autoupdate did not complete.')
758 # Do additional check for the devserver health. Ideally,
759 # the autoupdater.py could raise an exception when it
760 # detected network flake but that would require
761 # instrumenting the update engine and parsing it log.
762 if (num_of_attempts <= 0 or
763 devserver is None or
764 dev_server.DevServer.devserver_healthy(
765 devserver.url())):
Dan Shid07ee2e2015-09-24 14:49:25 -0700766 raise
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700767
Chris Sosae92399e2015-04-24 11:32:59 -0700768 logging.warn('Devserver looks unhealthy. Trying another')
769 update_url, devserver = self._stage_image_for_update(
770 requested_build)
771 logging.debug('New Update URL is %s', update_url)
772 updater = autoupdater.ChromiumOSUpdater(
773 update_url, host=self,
774 local_devserver=local_devserver)
775 else:
776 break
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700777
Chris Sosae92399e2015-04-24 11:32:59 -0700778 # Give it some time in case of IO issues.
779 time.sleep(10)
Dan Shi5699ac22014-12-19 10:55:49 -0800780
Chris Sosae92399e2015-04-24 11:32:59 -0700781 # Figure out active and inactive kernel.
782 active_kernel, inactive_kernel = updater.get_kernel_state()
Simran Basi13fa1ba2013-03-04 10:56:47 -0800783
Chris Sosae92399e2015-04-24 11:32:59 -0700784 # Ensure inactive kernel has higher priority than active.
785 if (updater.get_kernel_priority(inactive_kernel)
786 < updater.get_kernel_priority(active_kernel)):
787 raise autoupdater.ChromiumOSError(
788 'Update failed. The priority of the inactive kernel'
789 ' partition is less than that of the active kernel'
790 ' partition.')
791
792 # Updater has returned successfully; reboot the host.
793 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
794
795 self._post_update_processing(updater, inactive_kernel)
796 self.add_cros_version_labels_and_job_repo_url(
797 autoupdater.url_to_image_name(update_url))
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700798
799
Dan Shi9cb0eec2014-06-03 09:04:50 -0700800 def _clear_fw_version_labels(self):
801 """Clear firmware version labels from the machine."""
802 labels = self._AFE.get_labels(
Dan Shi0723bf52015-06-24 10:52:38 -0700803 name__startswith=provision.FW_RW_VERSION_PREFIX,
Dan Shi9cb0eec2014-06-03 09:04:50 -0700804 host__hostname=self.hostname)
805 for label in labels:
806 label.remove_hosts(hosts=[self.hostname])
807
808
809 def _add_fw_version_label(self, build):
810 """Add firmware version label to the machine.
811
812 @param build: Build of firmware.
813
814 """
815 fw_label = provision.fw_version_to_label(build)
MK Ryu73be9862015-07-06 12:25:00 -0700816 self._AFE.run('label_add_hosts', id=fw_label, hosts=[self.hostname])
Dan Shi9cb0eec2014-06-03 09:04:50 -0700817
818
819 def firmware_install(self, build=None):
820 """Install firmware to the DUT.
821
822 Use stateful update if the DUT is already running the same build.
823 Stateful update does not update kernel and tends to run much faster
824 than a full reimage. If the DUT is running a different build, or it
825 failed to do a stateful update, full update, including kernel update,
826 will be applied to the DUT.
827
828 Once a host enters firmware_install its fw_version label will be
829 removed. After the firmware is updated successfully, a new fw_version
830 label will be added to the host.
831
832 @param build: The build version to which we want to provision the
833 firmware of the machine,
834 e.g. 'link-firmware/R22-2695.1.144'.
835
836 TODO(dshi): After bug 381718 is fixed, update here with corresponding
837 exceptions that could be raised.
838
839 """
840 if not self.servo:
841 raise error.TestError('Host %s does not have servo.' %
842 self.hostname)
843
844 # TODO(fdeng): use host.get_board() after
845 # crbug.com/271834 is fixed.
846 board = self._get_board_from_afe()
847
Chris Sosae92399e2015-04-24 11:32:59 -0700848 # If build is not set, try to install firmware from stable CrOS.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700849 if not build:
Dan Shi3d7a0e12015-10-12 11:55:45 -0700850 build = self.get_repair_image_name(image_type='firmware')
851 if not build:
852 raise error.TestError(
853 'Failed to find stable firmware build for %s.',
854 self.hostname)
855 logging.info('Will install firmware from build %s.', build)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700856
857 config = FAFTConfig(board)
858 if config.use_u_boot:
859 ap_image = 'image-%s.bin' % board
860 else: # Depthcharge platform
861 ap_image = 'image.bin'
862 ec_image = 'ec.bin'
863 ds = dev_server.ImageServer.resolve(build)
864 ds.stage_artifacts(build, ['firmware'])
865
866 tmpd = autotemp.tempdir(unique_id='fwimage')
867 try:
868 fwurl = self._FW_IMAGE_URL_PATTERN % (ds.url(), build)
869 local_tarball = os.path.join(tmpd.name, os.path.basename(fwurl))
870 server_utils.system('wget -O %s %s' % (local_tarball, fwurl),
871 timeout=60)
872 server_utils.system('tar xf %s -C %s %s %s' %
873 (local_tarball, tmpd.name, ap_image, ec_image),
874 timeout=60)
875 server_utils.system('tar xf %s --wildcards -C %s "dts/*"' %
876 (local_tarball, tmpd.name),
877 timeout=60, ignore_status=True)
878
879 self._clear_fw_version_labels()
880 logging.info('Will re-program EC now')
881 self.servo.program_ec(os.path.join(tmpd.name, ec_image))
882 logging.info('Will re-program BIOS now')
883 self.servo.program_bios(os.path.join(tmpd.name, ap_image))
884 self.servo.get_power_state_controller().reset()
885 time.sleep(self.servo.BOOT_DELAY)
Dan Shia5fef052015-05-18 23:28:47 -0700886 self._add_fw_version_label(build)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700887 finally:
888 tmpd.clean()
889
890
Dan Shi10e992b2013-08-30 11:02:59 -0700891 def show_update_engine_log(self):
892 """Output update engine log."""
MK Ryu35d661e2014-09-25 17:44:10 -0700893 logging.debug('Dumping %s', client_constants.UPDATE_ENGINE_LOG)
894 self.run('cat %s' % client_constants.UPDATE_ENGINE_LOG)
Dan Shi10e992b2013-08-30 11:02:59 -0700895
896
Richard Barnette82c35912012-11-20 10:09:10 -0800897 def _get_board_from_afe(self):
898 """Retrieve this host's board from its labels in the AFE.
899
900 Looks for a host label of the form "board:<board>", and
901 returns the "<board>" part of the label. `None` is returned
902 if there is not a single, unique label matching the pattern.
903
904 @returns board from label, or `None`.
905 """
Dan Shia1ecd5c2013-06-06 11:21:31 -0700906 return server_utils.get_board_from_afe(self.hostname, self._AFE)
Simran Basi833814b2013-01-29 13:13:43 -0800907
908
909 def get_build(self):
910 """Retrieve the current build for this Host from the AFE.
911
912 Looks through this host's labels in the AFE to determine its build.
913
914 @returns The current build or None if it could not find it or if there
915 were multiple build labels assigned to this host.
916 """
Dan Shia1ecd5c2013-06-06 11:21:31 -0700917 return server_utils.get_build_from_afe(self.hostname, self._AFE)
Richard Barnette82c35912012-11-20 10:09:10 -0800918
919
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500920 def _install_repair(self):
Chris Sosae92399e2015-04-24 11:32:59 -0700921 """Attempt to repair this host using the update-engine.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500922
923 If the host is up, try installing the DUT with a stable
Dan Shi6964fa52014-12-18 11:04:27 -0800924 "repair" version of Chrome OS as defined in afe_stable_versions table.
925 If the table is not setup, global_config value under
926 CROS.stable_cros_version will be used instead.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500927
Scott Zawalski62bacae2013-03-05 10:40:32 -0500928 @raises AutoservRepairMethodNA if the DUT is not reachable.
929 @raises ChromiumOSError if the install failed for some reason.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500930
931 """
932 if not self.is_up():
Scott Zawalski62bacae2013-03-05 10:40:32 -0500933 raise error.AutoservRepairMethodNA('DUT unreachable for install.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500934 logging.info('Attempting to reimage machine to repair image.')
935 try:
Richard Barnette0b023a72015-04-24 16:07:30 +0000936 self.machine_install(repair=True)
Fang Dengd0672f32013-03-18 17:18:09 -0700937 except autoupdater.ChromiumOSError as e:
938 logging.exception(e)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500939 logging.info('Repair via install failed.')
Scott Zawalski62bacae2013-03-05 10:40:32 -0500940 raise
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500941
942
Dan Shi2c88eed2013-11-12 10:18:38 -0800943 def _install_repair_with_powerwash(self):
Dan Shi9cc48452013-11-12 12:39:26 -0800944 """Attempt to powerwash first then repair this host using update-engine.
Dan Shi2c88eed2013-11-12 10:18:38 -0800945
Dan Shi9cc48452013-11-12 12:39:26 -0800946 update-engine may fail due to a bad image. In such case, powerwash
947 may help to cleanup the DUT for update-engine to work again.
Dan Shi2c88eed2013-11-12 10:18:38 -0800948
949 @raises AutoservRepairMethodNA if the DUT is not reachable.
950 @raises ChromiumOSError if the install failed for some reason.
951
952 """
953 if not self.is_up():
954 raise error.AutoservRepairMethodNA('DUT unreachable for install.')
955
956 logging.info('Attempting to powerwash the DUT.')
957 self.run('echo "fast safe" > '
958 '/mnt/stateful_partition/factory_install_reset')
959 self.reboot(timeout=self.POWERWASH_BOOT_TIMEOUT, wait=True)
960 if not self.is_up():
Dan Shi9cc48452013-11-12 12:39:26 -0800961 logging.error('Powerwash failed. DUT did not come back after '
Dan Shi2c88eed2013-11-12 10:18:38 -0800962 'reboot.')
963 raise error.AutoservRepairFailure(
964 'DUT failed to boot from powerwash after %d seconds' %
965 self.POWERWASH_BOOT_TIMEOUT)
966
967 logging.info('Powerwash succeeded.')
968 self._install_repair()
969
970
beepsf079cfb2013-09-18 17:49:51 -0700971 def servo_install(self, image_url=None, usb_boot_timeout=USB_BOOT_TIMEOUT,
972 install_timeout=INSTALL_TIMEOUT):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500973 """
974 Re-install the OS on the DUT by:
975 1) installing a test image on a USB storage device attached to the Servo
976 board,
Richard Barnette03a0c132012-11-05 12:40:35 -0800977 2) booting that image in recovery mode, and then
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700978 3) installing the image with chromeos-install.
979
Scott Zawalski62bacae2013-03-05 10:40:32 -0500980 @param image_url: If specified use as the url to install on the DUT.
981 otherwise boot the currently staged image on the USB stick.
beepsf079cfb2013-09-18 17:49:51 -0700982 @param usb_boot_timeout: The usb_boot_timeout to use during reimage.
983 Factory images need a longer usb_boot_timeout than regular
984 cros images.
985 @param install_timeout: The timeout to use when installing the chromeos
986 image. Factory images need a longer install_timeout.
Richard Barnette03a0c132012-11-05 12:40:35 -0800987
Scott Zawalski62bacae2013-03-05 10:40:32 -0500988 @raises AutoservError if the image fails to boot.
beepsf079cfb2013-09-18 17:49:51 -0700989
J. Richard Barnette0199cc82014-12-05 17:08:40 -0800990 """
beepsf079cfb2013-09-18 17:49:51 -0700991 usb_boot_timer_key = ('servo_install.usb_boot_timeout_%s'
992 % usb_boot_timeout)
993 logging.info('Downloading image to USB, then booting from it. Usb boot '
994 'timeout = %s', usb_boot_timeout)
Gabe Black1e1c41b2015-02-04 23:55:15 -0800995 timer = autotest_stats.Timer(usb_boot_timer_key)
beepsf079cfb2013-09-18 17:49:51 -0700996 timer.start()
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700997 self.servo.install_recovery_image(image_url)
beepsf079cfb2013-09-18 17:49:51 -0700998 if not self.wait_up(timeout=usb_boot_timeout):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500999 raise error.AutoservRepairFailure(
1000 'DUT failed to boot from USB after %d seconds' %
beepsf079cfb2013-09-18 17:49:51 -07001001 usb_boot_timeout)
1002 timer.stop()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001003
Tom Wai-Hong Tamf6b4f812015-08-08 04:14:59 +08001004 # The new chromeos-tpm-recovery has been merged since R44-7073.0.0.
1005 # In old CrOS images, this command fails. Skip the error.
Tom Wai-Hong Tam27af7332015-07-25 06:09:39 +08001006 logging.info('Resetting the TPM status')
Tom Wai-Hong Tamf6b4f812015-08-08 04:14:59 +08001007 try:
1008 self.run('chromeos-tpm-recovery')
1009 except error.AutoservRunError:
1010 logging.warn('chromeos-tpm-recovery is too old.')
1011
Tom Wai-Hong Tam27af7332015-07-25 06:09:39 +08001012
beepsf079cfb2013-09-18 17:49:51 -07001013 install_timer_key = ('servo_install.install_timeout_%s'
1014 % install_timeout)
Gabe Black1e1c41b2015-02-04 23:55:15 -08001015 timer = autotest_stats.Timer(install_timer_key)
beepsf079cfb2013-09-18 17:49:51 -07001016 timer.start()
1017 logging.info('Installing image through chromeos-install.')
J. Richard Barnette9af19632015-09-25 12:18:03 -07001018 self.run('chromeos-install --yes', timeout=install_timeout)
1019 self.halt()
beepsf079cfb2013-09-18 17:49:51 -07001020 timer.stop()
1021
1022 logging.info('Power cycling DUT through servo.')
J. Richard Barnette0199cc82014-12-05 17:08:40 -08001023 self.servo.get_power_state_controller().power_off()
Fang Dengafb88142013-05-30 17:44:31 -07001024 self.servo.switch_usbkey('off')
J. Richard Barnette0199cc82014-12-05 17:08:40 -08001025 # N.B. The Servo API requires that we use power_on() here
1026 # for two reasons:
1027 # 1) After turning on a DUT in recovery mode, you must turn
1028 # it off and then on with power_on() once more to
1029 # disable recovery mode (this is a Parrot specific
1030 # requirement).
1031 # 2) After power_off(), the only way to turn on is with
1032 # power_on() (this is a Storm specific requirement).
J. Richard Barnettefbcc7122013-07-24 18:24:59 -07001033 self.servo.get_power_state_controller().power_on()
beepsf079cfb2013-09-18 17:49:51 -07001034
1035 logging.info('Waiting for DUT to come back up.')
Richard Barnette03a0c132012-11-05 12:40:35 -08001036 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
1037 raise error.AutoservError('DUT failed to reboot installed '
1038 'test image after %d seconds' %
Scott Zawalski62bacae2013-03-05 10:40:32 -05001039 self.BOOT_TIMEOUT)
1040
1041
Dan Shic1b8bdd2015-09-14 23:11:24 -07001042 def _setup_servo(self):
1043 """Try to force to create servo object if it's not set up yet.
1044 """
1045 if self.servo:
1046 return
1047
1048 try:
1049 # Setting servo_args to {} will force it to create the servo_host
1050 # object if possible.
1051 self._servo_host = servo_host.create_servo_host(
1052 dut=self.hostname, servo_args={})
1053 if self._servo_host:
1054 self.servo = self._servo_host.get_servo()
1055 else:
1056 logging.error('Failed to create servo_host object.')
1057 except Exception as e:
1058 logging.error('Failed to create servo object: %s', e)
1059
1060
J. Richard Barnettee4af8b92013-05-01 13:16:12 -07001061 def _servo_repair_reinstall(self):
Scott Zawalski62bacae2013-03-05 10:40:32 -05001062 """Reinstall the DUT utilizing servo and a test image.
1063
1064 Re-install the OS on the DUT by:
1065 1) installing a test image on a USB storage device attached to the Servo
1066 board,
Tom Wai-Hong Tam27af7332015-07-25 06:09:39 +08001067 2) booting that image in recovery mode,
1068 3) resetting the TPM status, and then
1069 4) installing the image with chromeos-install.
Scott Zawalski62bacae2013-03-05 10:40:32 -05001070
Scott Zawalski62bacae2013-03-05 10:40:32 -05001071 @raises AutoservRepairMethodNA if the device does not have servo
1072 support.
1073
1074 """
Dan Shic1b8bdd2015-09-14 23:11:24 -07001075 # To repair a DUT connected to a moblab, try to create a servo object if
1076 # it was failed to be created earlier as there may be a servo_host host
1077 # attribute for this host.
1078 if utils.is_moblab():
1079 self._setup_servo()
1080
Scott Zawalski62bacae2013-03-05 10:40:32 -05001081 if not self.servo:
1082 raise error.AutoservRepairMethodNA('Repair Reinstall NA: '
1083 'DUT has no servo support.')
1084
1085 logging.info('Attempting to recovery servo enabled device with '
1086 'servo_repair_reinstall')
1087
J. Richard Barnettee4af8b92013-05-01 13:16:12 -07001088 image_url = self.stage_image_for_servo()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001089 self.servo_install(image_url)
1090
1091
Dan Shi3d7a0e12015-10-12 11:55:45 -07001092 def _firmware_repair(self):
1093 """Reinstall the firmware image using servo.
1094
1095 This repair function attempts to install the stable firmware specified
1096 by the stable firmware version.
1097 Then reset the DUT and try to verify it. If verify fails, it will try to
1098 install the CrOS image using servo.
1099
1100 Note that the firmware repair is only applicable to DUTs in pools listed
1101 in global config CROS/pools_support_firmware_repair.
1102 """
1103 logging.info('Checking if host %s can be repaired with firmware '
1104 'repair.', self.hostname)
1105 pools = server_utils.get_labels_from_afe(self.hostname, 'pool:',
1106 self._AFE)
1107 pools_support_firmware_repair = CONFIG.get_config_value('CROS',
1108 'pools_support_firmware_repair', type=str).split(',')
1109 if (not pools or not pools_support_firmware_repair or
1110 not set(pools).intersection(set(pools_support_firmware_repair))):
1111 logging.info('Host %s is not in pools that support firmware repair.'
1112 ' pools supporting firmware repair are: %s.',
1113 self.hostname, pools_support_firmware_repair)
1114 raise error.AutoservRepairMethodNA(
1115 'Firmware repair is not applicable to host %s.' %
1116 self.hostname)
1117
1118 # To repair a DUT connected to a moblab, try to create a servo object if
1119 # it was failed to be created earlier as there may be a servo_host host
1120 # attribute for this host.
1121 if utils.is_moblab():
1122 self._setup_servo()
1123
1124 if not self.servo:
1125 raise error.AutoservRepairMethodNA('Repair Reinstall NA: '
1126 'DUT has no servo support.')
1127
1128 logging.info('Attempting to recovery servo enabled device with '
1129 'firmware_repair.')
1130 self.firmware_install()
1131
1132 logging.info('Firmware repaired. Check if the DUT can boot. If not, '
1133 'reinstall the CrOS using servo.')
1134 try:
1135 self.servo.reset()
1136 self.verify()
1137 except Exception as e:
1138 logging.warn('Failed to verify DUT, error: %s. Will try to repair '
1139 'the DUT with servo_repair_reinstall.', e)
1140 self._servo_repair_reinstall()
1141
1142
Scott Zawalski62bacae2013-03-05 10:40:32 -05001143 def _servo_repair_power(self):
1144 """Attempt to repair DUT using an attached Servo.
1145
1146 Attempt to power on the DUT via power_long_press.
1147
1148 @raises AutoservRepairMethodNA if the device does not have servo
1149 support.
1150 @raises AutoservRepairFailure if the repair fails for any reason.
1151 """
1152 if not self.servo:
1153 raise error.AutoservRepairMethodNA('Repair Power NA: '
1154 'DUT has no servo support.')
1155
1156 logging.info('Attempting to recover servo enabled device by '
1157 'powering it off and on.')
1158 self.servo.get_power_state_controller().power_off()
1159 self.servo.get_power_state_controller().power_on()
1160 if self.wait_up(self.BOOT_TIMEOUT):
1161 return
1162
1163 raise error.AutoservRepairFailure('DUT did not boot after long_press.')
Richard Barnette03a0c132012-11-05 12:40:35 -08001164
1165
Richard Barnette82c35912012-11-20 10:09:10 -08001166 def _powercycle_to_repair(self):
1167 """Utilize the RPM Infrastructure to bring the host back up.
1168
1169 If the host is not up/repaired after the first powercycle we utilize
1170 auto fallback to the last good install by powercycling and rebooting the
1171 host 6 times.
Scott Zawalski62bacae2013-03-05 10:40:32 -05001172
1173 @raises AutoservRepairMethodNA if the device does not support remote
1174 power.
1175 @raises AutoservRepairFailure if the repair fails for any reason.
1176
Richard Barnette82c35912012-11-20 10:09:10 -08001177 """
Scott Zawalski62bacae2013-03-05 10:40:32 -05001178 if not self.has_power():
1179 raise error.AutoservRepairMethodNA('Device does not support power.')
1180
Richard Barnette82c35912012-11-20 10:09:10 -08001181 logging.info('Attempting repair via RPM powercycle.')
1182 failed_cycles = 0
1183 self.power_cycle()
1184 while not self.wait_up(timeout=self.BOOT_TIMEOUT):
1185 failed_cycles += 1
1186 if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS:
Scott Zawalski62bacae2013-03-05 10:40:32 -05001187 raise error.AutoservRepairFailure(
1188 'Powercycled host %s %d times; device did not come back'
1189 ' online.' % (self.hostname, failed_cycles))
Richard Barnette82c35912012-11-20 10:09:10 -08001190 self.power_cycle()
1191 if failed_cycles == 0:
1192 logging.info('Powercycling was successful first time.')
1193 else:
1194 logging.info('Powercycling was successful after %d failures.',
1195 failed_cycles)
1196
1197
MK Ryu35d661e2014-09-25 17:44:10 -07001198 def _reboot_repair(self):
1199 """SSH to this host and reboot."""
1200 if not self.is_up(self._CHECK_HOST_UP_TIMEOUT_SECS):
1201 raise error.AutoservRepairMethodNA('DUT unreachable for reboot.')
1202 logging.info('Attempting repair via SSH reboot.')
1203 self.reboot(timeout=self.BOOT_TIMEOUT, wait=True)
1204
1205
Prashanth B4d8184f2014-05-05 12:22:02 -07001206 def check_device(self):
1207 """Check if a device is ssh-able, and if so, clean and verify it.
1208
1209 @raise AutoservSSHTimeout: If the ssh ping times out.
1210 @raise AutoservSshPermissionDeniedError: If ssh ping fails due to
1211 permissions.
1212 @raise AutoservSshPingHostError: For other AutoservRunErrors during
1213 ssh_ping.
1214 @raises AutoservError: As appropriate, during cleanup and verify.
1215 """
1216 self.ssh_ping()
1217 self.cleanup()
1218 self.verify()
1219
1220
Dan Shi90466352015-09-22 15:01:05 -07001221 def confirm_servo(self):
1222 """Confirm servo is initialized and verified.
1223
1224 @raise AutoservError: If servo is not initialized and verified.
1225 """
1226 if self._servo_host.required_by_test and self.servo:
1227 return
1228
1229 # Force to re-create the servo object to make sure servo is verified.
1230 logging.debug('Rebuilding the servo object.')
1231 self.servo = None
1232 self._servo_host = None
1233 self._setup_servo()
1234 if not self.servo:
1235 raise error.AutoservError('Failed to create servo object.')
1236
1237
Dan Shid07ee2e2015-09-24 14:49:25 -07001238 def _is_last_provision_failed(self):
1239 """Checks if the last provision job failed.
1240
1241 @return: True if there exists file /var/tmp/provision_failed, which
1242 indicates the last provision job failed.
1243 False if the file does not exist or the dut can't be reached.
1244 """
1245 try:
1246 result = self.run('test -f %s' % PROVISION_FAILED,
1247 ignore_status=True, timeout=5)
1248 return result.exit_status == 0
1249 except (error.AutoservRunError, error.AutoservSSHTimeout):
1250 # Default to False, for repair to try all repair method if the dut
1251 # can't be reached.
1252 return False
1253
1254
Richard Barnette82c35912012-11-20 10:09:10 -08001255 def repair_full(self):
1256 """Repair a host for repair level NO_PROTECTION.
1257
1258 This overrides the base class function for repair; it does
1259 not call back to the parent class, but instead offers a
1260 simplified implementation based on the capabilities in the
1261 Chrome OS test lab.
1262
Fang Deng5d518f42013-08-02 14:04:32 -07001263 It first verifies and repairs servo if it is a DUT in CrOS
Fang Deng03590af2013-10-07 17:34:20 -07001264 lab and a servo is attached.
Fang Deng5d518f42013-08-02 14:04:32 -07001265
Jakob Juelich82b7d1c2014-09-15 16:10:57 -07001266 This escalates in order through the following procedures and verifies
1267 the status using `self.check_device()` after each of them. This is done
1268 until both the repair and the veryfing step succeed.
1269
MK Ryu35d661e2014-09-25 17:44:10 -07001270 Escalation order of repair procedures from less intrusive to
1271 more intrusive repairs:
1272 1. SSH to the DUT and reboot.
Scott Zawalski62bacae2013-03-05 10:40:32 -05001273 2. If there's a servo for the DUT, try to power the DUT off and
1274 on.
MK Ryu35d661e2014-09-25 17:44:10 -07001275 3. If the DUT can be power-cycled via RPM, try to repair
Richard Barnette82c35912012-11-20 10:09:10 -08001276 by power-cycling.
MK Ryu35d661e2014-09-25 17:44:10 -07001277 4. Try to re-install to a known stable image using
1278 auto-update.
1279 5. If there's a servo for the DUT, try to re-install via
1280 the servo.
Richard Barnette82c35912012-11-20 10:09:10 -08001281
1282 As with the parent method, the last operation performed on
Prashanth B4d8184f2014-05-05 12:22:02 -07001283 the DUT must be to call `self.check_device()`; If that call fails the
1284 exception it raises is passed back to the caller.
J. Richard Barnettefde55fc2013-03-15 17:47:01 -07001285
Scott Zawalski62bacae2013-03-05 10:40:32 -05001286 @raises AutoservRepairTotalFailure if the repair process fails to
1287 fix the DUT.
Fang Deng5d518f42013-08-02 14:04:32 -07001288 @raises ServoHostRepairTotalFailure if the repair process fails to
1289 fix the servo host if one is attached to the DUT.
1290 @raises AutoservSshPermissionDeniedError if it is unable
1291 to ssh to the servo host due to permission error.
1292
Richard Barnette82c35912012-11-20 10:09:10 -08001293 """
Jakob Juelich82b7d1c2014-09-15 16:10:57 -07001294 # Caution: Deleting shards relies on repair to always reboot the DUT.
1295
Dan Shi4d478522014-02-14 13:46:32 -08001296 if self._servo_host and not self.servo:
Fang Deng03590af2013-10-07 17:34:20 -07001297 try:
Dan Shi4d478522014-02-14 13:46:32 -08001298 self._servo_host.repair_full()
Fang Deng03590af2013-10-07 17:34:20 -07001299 except Exception as e:
Fang Deng03590af2013-10-07 17:34:20 -07001300 logging.error('Could not create a healthy servo: %s', e)
Dan Shi4d478522014-02-14 13:46:32 -08001301 self.servo = self._servo_host.get_servo()
Fang Deng5d518f42013-08-02 14:04:32 -07001302
MK Ryu35d661e2014-09-25 17:44:10 -07001303 self.try_collect_crashlogs()
1304
Scott Zawalski62bacae2013-03-05 10:40:32 -05001305 # TODO(scottz): This should use something similar to label_decorator,
1306 # but needs to be populated in order so DUTs are repaired with the
1307 # least amount of effort.
Dan Shid07ee2e2015-09-24 14:49:25 -07001308 force_powerwash = self._is_last_provision_failed()
1309 if force_powerwash:
1310 logging.info('Last provision failed, try powerwash first.')
1311 autotest_stats.Counter(
1312 'repair_force_powerwash.TOTAL').increment()
1313 repair_funcs = [self._install_repair_with_powerwash,
Dan Shi3d7a0e12015-10-12 11:55:45 -07001314 self._servo_repair_reinstall,
1315 self._firmware_repair]
Dan Shid07ee2e2015-09-24 14:49:25 -07001316 else:
1317 repair_funcs = [self._reboot_repair,
1318 self._servo_repair_power,
1319 self._powercycle_to_repair,
1320 self._install_repair,
1321 self._install_repair_with_powerwash,
Dan Shi3d7a0e12015-10-12 11:55:45 -07001322 self._servo_repair_reinstall,
1323 self._firmware_repair]
Scott Zawalski62bacae2013-03-05 10:40:32 -05001324 errors = []
Simran Basie6130932013-10-01 14:07:52 -07001325 board = self._get_board_from_afe()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001326 for repair_func in repair_funcs:
1327 try:
1328 repair_func()
MK Ryu35d661e2014-09-25 17:44:10 -07001329 self.try_collect_crashlogs()
Prashanth B4d8184f2014-05-05 12:22:02 -07001330 self.check_device()
Gabe Black1e1c41b2015-02-04 23:55:15 -08001331 autotest_stats.Counter(
Simran Basie6130932013-10-01 14:07:52 -07001332 '%s.SUCCEEDED' % repair_func.__name__).increment()
1333 if board:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001334 autotest_stats.Counter(
Dan Shid07ee2e2015-09-24 14:49:25 -07001335 '%s.%s.SUCCEEDED' % (repair_func.__name__,
Simran Basie6130932013-10-01 14:07:52 -07001336 board)).increment()
Dan Shid07ee2e2015-09-24 14:49:25 -07001337 if force_powerwash:
1338 autotest_stats.Counter(
1339 'repair_force_powerwash.SUCCEEDED').increment()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001340 return
Simran Basie6130932013-10-01 14:07:52 -07001341 except error.AutoservRepairMethodNA as e:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001342 autotest_stats.Counter(
Simran Basie6130932013-10-01 14:07:52 -07001343 '%s.RepairNA' % repair_func.__name__).increment()
1344 if board:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001345 autotest_stats.Counter(
Dan Shid07ee2e2015-09-24 14:49:25 -07001346 '%s.%s.RepairNA' % (repair_func.__name__,
1347 board)).increment()
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001348 logging.warning('Repair function NA: %s', e)
Simran Basie6130932013-10-01 14:07:52 -07001349 errors.append(str(e))
Scott Zawalski62bacae2013-03-05 10:40:32 -05001350 except Exception as e:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001351 autotest_stats.Counter(
Simran Basie6130932013-10-01 14:07:52 -07001352 '%s.FAILED' % repair_func.__name__).increment()
1353 if board:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001354 autotest_stats.Counter(
Dan Shid07ee2e2015-09-24 14:49:25 -07001355 '%s.%s.FAILED' % (repair_func.__name__,
1356 board)).increment()
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001357 logging.warning('Failed to repair device: %s', e)
Scott Zawalski62bacae2013-03-05 10:40:32 -05001358 errors.append(str(e))
Scott Zawalski89c44dd2013-02-26 09:28:02 -05001359
Dan Shid07ee2e2015-09-24 14:49:25 -07001360 if force_powerwash:
1361 autotest_stats.Counter(
1362 'repair_force_powerwash.FAILED').increment()
Gabe Black1e1c41b2015-02-04 23:55:15 -08001363 autotest_stats.Counter('Full_Repair_Failed').increment()
Simran Basie6130932013-10-01 14:07:52 -07001364 if board:
Gabe Black1e1c41b2015-02-04 23:55:15 -08001365 autotest_stats.Counter(
Dan Shid07ee2e2015-09-24 14:49:25 -07001366 'Full_Repair_Failed.%s' % board).increment()
Scott Zawalski62bacae2013-03-05 10:40:32 -05001367 raise error.AutoservRepairTotalFailure(
1368 'All attempts at repairing the device failed:\n%s' %
1369 '\n'.join(errors))
Richard Barnette82c35912012-11-20 10:09:10 -08001370
1371
MK Ryu35d661e2014-09-25 17:44:10 -07001372 def try_collect_crashlogs(self, check_host_up=True):
1373 """
1374 Check if a host is up and logs need to be collected from the host,
1375 if yes, collect them.
1376
1377 @param check_host_up: Flag for checking host is up. Default is True.
1378 """
1379 try:
1380 crash_job = self._need_crash_logs()
1381 if crash_job:
1382 logging.debug('%s: Job %s was crashed', self._CRASHLOGS_PREFIX,
1383 crash_job)
1384 if not check_host_up or self.is_up(
1385 self._CHECK_HOST_UP_TIMEOUT_SECS):
1386 self._collect_crashlogs(crash_job)
1387 logging.debug('%s: Completed collecting logs for the '
1388 'crashed job %s', self._CRASHLOGS_PREFIX,
1389 crash_job)
1390 except Exception as e:
1391 # Exception should not result in repair failure.
1392 # Therefore, suppress all exceptions here.
1393 logging.error('%s: Failed while trying to collect crash-logs: %s',
1394 self._CRASHLOGS_PREFIX, e)
1395
1396
1397 def _need_crash_logs(self):
1398 """Get the value of need_crash_logs attribute of this host.
1399
1400 @return: Value string of need_crash_logs attribute
1401 None if there is no need_crash_logs attribute
1402 """
1403 attrs = self._AFE.get_host_attribute(constants.CRASHLOGS_HOST_ATTRIBUTE,
1404 hostname=self.hostname)
1405 assert len(attrs) < 2
1406 return attrs[0].value if attrs else None
1407
1408
1409 def _collect_crashlogs(self, job_id):
1410 """Grab logs from the host where a job was crashed.
1411
1412 First, check if PRIOR_LOGS_DIR exists in the host.
1413 If yes, collect them.
1414 Otherwise, check if a lab-machine marker (_LAB_MACHINE_FILE) exists
1415 in the host.
1416 If yes, the host was repaired automatically, and we collect normal
1417 system logs.
1418
1419 @param job_id: Id of the job that was crashed.
1420 """
1421 crashlogs_dir = crashcollect.get_crashinfo_dir(self,
1422 constants.CRASHLOGS_DEST_DIR_PREFIX)
1423 flag_prior_logs = False
1424
1425 if self.path_exists(client_constants.PRIOR_LOGS_DIR):
1426 flag_prior_logs = True
1427 self._collect_prior_logs(crashlogs_dir)
1428 elif self.path_exists(self._LAB_MACHINE_FILE):
1429 self._collect_system_logs(crashlogs_dir)
1430 else:
1431 logging.warning('%s: Host was manually re-installed without '
1432 '--lab_preserve_log option. Skip collecting '
1433 'crash-logs.', self._CRASHLOGS_PREFIX)
1434
1435 # We make crash collection be one-time effort.
1436 # _collect_prior_logs() and _collect_system_logs() will not throw
1437 # any exception, and following codes will be executed even when
1438 # those methods fail.
1439 # _collect_crashlogs() is called only when the host is up (refer
1440 # to try_collect_crashlogs()). We assume _collect_prior_logs() and
1441 # _collect_system_logs() fail rarely when the host is up.
1442 # In addition, it is not clear how many times we should try crash
1443 # collection again while not triggering next repair unnecessarily.
1444 # Threfore, we try crash collection one time.
1445
1446 # Create a marker file as soon as log collection is done.
1447 # Leave the job id to this marker for gs_offloader to consume.
1448 marker_file = os.path.join(crashlogs_dir, constants.CRASHLOGS_MARKER)
1449 with open(marker_file, 'a') as f:
1450 f.write('%s\n' % job_id)
1451
1452 # Remove need_crash_logs attribute
1453 logging.debug('%s: Remove attribute need_crash_logs from host %s',
1454 self._CRASHLOGS_PREFIX, self.hostname)
1455 self._AFE.set_host_attribute(constants.CRASHLOGS_HOST_ATTRIBUTE,
1456 None, hostname=self.hostname)
1457
1458 if flag_prior_logs:
1459 logging.debug('%s: Remove %s from host %s', self._CRASHLOGS_PREFIX,
1460 client_constants.PRIOR_LOGS_DIR, self.hostname)
1461 self.run('rm -rf %s; sync' % client_constants.PRIOR_LOGS_DIR)
1462 # Wait for a few seconds to make sure the prior command is
1463 # done deep through storage.
1464 time.sleep(self._SAFE_WAIT_SECS)
1465
1466
1467 def _collect_prior_logs(self, crashlogs_dir):
1468 """Grab prior logs that were stashed before re-installing a host.
1469
1470 @param crashlogs_dir: Directory path where crash-logs are stored.
1471 """
1472 logging.debug('%s: Found %s, collecting them...',
1473 self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR)
1474 try:
1475 self.collect_logs(client_constants.PRIOR_LOGS_DIR,
1476 crashlogs_dir, False)
1477 logging.debug('%s: %s is collected',
1478 self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR)
1479 except Exception as e:
1480 logging.error('%s: Failed to collect %s: %s',
1481 self._CRASHLOGS_PREFIX, client_constants.PRIOR_LOGS_DIR,
1482 e)
1483
1484
1485 def _collect_system_logs(self, crashlogs_dir):
1486 """Grab normal system logs from a host.
1487
1488 @param crashlogs_dir: Directory path where crash-logs are stored.
1489 """
1490 logging.debug('%s: Found %s, collecting system logs...',
1491 self._CRASHLOGS_PREFIX, self._LAB_MACHINE_FILE)
1492 sources = server_utils.parse_simple_config(self._LOGS_TO_COLLECT_FILE)
1493 for src in sources:
1494 try:
1495 if self.path_exists(src):
1496 logging.debug('%s: Collecting %s...',
1497 self._CRASHLOGS_PREFIX, src)
1498 dest = server_utils.concat_path_except_last(
1499 crashlogs_dir, src)
1500 self.collect_logs(src, dest, False)
1501 logging.debug('%s: %s is collected',
1502 self._CRASHLOGS_PREFIX, src)
1503 except Exception as e:
1504 logging.error('%s: Failed to collect %s: %s',
1505 self._CRASHLOGS_PREFIX, src, e)
1506
1507
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001508 def close(self):
Fang Deng0ca40e22013-08-27 17:47:44 -07001509 super(CrosHost, self).close()
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001510
1511
Dan Shi49ca0932014-11-14 11:22:27 -08001512 def get_power_supply_info(self):
1513 """Get the output of power_supply_info.
1514
1515 power_supply_info outputs the info of each power supply, e.g.,
1516 Device: Line Power
1517 online: no
1518 type: Mains
1519 voltage (V): 0
1520 current (A): 0
1521 Device: Battery
1522 state: Discharging
1523 percentage: 95.9276
1524 technology: Li-ion
1525
1526 Above output shows two devices, Line Power and Battery, with details of
1527 each device listed. This function parses the output into a dictionary,
1528 with key being the device name, and value being a dictionary of details
1529 of the device info.
1530
1531 @return: The dictionary of power_supply_info, e.g.,
1532 {'Line Power': {'online': 'yes', 'type': 'main'},
1533 'Battery': {'vendor': 'xyz', 'percentage': '100'}}
Dan Shie9b765d2014-12-29 16:59:49 -08001534 @raise error.AutoservRunError if power_supply_info tool is not found in
1535 the DUT. Caller should handle this error to avoid false failure
1536 on verification.
Dan Shi49ca0932014-11-14 11:22:27 -08001537 """
1538 result = self.run('power_supply_info').stdout.strip()
1539 info = {}
1540 device_name = None
1541 device_info = {}
1542 for line in result.split('\n'):
1543 pair = [v.strip() for v in line.split(':')]
1544 if len(pair) != 2:
1545 continue
1546 if pair[0] == 'Device':
1547 if device_name:
1548 info[device_name] = device_info
1549 device_name = pair[1]
1550 device_info = {}
1551 else:
1552 device_info[pair[0]] = pair[1]
1553 if device_name and not device_name in info:
1554 info[device_name] = device_info
1555 return info
1556
1557
1558 def get_battery_percentage(self):
1559 """Get the battery percentage.
1560
1561 @return: The percentage of battery level, value range from 0-100. Return
1562 None if the battery info cannot be retrieved.
1563 """
1564 try:
1565 info = self.get_power_supply_info()
1566 logging.info(info)
1567 return float(info['Battery']['percentage'])
Dan Shie9b765d2014-12-29 16:59:49 -08001568 except (KeyError, ValueError, error.AutoservRunError):
Dan Shi49ca0932014-11-14 11:22:27 -08001569 return None
1570
1571
1572 def is_ac_connected(self):
1573 """Check if the dut has power adapter connected and charging.
1574
1575 @return: True if power adapter is connected and charging.
1576 """
1577 try:
1578 info = self.get_power_supply_info()
1579 return info['Line Power']['online'] == 'yes'
Dan Shie9b765d2014-12-29 16:59:49 -08001580 except (KeyError, error.AutoservRunError):
1581 return None
Dan Shi49ca0932014-11-14 11:22:27 -08001582
1583
Simran Basi5e6339a2013-03-21 11:34:32 -07001584 def _cleanup_poweron(self):
1585 """Special cleanup method to make sure hosts always get power back."""
1586 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
1587 hosts = afe.get_hosts(hostname=self.hostname)
1588 if not hosts or not (self._RPM_OUTLET_CHANGED in
1589 hosts[0].attributes):
1590 return
1591 logging.debug('This host has recently interacted with the RPM'
1592 ' Infrastructure. Ensuring power is on.')
1593 try:
1594 self.power_on()
Dan Shi7dca56e2014-11-11 17:07:56 -08001595 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None,
1596 hostname=self.hostname)
Simran Basi5e6339a2013-03-21 11:34:32 -07001597 except rpm_client.RemotePowerException:
Simran Basi5e6339a2013-03-21 11:34:32 -07001598 logging.error('Failed to turn Power On for this host after '
1599 'cleanup through the RPM Infrastructure.')
Gabe Blackb72f4fb2015-01-20 16:47:13 -08001600 autotest_es.post(
Dan Shi7dca56e2014-11-11 17:07:56 -08001601 type_str='RPM_poweron_failure',
1602 metadata={'hostname': self.hostname})
Dan Shi49ca0932014-11-14 11:22:27 -08001603
1604 battery_percentage = self.get_battery_percentage()
Dan Shif01ebe22014-12-05 13:10:57 -08001605 if battery_percentage and battery_percentage < 50:
Dan Shi49ca0932014-11-14 11:22:27 -08001606 raise
1607 elif self.is_ac_connected():
1608 logging.info('The device has power adapter connected and '
1609 'charging. No need to try to turn RPM on '
1610 'again.')
1611 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None,
1612 hostname=self.hostname)
1613 logging.info('Battery level is now at %s%%. The device may '
1614 'still have enough power to run test, so no '
1615 'exception will be raised.', battery_percentage)
1616
Simran Basi5e6339a2013-03-21 11:34:32 -07001617
beepsc87ff602013-07-31 21:53:00 -07001618 def _is_factory_image(self):
1619 """Checks if the image on the DUT is a factory image.
1620
1621 @return: True if the image on the DUT is a factory image.
1622 False otherwise.
1623 """
1624 result = self.run('[ -f /root/.factory_test ]', ignore_status=True)
1625 return result.exit_status == 0
1626
1627
1628 def _restart_ui(self):
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001629 """Restart the Chrome UI.
beepsc87ff602013-07-31 21:53:00 -07001630
1631 @raises: FactoryImageCheckerException for factory images, since
1632 we cannot attempt to restart ui on them.
1633 error.AutoservRunError for any other type of error that
1634 occurs while restarting ui.
1635 """
1636 if self._is_factory_image():
Dan Shi549fb822015-03-24 18:01:11 -07001637 raise FactoryImageCheckerException('Cannot restart ui on factory '
1638 'images')
beepsc87ff602013-07-31 21:53:00 -07001639
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001640 # TODO(jrbarnette): The command to stop/start the ui job
1641 # should live inside cros_ui, too. However that would seem
1642 # to imply interface changes to the existing start()/restart()
1643 # functions, which is a bridge too far (for now).
J. Richard Barnette6069aa12015-06-08 09:10:24 -07001644 prompt = cros_ui.get_chrome_session_ident(self)
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001645 self.run('stop ui; start ui')
1646 cros_ui.wait_for_chrome_ready(prompt, self)
beepsc87ff602013-07-31 21:53:00 -07001647
1648
Dan Shi549fb822015-03-24 18:01:11 -07001649 def get_release_version(self):
1650 """Get the value of attribute CHROMEOS_RELEASE_VERSION from lsb-release.
1651
1652 @returns The version string in lsb-release, under attribute
1653 CHROMEOS_RELEASE_VERSION.
1654 """
1655 lsb_release_content = self.run(
1656 'cat "%s"' % client_constants.LSB_RELEASE).stdout.strip()
1657 return lsbrelease_utils.get_chromeos_release_version(
1658 lsb_release_content=lsb_release_content)
1659
1660
1661 def verify_cros_version_label(self):
1662 """ Make sure host's cros-version label match the actual image in dut.
1663
1664 Remove any cros-version: label that doesn't match that installed in
1665 the dut.
1666
1667 @param raise_error: Set to True to raise exception if any mismatch found
1668
1669 @raise error.AutoservError: If any mismatch between cros-version label
1670 and the build installed in dut is found.
1671 """
1672 labels = self._AFE.get_labels(
1673 name__startswith=ds_constants.VERSION_PREFIX,
1674 host__hostname=self.hostname)
1675 mismatch_found = False
1676 if labels:
1677 # Get CHROMEOS_RELEASE_VERSION from lsb-release, e.g., 6908.0.0.
1678 # Note that it's different from cros-version label, which has
1679 # builder and branch info, e.g.,
1680 # cros-version:peppy-release/R43-6908.0.0
1681 release_version = self.get_release_version()
1682 host_list = [self.hostname]
1683 for label in labels:
1684 # Remove any cros-version label that does not match
1685 # release_version.
1686 build_version = label.name[len(ds_constants.VERSION_PREFIX):]
1687 if not utils.version_match(build_version, release_version):
1688 logging.warn('cros-version label "%s" does not match '
1689 'release version %s. Removing the label.',
1690 label.name, release_version)
1691 label.remove_hosts(hosts=host_list)
1692 mismatch_found = True
1693 if mismatch_found:
Dan Shi1057bae2015-03-30 11:35:09 -07001694 autotest_es.post(use_http=True,
1695 type_str='cros_version_label_mismatch',
1696 metadata={'hostname': self.hostname})
Dan Shi549fb822015-03-24 18:01:11 -07001697 raise error.AutoservError('The host has wrong cros-version label.')
1698
1699
beepsc87ff602013-07-31 21:53:00 -07001700 def cleanup(self):
MK Ryu35d661e2014-09-25 17:44:10 -07001701 self.run('rm -f %s' % client_constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001702 try:
beepsc87ff602013-07-31 21:53:00 -07001703 self._restart_ui()
1704 except (error.AutotestRunError, error.AutoservRunError,
1705 FactoryImageCheckerException):
Ilja H. Friedel04be2bd2014-05-07 21:29:59 -07001706 logging.warning('Unable to restart ui, rebooting device.')
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001707 # Since restarting the UI fails fall back to normal Autotest
1708 # cleanup routines, i.e. reboot the machine.
Fang Deng0ca40e22013-08-27 17:47:44 -07001709 super(CrosHost, self).cleanup()
Simran Basi5e6339a2013-03-21 11:34:32 -07001710 # Check if the rpm outlet was manipulated.
Simran Basid5e5e272012-09-24 15:23:59 -07001711 if self.has_power():
Simran Basi5e6339a2013-03-21 11:34:32 -07001712 self._cleanup_poweron()
Dan Shi549fb822015-03-24 18:01:11 -07001713 self.verify_cros_version_label()
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001714
1715
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001716 def reboot(self, **dargs):
1717 """
1718 This function reboots the site host. The more generic
1719 RemoteHost.reboot() performs sync and sleeps for 5
1720 seconds. This is not necessary for Chrome OS devices as the
1721 sync should be finished in a short time during the reboot
1722 command.
1723 """
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001724 if 'reboot_cmd' not in dargs:
Doug Anderson7d5aeb22014-02-27 15:12:17 -08001725 reboot_timeout = dargs.get('reboot_timeout', 10)
J. Richard Barnette9af19632015-09-25 12:18:03 -07001726 dargs['reboot_cmd'] = ('sleep 1; '
1727 'reboot & sleep %d; '
1728 'reboot -f' % reboot_timeout)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001729 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001730 if 'fastsync' not in dargs:
1731 dargs['fastsync'] = True
Michael Liangda8c60a2014-06-03 13:24:51 -07001732
Charlie Mooneya8e6dab2014-05-29 14:37:55 -07001733 # For purposes of logging reboot times:
1734 # Get the board name i.e. 'daisy_spring'
Michael Liangca4f5a62014-07-10 15:45:13 -07001735 board_fullname = self.get_board()
1736
1737 # Strip the prefix and add it to dargs.
1738 dargs['board'] = board_fullname[board_fullname.find(':')+1:]
Fang Deng0ca40e22013-08-27 17:47:44 -07001739 super(CrosHost, self).reboot(**dargs)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001740
1741
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001742 def suspend(self, **dargs):
1743 """
1744 This function suspends the site host.
1745 """
1746 suspend_time = dargs.get('suspend_time', 60)
1747 dargs['timeout'] = suspend_time
1748 if 'suspend_cmd' not in dargs:
J. Richard Barnette9af19632015-09-25 12:18:03 -07001749 dargs['suspend_cmd'] = ' && '.join([
1750 'echo 0 > /sys/class/rtc/rtc0/wakealarm',
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001751 'echo +%d > /sys/class/rtc/rtc0/wakealarm' % suspend_time,
J. Richard Barnette9af19632015-09-25 12:18:03 -07001752 'powerd_dbus_suspend --delay=0'])
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001753 super(CrosHost, self).suspend(**dargs)
1754
1755
Simran Basiec564392014-08-25 16:48:09 -07001756 def upstart_status(self, service_name):
1757 """Check the status of an upstart init script.
1758
1759 @param service_name: Service to look up.
1760
1761 @returns True if the service is running, False otherwise.
1762 """
1763 return self.run('status %s | grep start/running' %
1764 service_name).stdout.strip() != ''
1765
1766
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001767 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001768 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001769
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001770 Tests for the following conditions:
1771 1. All conditions tested by the parent version of this
1772 function.
1773 2. Sufficient space in /mnt/stateful_partition.
Fang Deng6b05f5b2013-03-20 13:42:11 -07001774 3. Sufficient space in /mnt/stateful_partition/encrypted.
1775 4. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001776
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001777 """
MK Ryu35d661e2014-09-25 17:44:10 -07001778 # Check if a job was crashed on this host.
1779 # If yes, avoid verification until crash-logs are collected.
1780 if self._need_crash_logs():
1781 raise error.AutoservCrashLogCollectRequired(
1782 'Need to collect crash-logs before verification')
1783
Fang Deng0ca40e22013-08-27 17:47:44 -07001784 super(CrosHost, self).verify_software()
Dan Shib8540a52015-07-16 14:18:23 -07001785 default_kilo_inodes_required = CONFIG.get_config_value(
1786 'SERVER', 'kilo_inodes_required', type=int, default=100)
1787 board = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
1788 kilo_inodes_required = CONFIG.get_config_value(
1789 'SERVER', 'kilo_inodes_required_%s' % board,
1790 type=int, default=default_kilo_inodes_required)
1791 self.check_inodes('/mnt/stateful_partition', kilo_inodes_required)
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001792 self.check_diskspace(
1793 '/mnt/stateful_partition',
Dan Shib8540a52015-07-16 14:18:23 -07001794 CONFIG.get_config_value(
Fang Deng6b05f5b2013-03-20 13:42:11 -07001795 'SERVER', 'gb_diskspace_required', type=float,
1796 default=20.0))
Gaurav Shahe448af82014-06-19 15:18:59 -07001797 encrypted_stateful_path = '/mnt/stateful_partition/encrypted'
1798 # Not all targets build with encrypted stateful support.
1799 if self.path_exists(encrypted_stateful_path):
1800 self.check_diskspace(
1801 encrypted_stateful_path,
Dan Shib8540a52015-07-16 14:18:23 -07001802 CONFIG.get_config_value(
Gaurav Shahe448af82014-06-19 15:18:59 -07001803 'SERVER', 'gb_encrypted_diskspace_required', type=float,
1804 default=0.1))
beepsc87ff602013-07-31 21:53:00 -07001805
Simran Basiec564392014-08-25 16:48:09 -07001806 if not self.upstart_status('system-services'):
Prashanth B5d0a0512014-04-25 12:26:08 -07001807 raise error.AutoservError('Chrome failed to reach login. '
1808 'System services not running.')
1809
beepsc87ff602013-07-31 21:53:00 -07001810 # Factory images don't run update engine,
1811 # goofy controls dbus on these DUTs.
1812 if not self._is_factory_image():
1813 self.run('update_engine_client --status')
Scott Zawalskifbca4a92013-03-04 15:56:42 -05001814 # Makes sure python is present, loads and can use built in functions.
1815 # We have seen cases where importing cPickle fails with undefined
1816 # symbols in cPickle.so.
1817 self.run('python -c "import cPickle"')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001818
Dan Shi549fb822015-03-24 18:01:11 -07001819 self.verify_cros_version_label()
1820
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001821
Dan Shi49ca0932014-11-14 11:22:27 -08001822 def verify_hardware(self):
1823 """Verify hardware system of a Chrome OS system.
1824
1825 Check following hardware conditions:
1826 1. Battery level.
1827 2. Is power adapter connected.
1828 """
1829 logging.info('Battery percentage: %s', self.get_battery_percentage())
Dan Shie9b765d2014-12-29 16:59:49 -08001830 if self.is_ac_connected() is None:
1831 logging.info('Can not determine if the device has power adapter '
1832 'connected.')
1833 else:
1834 logging.info('Device %s power adapter connected and charging.',
1835 'has' if self.is_ac_connected() else 'does not have')
Dan Shi49ca0932014-11-14 11:22:27 -08001836
1837
Fang Deng96667ca2013-08-01 17:46:18 -07001838 def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None,
1839 connect_timeout=None, alive_interval=None):
1840 """Override default make_ssh_command to use options tuned for Chrome OS.
1841
1842 Tuning changes:
1843 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH
1844 connection failure. Consistency with remote_access.sh.
1845
Samuel Tan2ce155b2015-06-23 18:24:38 -07001846 - ServerAliveInterval=900; which causes SSH to ping connection every
1847 900 seconds. In conjunction with ServerAliveCountMax ensures
1848 that if the connection dies, Autotest will bail out.
Fang Deng96667ca2013-08-01 17:46:18 -07001849 Originally tried 60 secs, but saw frequent job ABORTS where
Samuel Tan2ce155b2015-06-23 18:24:38 -07001850 the test completed successfully. Later increased from 180 seconds to
1851 900 seconds to account for tests where the DUT is suspended for
1852 longer periods of time.
Fang Deng96667ca2013-08-01 17:46:18 -07001853
1854 - ServerAliveCountMax=3; consistency with remote_access.sh.
1855
1856 - ConnectAttempts=4; reduce flakiness in connection errors;
1857 consistency with remote_access.sh.
1858
1859 - UserKnownHostsFile=/dev/null; we don't care about the keys.
1860 Host keys change with every new installation, don't waste
1861 memory/space saving them.
1862
1863 - SSH protocol forced to 2; needed for ServerAliveInterval.
1864
1865 @param user User name to use for the ssh connection.
1866 @param port Port on the target host to use for ssh connection.
1867 @param opts Additional options to the ssh command.
1868 @param hosts_file Ignored.
1869 @param connect_timeout Ignored.
1870 @param alive_interval Ignored.
1871 """
Aviv Keshetc5947fa2013-09-04 14:06:29 -07001872 base_command = ('/usr/bin/ssh -a -x %s %s %s'
1873 ' -o StrictHostKeyChecking=no'
Fang Deng96667ca2013-08-01 17:46:18 -07001874 ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes'
Samuel Tan2ce155b2015-06-23 18:24:38 -07001875 ' -o ConnectTimeout=30 -o ServerAliveInterval=900'
Fang Deng96667ca2013-08-01 17:46:18 -07001876 ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4'
1877 ' -o Protocol=2 -l %s -p %d')
Aviv Keshetc5947fa2013-09-04 14:06:29 -07001878 return base_command % (self._ssh_verbosity_flag, self._ssh_options,
1879 opts, user, port)
Jason Abeleb6f924f2013-11-13 16:01:54 -08001880 def syslog(self, message, tag='autotest'):
1881 """Logs a message to syslog on host.
1882
1883 @param message String message to log into syslog
1884 @param tag String tag prefix for syslog
1885
1886 """
1887 self.run('logger -t "%s" "%s"' % (tag, message))
1888
1889
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001890 def _ping_check_status(self, status):
1891 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001892
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001893 @param status Check the ping status against this value.
1894 @return True iff `status` and the result of ping are the same
1895 (i.e. both True or both False).
1896
1897 """
1898 ping_val = utils.ping(self.hostname, tries=1, deadline=1)
1899 return not (status ^ (ping_val == 0))
1900
1901 def _ping_wait_for_status(self, status, timeout):
1902 """Wait for the host to have a given status (UP or DOWN).
1903
1904 Status is checked by polling. Polling will not last longer
1905 than the number of seconds in `timeout`. The polling
1906 interval will be long enough that only approximately
1907 _PING_WAIT_COUNT polling cycles will be executed, subject
1908 to a maximum interval of about one minute.
1909
1910 @param status Waiting will stop immediately if `ping` of the
1911 host returns this status.
1912 @param timeout Poll for at most this many seconds.
1913 @return True iff the host status from `ping` matched the
1914 requested status at the time of return.
1915
1916 """
1917 # _ping_check_status() takes about 1 second, hence the
1918 # "- 1" in the formula below.
1919 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
1920 end_time = time.time() + timeout
1921 while time.time() <= end_time:
1922 if self._ping_check_status(status):
1923 return True
1924 if poll_interval > 0:
1925 time.sleep(poll_interval)
1926
1927 # The last thing we did was sleep(poll_interval), so it may
1928 # have been too long since the last `ping`. Check one more
1929 # time, just to be sure.
1930 return self._ping_check_status(status)
1931
1932 def ping_wait_up(self, timeout):
1933 """Wait for the host to respond to `ping`.
1934
1935 N.B. This method is not a reliable substitute for
1936 `wait_up()`, because a host that responds to ping will not
1937 necessarily respond to ssh. This method should only be used
1938 if the target DUT can be considered functional even if it
1939 can't be reached via ssh.
1940
1941 @param timeout Minimum time to allow before declaring the
1942 host to be non-responsive.
1943 @return True iff the host answered to ping before the timeout.
1944
1945 """
1946 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001947
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001948 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001949 """Wait until the host no longer responds to `ping`.
1950
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001951 This function can be used as a slightly faster version of
1952 `wait_down()`, by avoiding potentially long ssh timeouts.
1953
1954 @param timeout Minimum time to allow for the host to become
1955 non-responsive.
1956 @return True iff the host quit answering ping before the
1957 timeout.
1958
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001959 """
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001960 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001961
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001962 def test_wait_for_sleep(self, sleep_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001963 """Wait for the client to enter low-power sleep mode.
1964
1965 The test for "is asleep" can't distinguish a system that is
1966 powered off; to confirm that the unit was asleep, it is
1967 necessary to force resume, and then call
1968 `test_wait_for_resume()`.
1969
1970 This function is expected to be called from a test as part
1971 of a sequence like the following:
1972
1973 ~~~~~~~~
1974 boot_id = host.get_boot_id()
1975 # trigger sleep on the host
1976 host.test_wait_for_sleep()
1977 # trigger resume on the host
1978 host.test_wait_for_resume(boot_id)
1979 ~~~~~~~~
1980
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001981 @param sleep_timeout time limit in seconds to allow the host sleep.
1982
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001983 @exception TestFail The host did not go to sleep within
1984 the allowed time.
1985 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001986 if sleep_timeout is None:
1987 sleep_timeout = self.SLEEP_TIMEOUT
1988
1989 if not self.ping_wait_down(timeout=sleep_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001990 raise error.TestFail(
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001991 'client failed to sleep after %d seconds' % sleep_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001992
1993
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08001994 def test_wait_for_resume(self, old_boot_id, resume_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001995 """Wait for the client to resume from low-power sleep mode.
1996
1997 The `old_boot_id` parameter should be the value from
1998 `get_boot_id()` obtained prior to entering sleep mode. A
1999 `TestFail` exception is raised if the boot id changes.
2000
2001 See @ref test_wait_for_sleep for more on this function's
2002 usage.
2003
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08002004 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002005 target host went to sleep.
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002006 @param resume_timeout time limit in seconds to allow the host up.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002007
2008 @exception TestFail The host did not respond within the
2009 allowed time.
2010 @exception TestFail The host responded, but the boot id test
2011 indicated a reboot rather than a sleep
2012 cycle.
2013 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002014 if resume_timeout is None:
2015 resume_timeout = self.RESUME_TIMEOUT
2016
2017 if not self.wait_up(timeout=resume_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002018 raise error.TestFail(
2019 'client failed to resume from sleep after %d seconds' %
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002020 resume_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002021 else:
2022 new_boot_id = self.get_boot_id()
2023 if new_boot_id != old_boot_id:
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002024 logging.error('client rebooted (old boot %s, new boot %s)',
2025 old_boot_id, new_boot_id)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002026 raise error.TestFail(
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002027 'client rebooted, but sleep was expected')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002028
2029
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002030 def test_wait_for_shutdown(self, shutdown_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002031 """Wait for the client to shut down.
2032
2033 The test for "has shut down" can't distinguish a system that
2034 is merely asleep; to confirm that the unit was down, it is
2035 necessary to force boot, and then call test_wait_for_boot().
2036
2037 This function is expected to be called from a test as part
2038 of a sequence like the following:
2039
2040 ~~~~~~~~
2041 boot_id = host.get_boot_id()
2042 # trigger shutdown on the host
2043 host.test_wait_for_shutdown()
2044 # trigger boot on the host
2045 host.test_wait_for_boot(boot_id)
2046 ~~~~~~~~
2047
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002048 @param shutdown_timeout time limit in seconds to allow the host down.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002049 @exception TestFail The host did not shut down within the
2050 allowed time.
2051 """
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002052 if shutdown_timeout is None:
2053 shutdown_timeout = self.SHUTDOWN_TIMEOUT
2054
2055 if not self.ping_wait_down(timeout=shutdown_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002056 raise error.TestFail(
2057 'client failed to shut down after %d seconds' %
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002058 shutdown_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002059
2060
2061 def test_wait_for_boot(self, old_boot_id=None):
2062 """Wait for the client to boot from cold power.
2063
2064 The `old_boot_id` parameter should be the value from
2065 `get_boot_id()` obtained prior to shutting down. A
2066 `TestFail` exception is raised if the boot id does not
2067 change. The boot id test is omitted if `old_boot_id` is not
2068 specified.
2069
2070 See @ref test_wait_for_shutdown for more on this function's
2071 usage.
2072
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08002073 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002074 shut down.
2075
2076 @exception TestFail The host did not respond within the
2077 allowed time.
2078 @exception TestFail The host responded, but the boot id test
2079 indicated that there was no reboot.
2080 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07002081 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002082 raise error.TestFail(
2083 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07002084 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002085 elif old_boot_id:
2086 if self.get_boot_id() == old_boot_id:
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002087 logging.error('client not rebooted (boot %s)',
2088 old_boot_id)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002089 raise error.TestFail(
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002090 'client is back up, but did not reboot')
Simran Basid5e5e272012-09-24 15:23:59 -07002091
2092
2093 @staticmethod
2094 def check_for_rpm_support(hostname):
2095 """For a given hostname, return whether or not it is powered by an RPM.
2096
Simran Basi1df55112013-09-06 11:25:09 -07002097 @param hostname: hostname to check for rpm support.
2098
Simran Basid5e5e272012-09-24 15:23:59 -07002099 @return None if this host does not follows the defined naming format
2100 for RPM powered DUT's in the lab. If it does follow the format,
2101 it returns a regular expression MatchObject instead.
2102 """
Fang Dengbaff9082015-01-06 13:46:15 -08002103 return re.match(CrosHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07002104
2105
2106 def has_power(self):
2107 """For this host, return whether or not it is powered by an RPM.
2108
2109 @return True if this host is in the CROS lab and follows the defined
2110 naming format.
2111 """
Fang Deng0ca40e22013-08-27 17:47:44 -07002112 return CrosHost.check_for_rpm_support(self.hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07002113
2114
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002115 def _set_power(self, state, power_method):
2116 """Sets the power to the host via RPM, Servo or manual.
2117
2118 @param state Specifies which power state to set to DUT
2119 @param power_method Specifies which method of power control to
2120 use. By default "RPM" will be used. Valid values
2121 are the strings "RPM", "manual", "servoj10".
2122
2123 """
2124 ACCEPTABLE_STATES = ['ON', 'OFF']
2125
2126 if state.upper() not in ACCEPTABLE_STATES:
2127 raise error.TestError('State must be one of: %s.'
2128 % (ACCEPTABLE_STATES,))
2129
2130 if power_method == self.POWER_CONTROL_SERVO:
2131 logging.info('Setting servo port J10 to %s', state)
2132 self.servo.set('prtctl3_pwren', state.lower())
2133 time.sleep(self._USB_POWER_TIMEOUT)
2134 elif power_method == self.POWER_CONTROL_MANUAL:
2135 logging.info('You have %d seconds to set the AC power to %s.',
2136 self._POWER_CYCLE_TIMEOUT, state)
2137 time.sleep(self._POWER_CYCLE_TIMEOUT)
2138 else:
2139 if not self.has_power():
2140 raise error.TestFail('DUT does not have RPM connected.')
Simran Basi5e6339a2013-03-21 11:34:32 -07002141 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
2142 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, True,
2143 hostname=self.hostname)
Simran Basi1df55112013-09-06 11:25:09 -07002144 rpm_client.set_power(self.hostname, state.upper(), timeout_mins=5)
Simran Basid5e5e272012-09-24 15:23:59 -07002145
2146
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002147 def power_off(self, power_method=POWER_CONTROL_RPM):
2148 """Turn off power to this host via RPM, Servo or manual.
2149
2150 @param power_method Specifies which method of power control to
2151 use. By default "RPM" will be used. Valid values
2152 are the strings "RPM", "manual", "servoj10".
2153
2154 """
2155 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07002156
2157
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002158 def power_on(self, power_method=POWER_CONTROL_RPM):
2159 """Turn on power to this host via RPM, Servo or manual.
2160
2161 @param power_method Specifies which method of power control to
2162 use. By default "RPM" will be used. Valid values
2163 are the strings "RPM", "manual", "servoj10".
2164
2165 """
2166 self._set_power('ON', power_method)
2167
2168
2169 def power_cycle(self, power_method=POWER_CONTROL_RPM):
2170 """Cycle power to this host by turning it OFF, then ON.
2171
2172 @param power_method Specifies which method of power control to
2173 use. By default "RPM" will be used. Valid values
2174 are the strings "RPM", "manual", "servoj10".
2175
2176 """
2177 if power_method in (self.POWER_CONTROL_SERVO,
2178 self.POWER_CONTROL_MANUAL):
2179 self.power_off(power_method=power_method)
2180 time.sleep(self._POWER_CYCLE_TIMEOUT)
2181 self.power_on(power_method=power_method)
2182 else:
2183 rpm_client.set_power(self.hostname, 'CYCLE')
Simran Basic6f1f7a2012-10-16 10:47:46 -07002184
2185
2186 def get_platform(self):
2187 """Determine the correct platform label for this host.
2188
2189 @returns a string representing this host's platform.
2190 """
2191 crossystem = utils.Crossystem(self)
2192 crossystem.init()
2193 # Extract fwid value and use the leading part as the platform id.
2194 # fwid generally follow the format of {platform}.{firmware version}
2195 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
2196 platform = crossystem.fwid().split('.')[0].lower()
2197 # Newer platforms start with 'Google_' while the older ones do not.
2198 return platform.replace('google_', '')
2199
2200
Hung-ying Tyanb1328032014-04-01 14:18:54 +08002201 def get_architecture(self):
2202 """Determine the correct architecture label for this host.
2203
2204 @returns a string representing this host's architecture.
2205 """
2206 crossystem = utils.Crossystem(self)
2207 crossystem.init()
2208 return crossystem.arch()
2209
2210
Luis Lozano40b7d0d2014-01-17 15:12:06 -08002211 def get_chrome_version(self):
2212 """Gets the Chrome version number and milestone as strings.
2213
2214 Invokes "chrome --version" to get the version number and milestone.
2215
2216 @return A tuple (chrome_ver, milestone) where "chrome_ver" is the
2217 current Chrome version number as a string (in the form "W.X.Y.Z")
2218 and "milestone" is the first component of the version number
2219 (the "W" from "W.X.Y.Z"). If the version number cannot be parsed
2220 in the "W.X.Y.Z" format, the "chrome_ver" will be the full output
2221 of "chrome --version" and the milestone will be the empty string.
2222
2223 """
MK Ryu35d661e2014-09-25 17:44:10 -07002224 version_string = self.run(client_constants.CHROME_VERSION_COMMAND).stdout
Luis Lozano40b7d0d2014-01-17 15:12:06 -08002225 return utils.parse_chrome_version(version_string)
2226
Aviv Keshet74c89a92013-02-04 15:18:30 -08002227 @label_decorator()
Simran Basic6f1f7a2012-10-16 10:47:46 -07002228 def get_board(self):
2229 """Determine the correct board label for this host.
2230
2231 @returns a string representing this host's board.
2232 """
2233 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
2234 run_method=self.run)
2235 board = release_info['CHROMEOS_RELEASE_BOARD']
2236 # Devices in the lab generally have the correct board name but our own
2237 # development devices have {board_name}-signed-{key_type}. The board
2238 # name may also begin with 'x86-' which we need to keep.
Simran Basi833814b2013-01-29 13:13:43 -08002239 board_format_string = ds_constants.BOARD_PREFIX + '%s'
Simran Basic6f1f7a2012-10-16 10:47:46 -07002240 if 'x86' not in board:
Simran Basi833814b2013-01-29 13:13:43 -08002241 return board_format_string % board.split('-')[0]
2242 return board_format_string % '-'.join(board.split('-')[0:2])
Simran Basic6f1f7a2012-10-16 10:47:46 -07002243
2244
Aviv Keshet74c89a92013-02-04 15:18:30 -08002245 @label_decorator('lightsensor')
Simran Basic6f1f7a2012-10-16 10:47:46 -07002246 def has_lightsensor(self):
2247 """Determine the correct board label for this host.
2248
2249 @returns the string 'lightsensor' if this host has a lightsensor or
2250 None if it does not.
2251 """
2252 search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % (
Richard Barnette82c35912012-11-20 10:09:10 -08002253 self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES))
Simran Basic6f1f7a2012-10-16 10:47:46 -07002254 try:
2255 # Run the search cmd following the symlinks. Stderr_tee is set to
2256 # None as there can be a symlink loop, but the command will still
2257 # execute correctly with a few messages printed to stderr.
2258 self.run(search_cmd, stdout_tee=None, stderr_tee=None)
2259 return 'lightsensor'
2260 except error.AutoservRunError:
2261 # egrep exited with a return code of 1 meaning none of the possible
2262 # lightsensor files existed.
2263 return None
2264
2265
Aviv Keshet74c89a92013-02-04 15:18:30 -08002266 @label_decorator('bluetooth')
Simran Basic6f1f7a2012-10-16 10:47:46 -07002267 def has_bluetooth(self):
2268 """Determine the correct board label for this host.
2269
2270 @returns the string 'bluetooth' if this host has bluetooth or
2271 None if it does not.
2272 """
2273 try:
2274 self.run('test -d /sys/class/bluetooth/hci0')
2275 # test exited with a return code of 0.
2276 return 'bluetooth'
2277 except error.AutoservRunError:
2278 # test exited with a return code 1 meaning the directory did not
2279 # exist.
2280 return None
2281
2282
Bill Richardson4f595f52014-02-13 16:20:26 -08002283 @label_decorator('ec')
2284 def get_ec(self):
2285 """
2286 Determine the type of EC on this host.
2287
2288 @returns a string representing this host's embedded controller type.
2289 At present, it only returns "ec:cros", for Chrome OS ECs. Other types
2290 of EC (or none) don't return any strings, since no tests depend on
2291 those.
2292 """
2293 cmd = 'mosys ec info'
2294 # The output should look like these, so that the last field should
2295 # match our EC version scheme:
2296 #
2297 # stm | stm32f100 | snow_v1.3.139-375eb9f
2298 # ti | Unknown-10de | peppy_v1.5.114-5d52788
2299 #
2300 # Non-Chrome OS ECs will look like these:
2301 #
2302 # ENE | KB932 | 00BE107A00
2303 # ite | it8518 | 3.08
2304 #
2305 # And some systems don't have ECs at all (Lumpy, for example).
2306 regexp = r'^.*\|\s*(\S+_v\d+\.\d+\.\d+-[0-9a-f]+)\s*$'
2307
2308 ecinfo = self.run(command=cmd, ignore_status=True)
2309 if ecinfo.exit_status == 0:
2310 res = re.search(regexp, ecinfo.stdout)
2311 if res:
2312 logging.info("EC version is %s", res.groups()[0])
2313 return 'ec:cros'
2314 logging.info("%s got: %s", cmd, ecinfo.stdout)
2315 # Has an EC, but it's not a Chrome OS EC
2316 return None
2317 logging.info("%s exited with status %d", cmd, ecinfo.exit_status)
2318 # No EC present
2319 return None
2320
2321
Alec Berg31b932b2014-04-04 16:09:11 -07002322 @label_decorator('accels')
2323 def get_accels(self):
2324 """
2325 Determine the type of accelerometers on this host.
2326
2327 @returns a string representing this host's accelerometer type.
2328 At present, it only returns "accel:cros-ec", for accelerometers
2329 attached to a Chrome OS EC, or none, if no accelerometers.
2330 """
2331 # Check to make sure we have ectool
2332 rv = self.run('which ectool', ignore_status=True)
2333 if rv.exit_status:
2334 logging.info("No ectool cmd found, assuming no EC accelerometers")
2335 return None
2336
2337 # Check that the EC supports the motionsense command
2338 rv = self.run('ectool motionsense', ignore_status=True)
2339 if rv.exit_status:
2340 logging.info("EC does not support motionsense command "
2341 "assuming no EC accelerometers")
2342 return None
2343
2344 # Check that EC motion sensors are active
2345 active = self.run('ectool motionsense active').stdout.split('\n')
2346 if active[0] == "0":
2347 logging.info("Motion sense inactive, assuming no EC accelerometers")
2348 return None
2349
2350 logging.info("EC accelerometers found")
2351 return 'accel:cros-ec'
2352
2353
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +08002354 @label_decorator('chameleon')
2355 def has_chameleon(self):
2356 """Determine if a Chameleon connected to this host.
2357
Tom Wai-Hong Tambadbb332014-10-10 02:59:41 +08002358 @returns a list containing two strings ('chameleon' and
2359 'chameleon:' + label, e.g. 'chameleon:hdmi') if this host
2360 has a Chameleon or None if it has not.
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +08002361 """
2362 if self._chameleon_host:
Tom Wai-Hong Tambadbb332014-10-10 02:59:41 +08002363 return ['chameleon', 'chameleon:' + self.chameleon.get_label()]
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +08002364 else:
2365 return None
2366
2367
Cheng-Yi Chiangf4104ff2014-12-23 19:39:01 +08002368 @label_decorator('audio_loopback_dongle')
2369 def has_loopback_dongle(self):
2370 """Determine if an audio loopback dongle is plugged to this host.
2371
2372 @returns 'audio_loopback_dongle' when there is an audio loopback dongle
2373 plugged to this host.
2374 None when there is no audio loopback dongle
2375 plugged to this host.
2376 """
Cheng-Yi Chiang8de78112015-05-27 14:47:08 +08002377 nodes_info = self.run(command=cras_utils.get_cras_nodes_cmd(),
2378 ignore_status=True).stdout
2379 if (cras_utils.node_type_is_plugged('HEADPHONE', nodes_info) and
2380 cras_utils.node_type_is_plugged('MIC', nodes_info)):
Cheng-Yi Chiangf4104ff2014-12-23 19:39:01 +08002381 return 'audio_loopback_dongle'
2382 else:
2383 return None
2384
2385
Derek Basehorec71ff622014-07-07 15:18:40 -07002386 @label_decorator('power_supply')
2387 def get_power_supply(self):
2388 """
2389 Determine what type of power supply the host has
2390
2391 @returns a string representing this host's power supply.
2392 'power:battery' when the device has a battery intended for
2393 extended use
2394 'power:AC_primary' when the device has a battery not intended
2395 for extended use (for moving the machine, etc)
2396 'power:AC_only' when the device has no battery at all.
2397 """
2398 psu = self.run(command='mosys psu type', ignore_status=True)
2399 if psu.exit_status:
2400 # The psu command for mosys is not included for all platforms. The
2401 # assumption is that the device will have a battery if the command
2402 # is not found.
2403 return 'power:battery'
2404
2405 psu_str = psu.stdout.strip()
2406 if psu_str == 'unknown':
2407 return None
2408
2409 return 'power:%s' % psu_str
2410
2411
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002412 @label_decorator('storage')
2413 def get_storage(self):
2414 """
2415 Determine the type of boot device for this host.
2416
2417 Determine if the internal device is SCSI or dw_mmc device.
2418 Then check that it is SSD or HDD or eMMC or something else.
2419
2420 @returns a string representing this host's internal device type.
2421 'storage:ssd' when internal device is solid state drive
2422 'storage:hdd' when internal device is hard disk drive
2423 'storage:mmc' when internal device is mmc drive
2424 None When internal device is something else or
2425 when we are unable to determine the type
2426 """
2427 # The output should be /dev/mmcblk* for SD/eMMC or /dev/sd* for scsi
2428 rootdev_cmd = ' '.join(['. /usr/sbin/write_gpt.sh;',
2429 '. /usr/share/misc/chromeos-common.sh;',
2430 'load_base_vars;',
2431 'get_fixed_dst_drive'])
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002432 rootdev = self.run(command=rootdev_cmd, ignore_status=True)
2433 if rootdev.exit_status:
2434 logging.info("Fail to run %s", rootdev_cmd)
2435 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002436 rootdev_str = rootdev.stdout.strip()
2437
2438 if not rootdev_str:
2439 return None
2440
2441 rootdev_base = os.path.basename(rootdev_str)
2442
2443 mmc_pattern = '/dev/mmcblk[0-9]'
2444 if re.match(mmc_pattern, rootdev_str):
2445 # Use type to determine if the internal device is eMMC or somthing
2446 # else. We can assume that MMC is always an internal device.
2447 type_cmd = 'cat /sys/block/%s/device/type' % rootdev_base
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002448 type = self.run(command=type_cmd, ignore_status=True)
2449 if type.exit_status:
2450 logging.info("Fail to run %s", type_cmd)
2451 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002452 type_str = type.stdout.strip()
2453
2454 if type_str == 'MMC':
2455 return 'storage:mmc'
2456
2457 scsi_pattern = '/dev/sd[a-z]+'
2458 if re.match(scsi_pattern, rootdev.stdout):
2459 # Read symlink for /sys/block/sd* to determine if the internal
2460 # device is connected via ata or usb.
2461 link_cmd = 'readlink /sys/block/%s' % rootdev_base
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002462 link = self.run(command=link_cmd, ignore_status=True)
2463 if link.exit_status:
2464 logging.info("Fail to run %s", link_cmd)
2465 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002466 link_str = link.stdout.strip()
2467 if 'usb' in link_str:
2468 return None
2469
2470 # Read rotation to determine if the internal device is ssd or hdd.
2471 rotate_cmd = str('cat /sys/block/%s/queue/rotational'
2472 % rootdev_base)
Puthikorn Voravootivat03c51682014-04-24 13:52:12 -07002473 rotate = self.run(command=rotate_cmd, ignore_status=True)
2474 if rotate.exit_status:
2475 logging.info("Fail to run %s", rotate_cmd)
2476 return None
Puthikorn Voravootivatfa011242014-03-14 18:45:11 -07002477 rotate_str = rotate.stdout.strip()
2478
2479 rotate_dict = {'0':'storage:ssd', '1':'storage:hdd'}
2480 return rotate_dict.get(rotate_str)
2481
2482 # All other internal device / error case will always fall here
2483 return None
2484
2485
Dan Shi4e9a2aa2014-03-24 14:28:42 -07002486 @label_decorator('servo')
2487 def get_servo(self):
2488 """Determine if the host has a servo attached.
2489
2490 If the host has a working servo attached, it should have a servo label.
2491
2492 @return: string 'servo' if the host has servo attached. Otherwise,
2493 returns None.
2494 """
2495 return 'servo' if self._servo_host else None
2496
2497
Dan Shi5beba472014-05-28 22:46:07 -07002498 @label_decorator('video_labels')
2499 def get_video_labels(self):
2500 """Run /usr/local/bin/avtest_label_detect to get a list of video labels.
2501
2502 Sample output of avtest_label_detect:
2503 Detected label: hw_video_acc_vp8
2504 Detected label: webcam
2505
2506 @return: A list of labels detected by tool avtest_label_detect.
2507 """
2508 try:
2509 result = self.run('/usr/local/bin/avtest_label_detect').stdout
2510 return re.findall('^Detected label: (\w+)$', result, re.M)
2511 except error.AutoservRunError:
2512 # The tool is not installed.
2513 return []
2514
2515
mussa584b4462014-06-20 15:13:28 -07002516 @label_decorator('video_glitch_detection')
2517 def is_video_glitch_detection_supported(self):
2518 """ Determine if a board under test is supported for video glitch
2519 detection tests.
2520
2521 @return: 'video_glitch_detection' if board is supported, None otherwise.
2522 """
Mussa5b589052015-10-26 17:55:26 -07002523 board = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
mussa584b4462014-06-20 15:13:28 -07002524
Mussa5b589052015-10-26 17:55:26 -07002525 if board in video_test_constants.SUPPORTED_BOARDS:
2526 return 'video_glitch_detection'
mussa584b4462014-06-20 15:13:28 -07002527
Mussa5b589052015-10-26 17:55:26 -07002528 return None
mussa584b4462014-06-20 15:13:28 -07002529
mussa584b4462014-06-20 15:13:28 -07002530
Katherine Threlkeld7b97a9f2014-06-24 13:47:14 -07002531 @label_decorator('touch_labels')
2532 def get_touch(self):
2533 """
2534 Determine whether board under test has a touchpad or touchscreen.
2535
2536 @return: A list of some combination of 'touchscreen' and 'touchpad',
2537 depending on what is present on the device.
Katherine Threlkeldab83d392015-06-18 16:45:57 -07002538
Katherine Threlkeld7b97a9f2014-06-24 13:47:14 -07002539 """
2540 labels = []
Katherine Threlkeldab83d392015-06-18 16:45:57 -07002541 looking_for = ['touchpad', 'touchscreen']
2542 player = input_playback.InputPlayback()
2543 input_events = self.run('ls /dev/input/event*').stdout.strip().split()
2544 filename = '/tmp/touch_labels'
2545 for event in input_events:
2546 self.run('evtest %s > %s' % (event, filename), timeout=1,
2547 ignore_timeout=True)
2548 properties = self.run('cat %s' % filename).stdout
2549 input_type = player._determine_input_type(properties)
2550 if input_type in looking_for:
2551 labels.append(input_type)
2552 looking_for.remove(input_type)
2553 if len(looking_for) == 0:
2554 break
2555 self.run('rm %s' % filename)
2556
Katherine Threlkeld7b97a9f2014-06-24 13:47:14 -07002557 return labels
2558
Hung-ying Tyana39b0542015-06-30 10:36:42 +08002559
2560 @label_decorator('internal_display')
2561 def has_internal_display(self):
2562 """Determine if the device under test is equipped with an internal
2563 display.
2564
2565 @return: 'internal_display' if one is present; None otherwise.
2566 """
2567 from autotest_lib.client.cros.graphics import graphics_utils
2568 from autotest_lib.client.common_lib import utils as common_utils
2569
2570 def __system_output(cmd):
2571 return self.run(cmd).stdout
2572
2573 def __read_file(remote_path):
2574 return self.run('cat %s' % remote_path).stdout
2575
2576 # Hijack the necessary client functions so that we can take advantage
2577 # of the client lib here.
2578 # FIXME: find a less hacky way than this
2579 original_system_output = utils.system_output
2580 original_read_file = common_utils.read_file
2581 utils.system_output = __system_output
2582 common_utils.read_file = __read_file
2583 try:
2584 return ('internal_display' if graphics_utils.has_internal_display()
2585 else None)
2586 finally:
2587 utils.system_output = original_system_output
2588 common_utils.read_file = original_read_file
2589
2590
Eric Carusoee673ac2015-08-05 17:03:04 -07002591 @label_decorator('lucidsleep')
2592 def has_lucid_sleep_support(self):
2593 """Determine if the device under test has support for lucid sleep.
2594
2595 @return 'lucidsleep' if this board supports lucid sleep; None otherwise
2596 """
2597 board = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
2598 return 'lucidsleep' if board in LUCID_SLEEP_BOARDS else None
2599
2600
Dan Shi85276d42014-04-08 22:11:45 -07002601 def is_boot_from_usb(self):
2602 """Check if DUT is boot from USB.
2603
2604 @return: True if DUT is boot from usb.
2605 """
2606 device = self.run('rootdev -s -d').stdout.strip()
2607 removable = int(self.run('cat /sys/block/%s/removable' %
2608 os.path.basename(device)).stdout.strip())
2609 return removable == 1
Helen Zhang17dae2b2014-11-11 09:25:52 -08002610
2611
2612 def read_from_meminfo(self, key):
Dan Shi49ca0932014-11-14 11:22:27 -08002613 """Return the memory info from /proc/meminfo
Helen Zhang17dae2b2014-11-11 09:25:52 -08002614
2615 @param key: meminfo requested
2616
2617 @return the memory value as a string
2618
2619 """
Helen Zhang17dae2b2014-11-11 09:25:52 -08002620 meminfo = self.run('grep %s /proc/meminfo' % key).stdout.strip()
2621 logging.debug('%s', meminfo)
2622 return int(re.search(r'\d+', meminfo).group(0))
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002623
2624
2625 def get_board_type(self):
2626 """
2627 Get the DUT's device type from /etc/lsb-release.
Danny Chan471a8d12015-08-18 14:57:41 -07002628 DEVICETYPE can be one of CHROMEBOX, CHROMEBASE, CHROMEBOOK or more.
2629
2630 @return value of DEVICETYPE param from lsb-release.
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002631 """
Danny Chan471a8d12015-08-18 14:57:41 -07002632 device_type = self.run('grep DEVICETYPE /etc/lsb-release',
2633 ignore_status=True).stdout
2634 if device_type:
Kalin Stoyanov524310b2015-08-21 16:24:04 -07002635 return device_type.split('=')[-1].strip()
Danny Chan471a8d12015-08-18 14:57:41 -07002636 return ''
Gilad Arnolda76bef02015-09-29 13:55:15 -07002637
2638
2639 def get_os_type(self):
2640 return 'cros'
Simran Basia5522a32015-10-06 11:01:24 -07002641
2642
2643 def enable_adb_testing(self):
2644 """Mark this host as an adb tester."""
Dan Shia2872172015-10-31 01:16:51 -07002645 self.run('touch %s' % constants.ANDROID_TESTER_FILEFLAG)