blob: 2647f85c98b518e966575921f8e828777af05ea7 [file] [log] [blame]
Derek Beckettf73baca2020-08-19 15:08:47 -07001# Lint as: python2, python3
J. Richard Barnette24adbf42012-04-11 15:04:53 -07002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtisaa5eedb2011-08-23 16:18:52 -07003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Derek Beckettf73baca2020-08-19 15:08:47 -07006from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
J. Richard Barnette1d78b012012-05-15 13:56:30 -070010import logging
Dan Shi0f466e82013-02-22 15:44:58 -080011import os
Simran Basid5e5e272012-09-24 15:23:59 -070012import re
Vincent Palatindf2372c2016-10-07 17:03:00 +020013import sys
Otabek Kasimovb7cb8422020-12-23 02:38:32 -080014import six
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070015import time
16
mussa584b4462014-06-20 15:13:28 -070017import common
J. Richard Barnette45e93de2012-04-11 17:24:15 -070018from autotest_lib.client.bin import utils
Dan Shi9cb0eec2014-06-03 09:04:50 -070019from autotest_lib.client.common_lib import autotemp
Richard Barnette0c73ffc2012-11-19 15:21:18 -080020from autotest_lib.client.common_lib import error
21from autotest_lib.client.common_lib import global_config
J. Richard Barnette91137f02016-03-10 16:52:26 -080022from autotest_lib.client.common_lib import hosts
Dan Shi549fb822015-03-24 18:01:11 -070023from autotest_lib.client.common_lib import lsbrelease_utils
Otabek Kasimov6825b762020-06-23 23:42:44 -070024from autotest_lib.client.common_lib import utils as common_utils
Greg Edelstona7b05d12020-04-01 16:00:51 -060025from autotest_lib.client.common_lib.cros import cros_config
Richard Barnette03a0c132012-11-05 12:40:35 -080026from autotest_lib.client.common_lib.cros import dev_server
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -070027from autotest_lib.client.common_lib.cros import retry
Hsinyu Chaoe0b08e62015-08-11 10:50:37 +000028from autotest_lib.client.cros import constants as client_constants
J. Richard Barnette84890bd2014-02-21 11:05:47 -080029from autotest_lib.client.cros import cros_ui
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
Derek Beckettcc0c8302021-07-14 09:53:48 -070033from autotest_lib.server.cros.dynamic_suite import tools
Garry Wang1a493d82020-08-31 21:01:19 -070034from autotest_lib.server.cros.device_health_profile import device_health_profile
Garry Wanga2e78172020-09-09 23:49:07 -070035from autotest_lib.server.cros.device_health_profile import profile_constants
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -070036from autotest_lib.server.cros.servo import pdtester
Fang Deng96667ca2013-08-01 17:46:18 -070037from autotest_lib.server.hosts import abstract_ssh
Kevin Chenga2619dc2016-03-28 11:42:08 -070038from autotest_lib.server.hosts import base_label
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +080039from autotest_lib.server.hosts import chameleon_host
Otabek Kasimov832d9162020-07-27 19:24:57 -070040from autotest_lib.server.hosts import cros_constants
Richard Barnetted31580e2018-05-14 19:58:00 +000041from autotest_lib.server.hosts import cros_label
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -070042from autotest_lib.server.hosts import pdtester_host
Fang Deng5d518f42013-08-02 14:04:32 -070043from autotest_lib.server.hosts import servo_host
Garry Wang11b5e872020-03-11 15:14:08 -070044from autotest_lib.server.hosts import servo_constants
Otabek Kasimov808cd832020-05-28 18:27:46 -070045from autotest_lib.site_utils.admin_audit import constants as audit_const
Otabek Kasimov27bb2862020-08-10 14:40:45 -070046from autotest_lib.site_utils.admin_audit import verifiers as audit_verify
Derek Beckettf73baca2020-08-19 15:08:47 -070047from six.moves import zip
Simran Basid5e5e272012-09-24 15:23:59 -070048
Andrew Luo4be621d2020-03-21 07:01:13 -070049
Dan Shib8540a52015-07-16 14:18:23 -070050CONFIG = global_config.global_config
51
beepsc87ff602013-07-31 21:53:00 -070052class FactoryImageCheckerException(error.AutoservError):
53 """Exception raised when an image is a factory image."""
54 pass
55
56
Fang Deng0ca40e22013-08-27 17:47:44 -070057class CrosHost(abstract_ssh.AbstractSSHHost):
J. Richard Barnette45e93de2012-04-11 17:24:15 -070058 """Chromium OS specific subclass of Host."""
59
Simran Basi5ace6f22016-01-06 17:30:44 -080060 VERSION_PREFIX = provision.CROS_VERSION_PREFIX
61
J. Richard Barnette45e93de2012-04-11 17:24:15 -070062
Richard Barnette03a0c132012-11-05 12:40:35 -080063 # Timeout values (in seconds) associated with various Chrome OS
64 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070065 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -080066 # In general, a good rule of thumb is that the timeout can be up
67 # to twice the typical measured value on the slowest platform.
68 # The times here have not necessarily been empirically tested to
69 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070070 #
71 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -080072 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
73 # time to restart the netwowrk.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080074 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070075 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -080076 # other things, this must account for the 30 second dev-mode
J. Richard Barnette417cc792015-10-01 09:56:36 -070077 # screen delay, time to start the network on the DUT, and the
78 # ssh timeout of 120 seconds.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070079 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -080080 # including the 30 second dev-mode delay and time to start the
J. Richard Barnetted4649c62013-03-06 17:42:27 -080081 # network.
beepsf079cfb2013-09-18 17:49:51 -070082 # INSTALL_TIMEOUT: Time to allow for chromeos-install.
Otabek Kasimovaeb47fe2021-01-26 20:53:55 -080083 # ADMIN_INSTALL_TIMEOUT: Time to allow for chromeos-install
84 # used by admin tasks.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080085 # POWERWASH_BOOT_TIMEOUT: Time to allow for a reboot that
86 # includes powerwash.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070087
88 SLEEP_TIMEOUT = 2
J. Richard Barnetted4649c62013-03-06 17:42:27 -080089 RESUME_TIMEOUT = 10
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +080090 SHUTDOWN_TIMEOUT = 10
J. Richard Barnette417cc792015-10-01 09:56:36 -070091 BOOT_TIMEOUT = 150
J. Richard Barnette5bab5f52015-08-03 13:14:38 -070092 USB_BOOT_TIMEOUT = 300
J. Richard Barnette7817b052014-08-28 09:47:29 -070093 INSTALL_TIMEOUT = 480
Otabek Kasimovaeb47fe2021-01-26 20:53:55 -080094 ADMIN_INSTALL_TIMEOUT = 600
Dan Shi2c88eed2013-11-12 10:18:38 -080095 POWERWASH_BOOT_TIMEOUT = 60
Chris Sosab76e0ee2013-05-22 16:55:41 -070096
Dan Shica503482015-03-30 17:23:25 -070097 # Minimum OS version that supports server side packaging. Older builds may
98 # not have server side package built or with Autotest code change to support
99 # server-side packaging.
Dan Shib8540a52015-07-16 14:18:23 -0700100 MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value(
Dan Shiced09e42015-04-17 16:09:34 -0700101 'AUTOSERV', 'min_version_support_ssp', type=int)
Dan Shica503482015-03-30 17:23:25 -0700102
Dana Goyettec172b172020-07-29 16:26:15 -0700103 USE_FSFREEZE = CONFIG.get_config_value(
Dana Goyette6242cb32020-09-23 11:02:57 -0700104 'CROS', 'enable_fs_freeze', type=bool, default=False)
Dana Goyettec172b172020-07-29 16:26:15 -0700105
J. Richard Barnette84890bd2014-02-21 11:05:47 -0800106 # REBOOT_TIMEOUT: How long to wait for a reboot.
107 #
Chris Sosab76e0ee2013-05-22 16:55:41 -0700108 # We have a long timeout to ensure we don't flakily fail due to other
109 # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate.
Simran Basi1160e2c2013-10-04 16:00:24 -0700110 # TODO(sbasi - crbug.com/276094) Restore to 5 mins once the 'host did not
111 # return from reboot' bug is solved.
112 REBOOT_TIMEOUT = 480
Chris Sosab76e0ee2013-05-22 16:55:41 -0700113
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800114 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
115 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
Garry Wang5e5538a2019-04-08 15:36:18 -0700116 # _CHANGE_SERVO_ROLE_TIMEOUT: Time to allow DUT regain network connection
117 # since changing servo role will reset USB state
118 # and causes temporary ethernet drop.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800119 _USB_POWER_TIMEOUT = 5
120 _POWER_CYCLE_TIMEOUT = 10
Garry Wang5e5538a2019-04-08 15:36:18 -0700121 _CHANGE_SERVO_ROLE_TIMEOUT = 180
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800122
Fang Dengdeba14f2014-11-14 11:54:09 -0800123 _RPM_HOSTNAME_REGEX = ('chromeos(\d+)(-row(\d+))?-rack(\d+[a-z]*)'
124 '-host(\d+)')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700125
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800126 # Constants used in ping_wait_up() and ping_wait_down().
127 #
128 # _PING_WAIT_COUNT is the approximate number of polling
129 # cycles to use when waiting for a host state change.
130 #
131 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
132 # for arguments to the internal _ping_wait_for_status()
133 # method.
134 _PING_WAIT_COUNT = 40
135 _PING_STATUS_DOWN = False
136 _PING_STATUS_UP = True
137
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800138 # Allowed values for the power_method argument.
139
Garry Wang5e5538a2019-04-08 15:36:18 -0700140 # POWER_CONTROL_RPM: Used in power_off/on/cycle() methods, default for all
141 # DUTs except those with servo_v4 CCD.
142 # POWER_CONTROL_CCD: Used in power_off/on/cycle() methods, default for all
143 # DUTs with servo_v4 CCD.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800144 # 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'
Garry Wang5e5538a2019-04-08 15:36:18 -0700147 POWER_CONTROL_CCD = 'CCD'
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800148 POWER_CONTROL_SERVO = 'servoj10'
149 POWER_CONTROL_MANUAL = 'manual'
150
151 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
Garry Wang5e5538a2019-04-08 15:36:18 -0700152 POWER_CONTROL_CCD,
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800153 POWER_CONTROL_SERVO,
154 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800155
Simran Basi5e6339a2013-03-21 11:34:32 -0700156 _RPM_OUTLET_CHANGED = 'outlet_changed'
157
Dan Shi9cb0eec2014-06-03 09:04:50 -0700158 # URL pattern to download firmware image.
Dan Shib8540a52015-07-16 14:18:23 -0700159 _FW_IMAGE_URL_PATTERN = CONFIG.get_config_value(
Dan Shi9cb0eec2014-06-03 09:04:50 -0700160 'CROS', 'firmware_url_pattern', type=str)
beeps687243d2013-07-18 15:29:27 -0700161
Brent Peterson1cb623a2020-01-09 13:14:28 -0800162 # Regular expression for extracting EC version string
163 _EC_REGEX = '(%s_\w*[-\.]\w*[-\.]\w*[-\.]\w*)'
164
165 # Regular expression for extracting BIOS version string
166 _BIOS_REGEX = '(%s\.\w*\.\w*\.\w*)'
167
Brent Petersonc70a1832020-01-24 15:54:35 -0800168 # Command to update firmware located on DUT
Namyoon Woo382e5892020-05-20 16:48:40 -0700169 _FW_UPDATE_CMD = 'chromeos-firmwareupdate --mode=recovery %s'
Brent Petersonc70a1832020-01-24 15:54:35 -0800170
J. Richard Barnette964fba02012-10-24 17:34:29 -0700171 @staticmethod
beeps46dadc92013-11-07 14:07:10 -0800172 def check_host(host, timeout=10):
173 """
174 Check if the given host is a chrome-os host.
175
176 @param host: An ssh host representing a device.
177 @param timeout: The timeout for the run command.
178
179 @return: True if the host device is chromeos.
180
beeps46dadc92013-11-07 14:07:10 -0800181 """
182 try:
Allen Liad719c12017-06-27 23:48:04 +0000183 result = host.run(
Simran Basi933c8af2015-04-29 14:05:07 -0700184 'grep -q CHROMEOS /etc/lsb-release && '
Garry Wange4b6d6e2019-06-17 17:08:46 -0700185 '! grep -q moblab /etc/lsb-release && '
Derek Beckett342e3e62021-01-05 17:17:23 -0800186 '! grep -q labstation /etc/lsb-release &&'
187 ' grep CHROMEOS_RELEASE_BOARD /etc/lsb-release',
188 ignore_status=True,
Laurence Goodby468de252017-06-08 17:22:53 -0700189 timeout=timeout).stdout
Derek Beckett342e3e62021-01-05 17:17:23 -0800190 if result:
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800191 return not (
192 lsbrelease_utils.is_jetstream(
Derek Beckett342e3e62021-01-05 17:17:23 -0800193 lsb_release_content=result) or
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800194 lsbrelease_utils.is_gce_board(
Derek Beckett342e3e62021-01-05 17:17:23 -0800195 lsb_release_content=result))
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800196
beeps46dadc92013-11-07 14:07:10 -0800197 except (error.AutoservRunError, error.AutoservSSHTimeout):
198 return False
Laurence Goodby468de252017-06-08 17:22:53 -0700199
200 return False
beeps46dadc92013-11-07 14:07:10 -0800201
202
203 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800204 def get_chameleon_arguments(args_dict):
205 """Extract chameleon options from `args_dict` and return the result.
206
207 Recommended usage:
208 ~~~~~~~~
209 args_dict = utils.args_to_dict(args)
210 chameleon_args = hosts.CrosHost.get_chameleon_arguments(args_dict)
211 host = hosts.create_host(machine, chameleon_args=chameleon_args)
212 ~~~~~~~~
213
214 @param args_dict Dictionary from which to extract the chameleon
215 arguments.
216 """
Sam McNally66594ca2019-12-09 12:45:44 +1100217 chameleon_args = {key: args_dict[key]
218 for key in ('chameleon_host', 'chameleon_port')
219 if key in args_dict}
220 if 'chameleon_ssh_port' in args_dict:
221 chameleon_args['port'] = int(args_dict['chameleon_ssh_port'])
222 return chameleon_args
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800223
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800224 @staticmethod
225 def get_btpeer_arguments(args_dict):
226 """Extract btpeer options from `args_dict` and return the result.
227
228 This is used to parse details of Bluetooth peer.
229 Recommended usage:
230 ~~~~~~~~
231 args_dict = utils.args_to_dict(args)
232 btpeer_args = hosts.CrosHost.get_btpeer_arguments(args_dict)
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700233 host = hosts.create_host(machine, btpeer_args=btpeer_args)
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800234 ~~~~~~~~
235
236 @param args_dict: Dictionary from which to extract the btpeer
237 arguments.
238 """
239 if 'btpeer_host_list' in args_dict:
240 result = []
241 for btpeer in args_dict['btpeer_host_list'].split(','):
Claire Changd0b19842020-11-04 22:28:45 +0800242 # IPv6 addresses including a port number should be enclosed in
243 # square brackets.
244 delimiter = ']:' if re.search(r':.*:', btpeer) else ':'
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800245 result.append({key: value for key,value in
246 zip(('btpeer_host','btpeer_port'),
Claire Changd0b19842020-11-04 22:28:45 +0800247 btpeer.strip('[]').split(delimiter))})
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800248 return result
249 else:
Anand K Mistrye8933092020-08-05 14:49:41 +1000250 return {key: args_dict[key]
251 for key in ('btpeer_host', 'btpeer_port', 'btpeer_ssh_port')
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800252 if key in args_dict}
253
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800254
255 @staticmethod
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700256 def get_pdtester_arguments(args_dict):
Scottfe06ed82015-11-05 17:15:01 -0800257 """Extract chameleon options from `args_dict` and return the result.
258
259 Recommended usage:
260 ~~~~~~~~
261 args_dict = utils.args_to_dict(args)
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700262 pdtester_args = hosts.CrosHost.get_pdtester_arguments(args_dict)
263 host = hosts.create_host(machine, pdtester_args=pdtester_args)
Scottfe06ed82015-11-05 17:15:01 -0800264 ~~~~~~~~
265
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700266 @param args_dict Dictionary from which to extract the pdtester
Scottfe06ed82015-11-05 17:15:01 -0800267 arguments.
268 """
Allen Li083866b2016-08-18 10:07:10 -0700269 return {key: args_dict[key]
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700270 for key in ('pdtester_host', 'pdtester_port')
Allen Li083866b2016-08-18 10:07:10 -0700271 if key in args_dict}
Scottfe06ed82015-11-05 17:15:01 -0800272
273
274 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800275 def get_servo_arguments(args_dict):
276 """Extract servo options from `args_dict` and return the result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800277
278 Recommended usage:
279 ~~~~~~~~
280 args_dict = utils.args_to_dict(args)
Fang Deng0ca40e22013-08-27 17:47:44 -0700281 servo_args = hosts.CrosHost.get_servo_arguments(args_dict)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800282 host = hosts.create_host(machine, servo_args=servo_args)
283 ~~~~~~~~
284
285 @param args_dict Dictionary from which to extract the servo
286 arguments.
287 """
Garry Wang11b5e872020-03-11 15:14:08 -0700288 servo_attrs = (servo_constants.SERVO_HOST_ATTR,
Andrew Luo4be621d2020-03-21 07:01:13 -0700289 servo_constants.SERVO_HOST_SSH_PORT_ATTR,
Garry Wang11b5e872020-03-11 15:14:08 -0700290 servo_constants.SERVO_PORT_ATTR,
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700291 servo_constants.SERVO_SERIAL_ATTR,
Garry Wang11b5e872020-03-11 15:14:08 -0700292 servo_constants.SERVO_BOARD_ATTR,
293 servo_constants.SERVO_MODEL_ATTR)
Armando Miraglia2ac802e2017-08-15 14:54:47 +0200294 servo_args = {key: args_dict[key]
295 for key in servo_attrs
296 if key in args_dict}
297 return (
298 None
Garry Wang11b5e872020-03-11 15:14:08 -0700299 if servo_constants.SERVO_HOST_ATTR in servo_args
300 and not servo_args[servo_constants.SERVO_HOST_ATTR]
Armando Miraglia2ac802e2017-08-15 14:54:47 +0200301 else servo_args)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700302
J. Richard Barnette964fba02012-10-24 17:34:29 -0700303
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800304 def _initialize(self,
305 hostname,
306 chameleon_args=None,
307 servo_args=None,
308 pdtester_args=None,
309 try_lab_servo=False,
310 try_servo_repair=False,
311 ssh_verbosity_flag='',
312 ssh_options='',
313 try_servo_recovery=False,
314 *args,
315 **dargs):
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800316 """Initialize superclasses, |self.chameleon|, and |self.servo|.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700317
Fang Denge545abb2014-12-30 18:43:47 -0800318 This method will attempt to create the test-assistant object
319 (chameleon/servo) when it is needed by the test. Check
320 the docstring of chameleon_host.create_chameleon_host and
321 servo_host.create_servo_host for how this is determined.
Fang Deng5d518f42013-08-02 14:04:32 -0700322
Fang Denge545abb2014-12-30 18:43:47 -0800323 @param hostname: Hostname of the dut.
324 @param chameleon_args: A dictionary that contains args for creating
325 a ChameleonHost. See chameleon_host for details.
326 @param servo_args: A dictionary that contains args for creating
327 a ServoHost object. See servo_host for details.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700328 @param try_lab_servo: When true, indicates that an attempt should
329 be made to create a ServoHost for a DUT in
330 the test lab, even if not required by
331 `servo_args`. See servo_host for details.
332 @param try_servo_repair: If a servo host is created, check it
333 with `repair()` rather than `verify()`.
Fang Denge545abb2014-12-30 18:43:47 -0800334 See servo_host for details.
335 @param ssh_verbosity_flag: String, to pass to the ssh command to control
336 verbosity.
337 @param ssh_options: String, other ssh options to pass to the ssh
338 command.
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800339 @param try_servo_recovery: When True, start servod in recovery mode.
340 See servo_host for details.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700341 """
Andrew Luo4be621d2020-03-21 07:01:13 -0700342 super(CrosHost, self)._initialize(hostname=hostname, *args, **dargs)
Derek Beckett3c5368c2021-08-03 14:13:58 -0700343 self._repair_strategy = None
Otabek Kasimov6825b762020-06-23 23:42:44 -0700344 # hold special dut_state for repair process
345 self._device_repair_state = None
Kevin Chenga2619dc2016-03-28 11:42:08 -0700346 self.labels = base_label.LabelRetriever(cros_label.CROS_LABELS)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700347 # self.env is a dictionary of environment variable settings
348 # to be exported for commands run on the host.
349 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
350 # errors that might happen.
351 self.env['LIBC_FATAL_STDERR_'] = '1'
Fang Dengd1c2b732013-08-20 12:59:46 -0700352 self._ssh_verbosity_flag = ssh_verbosity_flag
Aviv Keshetc5947fa2013-09-04 14:06:29 -0700353 self._ssh_options = ssh_options
Garry Wang1a493d82020-08-31 21:01:19 -0700354 self.health_profile = None
Garry Wang5e5538a2019-04-08 15:36:18 -0700355 self._default_power_method = None
Otabek Kasimov39637412020-11-23 19:09:27 -0800356 dut_health_profile = device_health_profile.DeviceHealthProfile(
357 hostname=self.hostname,
358 host_info=self.host_info_store.get(),
359 result_dir=self.get_result_dir())
Otabek Kasimovb61729d2020-11-24 00:42:43 -0800360
361 # TODO(otabek@): remove when b/171414073 closed
Andrew Luo4be621d2020-03-21 07:01:13 -0700362 if self.use_icmp:
Derek Beckettc7677812021-02-12 14:41:11 -0800363 pingable_before_servo = self.is_up_fast(count=1)
Andrew Luo4be621d2020-03-21 07:01:13 -0700364 if pingable_before_servo:
365 logging.info('DUT is pingable before init Servo.')
366 else:
367 logging.info('Skipping ping to DUT before init Servo.')
Otabek Kasimov39637412020-11-23 19:09:27 -0800368 _servo_host, servo_state = servo_host.create_servo_host(
369 dut=self,
370 servo_args=servo_args,
371 try_lab_servo=try_lab_servo,
372 try_servo_repair=try_servo_repair,
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800373 try_servo_recovery=try_servo_recovery,
Otabek Kasimov39637412020-11-23 19:09:27 -0800374 dut_host_info=self.host_info_store.get(),
375 dut_health_profile=dut_health_profile)
376 if dut_health_profile.is_loaded():
377 logging.info('Device health profile loaded.')
378 # The device profile is located in the servo_host which make it
379 # dependency. If profile is not loaded yet then we do not have it
380 # TODO(otabek@) persist device provide out of servo-host.
381 self.health_profile = dut_health_profile
382 self.set_servo_host(_servo_host, servo_state)
Richard Barnettee519dcd2016-08-15 17:37:17 -0700383
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800384 # TODO(waihong): Do the simplication on Chameleon too.
Shijin Abraham783a7dd2020-02-14 15:36:11 -0800385 self._chameleon_host = chameleon_host.create_chameleon_host(
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700386 dut=self.hostname,
387 chameleon_args=chameleon_args)
Shijin Abraham783a7dd2020-02-14 15:36:11 -0800388 if self._chameleon_host:
389 self.chameleon = self._chameleon_host.create_chameleon_board()
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800390 else:
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800391 self.chameleon = None
Fang Deng5d518f42013-08-02 14:04:32 -0700392
Shijin Abraham78ce4402020-09-08 22:04:27 -0700393 # Bluetooth peers will be populated by the test if needed
394 self._btpeer_host_list = []
395 self.btpeer_list = []
396 self.btpeer = None
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800397
howardchung83e55272019-08-08 14:08:05 +0800398 # Add pdtester host if pdtester args were added on command line
Wai-Hong Tam16e5edb2019-09-17 16:10:07 -0700399 self._pdtester_host = pdtester_host.create_pdtester_host(
Wai-Hong Tam90b164d2019-10-25 13:15:39 -0700400 pdtester_args, self._servo_host)
howardchung83e55272019-08-08 14:08:05 +0800401
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700402 if self._pdtester_host:
403 self.pdtester_servo = self._pdtester_host.get_servo()
404 logging.info('pdtester_servo: %r', self.pdtester_servo)
405 # Create the pdtester object used to access the ec uart
406 self.pdtester = pdtester.PDTester(self.pdtester_servo,
407 self._pdtester_host.get_servod_server_proxy())
Scottfe06ed82015-11-05 17:15:01 -0800408 else:
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700409 self.pdtester = None
Scottfe06ed82015-11-05 17:15:01 -0800410
Fang Deng5d518f42013-08-02 14:04:32 -0700411
Shijin Abraham78ce4402020-09-08 22:04:27 -0700412 def initialize_btpeer(self, btpeer_args=[]):
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800413 """ Initialize the Bluetooth peers
414
415 Initialize Bluetooth peer devices given in the arguments. Bluetooth peer
416 is chameleon host on Raspberry Pi.
417 @param btpeer_args: A dictionary that contains args for creating
418 a ChameleonHost. See chameleon_host for details.
419
420 """
Shijin Abraham78ce4402020-09-08 22:04:27 -0700421 logging.debug('Attempting to initialize bluetooth peers if available')
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700422 try:
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700423 if type(btpeer_args) is list:
424 btpeer_args_list = btpeer_args
425 else:
426 btpeer_args_list = [btpeer_args]
427
428 self._btpeer_host_list = chameleon_host.create_btpeer_host(
429 dut=self.hostname, btpeer_args_list=btpeer_args_list)
430 logging.debug('Bluetooth peer hosts are %s',
431 self._btpeer_host_list)
432 self.btpeer_list = [_host.create_chameleon_board() for _host in
433 self._btpeer_host_list if _host is not None]
434
435 if len(self.btpeer_list) > 0:
436 self.btpeer = self.btpeer_list[0]
437
438 logging.debug('After initialize_btpeer btpeer_list %s '
439 'btpeer_host_list is %s and btpeer is %s',
440 self.btpeer_list, self._btpeer_host_list,
441 self.btpeer)
442 except Exception as e:
443 logging.error('Exception %s in initialize_btpeer', str(e))
444
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800445
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700446 def host_version_prefix(self, image):
447 """Return version label prefix.
448
449 In case the CrOS provisioning version is something other than the
450 standard CrOS version e.g. CrOS TH version, this function will
451 find the prefix from provision.py.
452
453 @param image: The image name to find its version prefix.
454 @returns: A prefix string for the image type.
455 """
456 return provision.get_version_label_prefix(image)
457
Andrew Luo3332ab22020-04-28 16:42:03 -0700458 def stage_build_to_usb(self, build):
459 """Stage the current ChromeOS image on the USB stick connected to the
460 servo.
461
462 @param build: The build to download and send to USB.
463 """
464 if not self.servo:
465 raise error.TestError('Host %s does not have servo.' %
466 self.hostname)
467
468 _, update_url = self.stage_image_for_servo(build)
Andrew Luob0355ea2020-06-24 16:12:57 -0700469
470 try:
471 self.servo.image_to_servo_usb(update_url)
472 finally:
473 # servo.image_to_servo_usb turned the DUT off, so turn it back on
474 logging.debug('Turn DUT power back on.')
475 self.servo.get_power_state_controller().power_on()
476
Andrew Luo3332ab22020-04-28 16:42:03 -0700477 logging.debug('ChromeOS image %s is staged on the USB stick.',
478 build)
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700479
beepsdae65fd2013-07-26 16:24:41 -0700480 def verify_job_repo_url(self, tag=''):
beepscb6f1e22013-06-28 19:14:10 -0700481 """
482 Make sure job_repo_url of this host is valid.
483
joychen03eaad92013-06-26 09:55:21 -0700484 Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/\
beepscb6f1e22013-06-28 19:14:10 -0700485 lumpy-release/R29-4279.0.0/autotest/packages" claims to have the
486 autotest package for lumpy-release/R29-4279.0.0. If this isn't the case,
487 download and extract it. If the devserver embedded in the url is
488 unresponsive, update the job_repo_url of the host after staging it on
489 another devserver.
490
491 @param job_repo_url: A url pointing to the devserver where the autotest
492 package for this build should be staged.
beepsdae65fd2013-07-26 16:24:41 -0700493 @param tag: The tag from the server job, in the format
494 <job_id>-<user>/<hostname>, or <hostless> for a server job.
beepscb6f1e22013-06-28 19:14:10 -0700495
496 @raises DevServerException: If we could not resolve a devserver.
497 @raises AutoservError: If we're unable to save the new job_repo_url as
498 a result of choosing a new devserver because the old one failed to
499 respond to a health check.
beeps0c865032013-07-30 11:37:06 -0700500 @raises urllib2.URLError: If the devserver embedded in job_repo_url
501 doesn't respond within the timeout.
beepscb6f1e22013-06-28 19:14:10 -0700502 """
Prathmesh Prabhu368abdf2017-02-14 11:23:47 -0800503 info = self.host_info_store.get()
504 job_repo_url = info.attributes.get(ds_constants.JOB_REPO_URL, '')
beepscb6f1e22013-06-28 19:14:10 -0700505 if not job_repo_url:
506 logging.warning('No job repo url set on host %s', self.hostname)
507 return
508
509 logging.info('Verifying job repo url %s', job_repo_url)
510 devserver_url, image_name = tools.get_devserver_build_from_package_url(
511 job_repo_url)
512
beeps0c865032013-07-30 11:37:06 -0700513 ds = dev_server.ImageServer(devserver_url)
beepscb6f1e22013-06-28 19:14:10 -0700514
515 logging.info('Staging autotest artifacts for %s on devserver %s',
516 image_name, ds.url())
beeps687243d2013-07-18 15:29:27 -0700517
518 start_time = time.time()
Simran Basi25e7a922014-10-31 11:56:10 -0700519 ds.stage_artifacts(image_name, ['autotest_packages'])
beeps687243d2013-07-18 15:29:27 -0700520 stage_time = time.time() - start_time
521
522 # Record how much of the verification time comes from a devserver
523 # restage. If we're doing things right we should not see multiple
524 # devservers for a given board/build/branch path.
525 try:
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800526 board, build_type, branch = server_utils.ParseBuildName(
beeps687243d2013-07-18 15:29:27 -0700527 image_name)[:3]
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800528 except server_utils.ParseBuildNameException:
beeps687243d2013-07-18 15:29:27 -0700529 pass
530 else:
beeps0c865032013-07-30 11:37:06 -0700531 devserver = devserver_url[
Chris Sosa65425082013-10-16 13:26:22 -0700532 devserver_url.find('/') + 2:devserver_url.rfind(':')]
beeps687243d2013-07-18 15:29:27 -0700533 stats_key = {
534 'board': board,
535 'build_type': build_type,
536 'branch': branch,
beeps0c865032013-07-30 11:37:06 -0700537 'devserver': devserver.replace('.', '_'),
beeps687243d2013-07-18 15:29:27 -0700538 }
Dan Shi5e2efb72017-02-07 11:40:23 -0800539
540 monarch_fields = {
541 'board': board,
542 'build_type': build_type,
Dan Shi5e2efb72017-02-07 11:40:23 -0800543 'branch': branch,
544 'dev_server': devserver,
545 }
Dan Shi5e2efb72017-02-07 11:40:23 -0800546
Dan Shicf4d2032015-03-12 15:04:21 -0700547 def stage_server_side_package(self, image=None):
548 """Stage autotest server-side package on devserver.
549
550 @param image: Full path of an OS image to install or a build name.
551
552 @return: A url to the autotest server-side package.
Dan Shi14de7622016-08-22 11:09:06 -0700553
554 @raise: error.AutoservError if fail to locate the build to test with, or
555 fail to stage server-side package.
Dan Shicf4d2032015-03-12 15:04:21 -0700556 """
Dan Shid37736b2016-07-06 15:10:29 -0700557 # If enable_drone_in_restricted_subnet is False, do not set hostname
558 # in devserver.resolve call, so a devserver in non-restricted subnet
559 # is picked to stage autotest server package for drone to download.
560 hostname = self.hostname
561 if not server_utils.ENABLE_DRONE_IN_RESTRICTED_SUBNET:
562 hostname = None
Dan Shicf4d2032015-03-12 15:04:21 -0700563 if image:
564 image_name = tools.get_build_from_image(image)
565 if not image_name:
566 raise error.AutoservError(
567 'Failed to parse build name from %s' % image)
Dan Shid37736b2016-07-06 15:10:29 -0700568 ds = dev_server.ImageServer.resolve(image_name, hostname)
Dan Shicf4d2032015-03-12 15:04:21 -0700569 else:
Prathmesh Prabhu9235e4c2017-03-28 13:16:06 -0700570 info = self.host_info_store.get()
Prathmesh Prabhu368abdf2017-02-14 11:23:47 -0800571 job_repo_url = info.attributes.get(ds_constants.JOB_REPO_URL, '')
Dan Shicf4d2032015-03-12 15:04:21 -0700572 if job_repo_url:
573 devserver_url, image_name = (
574 tools.get_devserver_build_from_package_url(job_repo_url))
Dan Shid37736b2016-07-06 15:10:29 -0700575 # If enable_drone_in_restricted_subnet is True, use the
576 # existing devserver. Otherwise, resolve a new one in
577 # non-restricted subnet.
578 if server_utils.ENABLE_DRONE_IN_RESTRICTED_SUBNET:
579 ds = dev_server.ImageServer(devserver_url)
580 else:
581 ds = dev_server.ImageServer.resolve(image_name)
Prathmesh Prabhub6cea612017-02-09 15:41:19 -0800582 elif info.build is not None:
583 ds = dev_server.ImageServer.resolve(info.build, hostname)
Prathmesh Prabhu0c1dd4d2017-06-07 13:01:53 -0700584 image_name = info.build
Dan Shicf4d2032015-03-12 15:04:21 -0700585 else:
Prathmesh Prabhub6cea612017-02-09 15:41:19 -0800586 raise error.AutoservError(
587 'Failed to stage server-side package. The host has '
Garry Wang12b9baf2019-06-24 18:58:54 -0700588 'no job_repo_url attribute or cros-version label.')
Dan Shica503482015-03-30 17:23:25 -0700589
590 # Get the OS version of the build, for any build older than
591 # MIN_VERSION_SUPPORT_SSP, server side packaging is not supported.
592 match = re.match('.*/R\d+-(\d+)\.', image_name)
593 if match and int(match.group(1)) < self.MIN_VERSION_SUPPORT_SSP:
Dan Shi14de7622016-08-22 11:09:06 -0700594 raise error.AutoservError(
595 'Build %s is older than %s. Server side packaging is '
596 'disabled.' % (image_name, self.MIN_VERSION_SUPPORT_SSP))
Dan Shica503482015-03-30 17:23:25 -0700597
Dan Shicf4d2032015-03-12 15:04:21 -0700598 ds.stage_artifacts(image_name, ['autotest_server_package'])
599 return '%s/static/%s/%s' % (ds.url(), image_name,
600 'autotest_server_package.tar.bz2')
601
602
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700603 def stage_image_for_servo(self, image_name=None, artifact='test_image'):
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700604 """Stage a build on a devserver and return the update_url.
605
606 @param image_name: a name like lumpy-release/R27-3837.0.0
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700607 @param artifact: a string like 'test_image'. Requests
608 appropriate image to be staged.
Xixuan Wufee57542019-10-15 11:50:27 -0700609 @returns a tuple of (image_name, URL) like
610 (lumpy-release/R27-3837.0.0,
611 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700612 """
613 if not image_name:
Richard Barnetteb4b4d6f2018-05-05 01:47:41 +0000614 image_name = self.get_cros_repair_image_name()
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700615 logging.info('Staging build for servo install: %s', image_name)
Dan Shi216389c2015-12-22 11:03:06 -0800616 devserver = dev_server.ImageServer.resolve(image_name, self.hostname)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700617 devserver.stage_artifacts(image_name, [artifact])
618 if artifact == 'test_image':
Xixuan Wufee57542019-10-15 11:50:27 -0700619 return image_name, devserver.get_test_image_url(image_name)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700620 elif artifact == 'recovery_image':
Xixuan Wufee57542019-10-15 11:50:27 -0700621 return image_name, devserver.get_recovery_image_url(image_name)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700622 else:
623 raise error.AutoservError("Bad artifact!")
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700624
625
beepse539be02013-07-31 21:57:39 -0700626 def stage_factory_image_for_servo(self, image_name):
627 """Stage a build on a devserver and return the update_url.
628
629 @param image_name: a name like <baord>/4262.204.0
beeps12c0a3c2013-09-03 11:58:27 -0700630
beepse539be02013-07-31 21:57:39 -0700631 @return: An update URL, eg:
632 http://<devserver>/static/canary-channel/\
633 <board>/4262.204.0/factory_test/chromiumos_factory_image.bin
beeps12c0a3c2013-09-03 11:58:27 -0700634
635 @raises: ValueError if the factory artifact name is missing from
636 the config.
637
beepse539be02013-07-31 21:57:39 -0700638 """
639 if not image_name:
640 logging.error('Need an image_name to stage a factory image.')
641 return
642
Dan Shib8540a52015-07-16 14:18:23 -0700643 factory_artifact = CONFIG.get_config_value(
beeps12c0a3c2013-09-03 11:58:27 -0700644 'CROS', 'factory_artifact', type=str, default='')
645 if not factory_artifact:
646 raise ValueError('Cannot retrieve the factory artifact name from '
647 'autotest config, and hence cannot stage factory '
648 'artifacts.')
649
beepse539be02013-07-31 21:57:39 -0700650 logging.info('Staging build for servo install: %s', image_name)
Dan Shi216389c2015-12-22 11:03:06 -0800651 devserver = dev_server.ImageServer.resolve(image_name, self.hostname)
beepse539be02013-07-31 21:57:39 -0700652 devserver.stage_artifacts(
653 image_name,
beeps12c0a3c2013-09-03 11:58:27 -0700654 [factory_artifact],
655 archive_url=None)
beepse539be02013-07-31 21:57:39 -0700656
657 return tools.factory_image_url_pattern() % (devserver.url(), image_name)
658
659
Laurence Goodby778c9a42017-05-24 19:24:07 -0700660 def prepare_for_update(self):
661 """Prepares the DUT for an update.
662
663 Subclasses may override this to perform any special actions
664 required before updating.
665 """
Laurence Goodby468de252017-06-08 17:22:53 -0700666 pass
Laurence Goodby778c9a42017-05-24 19:24:07 -0700667
668
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800669 def _clear_fw_version_labels(self, rw_only):
670 """Clear firmware version labels from the machine.
671
672 @param rw_only: True to only clear fwrw_version; otherewise, clear
673 both fwro_version and fwrw_version.
674 """
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700675 info = self.host_info_store.get()
676 info.clear_version_labels(provision.FW_RW_VERSION_PREFIX)
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800677 if not rw_only:
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700678 info.clear_version_labels(provision.FW_RO_VERSION_PREFIX)
679 self.host_info_store.commit(info)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700680
681
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800682 def _add_fw_version_label(self, build, rw_only):
Dan Shi9cb0eec2014-06-03 09:04:50 -0700683 """Add firmware version label to the machine.
684
685 @param build: Build of firmware.
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800686 @param rw_only: True to only add fwrw_version; otherwise, add both
687 fwro_version and fwrw_version.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700688
689 """
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700690 info = self.host_info_store.get()
691 info.set_version_label(provision.FW_RW_VERSION_PREFIX, build)
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800692 if not rw_only:
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700693 info.set_version_label(provision.FW_RO_VERSION_PREFIX, build)
694 self.host_info_store.commit(info)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700695
696
Namyoon Woo33f38852020-04-13 17:26:58 -0700697 def get_latest_release_version(self, platform, ref_board=None):
Namyoon Woo5f894662019-11-15 15:23:23 -0800698 """Search for the latest package release version from the image archive,
699 and return it.
700
Namyoon Woo33f38852020-04-13 17:26:58 -0700701 @param platform: platform name, a.k.a. board or model
702 @param ref_board: reference board name, a.k.a. baseboard, parent
Namyoon Woo5f894662019-11-15 15:23:23 -0800703
Namyoon Woo33f38852020-04-13 17:26:58 -0700704 @return 'firmware-{platform}-{branch}-firmwarebranch/{release-version}/'
705 '{platform}'
Namyoon Woo5f894662019-11-15 15:23:23 -0800706 or None if LATEST release file does not exist.
707 """
708
Namyoon Woo33f38852020-04-13 17:26:58 -0700709 platforms = [ platform ]
Namyoon Woo5f894662019-11-15 15:23:23 -0800710
Namyoon Woo33f38852020-04-13 17:26:58 -0700711 # Search the image path in reference board archive as well.
712 # For example, bob has its binary image under its reference board (gru)
713 # image archive.
714 if ref_board:
715 platforms.append(ref_board)
Namyoon Woo5f894662019-11-15 15:23:23 -0800716
Namyoon Woo33f38852020-04-13 17:26:58 -0700717 for board in platforms:
718 # Read 'LATEST-1.0.0' file
719 branch_dir = provision.FW_BRANCH_GLOB % board
720 latest_file = os.path.join(provision.CROS_IMAGE_ARCHIVE, branch_dir,
721 'LATEST-1.0.0')
Namyoon Woo406c7d42020-01-24 15:57:11 -0800722
Namyoon Woo33f38852020-04-13 17:26:58 -0700723 try:
724 # The result could be one or more.
725 result = utils.system_output('gsutil ls -d ' + latest_file)
726
727 candidates = re.findall('gs://.*', result)
728
729 # Found the directory candidates. No need to check the other
730 # board name cadidates. Let's break the loop.
731 break
732 except error.CmdError:
733 # It doesn't exist. Let's move on to the next item.
734 pass
735 else:
Namyoon Woo5f894662019-11-15 15:23:23 -0800736 logging.error('No LATEST release info is available.')
737 return None
738
Namyoon Woo406c7d42020-01-24 15:57:11 -0800739 for cand_dir in candidates:
740 result = utils.system_output('gsutil cat ' + cand_dir)
Namyoon Woo5f894662019-11-15 15:23:23 -0800741
Namyoon Woo406c7d42020-01-24 15:57:11 -0800742 release_path = cand_dir.replace('LATEST-1.0.0', result)
Namyoon Woo33f38852020-04-13 17:26:58 -0700743 release_path = os.path.join(release_path, platform)
Namyoon Woo406c7d42020-01-24 15:57:11 -0800744 try:
745 # Check if release_path does exist.
746 release = utils.system_output('gsutil ls -d ' + release_path)
747 # Now 'release' has a full directory path: e.g.
748 # gs://chromeos-image-archive/firmware-octopus-11297.B-
749 # firmwarebranch/RNone-1.0.0-b4395530/octopus/
750
751 # Remove "gs://chromeos-image-archive".
752 release = release.replace(provision.CROS_IMAGE_ARCHIVE, '')
753
754 # Remove CROS_IMAGE_ARCHIVE and any surrounding '/'s.
755 return release.strip('/')
756 except error.CmdError:
757 # The directory might not exist. Let's try next candidate.
758 pass
759 else:
760 raise error.AutoservError('Cannot find the latest firmware')
Namyoon Woo5f894662019-11-15 15:23:23 -0800761
Brent Peterson1cb623a2020-01-09 13:14:28 -0800762 @staticmethod
763 def get_version_from_image(image, version_regex):
Brent Peterson8039b472020-02-14 10:51:23 -0800764 """Get version string from binary image using regular expression.
765
766 @param image: Binary image to search
767 @param version_regex: Regular expression to search for
768
769 @return Version string
770
771 @raises TestFail if no version string is found in image
772 """
Brent Peterson1cb623a2020-01-09 13:14:28 -0800773 with open(image, 'rb') as f:
774 image_data = f.read()
Derek Beckett98345552020-08-31 16:07:22 -0700775 match = re.findall(version_regex,
776 image_data.decode('ISO-8859-1', errors='ignore'))
Brent Peterson1cb623a2020-01-09 13:14:28 -0800777 if match:
778 return match[0]
779 else:
780 raise error.TestFail('Failed to read version from %s.' % image)
781
782
Garry Wangad2a1712020-03-26 15:06:43 -0700783 def firmware_install(self, build, rw_only=False, dest=None,
Brent Petersonc70a1832020-01-24 15:54:35 -0800784 local_tarball=None, verify_version=False,
Namyoon Woo382e5892020-05-20 16:48:40 -0700785 try_scp=False, install_ec=True, install_bios=True,
786 board_as=None):
Dan Shi9cb0eec2014-06-03 09:04:50 -0700787 """Install firmware to the DUT.
788
789 Use stateful update if the DUT is already running the same build.
790 Stateful update does not update kernel and tends to run much faster
791 than a full reimage. If the DUT is running a different build, or it
792 failed to do a stateful update, full update, including kernel update,
793 will be applied to the DUT.
794
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800795 Once a host enters firmware_install its fw[ro|rw]_version label will
796 be removed. After the firmware is updated successfully, a new
797 fw[ro|rw]_version label will be added to the host.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700798
799 @param build: The build version to which we want to provision the
800 firmware of the machine,
801 e.g. 'link-firmware/R22-2695.1.144'.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800802 @param rw_only: True to only install firmware to its RW portions. Keep
803 the RO portions unchanged.
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700804 @param dest: Directory to store the firmware in.
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800805 @param local_tarball: Path to local firmware image for installing
806 without devserver.
Brent Peterson1cb623a2020-01-09 13:14:28 -0800807 @param verify_version: True to verify EC and BIOS versions after
808 programming firmware, default is False.
Brent Petersonc70a1832020-01-24 15:54:35 -0800809 @param try_scp: False to always program using servo, true to try copying
810 the firmware and programming from the DUT.
Namyoon Woo382e5892020-05-20 16:48:40 -0700811 @param install_ec: True to install EC FW, and False to skip it.
812 @param install_bios: True to install BIOS, and False to skip it.
813 @param board_as: A board name to force to use.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700814
815 TODO(dshi): After bug 381718 is fixed, update here with corresponding
816 exceptions that could be raised.
817
818 """
819 if not self.servo:
820 raise error.TestError('Host %s does not have servo.' %
821 self.hostname)
822
Wai-Hong Tam3fa455a2018-07-18 14:40:43 -0700823 info = self.host_info_store.get()
824 board = info.board
Shelley Chenac61d5a2019-06-24 15:35:46 -0700825 model = info.model
Namyoon Woo8dbfcf92019-01-15 18:37:12 -0800826
827 if board is None or board == '':
828 board = self.servo.get_board()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700829
Namyoon Woo382e5892020-05-20 16:48:40 -0700830 # if board_as argument is passed, then use it instead of the original
831 # board name.
832 if board_as:
833 board = board_as
834
Mary Ruthvende14a8b2019-08-23 12:43:52 -0700835 if model is None or model == '':
Namyoon Woofb16eae2020-08-14 10:02:39 -0700836 try:
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700837 model = self.get_platform()
Namyoon Woofb16eae2020-08-14 10:02:39 -0700838 except Exception as e:
839 logging.warn('Dut is unresponsive: %s', str(e))
Mary Ruthvende14a8b2019-08-23 12:43:52 -0700840
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800841 # If local firmware path not provided fetch it from the dev server
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700842 tmpd = None
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800843 if not local_tarball:
Garry Wangad2a1712020-03-26 15:06:43 -0700844 logging.info('Will install firmware from build %s.', build)
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800845
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700846 try:
847 ds = dev_server.ImageServer.resolve(build, self.hostname)
848 ds.stage_artifacts(build, ['firmware'])
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800849
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700850 if not dest:
851 tmpd = autotemp.tempdir(unique_id='fwimage')
852 dest = tmpd.name
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800853
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700854 # Download firmware image
855 fwurl = self._FW_IMAGE_URL_PATTERN % (ds.url(), build)
856 local_tarball = os.path.join(dest, os.path.basename(fwurl))
857 ds.download_file(fwurl, local_tarball)
858 except Exception as e:
859 raise error.TestError('Failed to download firmware package: %s'
860 % str(e))
Dan Shi9cb0eec2014-06-03 09:04:50 -0700861
Namyoon Woo382e5892020-05-20 16:48:40 -0700862 ec_image = None
863 if install_ec:
864 # Extract EC image from tarball
865 logging.info('Extracting EC image.')
866 ec_image = self.servo.extract_ec_image(board, model, local_tarball)
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700867 logging.info('Extracted: %s', ec_image)
Brent Peterson1cb623a2020-01-09 13:14:28 -0800868
Namyoon Woo382e5892020-05-20 16:48:40 -0700869 bios_image = None
870 if install_bios:
871 # Extract BIOS image from tarball
872 logging.info('Extracting BIOS image.')
873 bios_image = self.servo.extract_bios_image(board, model,
874 local_tarball)
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700875 logging.info('Extracted: %s', bios_image)
Namyoon Woo382e5892020-05-20 16:48:40 -0700876
877 if not bios_image and not ec_image:
878 raise error.TestError('No firmware installation was processed.')
Brent Peterson1cb623a2020-01-09 13:14:28 -0800879
Brent Petersonc70a1832020-01-24 15:54:35 -0800880 # Clear firmware version labels
881 self._clear_fw_version_labels(rw_only)
882
883 # Install firmware from local tarball
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800884 try:
Garry Wang50e4a492020-08-05 12:29:57 -0700885 # Check if copying to DUT is enabled and DUT is available
886 if try_scp and self.is_up():
Brent Petersonc70a1832020-01-24 15:54:35 -0800887 # DUT is available, make temp firmware directory to store images
888 logging.info('Making temp folder.')
889 dest_folder = '/tmp/firmware'
890 self.run('mkdir -p ' + dest_folder)
891
Namyoon Woo68b68082020-06-02 13:13:14 -0700892 fw_cmd = self._FW_UPDATE_CMD % ('--wp=1' if rw_only else '')
Brent Petersonc70a1832020-01-24 15:54:35 -0800893
Namyoon Woo382e5892020-05-20 16:48:40 -0700894 if bios_image:
895 # Send BIOS firmware image to DUT
896 logging.info('Sending BIOS firmware.')
897 dest_bios_path = os.path.join(dest_folder,
898 os.path.basename(bios_image))
899 self.send_file(bios_image, dest_bios_path)
900
901 # Initialize firmware update command for BIOS image
902 fw_cmd += ' -i %s' % dest_bios_path
Brent Peterson669edf42020-02-07 15:07:54 -0800903
904 # Send EC firmware image to DUT when EC image was found
905 if ec_image:
906 logging.info('Sending EC firmware.')
907 dest_ec_path = os.path.join(dest_folder,
908 os.path.basename(ec_image))
909 self.send_file(ec_image, dest_ec_path)
910
911 # Add EC image to firmware update command
912 fw_cmd += ' -e %s' % dest_ec_path
913
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700914 # Make sure command is allowed to finish even if ssh fails.
915 fw_cmd = "trap '' SIGHUP; %s" % fw_cmd
916
Brent Peterson669edf42020-02-07 15:07:54 -0800917 # Update firmware on DUT
918 logging.info('Updating firmware.')
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700919 try:
Dana Goyette935b3fe2020-07-23 14:19:39 -0700920 self.run(fw_cmd, options="-o LogLevel=verbose")
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700921 except error.AutoservRunError as e:
922 if e.result_obj.exit_status != 255:
923 raise
924 elif ec_image:
925 logging.warn("DUT network dropped during update"
926 " (often caused by EC resetting USB)")
927 else:
928 logging.error("DUT network dropped during update"
929 " (unexpected, since no EC image)")
930 raise
Brent Petersonc70a1832020-01-24 15:54:35 -0800931 else:
932 # Host is not available, program firmware using servo
Brent Peterson669edf42020-02-07 15:07:54 -0800933 if ec_image:
934 self.servo.program_ec(ec_image, rw_only)
Namyoon Woo382e5892020-05-20 16:48:40 -0700935 if bios_image:
936 self.servo.program_bios(bios_image, rw_only)
Brent Petersonc70a1832020-01-24 15:54:35 -0800937 if utils.host_is_in_lab_zone(self.hostname):
938 self._add_fw_version_label(build, rw_only)
Brent Peterson1cb623a2020-01-09 13:14:28 -0800939
940 # Reboot and wait for DUT after installing firmware
941 logging.info('Rebooting DUT.')
942 self.servo.get_power_state_controller().reset()
943 time.sleep(self.servo.BOOT_DELAY)
944 self.test_wait_for_boot()
945
946 # When enabled verify EC and BIOS firmware version after programming
947 if verify_version:
Brent Peterson669edf42020-02-07 15:07:54 -0800948 # Check programmed EC firmware when EC image was found
949 if ec_image:
950 logging.info('Checking EC firmware version.')
951 dest_ec_version = self.get_ec_version()
Brent Peterson8039b472020-02-14 10:51:23 -0800952 ec_version_prefix = dest_ec_version.split('_', 1)[0]
953 ec_regex = self._EC_REGEX % ec_version_prefix
Brent Peterson669edf42020-02-07 15:07:54 -0800954 image_ec_version = self.get_version_from_image(ec_image,
Brent Peterson8039b472020-02-14 10:51:23 -0800955 ec_regex)
Brent Peterson669edf42020-02-07 15:07:54 -0800956 if dest_ec_version != image_ec_version:
957 raise error.TestFail(
Namyoon Wooe2c009a2020-07-22 10:09:03 -0700958 'Failed to update EC firmware, version %s '
959 '(expected %s)' % (dest_ec_version,
960 image_ec_version))
Brent Peterson1cb623a2020-01-09 13:14:28 -0800961
Namyoon Woo382e5892020-05-20 16:48:40 -0700962 if bios_image:
963 # Check programmed BIOS firmware against expected version
964 logging.info('Checking BIOS firmware version.')
965 dest_bios_version = self.get_firmware_version()
966 bios_version_prefix = dest_bios_version.split('.', 1)[0]
967 bios_regex = self._BIOS_REGEX % bios_version_prefix
968 image_bios_version = self.get_version_from_image(bios_image,
969 bios_regex)
970 if dest_bios_version != image_bios_version:
971 raise error.TestFail(
Namyoon Wooe2c009a2020-07-22 10:09:03 -0700972 'Failed to update BIOS, version %s '
Namyoon Woo382e5892020-05-20 16:48:40 -0700973 '(expected %s)' % (dest_bios_version,
974 image_bios_version))
Dan Shi9cb0eec2014-06-03 09:04:50 -0700975 finally:
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700976 if tmpd:
977 tmpd.clean()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700978
979
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -0800980 def install_image_to_servo_usb(self, image_url=None):
981 """Installing a test image on a USB storage device.
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700982
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -0800983 Download image to USB-storage attached to the Servo board.
Richard Barnette03a0c132012-11-05 12:40:35 -0800984
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -0800985 @param image_url: If specified use as the url to download to
986 USB-storage.
987
988 @raises AutoservError if the image fails to download.
beepsf079cfb2013-09-18 17:49:51 -0700989
J. Richard Barnette0199cc82014-12-05 17:08:40 -0800990 """
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -0800991 if not image_url:
992 logging.debug('Skip download as image_url not provided!')
993 return
Garry Wang7b0e1b72020-03-25 19:08:59 -0700994
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -0800995 logging.info('Downloading image to USB')
Derek Beckett5f4edf02021-07-27 14:56:44 -0700996 try:
997 self.servo.image_to_servo_usb(image_path=image_url,
998 power_off_dut=False)
999 except error.AutotestError as e:
1000 six.reraise(error.AutotestError, str(e), sys.exc_info()[2])
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -08001001
1002 def boot_in_recovery_mode(self,
1003 usb_boot_timeout=USB_BOOT_TIMEOUT,
1004 need_snk=False):
1005 """Booting host in recovery mode.
1006
1007 Boot device in recovery mode and verify that device booted from
1008 external storage as expected.
1009
1010 @param usb_boot_timeout: The usb_boot_timeout to use wait the host
1011 to boot. Factory images need a longer
1012 usb_boot_timeout than regular cros images.
1013 @param snk_mode: If True, switch servo_v4 role to 'snk'
1014 mode before boot DUT into recovery mode.
1015
1016 @raises AutoservError if the image fails to boot.
1017
1018 """
1019 logging.info('Booting from USB directly. Usb boot timeout: %s',
1020 usb_boot_timeout)
Derek Beckett5f4edf02021-07-27 14:56:44 -07001021 self.servo.boot_in_recovery_mode(snk_mode=need_snk)
1022 if not self.wait_up(timeout=usb_boot_timeout):
1023 if need_snk:
1024 # Attempt to restore servo_v4 role to 'src' mode.
1025 self.servo.set_servo_v4_role('src')
1026 raise hosts.AutoservRepairError(
1027 'DUT failed to boot from USB after %d seconds' %
1028 usb_boot_timeout, 'failed_to_boot_pre_install')
Scott Zawalski62bacae2013-03-05 10:40:32 -05001029
Garry Wang2e347df2020-10-30 14:04:26 -07001030 # Make sure the DUT is boot from an external device.
1031 if not self.is_boot_from_external_device():
1032 raise hosts.AutoservRepairError(
1033 'DUT is expected to boot from an external device(e.g. '
1034 'a usb stick), however it seems still boot from an'
1035 ' internal storage.', 'boot_from_internal_storage')
1036
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -08001037 def run_install_image(self,
1038 install_timeout=INSTALL_TIMEOUT,
1039 need_snk=False,
1040 is_repair=False):
1041 """Installing the image with chromeos-install.
1042
1043 Steps included:
1044 1) Recover TPM on the device
1045 2) Run chromeos-install
1046 2.a) if success: power off/on the device
1047 2.b) if fail:
1048 2.b.1) Mark for replacement if fail with hardware issue
1049 2.b.2) Run internal storage check. (Only if is_repair=True)
1050 3) Wait the device to boot as verifier of success install
1051
1052 Device has to booted from external storage.
1053
1054 @param install_timeout: The timeout to use when installing the
1055 chromeos image. Factory images need a
1056 longer install_timeout.
1057 @param snk_mode: If True, switch servo_v4 role to 'snk'
1058 mode before boot DUT into recovery mode.
1059 @param is_repair: Indicates if the method is called from a
1060 repair task.
1061
1062 @raises AutoservError if the fail in process of install image.
1063 @raises AutoservRepairError if fail to boot after install image.
1064
1065 """
Tom Wai-Hong Tamf6b4f812015-08-08 04:14:59 +08001066 # The new chromeos-tpm-recovery has been merged since R44-7073.0.0.
1067 # In old CrOS images, this command fails. Skip the error.
Tom Wai-Hong Tam27af7332015-07-25 06:09:39 +08001068 logging.info('Resetting the TPM status')
Tom Wai-Hong Tamf6b4f812015-08-08 04:14:59 +08001069 try:
1070 self.run('chromeos-tpm-recovery')
1071 except error.AutoservRunError:
1072 logging.warn('chromeos-tpm-recovery is too old.')
1073
Derek Beckett5f4edf02021-07-27 14:56:44 -07001074 logging.info('Installing image through chromeos-install.')
1075 try:
1076 self.run('chromeos-install --yes', timeout=install_timeout)
1077 self.halt()
1078 except Exception as e:
1079 storage_errors = [
1080 'No space left on device',
1081 'I/O error when trying to write primary GPT',
1082 'Input/output error while writing out',
1083 'cannot read GPT header',
1084 'can not determine destination device',
1085 'wrong fs type',
1086 'bad superblock on',
1087 ]
1088 has_error = [msg for msg in storage_errors if (msg in str(e))]
1089 if has_error:
1090 info = self.host_info_store.get()
1091 info.set_version_label(audit_const.DUT_STORAGE_STATE_PREFIX,
1092 audit_const.HW_STATE_NEED_REPLACEMENT)
1093 self.host_info_store.commit(info)
1094 self.set_device_repair_state(
Otabek Kasimov832d9162020-07-27 19:24:57 -07001095 cros_constants.DEVICE_STATE_NEEDS_REPLACEMENT)
Derek Beckett5f4edf02021-07-27 14:56:44 -07001096 logging.debug('Fail install image from USB; Storage error; %s',
1097 e)
1098 raise error.AutoservError(
Otabek Kasimov808cd832020-05-28 18:27:46 -07001099 'Failed to install image from USB due to a suspect '
1100 'disk failure, DUT storage state changed to '
1101 'need_replacement, please check debug log '
1102 'for details.')
Derek Beckett5f4edf02021-07-27 14:56:44 -07001103 else:
1104 if is_repair:
1105 # DUT will be marked for replacement if storage is bad.
1106 audit_verify.VerifyDutStorage(self).verify()
Otabek Kasimov27bb2862020-08-10 14:40:45 -07001107
Derek Beckett5f4edf02021-07-27 14:56:44 -07001108 logging.debug('Fail install image from USB; %s', e)
1109 raise error.AutoservError(
Otabek Kasimov808cd832020-05-28 18:27:46 -07001110 'Failed to install image from USB due to unexpected '
1111 'error, please check debug log for details.')
Derek Beckett5f4edf02021-07-27 14:56:44 -07001112 finally:
1113 # We need reset the DUT no matter re-install success or not,
1114 # as we don't want leave the DUT in boot from usb state.
1115 logging.info('Power cycling DUT through servo.')
1116 self.servo.get_power_state_controller().power_off()
1117 self.servo.switch_usbkey('off')
1118 if need_snk:
1119 # Attempt to restore servo_v4 role to 'src' mode.
1120 self.servo.set_servo_v4_role('src')
1121 # N.B. The Servo API requires that we use power_on() here
1122 # for two reasons:
1123 # 1) After turning on a DUT in recovery mode, you must turn
1124 # it off and then on with power_on() once more to
1125 # disable recovery mode (this is a Parrot specific
1126 # requirement).
1127 # 2) After power_off(), the only way to turn on is with
1128 # power_on() (this is a Storm specific requirement).
1129 self.servo.get_power_state_controller().power_on()
beepsf079cfb2013-09-18 17:49:51 -07001130
1131 logging.info('Waiting for DUT to come back up.')
Richard Barnette03a0c132012-11-05 12:40:35 -08001132 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
Garry Wang9ced7aa2020-04-10 17:26:35 -07001133 raise hosts.AutoservRepairError('DUT failed to reboot installed '
1134 'test image after %d seconds' %
1135 self.BOOT_TIMEOUT,
1136 'failed_to_boot_post_install')
Scott Zawalski62bacae2013-03-05 10:40:32 -05001137
Otabek Kasimov2cb72cf2021-02-25 10:59:22 -08001138 def servo_install(self,
1139 image_url=None,
1140 usb_boot_timeout=USB_BOOT_TIMEOUT,
1141 install_timeout=INSTALL_TIMEOUT,
1142 is_repair=False):
1143 """Re-install the OS on the DUT by:
1144
1145 Steps:
1146 1) Power off the host
1147 2) Installing an image on a USB-storage attached to the Servo board
1148 3) Booting that image in recovery mode
1149 4) Installing the image with chromeos-install.
1150
1151 @param image_url: If specified use as the url to install on
1152 the DUT otherwise boot the currently
1153 staged image on the USB stick.
1154 @param usb_boot_timeout: The usb_boot_timeout to use during
1155 re-image. Factory images need a longer
1156 usb_boot_timeout than regular cros images.
1157 @param install_timeout: The timeout to use when installing the
1158 chromeos image. Factory images need a
1159 longer install_timeout.
1160 @param is_repair: Indicates if the method is called from a
1161 repair task.
1162
1163 @raises AutoservError if the image fails to boot.
1164
1165 """
1166 self.servo.get_power_state_controller().power_off()
1167 if image_url:
1168 self.install_image_to_servo_usb(image_url=image_url)
1169 else:
1170 # Give the DUT some time to power_off if we skip
1171 # download image to usb. (crbug.com/982993)
1172 time.sleep(10)
1173
1174 need_snk = self.require_snk_mode_in_recovery()
1175
1176 self.boot_in_recovery_mode(usb_boot_timeout=usb_boot_timeout,
1177 need_snk=need_snk)
1178
1179 self.run_install_image(install_timeout=install_timeout,
1180 need_snk=need_snk,
1181 is_repair=is_repair)
Scott Zawalski62bacae2013-03-05 10:40:32 -05001182
Garry Wanga2e78172020-09-09 23:49:07 -07001183 def set_servo_host(self, host, servo_state=None):
Richard Barnette4aeb01c2018-09-20 09:36:12 -07001184 """Set our servo host member, and associated servo.
1185
1186 @param host Our new `ServoHost`.
1187 """
1188 self._servo_host = host
Derek Beckettb66e5c82020-08-12 15:31:02 -07001189 self.servo_pwr_supported = None
Richard Barnette4aeb01c2018-09-20 09:36:12 -07001190 if self._servo_host is not None:
1191 self.servo = self._servo_host.get_servo()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001192 servo_state = self._servo_host.get_servo_state()
Garry Wang000c6c02020-05-11 21:27:23 -07001193 self._set_smart_usbhub_label(self._servo_host.smart_usbhub)
Derek Beckettb66e5c82020-08-12 15:31:02 -07001194 try:
1195 self.servo_pwr_supported = self.servo.has_control('power_state')
1196 except Exception as e:
1197 logging.debug(
1198 "Could not get servo power state due to {}".format(e))
Gregory Nisbet93b23e22020-10-02 20:42:16 +00001199 else:
1200 self.servo = None
Derek Beckettb66e5c82020-08-12 15:31:02 -07001201 self.servo_pwr_supported = False
Otabek Kasimov41301a22020-05-10 15:28:21 -07001202 self.set_servo_type()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001203 self.set_servo_state(servo_state)
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07001204 self._set_servo_topology()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001205
Richard Barnette4aeb01c2018-09-20 09:36:12 -07001206
Richard Barnette9a26ad62016-06-10 12:03:08 -07001207 def repair_servo(self):
Dan Shi90466352015-09-22 15:01:05 -07001208 """
Richard Barnette9a26ad62016-06-10 12:03:08 -07001209 Confirm that servo is initialized and verified.
Dan Shi90466352015-09-22 15:01:05 -07001210
Richard Barnette9a26ad62016-06-10 12:03:08 -07001211 If the servo object is missing, attempt to repair the servo
1212 host. Repair failures are passed back to the caller.
1213
1214 @raise AutoservError: If there is no servo host for this CrOS
1215 host.
1216 """
1217 if self.servo:
1218 return
1219 if not self._servo_host:
1220 raise error.AutoservError('No servo host for %s.' %
1221 self.hostname)
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001222 try:
1223 self._servo_host.repair()
1224 except:
1225 raise
1226 finally:
1227 self.set_servo_host(self._servo_host)
1228
1229
Otabek Kasimov41301a22020-05-10 15:28:21 -07001230 def set_servo_type(self):
1231 """Set servo info labels to dut host_info"""
1232 if not self.servo:
Otabek Kasimovbea89e52020-05-12 15:40:00 -07001233 logging.debug('Servo is not initialized to get servo_type.')
Otabek Kasimov41301a22020-05-10 15:28:21 -07001234 return
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -08001235 if not self.is_servo_in_working_state():
1236 logging.debug('Servo is not good, skip update servo_type.')
1237 return
Otabek Kasimov41301a22020-05-10 15:28:21 -07001238 servo_type = self.servo.get_servo_type()
1239 if not servo_type:
Otabek Kasimovbea89e52020-05-12 15:40:00 -07001240 logging.debug('Cannot collect servo_type from servo'
Otabek Kasimov41301a22020-05-10 15:28:21 -07001241 ' by `dut-control servo_type`! Please file a bug'
1242 ' and inform infra team as we are not expected '
1243 ' to reach this point.')
1244 return
1245 host_info = self.host_info_store.get()
1246 prefix = servo_constants.SERVO_TYPE_LABEL_PREFIX
1247 old_type = host_info.get_label_value(prefix)
1248 if old_type == servo_type:
1249 # do not need update
1250 return
1251 host_info.set_version_label(prefix, servo_type)
1252 self.host_info_store.commit(host_info)
1253 logging.info('ServoHost: servo_type updated to %s '
1254 '(previous: %s)', servo_type, old_type)
1255
1256
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001257 def set_servo_state(self, servo_state):
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001258 """Set servo info labels to dut host_info"""
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001259 if servo_state is not None:
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001260 host_info = self.host_info_store.get()
Garry Wang11b5e872020-03-11 15:14:08 -07001261 servo_state_prefix = servo_constants.SERVO_STATE_LABEL_PREFIX
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001262 old_state = host_info.get_label_value(servo_state_prefix)
1263 if old_state == servo_state:
1264 # do not need update
1265 return
1266 host_info.set_version_label(servo_state_prefix, servo_state)
Otabek Kasimovcc9738e2020-02-14 16:17:15 -08001267 self.host_info_store.commit(host_info)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001268 logging.info('ServoHost: servo_state updated to %s (previous: %s)',
1269 servo_state, old_state)
Dan Shi90466352015-09-22 15:01:05 -07001270
1271
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001272 def get_servo_state(self):
1273 host_info = self.host_info_store.get()
Garry Wang11b5e872020-03-11 15:14:08 -07001274 servo_state_prefix = servo_constants.SERVO_STATE_LABEL_PREFIX
Otabek Kasimova7ba91a2020-03-09 08:31:01 -07001275 return host_info.get_label_value(servo_state_prefix)
1276
Otabek Kasimov5f039202020-10-28 15:45:29 -07001277 def is_servo_in_working_state(self):
1278 """Validate servo is in WORKING state."""
1279 servo_state = self.get_servo_state()
1280 return servo_state == servo_constants.SERVO_STATE_WORKING
1281
Dana Goyette655af512020-09-03 10:48:23 -07001282 def get_servo_usb_state(self):
1283 """Get the label value indicating the health of the USB drive.
1284
1285 @return: The label value if defined, otherwise '' (empty string).
1286 @rtype: str
1287 """
1288 host_info = self.host_info_store.get()
1289 servo_usb_state_prefix = audit_const.SERVO_USB_STATE_PREFIX
1290 return host_info.get_label_value(servo_usb_state_prefix)
1291
1292 def is_servo_usb_usable(self):
1293 """Check if the servo USB storage device is usable for FAFT.
1294
1295 @return: False if the label indicates a state that will break FAFT.
1296 True if state is okay, or if state is not defined.
1297 @rtype: bool
1298 """
1299 usb_state = self.get_servo_usb_state()
1300 return usb_state in ('', audit_const.HW_STATE_ACCEPTABLE,
1301 audit_const.HW_STATE_NORMAL,
1302 audit_const.HW_STATE_UNKNOWN)
Otabek Kasimov41301a22020-05-10 15:28:21 -07001303
Garry Wang000c6c02020-05-11 21:27:23 -07001304 def _set_smart_usbhub_label(self, smart_usbhub_detected):
1305 if smart_usbhub_detected is None:
1306 # skip the label update here as this indicate we wasn't able
1307 # to confirm usbhub type.
1308 return
1309 host_info = self.host_info_store.get()
1310 if (smart_usbhub_detected ==
1311 (servo_constants.SMART_USBHUB_LABEL in host_info.labels)):
1312 # skip label update if current label match the truth.
1313 return
1314 if smart_usbhub_detected:
1315 logging.info('Adding %s label to host %s',
1316 servo_constants.SMART_USBHUB_LABEL,
1317 self.hostname)
1318 host_info.labels.append(servo_constants.SMART_USBHUB_LABEL)
1319 else:
1320 logging.info('Removing %s label from host %s',
1321 servo_constants.SMART_USBHUB_LABEL,
1322 self.hostname)
1323 host_info.labels.remove(servo_constants.SMART_USBHUB_LABEL)
1324 self.host_info_store.commit(host_info)
1325
J. Richard Barnettec2d99cf2015-11-18 12:46:15 -08001326 def repair(self):
Derek Beckett3c5368c2021-08-03 14:13:58 -07001327 """Attempt to repair the DUT."""
1328 # TODO b/195447992: Wire this into lab services.
1329 logging.warning("Repair is being moved from built-in to lab services.")
Scott Zawalski89c44dd2013-02-26 09:28:02 -05001330
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001331 def get_verifier_state(self, tag):
Otabek Kasimov44273d22021-02-26 17:13:24 -08001332 """Return the state of host verifier by tag.
Otabek Kasimov8bb09912020-10-01 14:44:57 -07001333
1334 @returns: bool or None
1335 """
1336 return self._repair_strategy.verifier_is_good(tag)
Richard Barnette82c35912012-11-20 10:09:10 -08001337
Otabek Kasimov44273d22021-02-26 17:13:24 -08001338 def get_repair_strategy_node(self, tag):
1339 """Return the instance of verifier/repair node for host by tag.
1340
1341 @returns: _DependencyNode or None
1342 """
1343 return self._repair_strategy.node_by_tag(tag)
1344
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001345 def close(self):
David Rileye2c6be12017-12-11 10:20:57 -08001346 """Close connection."""
Fang Deng0ca40e22013-08-27 17:47:44 -07001347 super(CrosHost, self).close()
howardchung83e55272019-08-08 14:08:05 +08001348
Shijin Abraham783a7dd2020-02-14 15:36:11 -08001349 if self._chameleon_host:
1350 self._chameleon_host.close()
xixuand6011f12016-12-08 15:01:58 -08001351
Garry Wang1a493d82020-08-31 21:01:19 -07001352 if self.health_profile:
1353 try:
1354 self.health_profile.close()
1355 except Exception as e:
1356 logging.warning(
1357 'Failed to finalize device health profile; %s', e)
1358
xixuand6011f12016-12-08 15:01:58 -08001359 if self._servo_host:
1360 self._servo_host.close()
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001361
Dan Shi49ca0932014-11-14 11:22:27 -08001362 def get_power_supply_info(self):
1363 """Get the output of power_supply_info.
1364
1365 power_supply_info outputs the info of each power supply, e.g.,
1366 Device: Line Power
1367 online: no
1368 type: Mains
1369 voltage (V): 0
1370 current (A): 0
1371 Device: Battery
1372 state: Discharging
1373 percentage: 95.9276
1374 technology: Li-ion
1375
1376 Above output shows two devices, Line Power and Battery, with details of
1377 each device listed. This function parses the output into a dictionary,
1378 with key being the device name, and value being a dictionary of details
1379 of the device info.
1380
1381 @return: The dictionary of power_supply_info, e.g.,
1382 {'Line Power': {'online': 'yes', 'type': 'main'},
1383 'Battery': {'vendor': 'xyz', 'percentage': '100'}}
Dan Shie9b765d2014-12-29 16:59:49 -08001384 @raise error.AutoservRunError if power_supply_info tool is not found in
1385 the DUT. Caller should handle this error to avoid false failure
1386 on verification.
Dan Shi49ca0932014-11-14 11:22:27 -08001387 """
1388 result = self.run('power_supply_info').stdout.strip()
1389 info = {}
1390 device_name = None
1391 device_info = {}
1392 for line in result.split('\n'):
1393 pair = [v.strip() for v in line.split(':')]
1394 if len(pair) != 2:
1395 continue
1396 if pair[0] == 'Device':
1397 if device_name:
1398 info[device_name] = device_info
1399 device_name = pair[1]
1400 device_info = {}
1401 else:
1402 device_info[pair[0]] = pair[1]
1403 if device_name and not device_name in info:
1404 info[device_name] = device_info
1405 return info
1406
1407
1408 def get_battery_percentage(self):
1409 """Get the battery percentage.
1410
1411 @return: The percentage of battery level, value range from 0-100. Return
1412 None if the battery info cannot be retrieved.
1413 """
1414 try:
1415 info = self.get_power_supply_info()
1416 logging.info(info)
1417 return float(info['Battery']['percentage'])
Dan Shie9b765d2014-12-29 16:59:49 -08001418 except (KeyError, ValueError, error.AutoservRunError):
Dan Shi49ca0932014-11-14 11:22:27 -08001419 return None
1420
1421
Philip Chenaf69ead2020-03-27 13:06:42 -07001422 def get_battery_state(self):
1423 """Get the battery charging state.
1424
1425 @return: A string representing the battery charging state. It can be
1426 'Charging', 'Fully charged', or 'Discharging'.
1427 """
1428 try:
1429 info = self.get_power_supply_info()
1430 logging.info(info)
1431 return info['Battery']['state']
1432 except (KeyError, ValueError, error.AutoservRunError):
1433 return None
1434
1435
Daniel Campello8ca25c22019-12-13 16:48:26 -07001436 def get_battery_display_percentage(self):
1437 """Get the battery display percentage.
1438
1439 @return: The display percentage of battery level, value range from
1440 0-100. Return None if the battery info cannot be retrieved.
1441 """
1442 try:
1443 info = self.get_power_supply_info()
1444 logging.info(info)
1445 return float(info['Battery']['display percentage'])
1446 except (KeyError, ValueError, error.AutoservRunError):
1447 return None
1448
1449
Dan Shi49ca0932014-11-14 11:22:27 -08001450 def is_ac_connected(self):
1451 """Check if the dut has power adapter connected and charging.
1452
1453 @return: True if power adapter is connected and charging.
1454 """
1455 try:
1456 info = self.get_power_supply_info()
1457 return info['Line Power']['online'] == 'yes'
Dan Shie9b765d2014-12-29 16:59:49 -08001458 except (KeyError, error.AutoservRunError):
1459 return None
Dan Shi49ca0932014-11-14 11:22:27 -08001460
1461
Simran Basi5e6339a2013-03-21 11:34:32 -07001462 def _cleanup_poweron(self):
1463 """Special cleanup method to make sure hosts always get power back."""
Garry Wangad4d4fd2019-01-30 17:00:38 -08001464 info = self.host_info_store.get()
1465 if self._RPM_OUTLET_CHANGED not in info.attributes:
Simran Basi5e6339a2013-03-21 11:34:32 -07001466 return
1467 logging.debug('This host has recently interacted with the RPM'
1468 ' Infrastructure. Ensuring power is on.')
1469 try:
1470 self.power_on()
Garry Wangad4d4fd2019-01-30 17:00:38 -08001471 self._remove_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001472 except Exception:
1473 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basi5e6339a2013-03-21 11:34:32 -07001474 logging.error('Failed to turn Power On for this host after '
1475 'cleanup through the RPM Infrastructure.')
Dan Shi49ca0932014-11-14 11:22:27 -08001476
1477 battery_percentage = self.get_battery_percentage()
Otabek Kasimov58e22562020-11-03 17:17:41 -08001478 if (
1479 battery_percentage
1480 and battery_percentage < cros_constants.MIN_BATTERY_LEVEL):
Dan Shi49ca0932014-11-14 11:22:27 -08001481 raise
1482 elif self.is_ac_connected():
1483 logging.info('The device has power adapter connected and '
1484 'charging. No need to try to turn RPM on '
1485 'again.')
Garry Wangad4d4fd2019-01-30 17:00:38 -08001486 self._remove_rpm_changed_tag()
Dan Shi49ca0932014-11-14 11:22:27 -08001487 logging.info('Battery level is now at %s%%. The device may '
1488 'still have enough power to run test, so no '
1489 'exception will be raised.', battery_percentage)
1490
Simran Basi5e6339a2013-03-21 11:34:32 -07001491
Garry Wangad4d4fd2019-01-30 17:00:38 -08001492 def _remove_rpm_changed_tag(self):
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001493 # TODO b/195443964: Re-wire as needed once TLW is available.
1494 pass
Garry Wangad4d4fd2019-01-30 17:00:38 -08001495
1496
1497 def _add_rpm_changed_tag(self):
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001498 # TODO b/195443964: Re-wire as needed once TLW is available.
1499 pass
Garry Wangad4d4fd2019-01-30 17:00:38 -08001500
1501
beepsc87ff602013-07-31 21:53:00 -07001502 def _is_factory_image(self):
1503 """Checks if the image on the DUT is a factory image.
1504
1505 @return: True if the image on the DUT is a factory image.
1506 False otherwise.
1507 """
1508 result = self.run('[ -f /root/.factory_test ]', ignore_status=True)
1509 return result.exit_status == 0
1510
1511
1512 def _restart_ui(self):
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001513 """Restart the Chrome UI.
beepsc87ff602013-07-31 21:53:00 -07001514
1515 @raises: FactoryImageCheckerException for factory images, since
1516 we cannot attempt to restart ui on them.
1517 error.AutoservRunError for any other type of error that
1518 occurs while restarting ui.
1519 """
1520 if self._is_factory_image():
Dan Shi549fb822015-03-24 18:01:11 -07001521 raise FactoryImageCheckerException('Cannot restart ui on factory '
1522 'images')
beepsc87ff602013-07-31 21:53:00 -07001523
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001524 # TODO(jrbarnette): The command to stop/start the ui job
1525 # should live inside cros_ui, too. However that would seem
1526 # to imply interface changes to the existing start()/restart()
1527 # functions, which is a bridge too far (for now).
J. Richard Barnette6069aa12015-06-08 09:10:24 -07001528 prompt = cros_ui.get_chrome_session_ident(self)
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001529 self.run('stop ui; start ui')
1530 cros_ui.wait_for_chrome_ready(prompt, self)
beepsc87ff602013-07-31 21:53:00 -07001531
1532
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001533 def _start_powerd_if_needed(self):
1534 """Start powerd if it isn't already running."""
1535 self.run('start powerd', ignore_status=True)
1536
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001537 def _read_arc_prop_file(self, filename):
1538 for path in [
1539 '/usr/share/arcvm/properties/', '/usr/share/arc/properties/'
1540 ]:
1541 if self.path_exists(path + filename):
1542 return utils.parse_cmd_output('cat ' + path + filename,
1543 run_method=self.run)
1544 return None
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001545
Jiyoun Hac172ee72020-12-15 08:57:29 +09001546 def _get_arc_build_info(self):
1547 """Returns a dictionary mapping build properties to their values."""
1548 build_info = None
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001549 for filename in ['build.prop', 'vendor_build.prop']:
1550 properties = self._read_arc_prop_file(filename)
1551 if properties:
1552 if build_info:
1553 build_info.update(properties)
1554 else:
1555 build_info = properties
1556 else:
1557 logging.error('Failed to find %s in device.', filename)
Jiyoun Hac172ee72020-12-15 08:57:29 +09001558 return build_info
1559
Jiyoun Haba37f312021-01-13 09:44:16 +09001560 def get_arc_primary_abi(self):
Jiyoun Hac172ee72020-12-15 08:57:29 +09001561 """Returns the primary abi of the host."""
1562 return self._get_arc_build_info().get('ro.product.cpu.abi')
1563
Jiyoun Haba37f312021-01-13 09:44:16 +09001564 def get_arc_security_patch(self):
Jiyoun Hac172ee72020-12-15 08:57:29 +09001565 """Returns the security patch of the host."""
1566 return self._get_arc_build_info().get('ro.build.version.security_patch')
1567
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001568 def get_arc_first_api_level(self):
1569 """Returns the security patch of the host."""
1570 return self._get_arc_build_info().get('ro.product.first_api_level')
1571
xixuana3bbc422017-05-04 15:57:21 -07001572 def _get_lsb_release_content(self):
1573 """Return the content of lsb-release file of host."""
1574 return self.run(
1575 'cat "%s"' % client_constants.LSB_RELEASE).stdout.strip()
1576
1577
Dan Shi549fb822015-03-24 18:01:11 -07001578 def get_release_version(self):
1579 """Get the value of attribute CHROMEOS_RELEASE_VERSION from lsb-release.
1580
1581 @returns The version string in lsb-release, under attribute
1582 CHROMEOS_RELEASE_VERSION.
1583 """
Dan Shi549fb822015-03-24 18:01:11 -07001584 return lsbrelease_utils.get_chromeos_release_version(
xixuana3bbc422017-05-04 15:57:21 -07001585 lsb_release_content=self._get_lsb_release_content())
1586
1587
Don Garrettb9f35802018-01-22 18:25:40 -08001588 def get_release_builder_path(self):
1589 """Get the value of CHROMEOS_RELEASE_BUILDER_PATH from lsb-release.
1590
1591 @returns The version string in lsb-release, under attribute
1592 CHROMEOS_RELEASE_BUILDER_PATH.
1593 """
1594 return lsbrelease_utils.get_chromeos_release_builder_path(
1595 lsb_release_content=self._get_lsb_release_content())
1596
1597
xixuana3bbc422017-05-04 15:57:21 -07001598 def get_chromeos_release_milestone(self):
1599 """Get the value of attribute CHROMEOS_RELEASE_BUILD_TYPE
1600 from lsb-release.
1601
1602 @returns The version string in lsb-release, under attribute
1603 CHROMEOS_RELEASE_BUILD_TYPE.
1604 """
1605 return lsbrelease_utils.get_chromeos_release_milestone(
1606 lsb_release_content=self._get_lsb_release_content())
Dan Shi549fb822015-03-24 18:01:11 -07001607
1608
1609 def verify_cros_version_label(self):
Garry Wangd18e7b32020-08-07 18:31:44 -07001610 """Verify if host's cros-version label match the actual image in dut.
Dan Shi549fb822015-03-24 18:01:11 -07001611
Garry Wangd18e7b32020-08-07 18:31:44 -07001612 @returns True if the label match with image in dut, otherwise False
Dan Shi549fb822015-03-24 18:01:11 -07001613 """
Garry Wangd18e7b32020-08-07 18:31:44 -07001614 os_from_host = self.get_release_builder_path()
1615 info = self.host_info_store.get()
1616 os_from_label = info.get_label_value(self.VERSION_PREFIX)
1617 if not os_from_label:
1618 logging.debug('No existing %s label detected', self.VERSION_PREFIX)
1619 return True
1620
1621 # known cases where the version label will not match the
1622 # original CHROMEOS_RELEASE_BUILDER_PATH setting:
1623 # * Tests for the `arc-presubmit` append "-cheetsth" to the label.
1624 if os_from_label.endswith(provision.CHEETS_SUFFIX):
1625 logging.debug('%s label with %s suffix detected, this suffix will'
1626 ' be ignored when comparing label.',
1627 self.VERSION_PREFIX, provision.CHEETS_SUFFIX)
1628 os_from_label = os_from_label[:-len(provision.CHEETS_SUFFIX)]
1629 logging.debug('OS version from host: %s; OS verision cached in '
1630 'label: %s', os_from_host, os_from_label)
1631 return os_from_label == os_from_host
Dan Shi549fb822015-03-24 18:01:11 -07001632
1633
Laurence Goodby778c9a42017-05-24 19:24:07 -07001634 def cleanup_services(self):
1635 """Reinitializes the device for cleanup.
1636
1637 Subclasses may override this to customize the cleanup method.
1638
1639 To indicate failure of the reset, the implementation may raise
1640 any of:
1641 error.AutoservRunError
1642 error.AutotestRunError
1643 FactoryImageCheckerException
1644
1645 @raises error.AutoservRunError
1646 @raises error.AutotestRunError
1647 @raises error.FactoryImageCheckerException
1648 """
1649 self._restart_ui()
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001650 self._start_powerd_if_needed()
Laurence Goodby778c9a42017-05-24 19:24:07 -07001651
1652
Gregory Nisbetec615d62020-12-11 17:59:20 +00001653 def cleanup(self):
1654 """Cleanup state on device."""
MK Ryu35d661e2014-09-25 17:44:10 -07001655 self.run('rm -f %s' % client_constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001656 try:
Laurence Goodby778c9a42017-05-24 19:24:07 -07001657 self.cleanup_services()
beepsc87ff602013-07-31 21:53:00 -07001658 except (error.AutotestRunError, error.AutoservRunError,
1659 FactoryImageCheckerException):
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001660 logging.warning('Unable to restart ui.')
Namyoon Woo33f38852020-04-13 17:26:58 -07001661
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001662 # cleanup routines, i.e. reboot the machine.
Gregory Nisbetec615d62020-12-11 17:59:20 +00001663 super(CrosHost, self).cleanup()
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001664
Simran Basi5e6339a2013-03-21 11:34:32 -07001665 # Check if the rpm outlet was manipulated.
Simran Basid5e5e272012-09-24 15:23:59 -07001666 if self.has_power():
Simran Basi5e6339a2013-03-21 11:34:32 -07001667 self._cleanup_poweron()
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001668
Gregory Nisbetec615d62020-12-11 17:59:20 +00001669
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001670 def reboot(self, **dargs):
1671 """
1672 This function reboots the site host. The more generic
1673 RemoteHost.reboot() performs sync and sleeps for 5
1674 seconds. This is not necessary for Chrome OS devices as the
1675 sync should be finished in a short time during the reboot
1676 command.
1677 """
Gregory Nisbetec615d62020-12-11 17:59:20 +00001678 if 'reboot_cmd' not in dargs:
Doug Anderson7d5aeb22014-02-27 15:12:17 -08001679 reboot_timeout = dargs.get('reboot_timeout', 10)
J. Richard Barnette9af19632015-09-25 12:18:03 -07001680 dargs['reboot_cmd'] = ('sleep 1; '
1681 'reboot & sleep %d; '
1682 'reboot -f' % reboot_timeout)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001683 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001684 if 'fastsync' not in dargs:
1685 dargs['fastsync'] = True
Michael Liangda8c60a2014-06-03 13:24:51 -07001686
Prathmesh Prabhu53a4c1c2018-09-14 16:36:27 -07001687 dargs['board'] = self.host_info_store.get().board
Vincent Palatindf2372c2016-10-07 17:03:00 +02001688 # Record who called us
1689 orig = sys._getframe(1).f_code
Vincent Palatin80780b22016-07-27 16:02:37 +02001690 metric_fields = {'board' : dargs['board'],
Vincent Palatindf2372c2016-10-07 17:03:00 +02001691 'dut_host_name' : self.hostname,
1692 'success' : True}
1693 metric_debug_fields = {'board' : dargs['board'],
Prathmesh Prabhu53a4c1c2018-09-14 16:36:27 -07001694 'caller' : "%s:%s" % (orig.co_filename,
1695 orig.co_name),
Vincent Palatindf2372c2016-10-07 17:03:00 +02001696 'success' : True,
1697 'error' : ''}
1698
Vincent Palatin80780b22016-07-27 16:02:37 +02001699 try:
1700 super(CrosHost, self).reboot(**dargs)
1701 except Exception as e:
1702 metric_fields['success'] = False
Vincent Palatindf2372c2016-10-07 17:03:00 +02001703 metric_debug_fields['success'] = False
1704 metric_debug_fields['error'] = type(e).__name__
Vincent Palatin80780b22016-07-27 16:02:37 +02001705 raise
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001706
Kalin Stoyanov83d9caa2020-01-31 16:58:27 -08001707 def suspend(self, suspend_time=60, delay_seconds=0,
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001708 suspend_cmd=None, allow_early_resume=False):
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001709 """
1710 This function suspends the site host.
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001711
1712 @param suspend_time: How long to suspend as integer seconds.
1713 @param suspend_cmd: Suspend command to execute.
1714 @param allow_early_resume: If False and if device resumes before
1715 |suspend_time|, throw an error.
1716
1717 @exception AutoservSuspendError Host resumed earlier than
1718 |suspend_time|.
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001719 """
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001720
1721 if suspend_cmd is None:
1722 suspend_cmd = ' && '.join([
J. Richard Barnette9af19632015-09-25 12:18:03 -07001723 'echo 0 > /sys/class/rtc/rtc0/wakealarm',
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001724 'echo +%d > /sys/class/rtc/rtc0/wakealarm' % suspend_time,
Kalin Stoyanov83d9caa2020-01-31 16:58:27 -08001725 'powerd_dbus_suspend --delay=%d' % delay_seconds])
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001726 super(CrosHost, self).suspend(suspend_time, suspend_cmd,
1727 allow_early_resume);
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001728
1729
Simran Basiec564392014-08-25 16:48:09 -07001730 def upstart_status(self, service_name):
1731 """Check the status of an upstart init script.
1732
1733 @param service_name: Service to look up.
1734
1735 @returns True if the service is running, False otherwise.
1736 """
Richard Barnettee204dc52017-09-26 11:02:25 -07001737 return 'start/running' in self.run('status %s' % service_name,
1738 ignore_status=True).stdout
Simran Basiec564392014-08-25 16:48:09 -07001739
Tom Hughese9552342018-12-18 14:29:25 -08001740 def upstart_stop(self, service_name):
1741 """Stops an upstart job if it's running.
1742
1743 @param service_name: Service to stop
1744
1745 @returns True if service has been stopped or was already stopped
1746 False otherwise.
1747 """
1748 if not self.upstart_status(service_name):
1749 return True
1750
1751 result = self.run('stop %s' % service_name, ignore_status=True)
1752 if result.exit_status != 0:
1753 return False
1754 return True
1755
1756 def upstart_restart(self, service_name):
1757 """Restarts (or starts) an upstart job.
1758
1759 @param service_name: Service to start/restart
1760
1761 @returns True if service has been started/restarted, False otherwise.
1762 """
1763 cmd = 'start'
1764 if self.upstart_status(service_name):
1765 cmd = 'restart'
1766 cmd = cmd + ' %s' % service_name
1767 result = self.run(cmd)
1768 if result.exit_status != 0:
1769 return False
1770 return True
Simran Basiec564392014-08-25 16:48:09 -07001771
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001772 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001773 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001774
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001775 Tests for the following conditions:
1776 1. All conditions tested by the parent version of this
1777 function.
1778 2. Sufficient space in /mnt/stateful_partition.
Fang Deng6b05f5b2013-03-20 13:42:11 -07001779 3. Sufficient space in /mnt/stateful_partition/encrypted.
1780 4. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001781
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001782 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001783 super(CrosHost, self).verify_software()
Dan Shib8540a52015-07-16 14:18:23 -07001784 default_kilo_inodes_required = CONFIG.get_config_value(
1785 'SERVER', 'kilo_inodes_required', type=int, default=100)
1786 board = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
1787 kilo_inodes_required = CONFIG.get_config_value(
1788 'SERVER', 'kilo_inodes_required_%s' % board,
1789 type=int, default=default_kilo_inodes_required)
1790 self.check_inodes('/mnt/stateful_partition', kilo_inodes_required)
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001791 self.check_diskspace(
1792 '/mnt/stateful_partition',
Dan Shib8540a52015-07-16 14:18:23 -07001793 CONFIG.get_config_value(
Fang Deng6b05f5b2013-03-20 13:42:11 -07001794 'SERVER', 'gb_diskspace_required', type=float,
1795 default=20.0))
Gaurav Shahe448af82014-06-19 15:18:59 -07001796 encrypted_stateful_path = '/mnt/stateful_partition/encrypted'
1797 # Not all targets build with encrypted stateful support.
1798 if self.path_exists(encrypted_stateful_path):
1799 self.check_diskspace(
1800 encrypted_stateful_path,
Dan Shib8540a52015-07-16 14:18:23 -07001801 CONFIG.get_config_value(
Gaurav Shahe448af82014-06-19 15:18:59 -07001802 'SERVER', 'gb_encrypted_diskspace_required', type=float,
1803 default=0.1))
beepsc87ff602013-07-31 21:53:00 -07001804
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001805 self.wait_for_system_services()
Prashanth B5d0a0512014-04-25 12:26:08 -07001806
beepsc87ff602013-07-31 21:53:00 -07001807 # Factory images don't run update engine,
1808 # goofy controls dbus on these DUTs.
1809 if not self._is_factory_image():
1810 self.run('update_engine_client --status')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001811
1812
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001813 @retry.retry(error.AutoservError, timeout_min=5, delay_sec=10)
Kuo Jen Wei50ef6e82021-03-29 12:04:47 +08001814 def wait_for_service(self, service_name):
1815 """Wait for target status of an upstart init script.
1816
1817 @param service_name: Service to wait for.
1818 """
1819 if not self.upstart_status(service_name):
1820 raise error.AutoservError('Service %s not running.' % service_name)
1821
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001822 def wait_for_system_services(self):
1823 """Waits for system-services to be running.
1824
1825 Sometimes, update_engine will take a while to update firmware, so we
1826 should give this some time to finish. See crbug.com/765686#c38 for
1827 details.
1828 """
Kuo Jen Wei50ef6e82021-03-29 12:04:47 +08001829 self.wait_for_service('system-services')
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001830
1831
J. Richard Barnettea7e7fdc2016-02-12 12:35:36 -08001832 def verify(self):
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -08001833 """Verify Chrome OS system is in good state."""
Richard Barnetteabbdc252018-07-26 16:57:42 -07001834 message = 'Beginning verify for host %s board %s model %s'
1835 info = self.host_info_store.get()
1836 message %= (self.hostname, info.board, info.model)
1837 self.record('INFO', None, None, message)
Garry Wang87af1d02020-05-26 17:55:54 -07001838 try:
1839 self._repair_strategy.verify(self)
1840 except hosts.AutoservVerifyDependencyError as e:
1841 # We don't want flag a DUT as failed if only non-critical
1842 # verifier(s) failed during the repair.
1843 if e.is_critical():
1844 raise
J. Richard Barnettea7e7fdc2016-02-12 12:35:36 -08001845
1846
Fang Deng96667ca2013-08-01 17:46:18 -07001847 def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None,
Dean Liaoe3e75f62017-11-14 10:36:43 +08001848 connect_timeout=None, alive_interval=None,
1849 alive_count_max=None, connection_attempts=None):
Fang Deng96667ca2013-08-01 17:46:18 -07001850 """Override default make_ssh_command to use options tuned for Chrome OS.
1851
1852 Tuning changes:
1853 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH
1854 connection failure. Consistency with remote_access.sh.
1855
Samuel Tan2ce155b2015-06-23 18:24:38 -07001856 - ServerAliveInterval=900; which causes SSH to ping connection every
1857 900 seconds. In conjunction with ServerAliveCountMax ensures
1858 that if the connection dies, Autotest will bail out.
Fang Deng96667ca2013-08-01 17:46:18 -07001859 Originally tried 60 secs, but saw frequent job ABORTS where
Samuel Tan2ce155b2015-06-23 18:24:38 -07001860 the test completed successfully. Later increased from 180 seconds to
1861 900 seconds to account for tests where the DUT is suspended for
1862 longer periods of time.
Fang Deng96667ca2013-08-01 17:46:18 -07001863
1864 - ServerAliveCountMax=3; consistency with remote_access.sh.
1865
1866 - ConnectAttempts=4; reduce flakiness in connection errors;
1867 consistency with remote_access.sh.
1868
1869 - UserKnownHostsFile=/dev/null; we don't care about the keys.
1870 Host keys change with every new installation, don't waste
1871 memory/space saving them.
1872
1873 - SSH protocol forced to 2; needed for ServerAliveInterval.
1874
1875 @param user User name to use for the ssh connection.
1876 @param port Port on the target host to use for ssh connection.
1877 @param opts Additional options to the ssh command.
1878 @param hosts_file Ignored.
1879 @param connect_timeout Ignored.
1880 @param alive_interval Ignored.
Dean Liaoe3e75f62017-11-14 10:36:43 +08001881 @param alive_count_max Ignored.
1882 @param connection_attempts Ignored.
Fang Deng96667ca2013-08-01 17:46:18 -07001883 """
Dean Liaoe3e75f62017-11-14 10:36:43 +08001884 options = ' '.join([opts, '-o Protocol=2'])
1885 return super(CrosHost, self).make_ssh_command(
1886 user=user, port=port, opts=options, hosts_file='/dev/null',
1887 connect_timeout=30, alive_interval=900, alive_count_max=3,
1888 connection_attempts=4)
1889
1890
Jason Abeleb6f924f2013-11-13 16:01:54 -08001891 def syslog(self, message, tag='autotest'):
1892 """Logs a message to syslog on host.
1893
1894 @param message String message to log into syslog
1895 @param tag String tag prefix for syslog
1896
1897 """
1898 self.run('logger -t "%s" "%s"' % (tag, message))
1899
1900
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001901 def _ping_check_status(self, status):
1902 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001903
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001904 @param status Check the ping status against this value.
1905 @return True iff `status` and the result of ping are the same
1906 (i.e. both True or both False).
1907
1908 """
Abhishek Pandit-Subedi038df162020-09-14 16:37:43 -07001909 ping_val = utils.ping(self.hostname,
1910 tries=1,
1911 deadline=1,
1912 timeout=2,
1913 ignore_timeout=True)
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001914 return not (status ^ (ping_val == 0))
1915
1916 def _ping_wait_for_status(self, status, timeout):
1917 """Wait for the host to have a given status (UP or DOWN).
1918
1919 Status is checked by polling. Polling will not last longer
1920 than the number of seconds in `timeout`. The polling
1921 interval will be long enough that only approximately
1922 _PING_WAIT_COUNT polling cycles will be executed, subject
1923 to a maximum interval of about one minute.
1924
1925 @param status Waiting will stop immediately if `ping` of the
1926 host returns this status.
1927 @param timeout Poll for at most this many seconds.
1928 @return True iff the host status from `ping` matched the
1929 requested status at the time of return.
1930
1931 """
1932 # _ping_check_status() takes about 1 second, hence the
1933 # "- 1" in the formula below.
Nathan Ciobanu38480a32016-10-25 15:26:45 -07001934 # FIXME: if the ping command errors then _ping_check_status()
1935 # returns instantly. If timeout is also smaller than twice
1936 # _PING_WAIT_COUNT then the while loop below forks many
1937 # thousands of ping commands (see /tmp/test_that_results_XXXXX/
1938 # /results-1-logging_YYY.ZZZ/debug/autoserv.DEBUG) and hogs one
1939 # CPU core for 60 seconds.
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001940 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
1941 end_time = time.time() + timeout
1942 while time.time() <= end_time:
1943 if self._ping_check_status(status):
1944 return True
1945 if poll_interval > 0:
1946 time.sleep(poll_interval)
1947
1948 # The last thing we did was sleep(poll_interval), so it may
1949 # have been too long since the last `ping`. Check one more
1950 # time, just to be sure.
1951 return self._ping_check_status(status)
1952
1953 def ping_wait_up(self, timeout):
1954 """Wait for the host to respond to `ping`.
1955
1956 N.B. This method is not a reliable substitute for
1957 `wait_up()`, because a host that responds to ping will not
1958 necessarily respond to ssh. This method should only be used
1959 if the target DUT can be considered functional even if it
1960 can't be reached via ssh.
1961
1962 @param timeout Minimum time to allow before declaring the
1963 host to be non-responsive.
1964 @return True iff the host answered to ping before the timeout.
1965
1966 """
Andrew Luo4be621d2020-03-21 07:01:13 -07001967 if self.use_icmp:
1968 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
1969 else:
1970 logging.debug('Using SSH instead of ICMP for ping_wait_up.')
1971 return self.wait_up(timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001972
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001973 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001974 """Wait until the host no longer responds to `ping`.
1975
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001976 This function can be used as a slightly faster version of
1977 `wait_down()`, by avoiding potentially long ssh timeouts.
1978
1979 @param timeout Minimum time to allow for the host to become
1980 non-responsive.
1981 @return True iff the host quit answering ping before the
1982 timeout.
1983
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001984 """
Andrew Luo4be621d2020-03-21 07:01:13 -07001985 if self.use_icmp:
1986 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
1987 else:
1988 logging.debug('Using SSH instead of ICMP for ping_wait_down.')
1989 return self.wait_down(timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001990
Anand K Mistry50f218e2020-07-31 14:50:15 +10001991 def _is_host_port_forwarded(self):
Garry Wanga2e78172020-09-09 23:49:07 -07001992 """Checks if the dut is connected over port forwarding.
Anand K Mistry50f218e2020-07-31 14:50:15 +10001993
1994 N.B. This method does not detect all situations where port forwarding is
1995 occurring. Namely, running autotest on the dut may result in a
1996 false-positive, and port forwarding using a different machine on the
1997 same network will be a false-negative.
1998
1999 @return True if the dut is connected over port forwarding
2000 False otherwise
2001 """
Garry Wanga2e78172020-09-09 23:49:07 -07002002 is_localhost = self.hostname in ['localhost', '127.0.0.1']
2003 is_forwarded = is_localhost and not self.is_default_port
2004 if is_forwarded:
2005 logging.info('Detected DUT connected by port forwarding')
2006 return is_forwarded
Anand K Mistry50f218e2020-07-31 14:50:15 +10002007
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002008 def test_wait_for_sleep(self, sleep_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002009 """Wait for the client to enter low-power sleep mode.
2010
2011 The test for "is asleep" can't distinguish a system that is
2012 powered off; to confirm that the unit was asleep, it is
2013 necessary to force resume, and then call
2014 `test_wait_for_resume()`.
2015
2016 This function is expected to be called from a test as part
2017 of a sequence like the following:
2018
2019 ~~~~~~~~
2020 boot_id = host.get_boot_id()
2021 # trigger sleep on the host
2022 host.test_wait_for_sleep()
2023 # trigger resume on the host
2024 host.test_wait_for_resume(boot_id)
2025 ~~~~~~~~
2026
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002027 @param sleep_timeout time limit in seconds to allow the host sleep.
2028
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002029 @exception TestFail The host did not go to sleep within
2030 the allowed time.
2031 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002032 if sleep_timeout is None:
2033 sleep_timeout = self.SLEEP_TIMEOUT
2034
Anand K Mistry50f218e2020-07-31 14:50:15 +10002035 # If the dut is accessed over SSH port-forwarding, `ping` is not useful
2036 # for detecting the dut is down since a ping to localhost will always
2037 # succeed. In this case, fall back to wait_down() which uses SSH.
2038 if self._is_host_port_forwarded():
Garry Wanga2e78172020-09-09 23:49:07 -07002039 success = self.wait_down(timeout=sleep_timeout)
Anand K Mistry50f218e2020-07-31 14:50:15 +10002040 else:
Garry Wanga2e78172020-09-09 23:49:07 -07002041 success = self.ping_wait_down(timeout=sleep_timeout)
Anand K Mistry50f218e2020-07-31 14:50:15 +10002042
2043 if not success:
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002044 raise error.TestFail(
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002045 'client failed to sleep after %d seconds' % sleep_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002046
2047
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002048 def test_wait_for_resume(self, old_boot_id, resume_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002049 """Wait for the client to resume from low-power sleep mode.
2050
2051 The `old_boot_id` parameter should be the value from
2052 `get_boot_id()` obtained prior to entering sleep mode. A
2053 `TestFail` exception is raised if the boot id changes.
2054
2055 See @ref test_wait_for_sleep for more on this function's
2056 usage.
2057
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08002058 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002059 target host went to sleep.
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002060 @param resume_timeout time limit in seconds to allow the host up.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002061
2062 @exception TestFail The host did not respond within the
2063 allowed time.
2064 @exception TestFail The host responded, but the boot id test
2065 indicated a reboot rather than a sleep
2066 cycle.
2067 """
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002068 if resume_timeout is None:
2069 resume_timeout = self.RESUME_TIMEOUT
2070
2071 if not self.wait_up(timeout=resume_timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002072 raise error.TestFail(
2073 'client failed to resume from sleep after %d seconds' %
Tom Wai-Hong Tamfced4f62014-04-17 10:56:30 +08002074 resume_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002075 else:
2076 new_boot_id = self.get_boot_id()
2077 if new_boot_id != old_boot_id:
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002078 logging.error('client rebooted (old boot %s, new boot %s)',
2079 old_boot_id, new_boot_id)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002080 raise error.TestFail(
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002081 'client rebooted, but sleep was expected')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002082
2083
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002084 def test_wait_for_shutdown(self, shutdown_timeout=None):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002085 """Wait for the client to shut down.
2086
2087 The test for "has shut down" can't distinguish a system that
2088 is merely asleep; to confirm that the unit was down, it is
2089 necessary to force boot, and then call test_wait_for_boot().
2090
2091 This function is expected to be called from a test as part
2092 of a sequence like the following:
2093
2094 ~~~~~~~~
2095 boot_id = host.get_boot_id()
2096 # trigger shutdown on the host
2097 host.test_wait_for_shutdown()
2098 # trigger boot on the host
2099 host.test_wait_for_boot(boot_id)
2100 ~~~~~~~~
2101
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002102 @param shutdown_timeout time limit in seconds to allow the host down.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002103 @exception TestFail The host did not shut down within the
2104 allowed time.
2105 """
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002106 if shutdown_timeout is None:
2107 shutdown_timeout = self.SHUTDOWN_TIMEOUT
2108
Anand K Mistry50f218e2020-07-31 14:50:15 +10002109 if self._is_host_port_forwarded():
Garry Wanga2e78172020-09-09 23:49:07 -07002110 success = self.wait_down(timeout=shutdown_timeout)
Anand K Mistry50f218e2020-07-31 14:50:15 +10002111 else:
Garry Wanga2e78172020-09-09 23:49:07 -07002112 success = self.ping_wait_down(timeout=shutdown_timeout)
Anand K Mistry50f218e2020-07-31 14:50:15 +10002113
2114 if not success:
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002115 raise error.TestFail(
2116 'client failed to shut down after %d seconds' %
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +08002117 shutdown_timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002118
2119
2120 def test_wait_for_boot(self, old_boot_id=None):
2121 """Wait for the client to boot from cold power.
2122
2123 The `old_boot_id` parameter should be the value from
2124 `get_boot_id()` obtained prior to shutting down. A
2125 `TestFail` exception is raised if the boot id does not
2126 change. The boot id test is omitted if `old_boot_id` is not
2127 specified.
2128
2129 See @ref test_wait_for_shutdown for more on this function's
2130 usage.
2131
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08002132 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002133 shut down.
2134
2135 @exception TestFail The host did not respond within the
2136 allowed time.
2137 @exception TestFail The host responded, but the boot id test
2138 indicated that there was no reboot.
2139 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07002140 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002141 raise error.TestFail(
2142 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07002143 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002144 elif old_boot_id:
2145 if self.get_boot_id() == old_boot_id:
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002146 logging.error('client not rebooted (boot %s)',
2147 old_boot_id)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07002148 raise error.TestFail(
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08002149 'client is back up, but did not reboot')
Simran Basid5e5e272012-09-24 15:23:59 -07002150
2151
2152 @staticmethod
2153 def check_for_rpm_support(hostname):
2154 """For a given hostname, return whether or not it is powered by an RPM.
2155
Simran Basi1df55112013-09-06 11:25:09 -07002156 @param hostname: hostname to check for rpm support.
2157
Simran Basid5e5e272012-09-24 15:23:59 -07002158 @return None if this host does not follows the defined naming format
2159 for RPM powered DUT's in the lab. If it does follow the format,
2160 it returns a regular expression MatchObject instead.
2161 """
Fang Dengbaff9082015-01-06 13:46:15 -08002162 return re.match(CrosHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07002163
2164
2165 def has_power(self):
2166 """For this host, return whether or not it is powered by an RPM.
2167
2168 @return True if this host is in the CROS lab and follows the defined
2169 naming format.
2170 """
Fang Deng0ca40e22013-08-27 17:47:44 -07002171 return CrosHost.check_for_rpm_support(self.hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07002172
2173
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002174 def _set_power(self, state, power_method):
Garry Wang5e5538a2019-04-08 15:36:18 -07002175 """Sets the power to the host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002176
2177 @param state Specifies which power state to set to DUT
2178 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07002179 use. By default "RPM" or "CCD" will be used based
2180 on servo type. Valid values from
2181 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002182
2183 """
2184 ACCEPTABLE_STATES = ['ON', 'OFF']
2185
Garry Wang5e5538a2019-04-08 15:36:18 -07002186 if not power_method:
2187 power_method = self.get_default_power_method()
2188
2189 state = state.upper()
2190 if state not in ACCEPTABLE_STATES:
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002191 raise error.TestError('State must be one of: %s.'
2192 % (ACCEPTABLE_STATES,))
2193
2194 if power_method == self.POWER_CONTROL_SERVO:
2195 logging.info('Setting servo port J10 to %s', state)
2196 self.servo.set('prtctl3_pwren', state.lower())
2197 time.sleep(self._USB_POWER_TIMEOUT)
2198 elif power_method == self.POWER_CONTROL_MANUAL:
2199 logging.info('You have %d seconds to set the AC power to %s.',
2200 self._POWER_CYCLE_TIMEOUT, state)
2201 time.sleep(self._POWER_CYCLE_TIMEOUT)
Garry Wang5e5538a2019-04-08 15:36:18 -07002202 elif power_method == self.POWER_CONTROL_CCD:
2203 servo_role = 'src' if state == 'ON' else 'snk'
2204 logging.info('servo ccd power pass through detected,'
2205 ' changing servo_role to %s.', servo_role)
2206 self.servo.set_servo_v4_role(servo_role)
2207 if not self.ping_wait_up(timeout=self._CHANGE_SERVO_ROLE_TIMEOUT):
Garry Wang94bf9de2019-06-10 17:23:37 -07002208 # Make sure we don't leave DUT with no power(servo_role=snk)
2209 # when DUT is not pingable, as we raise a exception here
2210 # that may break a power cycle in the middle.
2211 self.servo.set_servo_v4_role('src')
Garry Wang5e5538a2019-04-08 15:36:18 -07002212 raise error.AutoservError(
2213 'DUT failed to regain network connection after %d seconds.'
2214 % self._CHANGE_SERVO_ROLE_TIMEOUT)
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002215 else:
2216 if not self.has_power():
2217 raise error.TestFail('DUT does not have RPM connected.')
Garry Wangad4d4fd2019-01-30 17:00:38 -08002218 self._add_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07002219 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basid5e5e272012-09-24 15:23:59 -07002220
2221
Garry Wang5e5538a2019-04-08 15:36:18 -07002222 def power_off(self, power_method=None):
2223 """Turn off power to this host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002224
2225 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07002226 use. By default "RPM" or "CCD" will be used based
2227 on servo type. Valid values from
2228 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002229
2230 """
Derek Beckettb66e5c82020-08-12 15:31:02 -07002231 self._sync_if_up()
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002232 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07002233
Derek Beckettb66e5c82020-08-12 15:31:02 -07002234 def _check_supported(self):
2235 """Throw an error if dts mode control is not supported."""
2236 if not self.servo_pwr_supported:
2237 raise error.TestFail('power_state controls not supported')
2238
2239 def _sync_if_up(self):
2240 """Run sync on the DUT and wait for completion if the DUT is up.
2241
2242 Additionally, try to sync and ignore status if its not up.
2243
2244 Useful prior to reboots to ensure files are written to disc.
2245
2246 """
2247 if self.is_up_fast():
2248 self.run("sync")
2249 return
2250 # If it is not up, attempt to sync in the rare event the DUT is up but
2251 # doesn't respond to a ping. Ignore any errors.
2252 try:
2253 self.run("sync", ignore_status=True, timeout=1)
2254 except Exception:
2255 pass
2256
2257 def power_off_via_servo(self):
2258 """Force the DUT to power off.
2259
2260 The DUT is guaranteed to be off at the end of this call,
2261 regardless of its previous state, provided that there is
2262 working EC and boot firmware. There is no requirement for
2263 working OS software.
2264
2265 """
2266 self._check_supported()
2267 self._sync_if_up()
2268 self.servo.set_nocheck('power_state', 'off')
2269
2270 def power_on_via_servo(self, rec_mode='on'):
2271 """Force the DUT to power on.
2272
2273 Prior to calling this function, the DUT must be powered off,
2274 e.g. with a call to `power_off()`.
2275
2276 At power on, recovery mode is set as specified by the
2277 corresponding argument. When booting with recovery mode on, it
2278 is the caller's responsibility to unplug/plug in a bootable
2279 external storage device.
2280
2281 If the DUT requires a delay after powering on but before
2282 processing inputs such as USB stick insertion, the delay is
2283 handled by this method; the caller is not responsible for such
2284 delays.
2285
2286 @param rec_mode Setting of recovery mode to be applied at
2287 power on. default: REC_OFF aka 'off'
2288
2289 """
2290 self._check_supported()
2291 self.servo.set_nocheck('power_state', rec_mode)
2292
2293 def reset_via_servo(self):
2294 """Force the DUT to reset.
2295
2296 The DUT is guaranteed to be on at the end of this call,
2297 regardless of its previous state, provided that there is
2298 working OS software. This also guarantees that the EC has
2299 been restarted.
2300
2301 """
2302 self._check_supported()
2303 self._sync_if_up()
2304 self.servo.set_nocheck('power_state', 'reset')
2305
Simran Basid5e5e272012-09-24 15:23:59 -07002306
Garry Wang5e5538a2019-04-08 15:36:18 -07002307 def power_on(self, power_method=None):
2308 """Turn on power to this host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002309
2310 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07002311 use. By default "RPM" or "CCD" will be used based
2312 on servo type. Valid values from
2313 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002314
2315 """
2316 self._set_power('ON', power_method)
2317
2318
Garry Wang5e5538a2019-04-08 15:36:18 -07002319 def power_cycle(self, power_method=None):
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002320 """Cycle power to this host by turning it OFF, then ON.
2321
2322 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07002323 use. By default "RPM" or "CCD" will be used based
2324 on servo type. Valid values from
2325 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002326
2327 """
Garry Wang5e5538a2019-04-08 15:36:18 -07002328 if not power_method:
2329 power_method = self.get_default_power_method()
2330
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002331 if power_method in (self.POWER_CONTROL_SERVO,
Garry Wang5e5538a2019-04-08 15:36:18 -07002332 self.POWER_CONTROL_MANUAL,
2333 self.POWER_CONTROL_CCD):
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08002334 self.power_off(power_method=power_method)
2335 time.sleep(self._POWER_CYCLE_TIMEOUT)
2336 self.power_on(power_method=power_method)
2337 else:
Garry Wangad4d4fd2019-01-30 17:00:38 -08002338 self._add_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07002339 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basic6f1f7a2012-10-16 10:47:46 -07002340
2341
Mary Ruthvende14a8b2019-08-23 12:43:52 -07002342 def get_platform_from_fwid(self):
2343 """Determine the platform from the crossystem fwid.
2344
2345 @returns a string representing this host's platform.
2346 """
Greg Edelstona7b05d12020-04-01 16:00:51 -06002347 # Look at the firmware for non-unibuild cases or if cros_config fails.
Mary Ruthvende14a8b2019-08-23 12:43:52 -07002348 crossystem = utils.Crossystem(self)
2349 crossystem.init()
2350 # Extract fwid value and use the leading part as the platform id.
2351 # fwid generally follow the format of {platform}.{firmware version}
2352 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
2353 platform = crossystem.fwid().split('.')[0].lower()
2354 # Newer platforms start with 'Google_' while the older ones do not.
2355 return platform.replace('google_', '')
2356
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08002357
Simran Basic6f1f7a2012-10-16 10:47:46 -07002358 def get_platform(self):
2359 """Determine the correct platform label for this host.
2360
2361 @returns a string representing this host's platform.
2362 """
C Shapiroed87c6f2018-04-19 09:13:58 -06002363 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
2364 run_method=self.run)
C Shapiroed87c6f2018-04-19 09:13:58 -06002365 platform = ''
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08002366 if release_info.get('CHROMEOS_RELEASE_UNIBUILD') == '1':
Greg Edelstona7b05d12020-04-01 16:00:51 -06002367 platform = self.get_model_from_cros_config()
Mary Ruthvende14a8b2019-08-23 12:43:52 -07002368 return platform if platform else self.get_platform_from_fwid()
Simran Basic6f1f7a2012-10-16 10:47:46 -07002369
2370
Greg Edelstona7b05d12020-04-01 16:00:51 -06002371 def get_model_from_cros_config(self):
2372 """Get the host model from cros_config command.
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08002373
Greg Edelstona7b05d12020-04-01 16:00:51 -06002374 @returns a string representing this host's model.
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08002375 """
Greg Edelstona7b05d12020-04-01 16:00:51 -06002376 return cros_config.call_cros_config_get_output('/ name',
2377 self.run, ignore_status=True)
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08002378
2379
Hung-ying Tyanb1328032014-04-01 14:18:54 +08002380 def get_architecture(self):
2381 """Determine the correct architecture label for this host.
2382
2383 @returns a string representing this host's architecture.
2384 """
2385 crossystem = utils.Crossystem(self)
2386 crossystem.init()
2387 return crossystem.arch()
2388
2389
Luis Lozano40b7d0d2014-01-17 15:12:06 -08002390 def get_chrome_version(self):
2391 """Gets the Chrome version number and milestone as strings.
2392
2393 Invokes "chrome --version" to get the version number and milestone.
2394
2395 @return A tuple (chrome_ver, milestone) where "chrome_ver" is the
2396 current Chrome version number as a string (in the form "W.X.Y.Z")
2397 and "milestone" is the first component of the version number
2398 (the "W" from "W.X.Y.Z"). If the version number cannot be parsed
2399 in the "W.X.Y.Z" format, the "chrome_ver" will be the full output
2400 of "chrome --version" and the milestone will be the empty string.
2401
2402 """
MK Ryu35d661e2014-09-25 17:44:10 -07002403 version_string = self.run(client_constants.CHROME_VERSION_COMMAND).stdout
Luis Lozano40b7d0d2014-01-17 15:12:06 -08002404 return utils.parse_chrome_version(version_string)
2405
J. Richard Barnetted2af5852016-02-05 15:03:10 -08002406
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08002407 def get_ec_version(self):
2408 """Get the ec version as strings.
2409
2410 @returns a string representing this host's ec version.
2411 """
Puthikorn Voravootivat65ad7672018-01-30 14:00:01 -08002412 command = 'mosys ec info -s fw_version'
Puthikorn Voravootivat720640d2018-02-16 17:32:38 -08002413 result = self.run(command, ignore_status=True)
2414 if result.exit_status != 0:
2415 return ''
2416 return result.stdout.strip()
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08002417
2418
2419 def get_firmware_version(self):
2420 """Get the firmware version as strings.
2421
2422 @returns a string representing this host's firmware version.
2423 """
2424 crossystem = utils.Crossystem(self)
2425 crossystem.init()
2426 return crossystem.fwid()
2427
2428
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08002429 def get_hardware_id(self):
2430 """Get hardware id as strings.
2431
2432 @returns a string representing this host's hardware id.
2433 """
2434 crossystem = utils.Crossystem(self)
2435 crossystem.init()
2436 return crossystem.hwid()
2437
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08002438 def get_hardware_revision(self):
2439 """Get the hardware revision as strings.
2440
2441 @returns a string representing this host's hardware revision.
2442 """
Puthikorn Voravootivat65ad7672018-01-30 14:00:01 -08002443 command = 'mosys platform version'
Puthikorn Voravootivat720640d2018-02-16 17:32:38 -08002444 result = self.run(command, ignore_status=True)
2445 if result.exit_status != 0:
2446 return ''
2447 return result.stdout.strip()
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08002448
2449
2450 def get_kernel_version(self):
2451 """Get the kernel version as strings.
2452
2453 @returns a string representing this host's kernel version.
2454 """
2455 return self.run('uname -r').stdout.strip()
2456
2457
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08002458 def get_cpu_name(self):
2459 """Get the cpu name as strings.
2460
2461 @returns a string representing this host's cpu name.
2462 """
2463
2464 # Try get cpu name from device tree first
2465 if self.path_exists('/proc/device-tree/compatible'):
2466 command = ' | '.join(
2467 ["sed -e 's/\\x0/\\n/g' /proc/device-tree/compatible",
2468 'tail -1'])
2469 return self.run(command).stdout.strip().replace(',', ' ')
2470
2471 # Get cpu name from uname -p
2472 command = 'uname -p'
2473 ret = self.run(command).stdout.strip()
2474
2475 # 'uname -p' return variant of unknown or amd64 or x86_64 or i686
2476 # Try get cpu name from /proc/cpuinfo instead
2477 if re.match("unknown|amd64|[ix][0-9]?86(_64)?", ret, re.IGNORECASE):
2478 command = "grep model.name /proc/cpuinfo | cut -f 2 -d: | head -1"
2479 self = self.run(command).stdout.strip()
2480
2481 # Remove bloat from CPU name, for example
2482 # Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz -> Intel Core i5-7Y57
2483 # Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz -> Intel Xeon E5-2690 v4
2484 # AMD A10-7850K APU with Radeon(TM) R7 Graphics -> AMD A10-7850K
2485 # AMD GX-212JC SOC with Radeon(TM) R2E Graphics -> AMD GX-212JC
2486 trim_re = r' (@|processor|apu|soc|radeon).*|\(.*?\)| cpu'
2487 return re.sub(trim_re, '', ret, flags=re.IGNORECASE)
2488
2489
2490 def get_screen_resolution(self):
2491 """Get the screen(s) resolution as strings.
2492 In case of more than 1 monitor, return resolution for each monitor
2493 separate with plus sign.
2494
2495 @returns a string representing this host's screen(s) resolution.
2496 """
2497 command = 'for f in /sys/class/drm/*/*/modes; do head -1 $f; done'
2498 ret = self.run(command, ignore_status=True)
2499 # We might have Chromebox without a screen
2500 if ret.exit_status != 0:
2501 return ''
2502 return ret.stdout.strip().replace('\n', '+')
2503
2504
2505 def get_mem_total_gb(self):
2506 """Get total memory available in the system in GiB (2^20).
2507
2508 @returns an integer representing total memory
2509 """
2510 mem_total_kb = self.read_from_meminfo('MemTotal')
2511 kb_in_gb = float(2 ** 20)
2512 return int(round(mem_total_kb / kb_in_gb))
2513
2514
2515 def get_disk_size_gb(self):
2516 """Get size of disk in GB (10^9)
2517
2518 @returns an integer representing size of disk, 0 in Error Case
2519 """
2520 command = 'grep $(rootdev -s -d | cut -f3 -d/)$ /proc/partitions'
2521 result = self.run(command, ignore_status=True)
2522 if result.exit_status != 0:
2523 return 0
2524 _, _, block, _ = re.split(r' +', result.stdout.strip())
2525 byte_per_block = 1024.0
2526 disk_kb_in_gb = 1e9
2527 return int(int(block) * byte_per_block / disk_kb_in_gb + 0.5)
2528
2529
2530 def get_battery_size(self):
2531 """Get size of battery in Watt-hour via sysfs
2532
2533 This method assumes that battery support voltage_min_design and
2534 charge_full_design sysfs.
2535
2536 @returns a float representing Battery size, 0 if error.
2537 """
2538 # sysfs report data in micro scale
2539 battery_scale = 1e6
2540
2541 command = 'cat /sys/class/power_supply/*/voltage_min_design'
2542 result = self.run(command, ignore_status=True)
2543 if result.exit_status != 0:
2544 return 0
2545 voltage = float(result.stdout.strip()) / battery_scale
2546
2547 command = 'cat /sys/class/power_supply/*/charge_full_design'
2548 result = self.run(command, ignore_status=True)
2549 if result.exit_status != 0:
2550 return 0
2551 amphereHour = float(result.stdout.strip()) / battery_scale
2552
2553 return voltage * amphereHour
2554
2555
2556 def get_low_battery_shutdown_percent(self):
2557 """Get the percent-based low-battery shutdown threshold.
2558
2559 @returns a float representing low-battery shutdown percent, 0 if error.
2560 """
2561 ret = 0.0
2562 try:
2563 command = 'check_powerd_config --low_battery_shutdown_percent'
2564 ret = float(self.run(command).stdout)
2565 except error.CmdError:
2566 logging.debug("Can't run %s", command)
2567 except ValueError:
2568 logging.debug("Didn't get number from %s", command)
2569
2570 return ret
2571
2572
Puthikorn Voravootivat09c83d72018-08-10 15:58:32 -07002573 def has_hammer(self):
2574 """Check whether DUT has hammer device or not.
2575
2576 @returns boolean whether device has hammer or not
2577 """
2578 command = 'grep Hammer /sys/bus/usb/devices/*/product'
2579 return self.run(command, ignore_status=True).exit_status == 0
2580
2581
Niranjan Kumar34618872017-05-31 12:57:09 -07002582 def is_chrome_switch_present(self, switch):
David Haddock3ce538e2017-06-22 13:37:05 -07002583 """Returns True if the specified switch was provided to Chrome.
2584
2585 @param switch The chrome switch to search for.
2586 """
Niranjan Kumar34618872017-05-31 12:57:09 -07002587
Niranjan Kumar5f23fe92017-06-22 15:18:55 -07002588 command = 'pgrep -x -f -c "/opt/google/chrome/chrome.*%s.*"' % switch
2589 return self.run(command, ignore_status=True).exit_status == 0
Niranjan Kumar34618872017-05-31 12:57:09 -07002590
2591
2592 def oobe_triggers_update(self):
2593 """Returns True if this host has an OOBE flow during which
2594 it will perform an update check and perhaps an update.
2595 One example of such a flow is Hands-Off Zero-Touch Enrollment.
2596 As more such flows are developed, code handling them needs
2597 to be added here.
2598
2599 @return Boolean indicating whether this host's OOBE triggers an update.
2600 """
2601 return self.is_chrome_switch_present(
2602 '--enterprise-enable-zero-touch-enrollment=hands-off')
2603
2604
Kevin Chenga2619dc2016-03-28 11:42:08 -07002605 # TODO(kevcheng): change this to just return the board without the
2606 # 'board:' prefix and fix up all the callers. Also look into removing the
2607 # need for this method.
Simran Basic6f1f7a2012-10-16 10:47:46 -07002608 def get_board(self):
2609 """Determine the correct board label for this host.
2610
2611 @returns a string representing this host's board.
2612 """
2613 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
2614 run_method=self.run)
J. Richard Barnetted2af5852016-02-05 15:03:10 -08002615 return (ds_constants.BOARD_PREFIX +
2616 release_info['CHROMEOS_RELEASE_BOARD'])
Simran Basic6f1f7a2012-10-16 10:47:46 -07002617
Po-Hsien Wang4618adf2017-10-10 14:40:21 -07002618 def get_channel(self):
2619 """Determine the correct channel label for this host.
Simran Basic6f1f7a2012-10-16 10:47:46 -07002620
Po-Hsien Wang4618adf2017-10-10 14:40:21 -07002621 @returns: a string represeting this host's build channel.
2622 (stable, dev, beta). None on fail.
2623 """
2624 return lsbrelease_utils.get_chromeos_channel(
2625 lsb_release_content=self._get_lsb_release_content())
Kevin Chenga328da62016-03-31 10:49:04 -07002626
Kevin Chenga328da62016-03-31 10:49:04 -07002627 def get_power_supply(self):
2628 """
2629 Determine what type of power supply the host has
2630
2631 @returns a string representing this host's power supply.
2632 'power:battery' when the device has a battery intended for
2633 extended use
2634 'power:AC_primary' when the device has a battery not intended
2635 for extended use (for moving the machine, etc)
2636 'power:AC_only' when the device has no battery at all.
2637 """
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002638 psu = self.run(command='cros_config /hardware-properties psu-type',
2639 ignore_status=True)
Kevin Chenga328da62016-03-31 10:49:04 -07002640 if psu.exit_status:
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002641 # Assume battery if unspecified in cros_config.
Kevin Chenga328da62016-03-31 10:49:04 -07002642 return 'power:battery'
2643
2644 psu_str = psu.stdout.strip()
2645 if psu_str == 'unknown':
2646 return None
2647
2648 return 'power:%s' % psu_str
2649
2650
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08002651 def has_battery(self):
2652 """Determine if DUT has a battery.
2653
2654 Returns:
2655 Boolean, False if known not to have battery, True otherwise.
2656 """
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002657 return self.get_power_supply() == 'power:battery'
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08002658
2659
Kevin Chenga328da62016-03-31 10:49:04 -07002660 def get_servo(self):
2661 """Determine if the host has a servo attached.
2662
2663 If the host has a working servo attached, it should have a servo label.
2664
2665 @return: string 'servo' if the host has servo attached. Otherwise,
2666 returns None.
2667 """
2668 return 'servo' if self._servo_host else None
2669
2670
Kevin Chenga328da62016-03-31 10:49:04 -07002671 def has_internal_display(self):
2672 """Determine if the device under test is equipped with an internal
2673 display.
2674
2675 @return: 'internal_display' if one is present; None otherwise.
2676 """
2677 from autotest_lib.client.cros.graphics import graphics_utils
2678 from autotest_lib.client.common_lib import utils as common_utils
2679
2680 def __system_output(cmd):
2681 return self.run(cmd).stdout
2682
2683 def __read_file(remote_path):
2684 return self.run('cat %s' % remote_path).stdout
2685
2686 # Hijack the necessary client functions so that we can take advantage
2687 # of the client lib here.
2688 # FIXME: find a less hacky way than this
2689 original_system_output = utils.system_output
2690 original_read_file = common_utils.read_file
2691 utils.system_output = __system_output
2692 common_utils.read_file = __read_file
2693 try:
2694 return ('internal_display' if graphics_utils.has_internal_display()
2695 else None)
2696 finally:
2697 utils.system_output = original_system_output
2698 common_utils.read_file = original_read_file
2699
2700
Dan Shi85276d42014-04-08 22:11:45 -07002701 def is_boot_from_usb(self):
2702 """Check if DUT is boot from USB.
2703
2704 @return: True if DUT is boot from usb.
2705 """
2706 device = self.run('rootdev -s -d').stdout.strip()
2707 removable = int(self.run('cat /sys/block/%s/removable' %
2708 os.path.basename(device)).stdout.strip())
2709 return removable == 1
Helen Zhang17dae2b2014-11-11 09:25:52 -08002710
Otabek Kasimov77a40332020-10-20 15:40:03 -07002711 def is_boot_from_external_device(self):
2712 """Check if DUT is boot from external storage.
2713
2714 @return: True if DUT is boot from external storage.
2715 """
2716 boot_device = self.run('rootdev -s -d', ignore_status=True,
2717 timeout=60).stdout.strip()
2718 if not boot_device:
2719 logging.debug('Boot storage not detected on the host.')
2720 return False
2721 main_storage_cmd = ('. /usr/sbin/write_gpt.sh;'
2722 ' . /usr/share/misc/chromeos-common.sh;'
2723 ' load_base_vars; get_fixed_dst_drive')
2724 main_storage = self.run(main_storage_cmd,
2725 ignore_status=True,
2726 timeout=60).stdout.strip()
Otabek Kasimov723e8562020-12-08 13:29:34 -08002727 if not main_storage or boot_device != main_storage:
2728 logging.debug('Device booted from external storage storage.')
2729 return True
2730 logging.debug('Device booted from main storage.')
2731 return False
Helen Zhang17dae2b2014-11-11 09:25:52 -08002732
2733 def read_from_meminfo(self, key):
Dan Shi49ca0932014-11-14 11:22:27 -08002734 """Return the memory info from /proc/meminfo
Helen Zhang17dae2b2014-11-11 09:25:52 -08002735
2736 @param key: meminfo requested
2737
2738 @return the memory value as a string
2739
2740 """
Helen Zhang17dae2b2014-11-11 09:25:52 -08002741 meminfo = self.run('grep %s /proc/meminfo' % key).stdout.strip()
2742 logging.debug('%s', meminfo)
2743 return int(re.search(r'\d+', meminfo).group(0))
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002744
2745
Rohit Makasana98e696f2016-06-03 18:48:10 -07002746 def get_cpu_arch(self):
2747 """Returns CPU arch of the device.
2748
2749 @return CPU architecture of the DUT.
2750 """
Allen Li2c32d6b2017-02-03 15:28:10 -08002751 # Add CPUs by following logic in client/bin/utils.py.
Rohit Makasana98e696f2016-06-03 18:48:10 -07002752 if self.run("grep '^flags.*:.* lm .*' /proc/cpuinfo",
2753 ignore_status=True).stdout:
2754 return 'x86_64'
2755 if self.run("grep -Ei 'ARM|CPU implementer' /proc/cpuinfo",
2756 ignore_status=True).stdout:
2757 return 'arm'
2758 return 'i386'
2759
2760
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002761 def get_board_type(self):
2762 """
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002763 Get the DUT's device type / form factor from cros_config. It can be one
2764 of CHROMEBOX, CHROMEBASE, CHROMEBOOK, or CHROMEBIT.
Danny Chan471a8d12015-08-18 14:57:41 -07002765
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002766 @return form factor value from cros_config.
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002767 """
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002768
2769 device_type = self.run('cros_config /hardware-properties form-factor',
2770 ignore_status=True).stdout
2771 if device_type:
2772 return device_type
2773
2774 # TODO: remove lsb-release fallback once cros_config works everywhere
Danny Chan471a8d12015-08-18 14:57:41 -07002775 device_type = self.run('grep DEVICETYPE /etc/lsb-release',
2776 ignore_status=True).stdout
2777 if device_type:
Kalin Stoyanov524310b2015-08-21 16:24:04 -07002778 return device_type.split('=')[-1].strip()
Danny Chan471a8d12015-08-18 14:57:41 -07002779 return ''
Gilad Arnolda76bef02015-09-29 13:55:15 -07002780
2781
Rohit Makasanadf0a3a32017-06-30 13:55:18 -07002782 def get_arc_version(self):
2783 """Return ARC version installed on the DUT.
2784
2785 @returns ARC version as string if the CrOS build has ARC, else None.
2786 """
2787 arc_version = self.run('grep CHROMEOS_ARC_VERSION /etc/lsb-release',
2788 ignore_status=True).stdout
2789 if arc_version:
2790 return arc_version.split('=')[-1].strip()
2791 return None
2792
2793
Gilad Arnolda76bef02015-09-29 13:55:15 -07002794 def get_os_type(self):
2795 return 'cros'
Simran Basia5522a32015-10-06 11:01:24 -07002796
2797
Kevin Chenga2619dc2016-03-28 11:42:08 -07002798 def get_labels(self):
Kevin Chengf8660142016-08-12 10:17:41 -07002799 """Return the detected labels on the host."""
Kevin Chenga2619dc2016-03-28 11:42:08 -07002800 return self.labels.get_labels(self)
Garry Wang5e5538a2019-04-08 15:36:18 -07002801
2802
2803 def get_default_power_method(self):
2804 """
2805 Get the default power method for power_on/off/cycle() methods.
2806 @return POWER_CONTROL_RPM or POWER_CONTROL_CCD
2807 """
2808 if not self._default_power_method:
Garry Wang1a004aa2019-05-16 22:56:51 -07002809 self._default_power_method = self.POWER_CONTROL_RPM
Ruben Rodriguez Buchillon3eeeab32019-10-02 15:29:58 -07002810 if self.servo and self.servo.supports_built_in_pd_control():
2811 self._default_power_method = self.POWER_CONTROL_CCD
2812 else:
2813 logging.debug('Either servo is unitialized or the servo '
2814 'setup does not support pd controls. Falling '
2815 'back to default RPM method.')
Garry Wang5e5538a2019-04-08 15:36:18 -07002816 return self._default_power_method
Puthikorn Voravootivat4a054792019-12-13 16:44:17 -08002817
2818
2819 def find_usb_devices(self, idVendor, idProduct):
2820 """
2821 Get usb device sysfs name for specific device.
2822
2823 @param idVendor Vendor ID to search in sysfs directory.
2824 @param idProduct Product ID to search in sysfs directory.
2825
2826 @return Usb node names in /sys/bus/usb/drivers/usb/ that match.
2827 """
2828 # Look for matching file and cut at position 7 to get dir name.
2829 grep_cmd = 'grep {} /sys/bus/usb/drivers/usb/*/{} | cut -f 7 -d /'
2830
2831 vendor_cmd = grep_cmd.format(idVendor, 'idVendor')
2832 product_cmd = grep_cmd.format(idProduct, 'idProduct')
2833
2834 # Use uniq -d to print duplicate line from both command
2835 cmd = 'sort <({}) <({}) | uniq -d'.format(vendor_cmd, product_cmd)
2836
2837 return self.run(cmd, ignore_status=True).stdout.strip().split('\n')
2838
2839
2840 def bind_usb_device(self, usb_node):
2841 """
2842 Bind usb device
2843
2844 @param usb_node Node name in /sys/bus/usb/drivers/usb/
2845 """
2846 cmd = 'echo {} > /sys/bus/usb/drivers/usb/bind'.format(usb_node)
2847 self.run(cmd, ignore_status=True)
2848
2849
2850 def unbind_usb_device(self, usb_node):
2851 """
2852 Unbind usb device
2853
2854 @param usb_node Node name in /sys/bus/usb/drivers/usb/
2855 """
2856 cmd = 'echo {} > /sys/bus/usb/drivers/usb/unbind'.format(usb_node)
2857 self.run(cmd, ignore_status=True)
2858
2859
2860 def get_wlan_ip(self):
2861 """
2862 Get ip address of wlan interface.
2863
2864 @return ip address of wlan or empty string if wlan is not connected.
2865 """
2866 cmds = [
2867 'iw dev', # List wlan physical device
2868 'grep Interface', # Grep only interface name
2869 'cut -f 2 -d" "', # Cut the name part
2870 'xargs ifconfig', # Feed it to ifconfig to get ip
2871 'grep -oE "inet [0-9.]+"', # Grep only ipv4
2872 'cut -f 2 -d " "' # Cut the ip part
2873 ]
2874 return self.run(' | '.join(cmds), ignore_status=True).stdout.strip()
Puthikorn Voravootivatcd0dc9e2020-01-22 14:22:22 -08002875
2876 def connect_to_wifi(self, ssid, passphrase=None, security=None):
2877 """
2878 Connect to wifi network
2879
2880 @param ssid SSID of the wifi network.
2881 @param passphrase Passphrase of the wifi network. None if not existed.
2882 @param security Security of the wifi network. Default to "psk" if
2883 passphase is given without security. Possible values
2884 are "none", "psk", "802_1x".
2885
2886 @return True if succeed, False if not.
2887 """
2888 cmd = '/usr/local/autotest/cros/scripts/wifi connect ' + ssid
2889 if passphrase:
2890 cmd += ' ' + passphrase
2891 if security:
2892 cmd += ' ' + security
2893 return self.run(cmd, ignore_status=True).exit_status == 0
Otabek Kasimov6825b762020-06-23 23:42:44 -07002894
2895 def get_device_repair_state(self):
2896 """Get device repair state"""
2897 return self._device_repair_state
2898
Otabek Kasimov44273d22021-02-26 17:13:24 -08002899 def is_marked_for_replacement(self):
2900 """Verify if device was marked for replacemnet during admin task."""
2901 expected_state = cros_constants.DEVICE_STATE_NEEDS_REPLACEMENT
2902 return self.get_device_repair_state() == expected_state
2903
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002904 def set_device_repair_state(self, state, resultdir=None):
Otabek Kasimov6825b762020-06-23 23:42:44 -07002905 """Set device repair state.
2906
2907 The special device state will be written to the 'dut_state.repair'
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002908 file in result directory. The file will be read by Lucifer. The
2909 file will not be created if result directory not specified.
2910
2911 @params state: The new state for the device.
2912 @params resultdir: The path to result directory. If path not provided
2913 will be attempt to get retrieve it from job
2914 if present.
Otabek Kasimov6825b762020-06-23 23:42:44 -07002915 """
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002916 resultdir = resultdir or getattr(self.job, 'resultdir', '')
2917 if resultdir:
2918 target = os.path.join(resultdir, 'dut_state.repair')
Otabek Kasimov6825b762020-06-23 23:42:44 -07002919 common_utils.open_write_close(target, state)
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002920 logging.info('Set device state as %s. '
2921 'Created dut_state.repair file.', state)
Otabek Kasimov6825b762020-06-23 23:42:44 -07002922 else:
2923 logging.debug('Cannot write the device state due missing info '
2924 'about result dir.')
2925 self._device_repair_state = state
2926
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002927 def set_device_needs_replacement(self, resultdir=None):
2928 """Set device as required replacement.
2929
2930 @params resultdir: The path to result directory. If path not provided
2931 will be attempt to get retrieve it from job
2932 if present.
2933 """
2934 self.set_device_repair_state(
2935 cros_constants.DEVICE_STATE_NEEDS_REPLACEMENT,
2936 resultdir=resultdir)
2937
Otabek Kasimovc7fa5072021-01-20 21:24:18 -08002938 def _dut_is_accessible_by_verifier(self):
2939 """Check if DUT accessible by SSH or PING verifier.
Otabek Kasimov86062d02020-11-17 13:30:22 -08002940
Otabek Kasimovc7fa5072021-01-20 21:24:18 -08002941 @returns: bool, True - verifier marked as success.
2942 False - result not reachable, verifier did not success.
Otabek Kasimov86062d02020-11-17 13:30:22 -08002943 """
2944 if not self._repair_strategy:
2945 return False
Otabek Kasimovc7fa5072021-01-20 21:24:18 -08002946 dut_ssh = self._repair_strategy.verifier_is_good('ssh')
2947 dut_ping = self._repair_strategy.verifier_is_good('ping')
2948 return dut_ssh == hosts.VERIFY_SUCCESS or dut_ssh == hosts.VERIFY_SUCCESS
Otabek Kasimov86062d02020-11-17 13:30:22 -08002949
Otabek Kasimovd48389b2020-12-07 02:38:34 -08002950 def _stat_if_pingable_but_not_sshable(self):
2951 """Check if DUT pingable but failed SSH verifier."""
2952 if not self._repair_strategy:
2953 return
Derek Beckett5f4edf02021-07-27 14:56:44 -07002954 self._repair_strategy.verifier_is_good('ssh')
2955 self._repair_strategy.verifier_is_good('ping')
Otabek Kasimovd48389b2020-12-07 02:38:34 -08002956
Otabek Kasimov7cad9ce2020-07-28 14:58:48 -07002957 def try_set_device_needs_manual_repair(self):
Otabek Kasimov6825b762020-06-23 23:42:44 -07002958 """Check if device require manual attention to be fixed.
2959
2960 The state 'needs_manual_repair' can be set when auto repair cannot
2961 fix the device due hardware or cable issues.
2962 """
2963 # ignore the logic if state present
2964 # state can be set by any cros repair actions
Otabek Kasimov86062d02020-11-17 13:30:22 -08002965 if self.get_device_repair_state():
Otabek Kasimov6825b762020-06-23 23:42:44 -07002966 return
Otabek Kasimovc7fa5072021-01-20 21:24:18 -08002967 if self._dut_is_accessible_by_verifier():
2968 # DUT is accessible and we still have many options to repair it.
Otabek Kasimovc9812582020-10-08 18:52:52 -07002969 return
Otabek Kasimov9189ede2020-11-09 14:08:58 -08002970 needs_manual_repair = False
2971 dhp = self.health_profile
Otabek Kasimov69253822020-11-24 10:52:27 -08002972 if dhp and dhp.get_repair_fail_count() > 49:
2973 # 42 = 6 times during 7 days. (every 4 hour repair)
2974 # round up to 50 in case somebody will run some attempt on it.
Otabek Kasimovc9812582020-10-08 18:52:52 -07002975 logging.info(
Otabek Kasimov9189ede2020-11-09 14:08:58 -08002976 'DUT is not sshable and fail %s times.'
Otabek Kasimov69253822020-11-24 10:52:27 -08002977 ' Limit to try repair is 50 times',
Otabek Kasimov9189ede2020-11-09 14:08:58 -08002978 dhp.get_repair_fail_count())
2979 needs_manual_repair = True
2980
2981 if not needs_manual_repair:
2982 # We cannot ssh to the DUT and we have hardware or set-up issues
2983 # with servo then we need request manual repair for the DUT.
2984 servo_state_required_manual_fix = [
2985 servo_constants.SERVO_STATE_DUT_NOT_CONNECTED,
2986 servo_constants.SERVO_STATE_NEED_REPLACEMENT,
2987 ]
2988 if self.get_servo_state() in servo_state_required_manual_fix:
2989 logging.info(
2990 'DUT required manual repair because it is not sshable'
2991 ' and possible have setup issue with Servo. Please'
2992 ' verify all connections and present of devices.')
2993 needs_manual_repair = True
2994
2995 if needs_manual_repair:
Otabek Kasimovc9812582020-10-08 18:52:52 -07002996 self.set_device_repair_state(
2997 cros_constants.DEVICE_STATE_NEEDS_MANUAL_REPAIR)
Otabek Kasimov42506d02020-07-29 14:44:57 -07002998
Otabek Kasimov86062d02020-11-17 13:30:22 -08002999 def _reboot_labstation_if_needed(self):
3000 """Place request to reboot the labstation if DUT is not sshable.
3001
3002 @returns: None
3003 """
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08003004 message_prefix = "Don't need to request servo-host reboot"
Otabek Kasimovc7fa5072021-01-20 21:24:18 -08003005 if self._dut_is_accessible_by_verifier():
Otabek Kasimov86062d02020-11-17 13:30:22 -08003006 return
3007 if not self._servo_host:
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08003008 logging.debug('%s as it not initialized', message_prefix)
Otabek Kasimov86062d02020-11-17 13:30:22 -08003009 return
3010 if not self._servo_host.is_up_fast():
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08003011 logging.debug('%s as servo-host is not sshable', message_prefix)
Otabek Kasimov86062d02020-11-17 13:30:22 -08003012 return
3013 if not self._servo_host.is_labstation():
3014 logging.debug('Servo_v3 is not requested to reboot for the DUT')
3015 return
3016 usb_path = self._servo_host.get_main_servo_usb_path()
3017 if usb_path:
3018 connected_port = os.path.basename(os.path.normpath(usb_path))
3019 # Directly connected servo to the labstation looks like '1-5.3'
3020 # and when connected by hub - '1-5.2.3' or '1-5.2.1.3'. Where:
3021 # - '1-5' - port on labstation
3022 # - '2' or '2.1' - port on the hub or smart-hub
3023 # - '3' - port on servo hub
3024 if len(connected_port.split('.')) > 2:
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08003025 logging.debug('%s as servo connected by hub', message_prefix)
Otabek Kasimov86062d02020-11-17 13:30:22 -08003026 return
3027 self._servo_host.request_reboot()
3028 logging.info('Requested labstation reboot because DUT is not sshable')
3029
Otabek Kasimov42506d02020-07-29 14:44:57 -07003030 def is_file_system_writable(self, testdirs=None):
3031 """Check is the file systems are writable.
3032
3033 The standard linux response to certain unexpected file system errors
3034 (including hardware errors in block devices) is to change the file
3035 system status to read-only. This checks that that hasn't happened.
3036
3037 @param testdirs: List of directories to check. If no data provided
3038 then '/mnt/stateful_partition' and '/var/tmp'
3039 directories will be checked.
3040
3041 @returns boolean whether file-system writable.
3042 """
3043 def _check_dir(testdir):
3044 # check if we can create a file
3045 filename = os.path.join(testdir, 'writable_my_test_file')
3046 command = 'touch %s && rm %s' % (filename, filename)
3047 rv = self.run(command=command,
3048 timeout=30,
3049 ignore_status=True)
3050 is_writable = rv.exit_status == 0
3051 if not is_writable:
3052 logging.info('Cannot create a file in "%s"!'
3053 ' Probably the FS is read-only', testdir)
3054 logging.info("FileSystem is not writable!")
3055 return False
3056 return True
3057
3058 if not testdirs or len(testdirs) == 0:
3059 # N.B. Order matters here: Encrypted stateful is loop-mounted
3060 # from a file in unencrypted stateful, so we don't test for
3061 # errors in encrypted stateful if unencrypted fails.
3062 testdirs = ['/mnt/stateful_partition', '/var/tmp']
3063
3064 for dir in testdirs:
3065 # loop will be stopped if any directory fill fail the check
3066 try:
3067 if not _check_dir(dir):
3068 return False
3069 except Exception as e:
3070 # here expected only timeout error, all other will
3071 # be catch by 'ignore_status=True'
3072 logging.debug('Fail to check %s to write in it', dir)
3073 return False
3074 return True
Garry Wang1a493d82020-08-31 21:01:19 -07003075
Dana Goyettec172b172020-07-29 16:26:15 -07003076 def blocking_sync(self, freeze_for_reset=False):
3077 """Sync root device and internal device, via script.
3078
3079 The actual calls end up logged by the run() call, since they're printed
3080 to stdout/stderr in the script.
3081
3082 @param freeze_for_reset: if True, prepare for reset by blocking writes
3083 (only if enable_fs_sync_fsfreeze=True)
3084 """
3085
3086 if freeze_for_reset and self.USE_FSFREEZE:
3087 logging.info('Blocking sync and freeze')
3088 elif freeze_for_reset:
3089 logging.info('Blocking sync for reset')
3090 else:
3091 logging.info('Blocking sync')
3092
3093 # client/bin is installed on the DUT as /usr/local/autotest/bin
3094 sync_cmd = '/usr/local/autotest/bin/fs_sync.py'
3095 if freeze_for_reset and self.USE_FSFREEZE:
3096 sync_cmd += ' --freeze'
3097 return self.run(sync_cmd)
3098
Garry Wanga2e78172020-09-09 23:49:07 -07003099 def set_health_profile_dut_state(self, state):
3100 if not self.health_profile:
3101 logging.debug('Device health profile is not initialized, skip'
3102 ' set dut state.')
3103 return
3104 reset_counters = state in profile_constants.STATES_NEED_RESET_COUNTER
3105 self.health_profile.update_dut_state(state, reset_counters)
Garry Wang53fc8f32020-09-18 13:30:08 -07003106
3107 def require_snk_mode_in_recovery(self):
3108 """Check whether we need to switch servo_v4 role to snk when
3109 booting into recovery mode. (See crbug.com/1129165)
3110 """
Garry Wanga8739cc2020-10-30 00:49:23 -07003111 has_battery = True
3112 # Determine if the host has battery based on host_info first.
3113 power_info = self.host_info_store.get().get_label_value('power')
3114 if power_info:
3115 has_battery = power_info == 'battery'
3116 elif self.is_up_fast():
3117 # when running local tests host_info is not available, so we
3118 # need to determine whether the host has battery by checking
3119 # from host side.
3120 logging.debug('Label `power` is not found in host_info, checking'
3121 ' if the host has battery from host side.')
3122 has_battery = self.has_battery()
3123
3124 if not has_battery:
Garry Wang53fc8f32020-09-18 13:30:08 -07003125 logging.info(
3126 '%s does not has battery, snk mode is not needed'
3127 ' for recovery.', self.hostname)
3128 return False
Garry Wanga8739cc2020-10-30 00:49:23 -07003129
Garry Wang53fc8f32020-09-18 13:30:08 -07003130 if not self.servo.supports_built_in_pd_control():
3131 logging.info('Power delivery is not supported on this servo, snk'
3132 ' mode is not needed for recovery.')
3133 return False
3134 try:
Garry Wang53fc8f32020-09-18 13:30:08 -07003135 battery_percent = self.servo.get('battery_charge_percent')
Otabek Kasimov58e22562020-11-03 17:17:41 -08003136 if battery_percent < cros_constants.MIN_BATTERY_LEVEL:
Garry Wang53fc8f32020-09-18 13:30:08 -07003137 logging.info(
3138 'Current battery level %s%% below %s%% threshold, we'
3139 ' will attempt to boot host in recovery mode without'
3140 ' changing servo to snk mode. Please note the host may'
3141 ' not able to see usb drive in recovery mode later due'
3142 ' to servo not in snk mode.', battery_percent,
Otabek Kasimov58e22562020-11-03 17:17:41 -08003143 cros_constants.MIN_BATTERY_LEVEL)
Garry Wang53fc8f32020-09-18 13:30:08 -07003144 return False
3145 except Exception as e:
3146 logging.info(
3147 'Unexpected error occurred when getting'
3148 ' battery_charge_percent from servo; %s', str(e))
3149 return False
3150 return True
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07003151
3152 def _set_servo_topology(self):
3153 """Set servo-topology info to the host-info."""
3154 logging.debug('Try to save servo topology to host-info.')
3155 if not self._servo_host:
Greg Edelstonff2665d2021-04-21 14:32:27 -06003156 logging.debug('Servo host is not initialized.')
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08003157 return
3158 if not self.is_servo_in_working_state():
3159 logging.debug('Is servo is not in working state then'
3160 ' update topology is not allowed.')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07003161 return
3162 if not self._servo_host.is_servo_topology_supported():
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08003163 logging.debug('Servo-topology is not supported.')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07003164 return
3165 servo_topology = self._servo_host.get_topology()
3166 if not servo_topology or servo_topology.is_empty():
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08003167 logging.debug('Servo topology is empty')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07003168 return
3169 servo_topology.save(self.host_info_store)