blob: d2aae7f94f47a50f7089b5ac40900807e715ceeb [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
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070014import time
15
mussa584b4462014-06-20 15:13:28 -070016import common
J. Richard Barnette45e93de2012-04-11 17:24:15 -070017from autotest_lib.client.bin import utils
Dan Shi9cb0eec2014-06-03 09:04:50 -070018from autotest_lib.client.common_lib import autotemp
Richard Barnette0c73ffc2012-11-19 15:21:18 -080019from autotest_lib.client.common_lib import error
20from autotest_lib.client.common_lib import global_config
Dan Shi549fb822015-03-24 18:01:11 -070021from autotest_lib.client.common_lib import lsbrelease_utils
Greg Edelstona7b05d12020-04-01 16:00:51 -060022from autotest_lib.client.common_lib.cros import cros_config
Richard Barnette03a0c132012-11-05 12:40:35 -080023from autotest_lib.client.common_lib.cros import dev_server
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -070024from autotest_lib.client.common_lib.cros import retry
Hsinyu Chaoe0b08e62015-08-11 10:50:37 +000025from autotest_lib.client.cros import constants as client_constants
J. Richard Barnette84890bd2014-02-21 11:05:47 -080026from autotest_lib.client.cros import cros_ui
Dan Shia1ecd5c2013-06-06 11:21:31 -070027from autotest_lib.server import utils as server_utils
Derek Beckett3d743402021-08-04 09:25:44 -070028from autotest_lib.server import tauto_warnings
Derek Beckett08ca8572021-08-25 14:46:35 -070029from autotest_lib.server.consts import consts
Scott Zawalski89c44dd2013-02-26 09:28:02 -050030from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
Derek Beckettcc0c8302021-07-14 09:53:48 -070031from autotest_lib.server.cros.dynamic_suite import tools
Garry Wang1a493d82020-08-31 21:01:19 -070032from autotest_lib.server.cros.device_health_profile import device_health_profile
Garry Wanga2e78172020-09-09 23:49:07 -070033from autotest_lib.server.cros.device_health_profile import profile_constants
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -070034from autotest_lib.server.cros.servo import pdtester
Fang Deng96667ca2013-08-01 17:46:18 -070035from autotest_lib.server.hosts import abstract_ssh
Kevin Chenga2619dc2016-03-28 11:42:08 -070036from autotest_lib.server.hosts import base_label
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +080037from autotest_lib.server.hosts import chameleon_host
Otabek Kasimov832d9162020-07-27 19:24:57 -070038from autotest_lib.server.hosts import cros_constants
Richard Barnetted31580e2018-05-14 19:58:00 +000039from autotest_lib.server.hosts import cros_label
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -070040from autotest_lib.server.hosts import pdtester_host
Fang Deng5d518f42013-08-02 14:04:32 -070041from autotest_lib.server.hosts import servo_host
Garry Wang11b5e872020-03-11 15:14:08 -070042from autotest_lib.server.hosts import servo_constants
Otabek Kasimov808cd832020-05-28 18:27:46 -070043from autotest_lib.site_utils.admin_audit import constants as audit_const
Derek Beckettf73baca2020-08-19 15:08:47 -070044from six.moves import zip
Simran Basid5e5e272012-09-24 15:23:59 -070045
Andrew Luo4be621d2020-03-21 07:01:13 -070046
Dan Shib8540a52015-07-16 14:18:23 -070047CONFIG = global_config.global_config
48
beepsc87ff602013-07-31 21:53:00 -070049class FactoryImageCheckerException(error.AutoservError):
50 """Exception raised when an image is a factory image."""
51 pass
52
53
Fang Deng0ca40e22013-08-27 17:47:44 -070054class CrosHost(abstract_ssh.AbstractSSHHost):
J. Richard Barnette45e93de2012-04-11 17:24:15 -070055 """Chromium OS specific subclass of Host."""
56
Derek Beckett08ca8572021-08-25 14:46:35 -070057 VERSION_PREFIX = consts.CROS_VERSION_PREFIX
Simran Basi5ace6f22016-01-06 17:30:44 -080058
J. Richard Barnette45e93de2012-04-11 17:24:15 -070059
Richard Barnette03a0c132012-11-05 12:40:35 -080060 # Timeout values (in seconds) associated with various Chrome OS
61 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070062 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -080063 # In general, a good rule of thumb is that the timeout can be up
64 # to twice the typical measured value on the slowest platform.
65 # The times here have not necessarily been empirically tested to
66 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070067 #
68 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -080069 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
70 # time to restart the netwowrk.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080071 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070072 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -080073 # other things, this must account for the 30 second dev-mode
J. Richard Barnette417cc792015-10-01 09:56:36 -070074 # screen delay, time to start the network on the DUT, and the
75 # ssh timeout of 120 seconds.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070076 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -080077 # including the 30 second dev-mode delay and time to start the
J. Richard Barnetted4649c62013-03-06 17:42:27 -080078 # network.
beepsf079cfb2013-09-18 17:49:51 -070079 # INSTALL_TIMEOUT: Time to allow for chromeos-install.
Otabek Kasimovaeb47fe2021-01-26 20:53:55 -080080 # ADMIN_INSTALL_TIMEOUT: Time to allow for chromeos-install
81 # used by admin tasks.
J. Richard Barnette84890bd2014-02-21 11:05:47 -080082 # POWERWASH_BOOT_TIMEOUT: Time to allow for a reboot that
83 # includes powerwash.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -070084
85 SLEEP_TIMEOUT = 2
J. Richard Barnetted4649c62013-03-06 17:42:27 -080086 RESUME_TIMEOUT = 10
Tom Wai-Hong Tamfe005c22014-12-03 09:25:44 +080087 SHUTDOWN_TIMEOUT = 10
J. Richard Barnette417cc792015-10-01 09:56:36 -070088 BOOT_TIMEOUT = 150
J. Richard Barnette5bab5f52015-08-03 13:14:38 -070089 USB_BOOT_TIMEOUT = 300
J. Richard Barnette7817b052014-08-28 09:47:29 -070090 INSTALL_TIMEOUT = 480
Otabek Kasimovaeb47fe2021-01-26 20:53:55 -080091 ADMIN_INSTALL_TIMEOUT = 600
Dan Shi2c88eed2013-11-12 10:18:38 -080092 POWERWASH_BOOT_TIMEOUT = 60
Chris Sosab76e0ee2013-05-22 16:55:41 -070093
Dan Shica503482015-03-30 17:23:25 -070094 # Minimum OS version that supports server side packaging. Older builds may
95 # not have server side package built or with Autotest code change to support
96 # server-side packaging.
Dan Shib8540a52015-07-16 14:18:23 -070097 MIN_VERSION_SUPPORT_SSP = CONFIG.get_config_value(
Dan Shiced09e42015-04-17 16:09:34 -070098 'AUTOSERV', 'min_version_support_ssp', type=int)
Dan Shica503482015-03-30 17:23:25 -070099
Dana Goyettec172b172020-07-29 16:26:15 -0700100 USE_FSFREEZE = CONFIG.get_config_value(
Dana Goyette6242cb32020-09-23 11:02:57 -0700101 'CROS', 'enable_fs_freeze', type=bool, default=False)
Dana Goyettec172b172020-07-29 16:26:15 -0700102
J. Richard Barnette84890bd2014-02-21 11:05:47 -0800103 # REBOOT_TIMEOUT: How long to wait for a reboot.
104 #
Chris Sosab76e0ee2013-05-22 16:55:41 -0700105 # We have a long timeout to ensure we don't flakily fail due to other
106 # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate.
Simran Basi1160e2c2013-10-04 16:00:24 -0700107 # TODO(sbasi - crbug.com/276094) Restore to 5 mins once the 'host did not
108 # return from reboot' bug is solved.
109 REBOOT_TIMEOUT = 480
Chris Sosab76e0ee2013-05-22 16:55:41 -0700110
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800111 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
112 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
Garry Wang5e5538a2019-04-08 15:36:18 -0700113 # _CHANGE_SERVO_ROLE_TIMEOUT: Time to allow DUT regain network connection
114 # since changing servo role will reset USB state
115 # and causes temporary ethernet drop.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800116 _USB_POWER_TIMEOUT = 5
117 _POWER_CYCLE_TIMEOUT = 10
Garry Wang5e5538a2019-04-08 15:36:18 -0700118 _CHANGE_SERVO_ROLE_TIMEOUT = 180
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800119
Fang Dengdeba14f2014-11-14 11:54:09 -0800120 _RPM_HOSTNAME_REGEX = ('chromeos(\d+)(-row(\d+))?-rack(\d+[a-z]*)'
121 '-host(\d+)')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700122
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800123 # Constants used in ping_wait_up() and ping_wait_down().
124 #
125 # _PING_WAIT_COUNT is the approximate number of polling
126 # cycles to use when waiting for a host state change.
127 #
128 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
129 # for arguments to the internal _ping_wait_for_status()
130 # method.
131 _PING_WAIT_COUNT = 40
132 _PING_STATUS_DOWN = False
133 _PING_STATUS_UP = True
134
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800135 # Allowed values for the power_method argument.
136
Garry Wang5e5538a2019-04-08 15:36:18 -0700137 # POWER_CONTROL_RPM: Used in power_off/on/cycle() methods, default for all
138 # DUTs except those with servo_v4 CCD.
139 # POWER_CONTROL_CCD: Used in power_off/on/cycle() methods, default for all
140 # DUTs with servo_v4 CCD.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800141 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
142 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
143 POWER_CONTROL_RPM = 'RPM'
Garry Wang5e5538a2019-04-08 15:36:18 -0700144 POWER_CONTROL_CCD = 'CCD'
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800145 POWER_CONTROL_SERVO = 'servoj10'
146 POWER_CONTROL_MANUAL = 'manual'
147
148 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
Garry Wang5e5538a2019-04-08 15:36:18 -0700149 POWER_CONTROL_CCD,
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800150 POWER_CONTROL_SERVO,
151 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800152
Simran Basi5e6339a2013-03-21 11:34:32 -0700153 _RPM_OUTLET_CHANGED = 'outlet_changed'
154
Dan Shi9cb0eec2014-06-03 09:04:50 -0700155 # URL pattern to download firmware image.
Dan Shib8540a52015-07-16 14:18:23 -0700156 _FW_IMAGE_URL_PATTERN = CONFIG.get_config_value(
Dan Shi9cb0eec2014-06-03 09:04:50 -0700157 'CROS', 'firmware_url_pattern', type=str)
beeps687243d2013-07-18 15:29:27 -0700158
Brent Peterson1cb623a2020-01-09 13:14:28 -0800159 # Regular expression for extracting EC version string
160 _EC_REGEX = '(%s_\w*[-\.]\w*[-\.]\w*[-\.]\w*)'
161
162 # Regular expression for extracting BIOS version string
163 _BIOS_REGEX = '(%s\.\w*\.\w*\.\w*)'
164
Brent Petersonc70a1832020-01-24 15:54:35 -0800165 # Command to update firmware located on DUT
Namyoon Woo382e5892020-05-20 16:48:40 -0700166 _FW_UPDATE_CMD = 'chromeos-firmwareupdate --mode=recovery %s'
Brent Petersonc70a1832020-01-24 15:54:35 -0800167
J. Richard Barnette964fba02012-10-24 17:34:29 -0700168 @staticmethod
beeps46dadc92013-11-07 14:07:10 -0800169 def check_host(host, timeout=10):
170 """
171 Check if the given host is a chrome-os host.
172
173 @param host: An ssh host representing a device.
174 @param timeout: The timeout for the run command.
175
176 @return: True if the host device is chromeos.
177
beeps46dadc92013-11-07 14:07:10 -0800178 """
179 try:
Allen Liad719c12017-06-27 23:48:04 +0000180 result = host.run(
Simran Basi933c8af2015-04-29 14:05:07 -0700181 'grep -q CHROMEOS /etc/lsb-release && '
Garry Wange4b6d6e2019-06-17 17:08:46 -0700182 '! grep -q moblab /etc/lsb-release && '
Derek Beckett342e3e62021-01-05 17:17:23 -0800183 '! grep -q labstation /etc/lsb-release &&'
184 ' grep CHROMEOS_RELEASE_BOARD /etc/lsb-release',
185 ignore_status=True,
Laurence Goodby468de252017-06-08 17:22:53 -0700186 timeout=timeout).stdout
Derek Beckett342e3e62021-01-05 17:17:23 -0800187 if result:
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800188 return not (
189 lsbrelease_utils.is_jetstream(
Derek Beckett342e3e62021-01-05 17:17:23 -0800190 lsb_release_content=result) or
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800191 lsbrelease_utils.is_gce_board(
Derek Beckett342e3e62021-01-05 17:17:23 -0800192 lsb_release_content=result))
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -0800193
beeps46dadc92013-11-07 14:07:10 -0800194 except (error.AutoservRunError, error.AutoservSSHTimeout):
195 return False
Laurence Goodby468de252017-06-08 17:22:53 -0700196
197 return False
beeps46dadc92013-11-07 14:07:10 -0800198
199
200 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800201 def get_chameleon_arguments(args_dict):
202 """Extract chameleon options from `args_dict` and return the result.
203
204 Recommended usage:
205 ~~~~~~~~
206 args_dict = utils.args_to_dict(args)
207 chameleon_args = hosts.CrosHost.get_chameleon_arguments(args_dict)
208 host = hosts.create_host(machine, chameleon_args=chameleon_args)
209 ~~~~~~~~
210
211 @param args_dict Dictionary from which to extract the chameleon
212 arguments.
213 """
Sam McNally66594ca2019-12-09 12:45:44 +1100214 chameleon_args = {key: args_dict[key]
215 for key in ('chameleon_host', 'chameleon_port')
216 if key in args_dict}
217 if 'chameleon_ssh_port' in args_dict:
218 chameleon_args['port'] = int(args_dict['chameleon_ssh_port'])
219 return chameleon_args
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800220
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800221 @staticmethod
222 def get_btpeer_arguments(args_dict):
223 """Extract btpeer options from `args_dict` and return the result.
224
225 This is used to parse details of Bluetooth peer.
226 Recommended usage:
227 ~~~~~~~~
228 args_dict = utils.args_to_dict(args)
229 btpeer_args = hosts.CrosHost.get_btpeer_arguments(args_dict)
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700230 host = hosts.create_host(machine, btpeer_args=btpeer_args)
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800231 ~~~~~~~~
232
233 @param args_dict: Dictionary from which to extract the btpeer
234 arguments.
235 """
236 if 'btpeer_host_list' in args_dict:
237 result = []
238 for btpeer in args_dict['btpeer_host_list'].split(','):
Claire Changd0b19842020-11-04 22:28:45 +0800239 # IPv6 addresses including a port number should be enclosed in
240 # square brackets.
241 delimiter = ']:' if re.search(r':.*:', btpeer) else ':'
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800242 result.append({key: value for key,value in
243 zip(('btpeer_host','btpeer_port'),
Claire Changd0b19842020-11-04 22:28:45 +0800244 btpeer.strip('[]').split(delimiter))})
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800245 return result
246 else:
Anand K Mistrye8933092020-08-05 14:49:41 +1000247 return {key: args_dict[key]
248 for key in ('btpeer_host', 'btpeer_port', 'btpeer_ssh_port')
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800249 if key in args_dict}
250
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800251
252 @staticmethod
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700253 def get_pdtester_arguments(args_dict):
Scottfe06ed82015-11-05 17:15:01 -0800254 """Extract chameleon options from `args_dict` and return the result.
255
256 Recommended usage:
257 ~~~~~~~~
258 args_dict = utils.args_to_dict(args)
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700259 pdtester_args = hosts.CrosHost.get_pdtester_arguments(args_dict)
260 host = hosts.create_host(machine, pdtester_args=pdtester_args)
Scottfe06ed82015-11-05 17:15:01 -0800261 ~~~~~~~~
262
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700263 @param args_dict Dictionary from which to extract the pdtester
Scottfe06ed82015-11-05 17:15:01 -0800264 arguments.
265 """
Allen Li083866b2016-08-18 10:07:10 -0700266 return {key: args_dict[key]
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700267 for key in ('pdtester_host', 'pdtester_port')
Allen Li083866b2016-08-18 10:07:10 -0700268 if key in args_dict}
Scottfe06ed82015-11-05 17:15:01 -0800269
270
271 @staticmethod
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800272 def get_servo_arguments(args_dict):
273 """Extract servo options from `args_dict` and return the result.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800274
275 Recommended usage:
276 ~~~~~~~~
277 args_dict = utils.args_to_dict(args)
Fang Deng0ca40e22013-08-27 17:47:44 -0700278 servo_args = hosts.CrosHost.get_servo_arguments(args_dict)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800279 host = hosts.create_host(machine, servo_args=servo_args)
280 ~~~~~~~~
281
282 @param args_dict Dictionary from which to extract the servo
283 arguments.
284 """
Garry Wang11b5e872020-03-11 15:14:08 -0700285 servo_attrs = (servo_constants.SERVO_HOST_ATTR,
Andrew Luo4be621d2020-03-21 07:01:13 -0700286 servo_constants.SERVO_HOST_SSH_PORT_ATTR,
Garry Wang11b5e872020-03-11 15:14:08 -0700287 servo_constants.SERVO_PORT_ATTR,
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700288 servo_constants.SERVO_SERIAL_ATTR,
Garry Wang11b5e872020-03-11 15:14:08 -0700289 servo_constants.SERVO_BOARD_ATTR,
290 servo_constants.SERVO_MODEL_ATTR)
Armando Miraglia2ac802e2017-08-15 14:54:47 +0200291 servo_args = {key: args_dict[key]
292 for key in servo_attrs
293 if key in args_dict}
294 return (
295 None
Garry Wang11b5e872020-03-11 15:14:08 -0700296 if servo_constants.SERVO_HOST_ATTR in servo_args
297 and not servo_args[servo_constants.SERVO_HOST_ATTR]
Armando Miraglia2ac802e2017-08-15 14:54:47 +0200298 else servo_args)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700299
J. Richard Barnette964fba02012-10-24 17:34:29 -0700300
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800301 def _initialize(self,
302 hostname,
303 chameleon_args=None,
304 servo_args=None,
305 pdtester_args=None,
306 try_lab_servo=False,
307 try_servo_repair=False,
308 ssh_verbosity_flag='',
309 ssh_options='',
310 try_servo_recovery=False,
311 *args,
312 **dargs):
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800313 """Initialize superclasses, |self.chameleon|, and |self.servo|.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700314
Fang Denge545abb2014-12-30 18:43:47 -0800315 This method will attempt to create the test-assistant object
316 (chameleon/servo) when it is needed by the test. Check
317 the docstring of chameleon_host.create_chameleon_host and
318 servo_host.create_servo_host for how this is determined.
Fang Deng5d518f42013-08-02 14:04:32 -0700319
Fang Denge545abb2014-12-30 18:43:47 -0800320 @param hostname: Hostname of the dut.
321 @param chameleon_args: A dictionary that contains args for creating
322 a ChameleonHost. See chameleon_host for details.
323 @param servo_args: A dictionary that contains args for creating
324 a ServoHost object. See servo_host for details.
Richard Barnette9a26ad62016-06-10 12:03:08 -0700325 @param try_lab_servo: When true, indicates that an attempt should
326 be made to create a ServoHost for a DUT in
327 the test lab, even if not required by
328 `servo_args`. See servo_host for details.
329 @param try_servo_repair: If a servo host is created, check it
330 with `repair()` rather than `verify()`.
Fang Denge545abb2014-12-30 18:43:47 -0800331 See servo_host for details.
332 @param ssh_verbosity_flag: String, to pass to the ssh command to control
333 verbosity.
334 @param ssh_options: String, other ssh options to pass to the ssh
335 command.
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800336 @param try_servo_recovery: When True, start servod in recovery mode.
337 See servo_host for details.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700338 """
Andrew Luo4be621d2020-03-21 07:01:13 -0700339 super(CrosHost, self)._initialize(hostname=hostname, *args, **dargs)
Otabek Kasimov6825b762020-06-23 23:42:44 -0700340 # hold special dut_state for repair process
341 self._device_repair_state = None
Kevin Chenga2619dc2016-03-28 11:42:08 -0700342 self.labels = base_label.LabelRetriever(cros_label.CROS_LABELS)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700343 # self.env is a dictionary of environment variable settings
344 # to be exported for commands run on the host.
345 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
346 # errors that might happen.
347 self.env['LIBC_FATAL_STDERR_'] = '1'
Fang Dengd1c2b732013-08-20 12:59:46 -0700348 self._ssh_verbosity_flag = ssh_verbosity_flag
Aviv Keshetc5947fa2013-09-04 14:06:29 -0700349 self._ssh_options = ssh_options
Garry Wang1a493d82020-08-31 21:01:19 -0700350 self.health_profile = None
Garry Wang5e5538a2019-04-08 15:36:18 -0700351 self._default_power_method = None
Otabek Kasimov39637412020-11-23 19:09:27 -0800352 dut_health_profile = device_health_profile.DeviceHealthProfile(
353 hostname=self.hostname,
354 host_info=self.host_info_store.get(),
355 result_dir=self.get_result_dir())
Otabek Kasimovb61729d2020-11-24 00:42:43 -0800356
357 # TODO(otabek@): remove when b/171414073 closed
Andrew Luo4be621d2020-03-21 07:01:13 -0700358 if self.use_icmp:
Derek Beckettc7677812021-02-12 14:41:11 -0800359 pingable_before_servo = self.is_up_fast(count=1)
Andrew Luo4be621d2020-03-21 07:01:13 -0700360 if pingable_before_servo:
361 logging.info('DUT is pingable before init Servo.')
362 else:
363 logging.info('Skipping ping to DUT before init Servo.')
Otabek Kasimov39637412020-11-23 19:09:27 -0800364 _servo_host, servo_state = servo_host.create_servo_host(
365 dut=self,
366 servo_args=servo_args,
367 try_lab_servo=try_lab_servo,
368 try_servo_repair=try_servo_repair,
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800369 try_servo_recovery=try_servo_recovery,
Otabek Kasimov39637412020-11-23 19:09:27 -0800370 dut_host_info=self.host_info_store.get(),
371 dut_health_profile=dut_health_profile)
372 if dut_health_profile.is_loaded():
373 logging.info('Device health profile loaded.')
374 # The device profile is located in the servo_host which make it
375 # dependency. If profile is not loaded yet then we do not have it
376 # TODO(otabek@) persist device provide out of servo-host.
377 self.health_profile = dut_health_profile
378 self.set_servo_host(_servo_host, servo_state)
Richard Barnettee519dcd2016-08-15 17:37:17 -0700379
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800380 # TODO(waihong): Do the simplication on Chameleon too.
Shijin Abraham783a7dd2020-02-14 15:36:11 -0800381 self._chameleon_host = chameleon_host.create_chameleon_host(
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700382 dut=self.hostname,
383 chameleon_args=chameleon_args)
Shijin Abraham783a7dd2020-02-14 15:36:11 -0800384 if self._chameleon_host:
385 self.chameleon = self._chameleon_host.create_chameleon_board()
Tom Wai-Hong Tamefe1c7f2014-01-02 14:00:11 +0800386 else:
Tom Wai-Hong Tam3d6790d2014-04-14 16:15:47 +0800387 self.chameleon = None
Fang Deng5d518f42013-08-02 14:04:32 -0700388
Shijin Abraham78ce4402020-09-08 22:04:27 -0700389 # Bluetooth peers will be populated by the test if needed
390 self._btpeer_host_list = []
391 self.btpeer_list = []
392 self.btpeer = None
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800393
howardchung83e55272019-08-08 14:08:05 +0800394 # Add pdtester host if pdtester args were added on command line
Wai-Hong Tam16e5edb2019-09-17 16:10:07 -0700395 self._pdtester_host = pdtester_host.create_pdtester_host(
Wai-Hong Tam90b164d2019-10-25 13:15:39 -0700396 pdtester_args, self._servo_host)
howardchung83e55272019-08-08 14:08:05 +0800397
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700398 if self._pdtester_host:
399 self.pdtester_servo = self._pdtester_host.get_servo()
400 logging.info('pdtester_servo: %r', self.pdtester_servo)
401 # Create the pdtester object used to access the ec uart
402 self.pdtester = pdtester.PDTester(self.pdtester_servo,
403 self._pdtester_host.get_servod_server_proxy())
Scottfe06ed82015-11-05 17:15:01 -0800404 else:
Wai-Hong Tam0ed9fe12019-05-23 11:00:58 -0700405 self.pdtester = None
Scottfe06ed82015-11-05 17:15:01 -0800406
Fang Deng5d518f42013-08-02 14:04:32 -0700407
Shijin Abraham78ce4402020-09-08 22:04:27 -0700408 def initialize_btpeer(self, btpeer_args=[]):
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800409 """ Initialize the Bluetooth peers
410
411 Initialize Bluetooth peer devices given in the arguments. Bluetooth peer
412 is chameleon host on Raspberry Pi.
413 @param btpeer_args: A dictionary that contains args for creating
414 a ChameleonHost. See chameleon_host for details.
415
416 """
Shijin Abraham78ce4402020-09-08 22:04:27 -0700417 logging.debug('Attempting to initialize bluetooth peers if available')
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700418 try:
Shijin Abraham22f24cb2020-03-26 09:07:41 -0700419 if type(btpeer_args) is list:
420 btpeer_args_list = btpeer_args
421 else:
422 btpeer_args_list = [btpeer_args]
423
424 self._btpeer_host_list = chameleon_host.create_btpeer_host(
425 dut=self.hostname, btpeer_args_list=btpeer_args_list)
426 logging.debug('Bluetooth peer hosts are %s',
427 self._btpeer_host_list)
428 self.btpeer_list = [_host.create_chameleon_board() for _host in
429 self._btpeer_host_list if _host is not None]
430
431 if len(self.btpeer_list) > 0:
432 self.btpeer = self.btpeer_list[0]
433
434 logging.debug('After initialize_btpeer btpeer_list %s '
435 'btpeer_host_list is %s and btpeer is %s',
436 self.btpeer_list, self._btpeer_host_list,
437 self.btpeer)
438 except Exception as e:
439 logging.error('Exception %s in initialize_btpeer', str(e))
440
Shijin Abrahamc09587d2020-02-14 20:46:55 -0800441
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700442 def host_version_prefix(self, image):
443 """Return version label prefix.
444
445 In case the CrOS provisioning version is something other than the
446 standard CrOS version e.g. CrOS TH version, this function will
Derek Beckett08ca8572021-08-25 14:46:35 -0700447 find the prefix from consts.py.
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700448
449 @param image: The image name to find its version prefix.
450 @returns: A prefix string for the image type.
451 """
Derek Beckett08ca8572021-08-25 14:46:35 -0700452 return consts.get_version_label_prefix(image)
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700453
Andrew Luo3332ab22020-04-28 16:42:03 -0700454 def stage_build_to_usb(self, build):
455 """Stage the current ChromeOS image on the USB stick connected to the
456 servo.
457
458 @param build: The build to download and send to USB.
459 """
460 if not self.servo:
461 raise error.TestError('Host %s does not have servo.' %
462 self.hostname)
463
464 _, update_url = self.stage_image_for_servo(build)
Andrew Luob0355ea2020-06-24 16:12:57 -0700465
466 try:
467 self.servo.image_to_servo_usb(update_url)
468 finally:
469 # servo.image_to_servo_usb turned the DUT off, so turn it back on
470 logging.debug('Turn DUT power back on.')
471 self.servo.get_power_state_controller().power_on()
472
Andrew Luo3332ab22020-04-28 16:42:03 -0700473 logging.debug('ChromeOS image %s is staged on the USB stick.',
474 build)
Rohit Makasanadf0a3a32017-06-30 13:55:18 -0700475
beepsdae65fd2013-07-26 16:24:41 -0700476 def verify_job_repo_url(self, tag=''):
beepscb6f1e22013-06-28 19:14:10 -0700477 """
478 Make sure job_repo_url of this host is valid.
479
joychen03eaad92013-06-26 09:55:21 -0700480 Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/\
beepscb6f1e22013-06-28 19:14:10 -0700481 lumpy-release/R29-4279.0.0/autotest/packages" claims to have the
482 autotest package for lumpy-release/R29-4279.0.0. If this isn't the case,
483 download and extract it. If the devserver embedded in the url is
484 unresponsive, update the job_repo_url of the host after staging it on
485 another devserver.
486
487 @param job_repo_url: A url pointing to the devserver where the autotest
488 package for this build should be staged.
beepsdae65fd2013-07-26 16:24:41 -0700489 @param tag: The tag from the server job, in the format
490 <job_id>-<user>/<hostname>, or <hostless> for a server job.
beepscb6f1e22013-06-28 19:14:10 -0700491
492 @raises DevServerException: If we could not resolve a devserver.
493 @raises AutoservError: If we're unable to save the new job_repo_url as
494 a result of choosing a new devserver because the old one failed to
495 respond to a health check.
beeps0c865032013-07-30 11:37:06 -0700496 @raises urllib2.URLError: If the devserver embedded in job_repo_url
497 doesn't respond within the timeout.
beepscb6f1e22013-06-28 19:14:10 -0700498 """
Prathmesh Prabhu368abdf2017-02-14 11:23:47 -0800499 info = self.host_info_store.get()
500 job_repo_url = info.attributes.get(ds_constants.JOB_REPO_URL, '')
beepscb6f1e22013-06-28 19:14:10 -0700501 if not job_repo_url:
502 logging.warning('No job repo url set on host %s', self.hostname)
503 return
504
505 logging.info('Verifying job repo url %s', job_repo_url)
506 devserver_url, image_name = tools.get_devserver_build_from_package_url(
507 job_repo_url)
508
beeps0c865032013-07-30 11:37:06 -0700509 ds = dev_server.ImageServer(devserver_url)
beepscb6f1e22013-06-28 19:14:10 -0700510
511 logging.info('Staging autotest artifacts for %s on devserver %s',
512 image_name, ds.url())
beeps687243d2013-07-18 15:29:27 -0700513
514 start_time = time.time()
Simran Basi25e7a922014-10-31 11:56:10 -0700515 ds.stage_artifacts(image_name, ['autotest_packages'])
beeps687243d2013-07-18 15:29:27 -0700516 stage_time = time.time() - start_time
517
518 # Record how much of the verification time comes from a devserver
519 # restage. If we're doing things right we should not see multiple
520 # devservers for a given board/build/branch path.
521 try:
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800522 board, build_type, branch = server_utils.ParseBuildName(
beeps687243d2013-07-18 15:29:27 -0700523 image_name)[:3]
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800524 except server_utils.ParseBuildNameException:
beeps687243d2013-07-18 15:29:27 -0700525 pass
526 else:
beeps0c865032013-07-30 11:37:06 -0700527 devserver = devserver_url[
Chris Sosa65425082013-10-16 13:26:22 -0700528 devserver_url.find('/') + 2:devserver_url.rfind(':')]
beeps687243d2013-07-18 15:29:27 -0700529 stats_key = {
530 'board': board,
531 'build_type': build_type,
532 'branch': branch,
beeps0c865032013-07-30 11:37:06 -0700533 'devserver': devserver.replace('.', '_'),
beeps687243d2013-07-18 15:29:27 -0700534 }
Dan Shi5e2efb72017-02-07 11:40:23 -0800535
536 monarch_fields = {
537 'board': board,
538 'build_type': build_type,
Dan Shi5e2efb72017-02-07 11:40:23 -0800539 'branch': branch,
540 'dev_server': devserver,
541 }
Dan Shi5e2efb72017-02-07 11:40:23 -0800542
Dan Shicf4d2032015-03-12 15:04:21 -0700543
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700544 def stage_image_for_servo(self, image_name=None, artifact='test_image'):
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700545 """Stage a build on a devserver and return the update_url.
546
547 @param image_name: a name like lumpy-release/R27-3837.0.0
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700548 @param artifact: a string like 'test_image'. Requests
549 appropriate image to be staged.
Xixuan Wufee57542019-10-15 11:50:27 -0700550 @returns a tuple of (image_name, URL) like
551 (lumpy-release/R27-3837.0.0,
552 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700553 """
554 if not image_name:
Richard Barnetteb4b4d6f2018-05-05 01:47:41 +0000555 image_name = self.get_cros_repair_image_name()
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700556 logging.info('Staging build for servo install: %s', image_name)
Dan Shi216389c2015-12-22 11:03:06 -0800557 devserver = dev_server.ImageServer.resolve(image_name, self.hostname)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700558 devserver.stage_artifacts(image_name, [artifact])
559 if artifact == 'test_image':
Xixuan Wufee57542019-10-15 11:50:27 -0700560 return image_name, devserver.get_test_image_url(image_name)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700561 elif artifact == 'recovery_image':
Xixuan Wufee57542019-10-15 11:50:27 -0700562 return image_name, devserver.get_recovery_image_url(image_name)
Kalin Stoyanovb3c11f32018-05-11 09:02:00 -0700563 else:
564 raise error.AutoservError("Bad artifact!")
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700565
Laurence Goodby778c9a42017-05-24 19:24:07 -0700566 def prepare_for_update(self):
567 """Prepares the DUT for an update.
568
569 Subclasses may override this to perform any special actions
570 required before updating.
571 """
Laurence Goodby468de252017-06-08 17:22:53 -0700572 pass
Laurence Goodby778c9a42017-05-24 19:24:07 -0700573
574
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800575 def _clear_fw_version_labels(self, rw_only):
576 """Clear firmware version labels from the machine.
577
578 @param rw_only: True to only clear fwrw_version; otherewise, clear
579 both fwro_version and fwrw_version.
580 """
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700581 info = self.host_info_store.get()
Derek Beckett08ca8572021-08-25 14:46:35 -0700582 info.clear_version_labels(consts.FW_RW_VERSION_PREFIX)
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800583 if not rw_only:
Derek Beckett08ca8572021-08-25 14:46:35 -0700584 info.clear_version_labels(consts.FW_RO_VERSION_PREFIX)
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700585 self.host_info_store.commit(info)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700586
587
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800588 def _add_fw_version_label(self, build, rw_only):
Dan Shi9cb0eec2014-06-03 09:04:50 -0700589 """Add firmware version label to the machine.
590
591 @param build: Build of firmware.
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800592 @param rw_only: True to only add fwrw_version; otherwise, add both
593 fwro_version and fwrw_version.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700594
595 """
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700596 info = self.host_info_store.get()
Derek Beckett08ca8572021-08-25 14:46:35 -0700597 info.set_version_label(consts.FW_RW_VERSION_PREFIX, build)
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800598 if not rw_only:
Derek Beckett08ca8572021-08-25 14:46:35 -0700599 info.set_version_label(consts.FW_RO_VERSION_PREFIX, build)
Prathmesh Prabhuf8ae9a82019-10-04 15:34:13 -0700600 self.host_info_store.commit(info)
Dan Shi9cb0eec2014-06-03 09:04:50 -0700601
Brent Peterson1cb623a2020-01-09 13:14:28 -0800602 @staticmethod
603 def get_version_from_image(image, version_regex):
Brent Peterson8039b472020-02-14 10:51:23 -0800604 """Get version string from binary image using regular expression.
605
606 @param image: Binary image to search
607 @param version_regex: Regular expression to search for
608
609 @return Version string
610
611 @raises TestFail if no version string is found in image
612 """
Brent Peterson1cb623a2020-01-09 13:14:28 -0800613 with open(image, 'rb') as f:
614 image_data = f.read()
Derek Beckett98345552020-08-31 16:07:22 -0700615 match = re.findall(version_regex,
616 image_data.decode('ISO-8859-1', errors='ignore'))
Brent Peterson1cb623a2020-01-09 13:14:28 -0800617 if match:
618 return match[0]
619 else:
620 raise error.TestFail('Failed to read version from %s.' % image)
621
622
Garry Wangad2a1712020-03-26 15:06:43 -0700623 def firmware_install(self, build, rw_only=False, dest=None,
Brent Petersonc70a1832020-01-24 15:54:35 -0800624 local_tarball=None, verify_version=False,
Namyoon Woo382e5892020-05-20 16:48:40 -0700625 try_scp=False, install_ec=True, install_bios=True,
626 board_as=None):
Dan Shi9cb0eec2014-06-03 09:04:50 -0700627 """Install firmware to the DUT.
628
629 Use stateful update if the DUT is already running the same build.
630 Stateful update does not update kernel and tends to run much faster
631 than a full reimage. If the DUT is running a different build, or it
632 failed to do a stateful update, full update, including kernel update,
633 will be applied to the DUT.
634
Tom Wai-Hong Tam2d00cb22016-01-08 06:40:50 +0800635 Once a host enters firmware_install its fw[ro|rw]_version label will
636 be removed. After the firmware is updated successfully, a new
637 fw[ro|rw]_version label will be added to the host.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700638
639 @param build: The build version to which we want to provision the
640 firmware of the machine,
641 e.g. 'link-firmware/R22-2695.1.144'.
Tom Wai-Hong Tam4ac78982016-01-08 02:34:37 +0800642 @param rw_only: True to only install firmware to its RW portions. Keep
643 the RO portions unchanged.
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700644 @param dest: Directory to store the firmware in.
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800645 @param local_tarball: Path to local firmware image for installing
646 without devserver.
Brent Peterson1cb623a2020-01-09 13:14:28 -0800647 @param verify_version: True to verify EC and BIOS versions after
648 programming firmware, default is False.
Brent Petersonc70a1832020-01-24 15:54:35 -0800649 @param try_scp: False to always program using servo, true to try copying
650 the firmware and programming from the DUT.
Namyoon Woo382e5892020-05-20 16:48:40 -0700651 @param install_ec: True to install EC FW, and False to skip it.
652 @param install_bios: True to install BIOS, and False to skip it.
653 @param board_as: A board name to force to use.
Dan Shi9cb0eec2014-06-03 09:04:50 -0700654
655 TODO(dshi): After bug 381718 is fixed, update here with corresponding
656 exceptions that could be raised.
657
658 """
659 if not self.servo:
660 raise error.TestError('Host %s does not have servo.' %
661 self.hostname)
662
Wai-Hong Tam3fa455a2018-07-18 14:40:43 -0700663 info = self.host_info_store.get()
664 board = info.board
Shelley Chenac61d5a2019-06-24 15:35:46 -0700665 model = info.model
Namyoon Woo8dbfcf92019-01-15 18:37:12 -0800666
667 if board is None or board == '':
668 board = self.servo.get_board()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700669
Namyoon Woo382e5892020-05-20 16:48:40 -0700670 # if board_as argument is passed, then use it instead of the original
671 # board name.
672 if board_as:
673 board = board_as
674
Mary Ruthvende14a8b2019-08-23 12:43:52 -0700675 if model is None or model == '':
Namyoon Woofb16eae2020-08-14 10:02:39 -0700676 try:
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700677 model = self.get_platform()
Namyoon Woofb16eae2020-08-14 10:02:39 -0700678 except Exception as e:
679 logging.warn('Dut is unresponsive: %s', str(e))
Mary Ruthvende14a8b2019-08-23 12:43:52 -0700680
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800681 # If local firmware path not provided fetch it from the dev server
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700682 tmpd = None
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800683 if not local_tarball:
Garry Wangad2a1712020-03-26 15:06:43 -0700684 logging.info('Will install firmware from build %s.', build)
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800685
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700686 try:
687 ds = dev_server.ImageServer.resolve(build, self.hostname)
688 ds.stage_artifacts(build, ['firmware'])
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800689
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700690 if not dest:
691 tmpd = autotemp.tempdir(unique_id='fwimage')
692 dest = tmpd.name
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800693
Namyoon Woo43c1cb22020-05-28 18:58:34 -0700694 # Download firmware image
695 fwurl = self._FW_IMAGE_URL_PATTERN % (ds.url(), build)
696 local_tarball = os.path.join(dest, os.path.basename(fwurl))
697 ds.download_file(fwurl, local_tarball)
698 except Exception as e:
699 raise error.TestError('Failed to download firmware package: %s'
700 % str(e))
Dan Shi9cb0eec2014-06-03 09:04:50 -0700701
Namyoon Woo382e5892020-05-20 16:48:40 -0700702 ec_image = None
703 if install_ec:
704 # Extract EC image from tarball
705 logging.info('Extracting EC image.')
706 ec_image = self.servo.extract_ec_image(board, model, local_tarball)
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700707 logging.info('Extracted: %s', ec_image)
Brent Peterson1cb623a2020-01-09 13:14:28 -0800708
Namyoon Woo382e5892020-05-20 16:48:40 -0700709 bios_image = None
710 if install_bios:
711 # Extract BIOS image from tarball
712 logging.info('Extracting BIOS image.')
713 bios_image = self.servo.extract_bios_image(board, model,
714 local_tarball)
Mary Ruthven9c15a4e2020-10-23 10:10:53 -0700715 logging.info('Extracted: %s', bios_image)
Namyoon Woo382e5892020-05-20 16:48:40 -0700716
717 if not bios_image and not ec_image:
718 raise error.TestError('No firmware installation was processed.')
Brent Peterson1cb623a2020-01-09 13:14:28 -0800719
Brent Petersonc70a1832020-01-24 15:54:35 -0800720 # Clear firmware version labels
721 self._clear_fw_version_labels(rw_only)
722
723 # Install firmware from local tarball
Brent Peterson5b7c5b12020-01-09 13:14:28 -0800724 try:
Garry Wang50e4a492020-08-05 12:29:57 -0700725 # Check if copying to DUT is enabled and DUT is available
726 if try_scp and self.is_up():
Brent Petersonc70a1832020-01-24 15:54:35 -0800727 # DUT is available, make temp firmware directory to store images
728 logging.info('Making temp folder.')
729 dest_folder = '/tmp/firmware'
730 self.run('mkdir -p ' + dest_folder)
731
Namyoon Woo68b68082020-06-02 13:13:14 -0700732 fw_cmd = self._FW_UPDATE_CMD % ('--wp=1' if rw_only else '')
Brent Petersonc70a1832020-01-24 15:54:35 -0800733
Namyoon Woo382e5892020-05-20 16:48:40 -0700734 if bios_image:
735 # Send BIOS firmware image to DUT
736 logging.info('Sending BIOS firmware.')
737 dest_bios_path = os.path.join(dest_folder,
738 os.path.basename(bios_image))
739 self.send_file(bios_image, dest_bios_path)
740
741 # Initialize firmware update command for BIOS image
742 fw_cmd += ' -i %s' % dest_bios_path
Brent Peterson669edf42020-02-07 15:07:54 -0800743
744 # Send EC firmware image to DUT when EC image was found
745 if ec_image:
746 logging.info('Sending EC firmware.')
747 dest_ec_path = os.path.join(dest_folder,
748 os.path.basename(ec_image))
749 self.send_file(ec_image, dest_ec_path)
750
751 # Add EC image to firmware update command
752 fw_cmd += ' -e %s' % dest_ec_path
753
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700754 # Make sure command is allowed to finish even if ssh fails.
755 fw_cmd = "trap '' SIGHUP; %s" % fw_cmd
756
Brent Peterson669edf42020-02-07 15:07:54 -0800757 # Update firmware on DUT
758 logging.info('Updating firmware.')
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700759 try:
Dana Goyette935b3fe2020-07-23 14:19:39 -0700760 self.run(fw_cmd, options="-o LogLevel=verbose")
Dana Goyettee84ef8d2020-07-09 11:55:09 -0700761 except error.AutoservRunError as e:
762 if e.result_obj.exit_status != 255:
763 raise
764 elif ec_image:
765 logging.warn("DUT network dropped during update"
766 " (often caused by EC resetting USB)")
767 else:
768 logging.error("DUT network dropped during update"
769 " (unexpected, since no EC image)")
770 raise
Brent Petersonc70a1832020-01-24 15:54:35 -0800771 else:
772 # Host is not available, program firmware using servo
Brent Peterson669edf42020-02-07 15:07:54 -0800773 if ec_image:
774 self.servo.program_ec(ec_image, rw_only)
Namyoon Woo382e5892020-05-20 16:48:40 -0700775 if bios_image:
776 self.servo.program_bios(bios_image, rw_only)
Brent Petersonc70a1832020-01-24 15:54:35 -0800777 if utils.host_is_in_lab_zone(self.hostname):
778 self._add_fw_version_label(build, rw_only)
Brent Peterson1cb623a2020-01-09 13:14:28 -0800779
780 # Reboot and wait for DUT after installing firmware
781 logging.info('Rebooting DUT.')
782 self.servo.get_power_state_controller().reset()
783 time.sleep(self.servo.BOOT_DELAY)
784 self.test_wait_for_boot()
785
786 # When enabled verify EC and BIOS firmware version after programming
787 if verify_version:
Brent Peterson669edf42020-02-07 15:07:54 -0800788 # Check programmed EC firmware when EC image was found
789 if ec_image:
790 logging.info('Checking EC firmware version.')
791 dest_ec_version = self.get_ec_version()
Brent Peterson8039b472020-02-14 10:51:23 -0800792 ec_version_prefix = dest_ec_version.split('_', 1)[0]
793 ec_regex = self._EC_REGEX % ec_version_prefix
Brent Peterson669edf42020-02-07 15:07:54 -0800794 image_ec_version = self.get_version_from_image(ec_image,
Brent Peterson8039b472020-02-14 10:51:23 -0800795 ec_regex)
Brent Peterson669edf42020-02-07 15:07:54 -0800796 if dest_ec_version != image_ec_version:
797 raise error.TestFail(
Namyoon Wooe2c009a2020-07-22 10:09:03 -0700798 'Failed to update EC firmware, version %s '
799 '(expected %s)' % (dest_ec_version,
800 image_ec_version))
Brent Peterson1cb623a2020-01-09 13:14:28 -0800801
Namyoon Woo382e5892020-05-20 16:48:40 -0700802 if bios_image:
803 # Check programmed BIOS firmware against expected version
804 logging.info('Checking BIOS firmware version.')
805 dest_bios_version = self.get_firmware_version()
806 bios_version_prefix = dest_bios_version.split('.', 1)[0]
807 bios_regex = self._BIOS_REGEX % bios_version_prefix
808 image_bios_version = self.get_version_from_image(bios_image,
809 bios_regex)
810 if dest_bios_version != image_bios_version:
811 raise error.TestFail(
Namyoon Wooe2c009a2020-07-22 10:09:03 -0700812 'Failed to update BIOS, version %s '
Namyoon Woo382e5892020-05-20 16:48:40 -0700813 '(expected %s)' % (dest_bios_version,
814 image_bios_version))
Dan Shi9cb0eec2014-06-03 09:04:50 -0700815 finally:
Mary Ruthven6481a9f2019-08-23 12:46:05 -0700816 if tmpd:
817 tmpd.clean()
Dan Shi9cb0eec2014-06-03 09:04:50 -0700818
Garry Wanga2e78172020-09-09 23:49:07 -0700819 def set_servo_host(self, host, servo_state=None):
Richard Barnette4aeb01c2018-09-20 09:36:12 -0700820 """Set our servo host member, and associated servo.
821
822 @param host Our new `ServoHost`.
823 """
824 self._servo_host = host
Derek Beckettb66e5c82020-08-12 15:31:02 -0700825 self.servo_pwr_supported = None
Richard Barnette4aeb01c2018-09-20 09:36:12 -0700826 if self._servo_host is not None:
827 self.servo = self._servo_host.get_servo()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700828 servo_state = self._servo_host.get_servo_state()
Garry Wang000c6c02020-05-11 21:27:23 -0700829 self._set_smart_usbhub_label(self._servo_host.smart_usbhub)
Derek Beckettb66e5c82020-08-12 15:31:02 -0700830 try:
831 self.servo_pwr_supported = self.servo.has_control('power_state')
832 except Exception as e:
833 logging.debug(
834 "Could not get servo power state due to {}".format(e))
Gregory Nisbet93b23e22020-10-02 20:42:16 +0000835 else:
836 self.servo = None
Derek Beckettb66e5c82020-08-12 15:31:02 -0700837 self.servo_pwr_supported = False
Otabek Kasimov41301a22020-05-10 15:28:21 -0700838 self.set_servo_type()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700839 self.set_servo_state(servo_state)
Otabek Kasimov382c3bb2020-10-28 13:22:45 -0700840 self._set_servo_topology()
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700841
Otabek Kasimov41301a22020-05-10 15:28:21 -0700842 def set_servo_type(self):
843 """Set servo info labels to dut host_info"""
844 if not self.servo:
Otabek Kasimovbea89e52020-05-12 15:40:00 -0700845 logging.debug('Servo is not initialized to get servo_type.')
Otabek Kasimov41301a22020-05-10 15:28:21 -0700846 return
Otabek Kasimov1b70e8d2020-12-30 13:51:00 -0800847 if not self.is_servo_in_working_state():
848 logging.debug('Servo is not good, skip update servo_type.')
849 return
Otabek Kasimov41301a22020-05-10 15:28:21 -0700850 servo_type = self.servo.get_servo_type()
851 if not servo_type:
Otabek Kasimovbea89e52020-05-12 15:40:00 -0700852 logging.debug('Cannot collect servo_type from servo'
Otabek Kasimov41301a22020-05-10 15:28:21 -0700853 ' by `dut-control servo_type`! Please file a bug'
854 ' and inform infra team as we are not expected '
855 ' to reach this point.')
856 return
857 host_info = self.host_info_store.get()
858 prefix = servo_constants.SERVO_TYPE_LABEL_PREFIX
859 old_type = host_info.get_label_value(prefix)
860 if old_type == servo_type:
861 # do not need update
862 return
863 host_info.set_version_label(prefix, servo_type)
864 self.host_info_store.commit(host_info)
865 logging.info('ServoHost: servo_type updated to %s '
866 '(previous: %s)', servo_type, old_type)
867
868
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700869 def set_servo_state(self, servo_state):
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800870 """Set servo info labels to dut host_info"""
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700871 if servo_state is not None:
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800872 host_info = self.host_info_store.get()
Garry Wang11b5e872020-03-11 15:14:08 -0700873 servo_state_prefix = servo_constants.SERVO_STATE_LABEL_PREFIX
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700874 old_state = host_info.get_label_value(servo_state_prefix)
875 if old_state == servo_state:
876 # do not need update
877 return
878 host_info.set_version_label(servo_state_prefix, servo_state)
Otabek Kasimovcc9738e2020-02-14 16:17:15 -0800879 self.host_info_store.commit(host_info)
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700880 logging.info('ServoHost: servo_state updated to %s (previous: %s)',
881 servo_state, old_state)
Dan Shi90466352015-09-22 15:01:05 -0700882
883
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700884 def get_servo_state(self):
885 host_info = self.host_info_store.get()
Garry Wang11b5e872020-03-11 15:14:08 -0700886 servo_state_prefix = servo_constants.SERVO_STATE_LABEL_PREFIX
Otabek Kasimova7ba91a2020-03-09 08:31:01 -0700887 return host_info.get_label_value(servo_state_prefix)
888
Otabek Kasimov5f039202020-10-28 15:45:29 -0700889 def is_servo_in_working_state(self):
890 """Validate servo is in WORKING state."""
891 servo_state = self.get_servo_state()
892 return servo_state == servo_constants.SERVO_STATE_WORKING
893
Dana Goyette655af512020-09-03 10:48:23 -0700894 def get_servo_usb_state(self):
895 """Get the label value indicating the health of the USB drive.
896
897 @return: The label value if defined, otherwise '' (empty string).
898 @rtype: str
899 """
900 host_info = self.host_info_store.get()
901 servo_usb_state_prefix = audit_const.SERVO_USB_STATE_PREFIX
902 return host_info.get_label_value(servo_usb_state_prefix)
903
904 def is_servo_usb_usable(self):
905 """Check if the servo USB storage device is usable for FAFT.
906
907 @return: False if the label indicates a state that will break FAFT.
908 True if state is okay, or if state is not defined.
909 @rtype: bool
910 """
911 usb_state = self.get_servo_usb_state()
912 return usb_state in ('', audit_const.HW_STATE_ACCEPTABLE,
913 audit_const.HW_STATE_NORMAL,
914 audit_const.HW_STATE_UNKNOWN)
Otabek Kasimov41301a22020-05-10 15:28:21 -0700915
Garry Wang000c6c02020-05-11 21:27:23 -0700916 def _set_smart_usbhub_label(self, smart_usbhub_detected):
917 if smart_usbhub_detected is None:
918 # skip the label update here as this indicate we wasn't able
919 # to confirm usbhub type.
920 return
921 host_info = self.host_info_store.get()
922 if (smart_usbhub_detected ==
923 (servo_constants.SMART_USBHUB_LABEL in host_info.labels)):
924 # skip label update if current label match the truth.
925 return
926 if smart_usbhub_detected:
927 logging.info('Adding %s label to host %s',
928 servo_constants.SMART_USBHUB_LABEL,
929 self.hostname)
930 host_info.labels.append(servo_constants.SMART_USBHUB_LABEL)
931 else:
932 logging.info('Removing %s label from host %s',
933 servo_constants.SMART_USBHUB_LABEL,
934 self.hostname)
935 host_info.labels.remove(servo_constants.SMART_USBHUB_LABEL)
936 self.host_info_store.commit(host_info)
937
J. Richard Barnettec2d99cf2015-11-18 12:46:15 -0800938 def repair(self):
Derek Beckett3c5368c2021-08-03 14:13:58 -0700939 """Attempt to repair the DUT."""
Derek Beckett3d743402021-08-04 09:25:44 -0700940 # TODO b/195447992: Wire this into lab services (If needed?).
941 tauto_warnings.lab_services_warn_and_error("Cros repair")
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500942
Otabek Kasimov8bb09912020-10-01 14:44:57 -0700943 def get_verifier_state(self, tag):
Otabek Kasimov44273d22021-02-26 17:13:24 -0800944 """Return the state of host verifier by tag.
Otabek Kasimov8bb09912020-10-01 14:44:57 -0700945
946 @returns: bool or None
947 """
Derek Beckett3d743402021-08-04 09:25:44 -0700948 # TODO b/195447992: Wire this into lab services.
949 tauto_warnings.lab_services_warn_and_error("Cros verify state")
Richard Barnette82c35912012-11-20 10:09:10 -0800950
Otabek Kasimov44273d22021-02-26 17:13:24 -0800951 def get_repair_strategy_node(self, tag):
952 """Return the instance of verifier/repair node for host by tag.
953
954 @returns: _DependencyNode or None
955 """
Derek Beckett3d743402021-08-04 09:25:44 -0700956 # TODO b/195447992: Wire this into lab services.
957 tauto_warnings.lab_services_warn_and_error("Cros node")
Otabek Kasimov44273d22021-02-26 17:13:24 -0800958
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700959 def close(self):
David Rileye2c6be12017-12-11 10:20:57 -0800960 """Close connection."""
Fang Deng0ca40e22013-08-27 17:47:44 -0700961 super(CrosHost, self).close()
howardchung83e55272019-08-08 14:08:05 +0800962
Shijin Abraham783a7dd2020-02-14 15:36:11 -0800963 if self._chameleon_host:
964 self._chameleon_host.close()
xixuand6011f12016-12-08 15:01:58 -0800965
Garry Wang1a493d82020-08-31 21:01:19 -0700966 if self.health_profile:
967 try:
968 self.health_profile.close()
969 except Exception as e:
970 logging.warning(
971 'Failed to finalize device health profile; %s', e)
972
xixuand6011f12016-12-08 15:01:58 -0800973 if self._servo_host:
974 self._servo_host.close()
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700975
Dan Shi49ca0932014-11-14 11:22:27 -0800976 def get_power_supply_info(self):
977 """Get the output of power_supply_info.
978
979 power_supply_info outputs the info of each power supply, e.g.,
980 Device: Line Power
981 online: no
982 type: Mains
983 voltage (V): 0
984 current (A): 0
985 Device: Battery
986 state: Discharging
987 percentage: 95.9276
988 technology: Li-ion
989
990 Above output shows two devices, Line Power and Battery, with details of
991 each device listed. This function parses the output into a dictionary,
992 with key being the device name, and value being a dictionary of details
993 of the device info.
994
995 @return: The dictionary of power_supply_info, e.g.,
996 {'Line Power': {'online': 'yes', 'type': 'main'},
997 'Battery': {'vendor': 'xyz', 'percentage': '100'}}
Dan Shie9b765d2014-12-29 16:59:49 -0800998 @raise error.AutoservRunError if power_supply_info tool is not found in
999 the DUT. Caller should handle this error to avoid false failure
1000 on verification.
Dan Shi49ca0932014-11-14 11:22:27 -08001001 """
1002 result = self.run('power_supply_info').stdout.strip()
1003 info = {}
1004 device_name = None
1005 device_info = {}
1006 for line in result.split('\n'):
1007 pair = [v.strip() for v in line.split(':')]
1008 if len(pair) != 2:
1009 continue
1010 if pair[0] == 'Device':
1011 if device_name:
1012 info[device_name] = device_info
1013 device_name = pair[1]
1014 device_info = {}
1015 else:
1016 device_info[pair[0]] = pair[1]
1017 if device_name and not device_name in info:
1018 info[device_name] = device_info
1019 return info
1020
1021
1022 def get_battery_percentage(self):
1023 """Get the battery percentage.
1024
1025 @return: The percentage of battery level, value range from 0-100. Return
1026 None if the battery info cannot be retrieved.
1027 """
1028 try:
1029 info = self.get_power_supply_info()
1030 logging.info(info)
1031 return float(info['Battery']['percentage'])
Dan Shie9b765d2014-12-29 16:59:49 -08001032 except (KeyError, ValueError, error.AutoservRunError):
Dan Shi49ca0932014-11-14 11:22:27 -08001033 return None
1034
1035
Philip Chenaf69ead2020-03-27 13:06:42 -07001036 def get_battery_state(self):
1037 """Get the battery charging state.
1038
1039 @return: A string representing the battery charging state. It can be
1040 'Charging', 'Fully charged', or 'Discharging'.
1041 """
1042 try:
1043 info = self.get_power_supply_info()
1044 logging.info(info)
1045 return info['Battery']['state']
1046 except (KeyError, ValueError, error.AutoservRunError):
1047 return None
1048
1049
Daniel Campello8ca25c22019-12-13 16:48:26 -07001050 def get_battery_display_percentage(self):
1051 """Get the battery display percentage.
1052
1053 @return: The display percentage of battery level, value range from
1054 0-100. Return None if the battery info cannot be retrieved.
1055 """
1056 try:
1057 info = self.get_power_supply_info()
1058 logging.info(info)
1059 return float(info['Battery']['display percentage'])
1060 except (KeyError, ValueError, error.AutoservRunError):
1061 return None
1062
1063
Dan Shi49ca0932014-11-14 11:22:27 -08001064 def is_ac_connected(self):
1065 """Check if the dut has power adapter connected and charging.
1066
1067 @return: True if power adapter is connected and charging.
1068 """
1069 try:
1070 info = self.get_power_supply_info()
1071 return info['Line Power']['online'] == 'yes'
Dan Shie9b765d2014-12-29 16:59:49 -08001072 except (KeyError, error.AutoservRunError):
1073 return None
Dan Shi49ca0932014-11-14 11:22:27 -08001074
1075
Simran Basi5e6339a2013-03-21 11:34:32 -07001076 def _cleanup_poweron(self):
1077 """Special cleanup method to make sure hosts always get power back."""
Garry Wangad4d4fd2019-01-30 17:00:38 -08001078 info = self.host_info_store.get()
1079 if self._RPM_OUTLET_CHANGED not in info.attributes:
Simran Basi5e6339a2013-03-21 11:34:32 -07001080 return
1081 logging.debug('This host has recently interacted with the RPM'
1082 ' Infrastructure. Ensuring power is on.')
1083 try:
1084 self.power_on()
Garry Wangad4d4fd2019-01-30 17:00:38 -08001085 self._remove_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001086 except Exception:
1087 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basi5e6339a2013-03-21 11:34:32 -07001088 logging.error('Failed to turn Power On for this host after '
1089 'cleanup through the RPM Infrastructure.')
Dan Shi49ca0932014-11-14 11:22:27 -08001090
1091 battery_percentage = self.get_battery_percentage()
Otabek Kasimov58e22562020-11-03 17:17:41 -08001092 if (
1093 battery_percentage
1094 and battery_percentage < cros_constants.MIN_BATTERY_LEVEL):
Dan Shi49ca0932014-11-14 11:22:27 -08001095 raise
1096 elif self.is_ac_connected():
1097 logging.info('The device has power adapter connected and '
1098 'charging. No need to try to turn RPM on '
1099 'again.')
Garry Wangad4d4fd2019-01-30 17:00:38 -08001100 self._remove_rpm_changed_tag()
Dan Shi49ca0932014-11-14 11:22:27 -08001101 logging.info('Battery level is now at %s%%. The device may '
1102 'still have enough power to run test, so no '
1103 'exception will be raised.', battery_percentage)
1104
Simran Basi5e6339a2013-03-21 11:34:32 -07001105
Garry Wangad4d4fd2019-01-30 17:00:38 -08001106 def _remove_rpm_changed_tag(self):
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001107 # TODO b/195443964: Re-wire as needed once TLW is available.
1108 pass
Garry Wangad4d4fd2019-01-30 17:00:38 -08001109
1110
1111 def _add_rpm_changed_tag(self):
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001112 # TODO b/195443964: Re-wire as needed once TLW is available.
1113 pass
Garry Wangad4d4fd2019-01-30 17:00:38 -08001114
1115
beepsc87ff602013-07-31 21:53:00 -07001116 def _is_factory_image(self):
1117 """Checks if the image on the DUT is a factory image.
1118
1119 @return: True if the image on the DUT is a factory image.
1120 False otherwise.
1121 """
1122 result = self.run('[ -f /root/.factory_test ]', ignore_status=True)
1123 return result.exit_status == 0
1124
1125
1126 def _restart_ui(self):
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001127 """Restart the Chrome UI.
beepsc87ff602013-07-31 21:53:00 -07001128
1129 @raises: FactoryImageCheckerException for factory images, since
1130 we cannot attempt to restart ui on them.
1131 error.AutoservRunError for any other type of error that
1132 occurs while restarting ui.
1133 """
1134 if self._is_factory_image():
Dan Shi549fb822015-03-24 18:01:11 -07001135 raise FactoryImageCheckerException('Cannot restart ui on factory '
1136 'images')
beepsc87ff602013-07-31 21:53:00 -07001137
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001138 # TODO(jrbarnette): The command to stop/start the ui job
1139 # should live inside cros_ui, too. However that would seem
1140 # to imply interface changes to the existing start()/restart()
1141 # functions, which is a bridge too far (for now).
J. Richard Barnette6069aa12015-06-08 09:10:24 -07001142 prompt = cros_ui.get_chrome_session_ident(self)
J. Richard Barnette84890bd2014-02-21 11:05:47 -08001143 self.run('stop ui; start ui')
1144 cros_ui.wait_for_chrome_ready(prompt, self)
beepsc87ff602013-07-31 21:53:00 -07001145
1146
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001147 def _start_powerd_if_needed(self):
1148 """Start powerd if it isn't already running."""
1149 self.run('start powerd', ignore_status=True)
1150
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001151 def _read_arc_prop_file(self, filename):
1152 for path in [
1153 '/usr/share/arcvm/properties/', '/usr/share/arc/properties/'
1154 ]:
1155 if self.path_exists(path + filename):
1156 return utils.parse_cmd_output('cat ' + path + filename,
1157 run_method=self.run)
1158 return None
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001159
Jiyoun Hac172ee72020-12-15 08:57:29 +09001160 def _get_arc_build_info(self):
1161 """Returns a dictionary mapping build properties to their values."""
1162 build_info = None
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001163 for filename in ['build.prop', 'vendor_build.prop']:
1164 properties = self._read_arc_prop_file(filename)
1165 if properties:
1166 if build_info:
1167 build_info.update(properties)
1168 else:
1169 build_info = properties
1170 else:
1171 logging.error('Failed to find %s in device.', filename)
Jiyoun Hac172ee72020-12-15 08:57:29 +09001172 return build_info
1173
Jiyoun Haba37f312021-01-13 09:44:16 +09001174 def get_arc_primary_abi(self):
Jiyoun Hac172ee72020-12-15 08:57:29 +09001175 """Returns the primary abi of the host."""
1176 return self._get_arc_build_info().get('ro.product.cpu.abi')
1177
Jiyoun Haba37f312021-01-13 09:44:16 +09001178 def get_arc_security_patch(self):
Jiyoun Hac172ee72020-12-15 08:57:29 +09001179 """Returns the security patch of the host."""
1180 return self._get_arc_build_info().get('ro.build.version.security_patch')
1181
Kazuhiro Inabaa7a00492020-12-17 16:05:12 +09001182 def get_arc_first_api_level(self):
1183 """Returns the security patch of the host."""
1184 return self._get_arc_build_info().get('ro.product.first_api_level')
1185
xixuana3bbc422017-05-04 15:57:21 -07001186 def _get_lsb_release_content(self):
1187 """Return the content of lsb-release file of host."""
1188 return self.run(
1189 'cat "%s"' % client_constants.LSB_RELEASE).stdout.strip()
1190
1191
Dan Shi549fb822015-03-24 18:01:11 -07001192 def get_release_version(self):
1193 """Get the value of attribute CHROMEOS_RELEASE_VERSION from lsb-release.
1194
1195 @returns The version string in lsb-release, under attribute
1196 CHROMEOS_RELEASE_VERSION.
1197 """
Dan Shi549fb822015-03-24 18:01:11 -07001198 return lsbrelease_utils.get_chromeos_release_version(
xixuana3bbc422017-05-04 15:57:21 -07001199 lsb_release_content=self._get_lsb_release_content())
1200
1201
Don Garrettb9f35802018-01-22 18:25:40 -08001202 def get_release_builder_path(self):
1203 """Get the value of CHROMEOS_RELEASE_BUILDER_PATH from lsb-release.
1204
1205 @returns The version string in lsb-release, under attribute
1206 CHROMEOS_RELEASE_BUILDER_PATH.
1207 """
1208 return lsbrelease_utils.get_chromeos_release_builder_path(
1209 lsb_release_content=self._get_lsb_release_content())
1210
1211
xixuana3bbc422017-05-04 15:57:21 -07001212 def get_chromeos_release_milestone(self):
1213 """Get the value of attribute CHROMEOS_RELEASE_BUILD_TYPE
1214 from lsb-release.
1215
1216 @returns The version string in lsb-release, under attribute
1217 CHROMEOS_RELEASE_BUILD_TYPE.
1218 """
1219 return lsbrelease_utils.get_chromeos_release_milestone(
1220 lsb_release_content=self._get_lsb_release_content())
Dan Shi549fb822015-03-24 18:01:11 -07001221
Laurence Goodby778c9a42017-05-24 19:24:07 -07001222 def cleanup_services(self):
1223 """Reinitializes the device for cleanup.
1224
1225 Subclasses may override this to customize the cleanup method.
1226
1227 To indicate failure of the reset, the implementation may raise
1228 any of:
1229 error.AutoservRunError
1230 error.AutotestRunError
1231 FactoryImageCheckerException
1232
1233 @raises error.AutoservRunError
1234 @raises error.AutotestRunError
1235 @raises error.FactoryImageCheckerException
1236 """
1237 self._restart_ui()
Daniel Erat4b5f7e02018-07-04 22:05:30 -07001238 self._start_powerd_if_needed()
Laurence Goodby778c9a42017-05-24 19:24:07 -07001239
1240
Gregory Nisbetec615d62020-12-11 17:59:20 +00001241 def cleanup(self):
1242 """Cleanup state on device."""
MK Ryu35d661e2014-09-25 17:44:10 -07001243 self.run('rm -f %s' % client_constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -05001244 try:
Laurence Goodby778c9a42017-05-24 19:24:07 -07001245 self.cleanup_services()
beepsc87ff602013-07-31 21:53:00 -07001246 except (error.AutotestRunError, error.AutoservRunError,
1247 FactoryImageCheckerException):
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001248 logging.warning('Unable to restart ui.')
Namyoon Woo33f38852020-04-13 17:26:58 -07001249
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001250 # cleanup routines, i.e. reboot the machine.
Gregory Nisbetec615d62020-12-11 17:59:20 +00001251 super(CrosHost, self).cleanup()
Otabek Kasimovc0d77e82020-03-27 15:01:57 -07001252
Simran Basi5e6339a2013-03-21 11:34:32 -07001253 # Check if the rpm outlet was manipulated.
Simran Basid5e5e272012-09-24 15:23:59 -07001254 if self.has_power():
Simran Basi5e6339a2013-03-21 11:34:32 -07001255 self._cleanup_poweron()
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001256
Gregory Nisbetec615d62020-12-11 17:59:20 +00001257
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001258 def reboot(self, **dargs):
1259 """
1260 This function reboots the site host. The more generic
1261 RemoteHost.reboot() performs sync and sleeps for 5
1262 seconds. This is not necessary for Chrome OS devices as the
1263 sync should be finished in a short time during the reboot
1264 command.
1265 """
Gregory Nisbetec615d62020-12-11 17:59:20 +00001266 if 'reboot_cmd' not in dargs:
Doug Anderson7d5aeb22014-02-27 15:12:17 -08001267 reboot_timeout = dargs.get('reboot_timeout', 10)
J. Richard Barnette9af19632015-09-25 12:18:03 -07001268 dargs['reboot_cmd'] = ('sleep 1; '
1269 'reboot & sleep %d; '
1270 'reboot -f' % reboot_timeout)
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001271 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +08001272 if 'fastsync' not in dargs:
1273 dargs['fastsync'] = True
Michael Liangda8c60a2014-06-03 13:24:51 -07001274
Prathmesh Prabhu53a4c1c2018-09-14 16:36:27 -07001275 dargs['board'] = self.host_info_store.get().board
Vincent Palatindf2372c2016-10-07 17:03:00 +02001276 # Record who called us
1277 orig = sys._getframe(1).f_code
Vincent Palatin80780b22016-07-27 16:02:37 +02001278 metric_fields = {'board' : dargs['board'],
Vincent Palatindf2372c2016-10-07 17:03:00 +02001279 'dut_host_name' : self.hostname,
1280 'success' : True}
1281 metric_debug_fields = {'board' : dargs['board'],
Prathmesh Prabhu53a4c1c2018-09-14 16:36:27 -07001282 'caller' : "%s:%s" % (orig.co_filename,
1283 orig.co_name),
Vincent Palatindf2372c2016-10-07 17:03:00 +02001284 'success' : True,
1285 'error' : ''}
1286
Vincent Palatin80780b22016-07-27 16:02:37 +02001287 try:
1288 super(CrosHost, self).reboot(**dargs)
1289 except Exception as e:
1290 metric_fields['success'] = False
Vincent Palatindf2372c2016-10-07 17:03:00 +02001291 metric_debug_fields['success'] = False
1292 metric_debug_fields['error'] = type(e).__name__
Vincent Palatin80780b22016-07-27 16:02:37 +02001293 raise
Yu-Ju Honga2be94a2012-07-31 09:48:52 -07001294
Kalin Stoyanov83d9caa2020-01-31 16:58:27 -08001295 def suspend(self, suspend_time=60, delay_seconds=0,
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001296 suspend_cmd=None, allow_early_resume=False):
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001297 """
1298 This function suspends the site host.
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001299
1300 @param suspend_time: How long to suspend as integer seconds.
1301 @param suspend_cmd: Suspend command to execute.
1302 @param allow_early_resume: If False and if device resumes before
1303 |suspend_time|, throw an error.
1304
1305 @exception AutoservSuspendError Host resumed earlier than
1306 |suspend_time|.
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001307 """
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001308
1309 if suspend_cmd is None:
1310 suspend_cmd = ' && '.join([
J. Richard Barnette9af19632015-09-25 12:18:03 -07001311 'echo 0 > /sys/class/rtc/rtc0/wakealarm',
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001312 'echo +%d > /sys/class/rtc/rtc0/wakealarm' % suspend_time,
Kalin Stoyanov83d9caa2020-01-31 16:58:27 -08001313 'powerd_dbus_suspend --delay=%d' % delay_seconds])
Ravi Chandra Sadineni812e61b2018-07-09 11:16:50 -07001314 super(CrosHost, self).suspend(suspend_time, suspend_cmd,
1315 allow_early_resume);
Gwendal Grignou7a61d2f2014-05-23 11:05:51 -07001316
1317
Simran Basiec564392014-08-25 16:48:09 -07001318 def upstart_status(self, service_name):
1319 """Check the status of an upstart init script.
1320
1321 @param service_name: Service to look up.
1322
1323 @returns True if the service is running, False otherwise.
1324 """
Richard Barnettee204dc52017-09-26 11:02:25 -07001325 return 'start/running' in self.run('status %s' % service_name,
1326 ignore_status=True).stdout
Simran Basiec564392014-08-25 16:48:09 -07001327
Tom Hughese9552342018-12-18 14:29:25 -08001328 def upstart_stop(self, service_name):
1329 """Stops an upstart job if it's running.
1330
1331 @param service_name: Service to stop
1332
1333 @returns True if service has been stopped or was already stopped
1334 False otherwise.
1335 """
1336 if not self.upstart_status(service_name):
1337 return True
1338
1339 result = self.run('stop %s' % service_name, ignore_status=True)
1340 if result.exit_status != 0:
1341 return False
1342 return True
1343
1344 def upstart_restart(self, service_name):
1345 """Restarts (or starts) an upstart job.
1346
1347 @param service_name: Service to start/restart
1348
1349 @returns True if service has been started/restarted, False otherwise.
1350 """
1351 cmd = 'start'
1352 if self.upstart_status(service_name):
1353 cmd = 'restart'
1354 cmd = cmd + ' %s' % service_name
1355 result = self.run(cmd)
1356 if result.exit_status != 0:
1357 return False
1358 return True
Simran Basiec564392014-08-25 16:48:09 -07001359
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001360 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001361 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001362
Richard Barnetteb2bc13c2013-01-08 17:32:51 -08001363 Tests for the following conditions:
1364 1. All conditions tested by the parent version of this
1365 function.
1366 2. Sufficient space in /mnt/stateful_partition.
Fang Deng6b05f5b2013-03-20 13:42:11 -07001367 3. Sufficient space in /mnt/stateful_partition/encrypted.
1368 4. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001369
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001370 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001371 super(CrosHost, self).verify_software()
Dan Shib8540a52015-07-16 14:18:23 -07001372 default_kilo_inodes_required = CONFIG.get_config_value(
1373 'SERVER', 'kilo_inodes_required', type=int, default=100)
1374 board = self.get_board().replace(ds_constants.BOARD_PREFIX, '')
1375 kilo_inodes_required = CONFIG.get_config_value(
1376 'SERVER', 'kilo_inodes_required_%s' % board,
1377 type=int, default=default_kilo_inodes_required)
1378 self.check_inodes('/mnt/stateful_partition', kilo_inodes_required)
J. Richard Barnette45e93de2012-04-11 17:24:15 -07001379 self.check_diskspace(
1380 '/mnt/stateful_partition',
Dan Shib8540a52015-07-16 14:18:23 -07001381 CONFIG.get_config_value(
Fang Deng6b05f5b2013-03-20 13:42:11 -07001382 'SERVER', 'gb_diskspace_required', type=float,
1383 default=20.0))
Gaurav Shahe448af82014-06-19 15:18:59 -07001384 encrypted_stateful_path = '/mnt/stateful_partition/encrypted'
1385 # Not all targets build with encrypted stateful support.
1386 if self.path_exists(encrypted_stateful_path):
1387 self.check_diskspace(
1388 encrypted_stateful_path,
Dan Shib8540a52015-07-16 14:18:23 -07001389 CONFIG.get_config_value(
Gaurav Shahe448af82014-06-19 15:18:59 -07001390 'SERVER', 'gb_encrypted_diskspace_required', type=float,
1391 default=0.1))
beepsc87ff602013-07-31 21:53:00 -07001392
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001393 self.wait_for_system_services()
Prashanth B5d0a0512014-04-25 12:26:08 -07001394
beepsc87ff602013-07-31 21:53:00 -07001395 # Factory images don't run update engine,
1396 # goofy controls dbus on these DUTs.
1397 if not self._is_factory_image():
1398 self.run('update_engine_client --status')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001399
1400
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001401 @retry.retry(error.AutoservError, timeout_min=5, delay_sec=10)
Kuo Jen Wei50ef6e82021-03-29 12:04:47 +08001402 def wait_for_service(self, service_name):
1403 """Wait for target status of an upstart init script.
1404
1405 @param service_name: Service to wait for.
1406 """
1407 if not self.upstart_status(service_name):
1408 raise error.AutoservError('Service %s not running.' % service_name)
1409
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001410 def wait_for_system_services(self):
1411 """Waits for system-services to be running.
1412
1413 Sometimes, update_engine will take a while to update firmware, so we
1414 should give this some time to finish. See crbug.com/765686#c38 for
1415 details.
1416 """
Kuo Jen Wei50ef6e82021-03-29 12:04:47 +08001417 self.wait_for_service('system-services')
Justin TerAvest3b0c5ba2017-11-07 09:59:11 -07001418
1419
J. Richard Barnettea7e7fdc2016-02-12 12:35:36 -08001420 def verify(self):
Pradeep Sawlaniaa013fa2017-11-09 06:34:18 -08001421 """Verify Chrome OS system is in good state."""
Richard Barnetteabbdc252018-07-26 16:57:42 -07001422 message = 'Beginning verify for host %s board %s model %s'
1423 info = self.host_info_store.get()
1424 message %= (self.hostname, info.board, info.model)
1425 self.record('INFO', None, None, message)
Derek Beckett3d743402021-08-04 09:25:44 -07001426 # TODO b/195447992: Wire this into lab services.
1427 tauto_warnings.lab_services_warn_and_error("Repair")
J. Richard Barnettea7e7fdc2016-02-12 12:35:36 -08001428
1429
Fang Deng96667ca2013-08-01 17:46:18 -07001430 def make_ssh_command(self, user='root', port=22, opts='', hosts_file=None,
Dean Liaoe3e75f62017-11-14 10:36:43 +08001431 connect_timeout=None, alive_interval=None,
1432 alive_count_max=None, connection_attempts=None):
Fang Deng96667ca2013-08-01 17:46:18 -07001433 """Override default make_ssh_command to use options tuned for Chrome OS.
1434
1435 Tuning changes:
1436 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH
1437 connection failure. Consistency with remote_access.sh.
1438
Samuel Tan2ce155b2015-06-23 18:24:38 -07001439 - ServerAliveInterval=900; which causes SSH to ping connection every
1440 900 seconds. In conjunction with ServerAliveCountMax ensures
1441 that if the connection dies, Autotest will bail out.
Fang Deng96667ca2013-08-01 17:46:18 -07001442 Originally tried 60 secs, but saw frequent job ABORTS where
Samuel Tan2ce155b2015-06-23 18:24:38 -07001443 the test completed successfully. Later increased from 180 seconds to
1444 900 seconds to account for tests where the DUT is suspended for
1445 longer periods of time.
Fang Deng96667ca2013-08-01 17:46:18 -07001446
1447 - ServerAliveCountMax=3; consistency with remote_access.sh.
1448
1449 - ConnectAttempts=4; reduce flakiness in connection errors;
1450 consistency with remote_access.sh.
1451
1452 - UserKnownHostsFile=/dev/null; we don't care about the keys.
1453 Host keys change with every new installation, don't waste
1454 memory/space saving them.
1455
1456 - SSH protocol forced to 2; needed for ServerAliveInterval.
1457
1458 @param user User name to use for the ssh connection.
1459 @param port Port on the target host to use for ssh connection.
1460 @param opts Additional options to the ssh command.
1461 @param hosts_file Ignored.
1462 @param connect_timeout Ignored.
1463 @param alive_interval Ignored.
Dean Liaoe3e75f62017-11-14 10:36:43 +08001464 @param alive_count_max Ignored.
1465 @param connection_attempts Ignored.
Fang Deng96667ca2013-08-01 17:46:18 -07001466 """
Dean Liaoe3e75f62017-11-14 10:36:43 +08001467 options = ' '.join([opts, '-o Protocol=2'])
1468 return super(CrosHost, self).make_ssh_command(
1469 user=user, port=port, opts=options, hosts_file='/dev/null',
1470 connect_timeout=30, alive_interval=900, alive_count_max=3,
1471 connection_attempts=4)
1472
1473
Jason Abeleb6f924f2013-11-13 16:01:54 -08001474 def syslog(self, message, tag='autotest'):
1475 """Logs a message to syslog on host.
1476
1477 @param message String message to log into syslog
1478 @param tag String tag prefix for syslog
1479
1480 """
1481 self.run('logger -t "%s" "%s"' % (tag, message))
1482
1483
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001484 def _ping_check_status(self, status):
1485 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001486
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001487 @param status Check the ping status against this value.
1488 @return True iff `status` and the result of ping are the same
1489 (i.e. both True or both False).
1490
1491 """
Abhishek Pandit-Subedi038df162020-09-14 16:37:43 -07001492 ping_val = utils.ping(self.hostname,
1493 tries=1,
1494 deadline=1,
1495 timeout=2,
1496 ignore_timeout=True)
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001497 return not (status ^ (ping_val == 0))
1498
1499 def _ping_wait_for_status(self, status, timeout):
1500 """Wait for the host to have a given status (UP or DOWN).
1501
1502 Status is checked by polling. Polling will not last longer
1503 than the number of seconds in `timeout`. The polling
1504 interval will be long enough that only approximately
1505 _PING_WAIT_COUNT polling cycles will be executed, subject
1506 to a maximum interval of about one minute.
1507
1508 @param status Waiting will stop immediately if `ping` of the
1509 host returns this status.
1510 @param timeout Poll for at most this many seconds.
1511 @return True iff the host status from `ping` matched the
1512 requested status at the time of return.
1513
1514 """
1515 # _ping_check_status() takes about 1 second, hence the
1516 # "- 1" in the formula below.
Nathan Ciobanu38480a32016-10-25 15:26:45 -07001517 # FIXME: if the ping command errors then _ping_check_status()
1518 # returns instantly. If timeout is also smaller than twice
1519 # _PING_WAIT_COUNT then the while loop below forks many
1520 # thousands of ping commands (see /tmp/test_that_results_XXXXX/
1521 # /results-1-logging_YYY.ZZZ/debug/autoserv.DEBUG) and hogs one
1522 # CPU core for 60 seconds.
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001523 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
1524 end_time = time.time() + timeout
1525 while time.time() <= end_time:
1526 if self._ping_check_status(status):
1527 return True
1528 if poll_interval > 0:
1529 time.sleep(poll_interval)
1530
1531 # The last thing we did was sleep(poll_interval), so it may
1532 # have been too long since the last `ping`. Check one more
1533 # time, just to be sure.
1534 return self._ping_check_status(status)
1535
1536 def ping_wait_up(self, timeout):
1537 """Wait for the host to respond to `ping`.
1538
1539 N.B. This method is not a reliable substitute for
1540 `wait_up()`, because a host that responds to ping will not
1541 necessarily respond to ssh. This method should only be used
1542 if the target DUT can be considered functional even if it
1543 can't be reached via ssh.
1544
1545 @param timeout Minimum time to allow before declaring the
1546 host to be non-responsive.
1547 @return True iff the host answered to ping before the timeout.
1548
1549 """
Andrew Luo4be621d2020-03-21 07:01:13 -07001550 if self.use_icmp:
1551 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
1552 else:
1553 logging.debug('Using SSH instead of ICMP for ping_wait_up.')
1554 return self.wait_up(timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001555
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001556 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001557 """Wait until the host no longer responds to `ping`.
1558
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001559 This function can be used as a slightly faster version of
1560 `wait_down()`, by avoiding potentially long ssh timeouts.
1561
1562 @param timeout Minimum time to allow for the host to become
1563 non-responsive.
1564 @return True iff the host quit answering ping before the
1565 timeout.
1566
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001567 """
Andrew Luo4be621d2020-03-21 07:01:13 -07001568 if self.use_icmp:
1569 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
1570 else:
1571 logging.debug('Using SSH instead of ICMP for ping_wait_down.')
1572 return self.wait_down(timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001573
Anand K Mistry50f218e2020-07-31 14:50:15 +10001574 def _is_host_port_forwarded(self):
Garry Wanga2e78172020-09-09 23:49:07 -07001575 """Checks if the dut is connected over port forwarding.
Anand K Mistry50f218e2020-07-31 14:50:15 +10001576
1577 N.B. This method does not detect all situations where port forwarding is
1578 occurring. Namely, running autotest on the dut may result in a
1579 false-positive, and port forwarding using a different machine on the
1580 same network will be a false-negative.
1581
1582 @return True if the dut is connected over port forwarding
1583 False otherwise
1584 """
Garry Wanga2e78172020-09-09 23:49:07 -07001585 is_localhost = self.hostname in ['localhost', '127.0.0.1']
1586 is_forwarded = is_localhost and not self.is_default_port
1587 if is_forwarded:
1588 logging.info('Detected DUT connected by port forwarding')
1589 return is_forwarded
Anand K Mistry50f218e2020-07-31 14:50:15 +10001590
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001591 def test_wait_for_boot(self, old_boot_id=None):
1592 """Wait for the client to boot from cold power.
1593
1594 The `old_boot_id` parameter should be the value from
1595 `get_boot_id()` obtained prior to shutting down. A
1596 `TestFail` exception is raised if the boot id does not
1597 change. The boot id test is omitted if `old_boot_id` is not
1598 specified.
1599
1600 See @ref test_wait_for_shutdown for more on this function's
1601 usage.
1602
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001603 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001604 shut down.
1605
1606 @exception TestFail The host did not respond within the
1607 allowed time.
1608 @exception TestFail The host responded, but the boot id test
1609 indicated that there was no reboot.
1610 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001611 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001612 raise error.TestFail(
1613 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001614 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001615 elif old_boot_id:
1616 if self.get_boot_id() == old_boot_id:
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08001617 logging.error('client not rebooted (boot %s)',
1618 old_boot_id)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001619 raise error.TestFail(
Tom Wai-Hong Tam01792682015-01-06 08:00:46 +08001620 'client is back up, but did not reboot')
Simran Basid5e5e272012-09-24 15:23:59 -07001621
1622
1623 @staticmethod
1624 def check_for_rpm_support(hostname):
1625 """For a given hostname, return whether or not it is powered by an RPM.
1626
Simran Basi1df55112013-09-06 11:25:09 -07001627 @param hostname: hostname to check for rpm support.
1628
Simran Basid5e5e272012-09-24 15:23:59 -07001629 @return None if this host does not follows the defined naming format
1630 for RPM powered DUT's in the lab. If it does follow the format,
1631 it returns a regular expression MatchObject instead.
1632 """
Fang Dengbaff9082015-01-06 13:46:15 -08001633 return re.match(CrosHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001634
1635
1636 def has_power(self):
1637 """For this host, return whether or not it is powered by an RPM.
1638
1639 @return True if this host is in the CROS lab and follows the defined
1640 naming format.
1641 """
Fang Deng0ca40e22013-08-27 17:47:44 -07001642 return CrosHost.check_for_rpm_support(self.hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001643
1644
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001645 def _set_power(self, state, power_method):
Garry Wang5e5538a2019-04-08 15:36:18 -07001646 """Sets the power to the host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001647
1648 @param state Specifies which power state to set to DUT
1649 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07001650 use. By default "RPM" or "CCD" will be used based
1651 on servo type. Valid values from
1652 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001653
1654 """
1655 ACCEPTABLE_STATES = ['ON', 'OFF']
1656
Garry Wang5e5538a2019-04-08 15:36:18 -07001657 if not power_method:
1658 power_method = self.get_default_power_method()
1659
1660 state = state.upper()
1661 if state not in ACCEPTABLE_STATES:
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001662 raise error.TestError('State must be one of: %s.'
1663 % (ACCEPTABLE_STATES,))
1664
1665 if power_method == self.POWER_CONTROL_SERVO:
1666 logging.info('Setting servo port J10 to %s', state)
1667 self.servo.set('prtctl3_pwren', state.lower())
1668 time.sleep(self._USB_POWER_TIMEOUT)
1669 elif power_method == self.POWER_CONTROL_MANUAL:
1670 logging.info('You have %d seconds to set the AC power to %s.',
1671 self._POWER_CYCLE_TIMEOUT, state)
1672 time.sleep(self._POWER_CYCLE_TIMEOUT)
Garry Wang5e5538a2019-04-08 15:36:18 -07001673 elif power_method == self.POWER_CONTROL_CCD:
1674 servo_role = 'src' if state == 'ON' else 'snk'
1675 logging.info('servo ccd power pass through detected,'
1676 ' changing servo_role to %s.', servo_role)
1677 self.servo.set_servo_v4_role(servo_role)
1678 if not self.ping_wait_up(timeout=self._CHANGE_SERVO_ROLE_TIMEOUT):
Garry Wang94bf9de2019-06-10 17:23:37 -07001679 # Make sure we don't leave DUT with no power(servo_role=snk)
1680 # when DUT is not pingable, as we raise a exception here
1681 # that may break a power cycle in the middle.
1682 self.servo.set_servo_v4_role('src')
Garry Wang5e5538a2019-04-08 15:36:18 -07001683 raise error.AutoservError(
1684 'DUT failed to regain network connection after %d seconds.'
1685 % self._CHANGE_SERVO_ROLE_TIMEOUT)
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001686 else:
1687 if not self.has_power():
1688 raise error.TestFail('DUT does not have RPM connected.')
Garry Wangad4d4fd2019-01-30 17:00:38 -08001689 self._add_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001690 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basid5e5e272012-09-24 15:23:59 -07001691
1692
Garry Wang5e5538a2019-04-08 15:36:18 -07001693 def power_off(self, power_method=None):
1694 """Turn off power to this host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001695
1696 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07001697 use. By default "RPM" or "CCD" will be used based
1698 on servo type. Valid values from
1699 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001700
1701 """
Derek Beckettb66e5c82020-08-12 15:31:02 -07001702 self._sync_if_up()
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001703 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07001704
Derek Beckettb66e5c82020-08-12 15:31:02 -07001705 def _check_supported(self):
1706 """Throw an error if dts mode control is not supported."""
1707 if not self.servo_pwr_supported:
1708 raise error.TestFail('power_state controls not supported')
1709
1710 def _sync_if_up(self):
1711 """Run sync on the DUT and wait for completion if the DUT is up.
1712
1713 Additionally, try to sync and ignore status if its not up.
1714
1715 Useful prior to reboots to ensure files are written to disc.
1716
1717 """
1718 if self.is_up_fast():
1719 self.run("sync")
1720 return
1721 # If it is not up, attempt to sync in the rare event the DUT is up but
1722 # doesn't respond to a ping. Ignore any errors.
1723 try:
1724 self.run("sync", ignore_status=True, timeout=1)
1725 except Exception:
1726 pass
1727
1728 def power_off_via_servo(self):
1729 """Force the DUT to power off.
1730
1731 The DUT is guaranteed to be off at the end of this call,
1732 regardless of its previous state, provided that there is
1733 working EC and boot firmware. There is no requirement for
1734 working OS software.
1735
1736 """
1737 self._check_supported()
1738 self._sync_if_up()
1739 self.servo.set_nocheck('power_state', 'off')
1740
1741 def power_on_via_servo(self, rec_mode='on'):
1742 """Force the DUT to power on.
1743
1744 Prior to calling this function, the DUT must be powered off,
1745 e.g. with a call to `power_off()`.
1746
1747 At power on, recovery mode is set as specified by the
1748 corresponding argument. When booting with recovery mode on, it
1749 is the caller's responsibility to unplug/plug in a bootable
1750 external storage device.
1751
1752 If the DUT requires a delay after powering on but before
1753 processing inputs such as USB stick insertion, the delay is
1754 handled by this method; the caller is not responsible for such
1755 delays.
1756
1757 @param rec_mode Setting of recovery mode to be applied at
1758 power on. default: REC_OFF aka 'off'
1759
1760 """
1761 self._check_supported()
1762 self.servo.set_nocheck('power_state', rec_mode)
1763
1764 def reset_via_servo(self):
1765 """Force the DUT to reset.
1766
1767 The DUT is guaranteed to be on at the end of this call,
1768 regardless of its previous state, provided that there is
1769 working OS software. This also guarantees that the EC has
1770 been restarted.
1771
1772 """
1773 self._check_supported()
1774 self._sync_if_up()
1775 self.servo.set_nocheck('power_state', 'reset')
1776
Simran Basid5e5e272012-09-24 15:23:59 -07001777
Garry Wang5e5538a2019-04-08 15:36:18 -07001778 def power_on(self, power_method=None):
1779 """Turn on power to this host via RPM, CCD, Servo or manual.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001780
1781 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07001782 use. By default "RPM" or "CCD" will be used based
1783 on servo type. Valid values from
1784 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001785
1786 """
1787 self._set_power('ON', power_method)
1788
1789
Garry Wang5e5538a2019-04-08 15:36:18 -07001790 def power_cycle(self, power_method=None):
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001791 """Cycle power to this host by turning it OFF, then ON.
1792
1793 @param power_method Specifies which method of power control to
Garry Wang5e5538a2019-04-08 15:36:18 -07001794 use. By default "RPM" or "CCD" will be used based
1795 on servo type. Valid values from
1796 POWER_CONTROL_VALID_ARGS, or None to use default.
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001797
1798 """
Garry Wang5e5538a2019-04-08 15:36:18 -07001799 if not power_method:
1800 power_method = self.get_default_power_method()
1801
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001802 if power_method in (self.POWER_CONTROL_SERVO,
Garry Wang5e5538a2019-04-08 15:36:18 -07001803 self.POWER_CONTROL_MANUAL,
1804 self.POWER_CONTROL_CCD):
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001805 self.power_off(power_method=power_method)
1806 time.sleep(self._POWER_CYCLE_TIMEOUT)
1807 self.power_on(power_method=power_method)
1808 else:
Garry Wangad4d4fd2019-01-30 17:00:38 -08001809 self._add_rpm_changed_tag()
Derek Beckett5ea7bf32021-08-03 13:09:20 -07001810 # TODO b/195443964: Re-wire as needed once TLW is available.
Simran Basic6f1f7a2012-10-16 10:47:46 -07001811
1812
Mary Ruthvende14a8b2019-08-23 12:43:52 -07001813 def get_platform_from_fwid(self):
1814 """Determine the platform from the crossystem fwid.
1815
1816 @returns a string representing this host's platform.
1817 """
Greg Edelstona7b05d12020-04-01 16:00:51 -06001818 # Look at the firmware for non-unibuild cases or if cros_config fails.
Mary Ruthvende14a8b2019-08-23 12:43:52 -07001819 crossystem = utils.Crossystem(self)
1820 crossystem.init()
1821 # Extract fwid value and use the leading part as the platform id.
1822 # fwid generally follow the format of {platform}.{firmware version}
1823 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
1824 platform = crossystem.fwid().split('.')[0].lower()
1825 # Newer platforms start with 'Google_' while the older ones do not.
1826 return platform.replace('google_', '')
1827
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08001828
Simran Basic6f1f7a2012-10-16 10:47:46 -07001829 def get_platform(self):
1830 """Determine the correct platform label for this host.
1831
1832 @returns a string representing this host's platform.
1833 """
C Shapiroed87c6f2018-04-19 09:13:58 -06001834 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
1835 run_method=self.run)
C Shapiroed87c6f2018-04-19 09:13:58 -06001836 platform = ''
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08001837 if release_info.get('CHROMEOS_RELEASE_UNIBUILD') == '1':
Greg Edelstona7b05d12020-04-01 16:00:51 -06001838 platform = self.get_model_from_cros_config()
Mary Ruthvende14a8b2019-08-23 12:43:52 -07001839 return platform if platform else self.get_platform_from_fwid()
Simran Basic6f1f7a2012-10-16 10:47:46 -07001840
1841
Greg Edelstona7b05d12020-04-01 16:00:51 -06001842 def get_model_from_cros_config(self):
1843 """Get the host model from cros_config command.
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08001844
Greg Edelstona7b05d12020-04-01 16:00:51 -06001845 @returns a string representing this host's model.
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08001846 """
Greg Edelstona7b05d12020-04-01 16:00:51 -06001847 return cros_config.call_cros_config_get_output('/ name',
1848 self.run, ignore_status=True)
Puthikorn Voravootivat0f7c3d82019-11-27 13:48:39 -08001849
1850
Hung-ying Tyanb1328032014-04-01 14:18:54 +08001851 def get_architecture(self):
1852 """Determine the correct architecture label for this host.
1853
1854 @returns a string representing this host's architecture.
1855 """
1856 crossystem = utils.Crossystem(self)
1857 crossystem.init()
1858 return crossystem.arch()
1859
1860
Luis Lozano40b7d0d2014-01-17 15:12:06 -08001861 def get_chrome_version(self):
1862 """Gets the Chrome version number and milestone as strings.
1863
1864 Invokes "chrome --version" to get the version number and milestone.
1865
1866 @return A tuple (chrome_ver, milestone) where "chrome_ver" is the
1867 current Chrome version number as a string (in the form "W.X.Y.Z")
1868 and "milestone" is the first component of the version number
1869 (the "W" from "W.X.Y.Z"). If the version number cannot be parsed
1870 in the "W.X.Y.Z" format, the "chrome_ver" will be the full output
1871 of "chrome --version" and the milestone will be the empty string.
1872
1873 """
MK Ryu35d661e2014-09-25 17:44:10 -07001874 version_string = self.run(client_constants.CHROME_VERSION_COMMAND).stdout
Luis Lozano40b7d0d2014-01-17 15:12:06 -08001875 return utils.parse_chrome_version(version_string)
1876
J. Richard Barnetted2af5852016-02-05 15:03:10 -08001877
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08001878 def get_ec_version(self):
1879 """Get the ec version as strings.
1880
1881 @returns a string representing this host's ec version.
1882 """
Puthikorn Voravootivat65ad7672018-01-30 14:00:01 -08001883 command = 'mosys ec info -s fw_version'
Puthikorn Voravootivat720640d2018-02-16 17:32:38 -08001884 result = self.run(command, ignore_status=True)
1885 if result.exit_status != 0:
1886 return ''
1887 return result.stdout.strip()
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08001888
1889
1890 def get_firmware_version(self):
1891 """Get the firmware version as strings.
1892
1893 @returns a string representing this host's firmware version.
1894 """
1895 crossystem = utils.Crossystem(self)
1896 crossystem.init()
1897 return crossystem.fwid()
1898
1899
Puthikorn Voravootivat58f739b2021-01-07 08:28:36 -08001900 def get_hardware_id(self):
1901 """Get hardware id as strings.
1902
1903 @returns a string representing this host's hardware id.
1904 """
1905 crossystem = utils.Crossystem(self)
1906 crossystem.init()
1907 return crossystem.hwid()
1908
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08001909 def get_hardware_revision(self):
1910 """Get the hardware revision as strings.
1911
1912 @returns a string representing this host's hardware revision.
1913 """
Puthikorn Voravootivat65ad7672018-01-30 14:00:01 -08001914 command = 'mosys platform version'
Puthikorn Voravootivat720640d2018-02-16 17:32:38 -08001915 result = self.run(command, ignore_status=True)
1916 if result.exit_status != 0:
1917 return ''
1918 return result.stdout.strip()
Puthikorn Voravootivatfd132172017-11-16 18:24:38 -08001919
1920
1921 def get_kernel_version(self):
1922 """Get the kernel version as strings.
1923
1924 @returns a string representing this host's kernel version.
1925 """
1926 return self.run('uname -r').stdout.strip()
1927
1928
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08001929 def get_cpu_name(self):
1930 """Get the cpu name as strings.
1931
1932 @returns a string representing this host's cpu name.
1933 """
1934
1935 # Try get cpu name from device tree first
1936 if self.path_exists('/proc/device-tree/compatible'):
1937 command = ' | '.join(
1938 ["sed -e 's/\\x0/\\n/g' /proc/device-tree/compatible",
1939 'tail -1'])
1940 return self.run(command).stdout.strip().replace(',', ' ')
1941
1942 # Get cpu name from uname -p
1943 command = 'uname -p'
1944 ret = self.run(command).stdout.strip()
1945
1946 # 'uname -p' return variant of unknown or amd64 or x86_64 or i686
1947 # Try get cpu name from /proc/cpuinfo instead
1948 if re.match("unknown|amd64|[ix][0-9]?86(_64)?", ret, re.IGNORECASE):
1949 command = "grep model.name /proc/cpuinfo | cut -f 2 -d: | head -1"
1950 self = self.run(command).stdout.strip()
1951
1952 # Remove bloat from CPU name, for example
1953 # Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz -> Intel Core i5-7Y57
1954 # Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz -> Intel Xeon E5-2690 v4
1955 # AMD A10-7850K APU with Radeon(TM) R7 Graphics -> AMD A10-7850K
1956 # AMD GX-212JC SOC with Radeon(TM) R2E Graphics -> AMD GX-212JC
1957 trim_re = r' (@|processor|apu|soc|radeon).*|\(.*?\)| cpu'
1958 return re.sub(trim_re, '', ret, flags=re.IGNORECASE)
1959
1960
1961 def get_screen_resolution(self):
1962 """Get the screen(s) resolution as strings.
1963 In case of more than 1 monitor, return resolution for each monitor
1964 separate with plus sign.
1965
1966 @returns a string representing this host's screen(s) resolution.
1967 """
1968 command = 'for f in /sys/class/drm/*/*/modes; do head -1 $f; done'
1969 ret = self.run(command, ignore_status=True)
1970 # We might have Chromebox without a screen
1971 if ret.exit_status != 0:
1972 return ''
1973 return ret.stdout.strip().replace('\n', '+')
1974
1975
1976 def get_mem_total_gb(self):
1977 """Get total memory available in the system in GiB (2^20).
1978
1979 @returns an integer representing total memory
1980 """
1981 mem_total_kb = self.read_from_meminfo('MemTotal')
1982 kb_in_gb = float(2 ** 20)
1983 return int(round(mem_total_kb / kb_in_gb))
1984
1985
1986 def get_disk_size_gb(self):
1987 """Get size of disk in GB (10^9)
1988
1989 @returns an integer representing size of disk, 0 in Error Case
1990 """
1991 command = 'grep $(rootdev -s -d | cut -f3 -d/)$ /proc/partitions'
1992 result = self.run(command, ignore_status=True)
1993 if result.exit_status != 0:
1994 return 0
1995 _, _, block, _ = re.split(r' +', result.stdout.strip())
1996 byte_per_block = 1024.0
1997 disk_kb_in_gb = 1e9
1998 return int(int(block) * byte_per_block / disk_kb_in_gb + 0.5)
1999
2000
2001 def get_battery_size(self):
2002 """Get size of battery in Watt-hour via sysfs
2003
2004 This method assumes that battery support voltage_min_design and
2005 charge_full_design sysfs.
2006
2007 @returns a float representing Battery size, 0 if error.
2008 """
2009 # sysfs report data in micro scale
2010 battery_scale = 1e6
2011
2012 command = 'cat /sys/class/power_supply/*/voltage_min_design'
2013 result = self.run(command, ignore_status=True)
2014 if result.exit_status != 0:
2015 return 0
2016 voltage = float(result.stdout.strip()) / battery_scale
2017
2018 command = 'cat /sys/class/power_supply/*/charge_full_design'
2019 result = self.run(command, ignore_status=True)
2020 if result.exit_status != 0:
2021 return 0
2022 amphereHour = float(result.stdout.strip()) / battery_scale
2023
2024 return voltage * amphereHour
2025
2026
2027 def get_low_battery_shutdown_percent(self):
2028 """Get the percent-based low-battery shutdown threshold.
2029
2030 @returns a float representing low-battery shutdown percent, 0 if error.
2031 """
2032 ret = 0.0
2033 try:
2034 command = 'check_powerd_config --low_battery_shutdown_percent'
2035 ret = float(self.run(command).stdout)
2036 except error.CmdError:
2037 logging.debug("Can't run %s", command)
2038 except ValueError:
2039 logging.debug("Didn't get number from %s", command)
2040
2041 return ret
2042
2043
Puthikorn Voravootivat09c83d72018-08-10 15:58:32 -07002044 def has_hammer(self):
2045 """Check whether DUT has hammer device or not.
2046
2047 @returns boolean whether device has hammer or not
2048 """
2049 command = 'grep Hammer /sys/bus/usb/devices/*/product'
2050 return self.run(command, ignore_status=True).exit_status == 0
2051
2052
Niranjan Kumar34618872017-05-31 12:57:09 -07002053 def is_chrome_switch_present(self, switch):
David Haddock3ce538e2017-06-22 13:37:05 -07002054 """Returns True if the specified switch was provided to Chrome.
2055
2056 @param switch The chrome switch to search for.
2057 """
Niranjan Kumar34618872017-05-31 12:57:09 -07002058
Niranjan Kumar5f23fe92017-06-22 15:18:55 -07002059 command = 'pgrep -x -f -c "/opt/google/chrome/chrome.*%s.*"' % switch
2060 return self.run(command, ignore_status=True).exit_status == 0
Niranjan Kumar34618872017-05-31 12:57:09 -07002061
2062
2063 def oobe_triggers_update(self):
2064 """Returns True if this host has an OOBE flow during which
2065 it will perform an update check and perhaps an update.
2066 One example of such a flow is Hands-Off Zero-Touch Enrollment.
2067 As more such flows are developed, code handling them needs
2068 to be added here.
2069
2070 @return Boolean indicating whether this host's OOBE triggers an update.
2071 """
2072 return self.is_chrome_switch_present(
2073 '--enterprise-enable-zero-touch-enrollment=hands-off')
2074
2075
Kevin Chenga2619dc2016-03-28 11:42:08 -07002076 # TODO(kevcheng): change this to just return the board without the
2077 # 'board:' prefix and fix up all the callers. Also look into removing the
2078 # need for this method.
Simran Basic6f1f7a2012-10-16 10:47:46 -07002079 def get_board(self):
2080 """Determine the correct board label for this host.
2081
2082 @returns a string representing this host's board.
2083 """
2084 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
2085 run_method=self.run)
J. Richard Barnetted2af5852016-02-05 15:03:10 -08002086 return (ds_constants.BOARD_PREFIX +
2087 release_info['CHROMEOS_RELEASE_BOARD'])
Simran Basic6f1f7a2012-10-16 10:47:46 -07002088
Po-Hsien Wang4618adf2017-10-10 14:40:21 -07002089 def get_channel(self):
2090 """Determine the correct channel label for this host.
Simran Basic6f1f7a2012-10-16 10:47:46 -07002091
Po-Hsien Wang4618adf2017-10-10 14:40:21 -07002092 @returns: a string represeting this host's build channel.
2093 (stable, dev, beta). None on fail.
2094 """
2095 return lsbrelease_utils.get_chromeos_channel(
2096 lsb_release_content=self._get_lsb_release_content())
Kevin Chenga328da62016-03-31 10:49:04 -07002097
Kevin Chenga328da62016-03-31 10:49:04 -07002098 def get_power_supply(self):
2099 """
2100 Determine what type of power supply the host has
2101
2102 @returns a string representing this host's power supply.
2103 'power:battery' when the device has a battery intended for
2104 extended use
2105 'power:AC_primary' when the device has a battery not intended
2106 for extended use (for moving the machine, etc)
2107 'power:AC_only' when the device has no battery at all.
2108 """
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002109 psu = self.run(command='cros_config /hardware-properties psu-type',
2110 ignore_status=True)
Kevin Chenga328da62016-03-31 10:49:04 -07002111 if psu.exit_status:
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002112 # Assume battery if unspecified in cros_config.
Kevin Chenga328da62016-03-31 10:49:04 -07002113 return 'power:battery'
2114
2115 psu_str = psu.stdout.strip()
2116 if psu_str == 'unknown':
2117 return None
2118
2119 return 'power:%s' % psu_str
2120
2121
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08002122 def has_battery(self):
2123 """Determine if DUT has a battery.
2124
2125 Returns:
2126 Boolean, False if known not to have battery, True otherwise.
2127 """
Jack Rosenthal01ee2cf2021-03-30 21:01:32 -06002128 return self.get_power_supply() == 'power:battery'
Puthikorn Voravootivat54aa53c2018-03-01 19:00:25 -08002129
2130
Kevin Chenga328da62016-03-31 10:49:04 -07002131 def get_servo(self):
2132 """Determine if the host has a servo attached.
2133
2134 If the host has a working servo attached, it should have a servo label.
2135
2136 @return: string 'servo' if the host has servo attached. Otherwise,
2137 returns None.
2138 """
2139 return 'servo' if self._servo_host else None
2140
2141
Kevin Chenga328da62016-03-31 10:49:04 -07002142 def has_internal_display(self):
2143 """Determine if the device under test is equipped with an internal
2144 display.
2145
2146 @return: 'internal_display' if one is present; None otherwise.
2147 """
2148 from autotest_lib.client.cros.graphics import graphics_utils
2149 from autotest_lib.client.common_lib import utils as common_utils
2150
2151 def __system_output(cmd):
2152 return self.run(cmd).stdout
2153
2154 def __read_file(remote_path):
2155 return self.run('cat %s' % remote_path).stdout
2156
2157 # Hijack the necessary client functions so that we can take advantage
2158 # of the client lib here.
2159 # FIXME: find a less hacky way than this
2160 original_system_output = utils.system_output
2161 original_read_file = common_utils.read_file
2162 utils.system_output = __system_output
2163 common_utils.read_file = __read_file
2164 try:
2165 return ('internal_display' if graphics_utils.has_internal_display()
2166 else None)
2167 finally:
2168 utils.system_output = original_system_output
2169 common_utils.read_file = original_read_file
2170
2171
Dan Shi85276d42014-04-08 22:11:45 -07002172 def is_boot_from_usb(self):
2173 """Check if DUT is boot from USB.
2174
2175 @return: True if DUT is boot from usb.
2176 """
2177 device = self.run('rootdev -s -d').stdout.strip()
2178 removable = int(self.run('cat /sys/block/%s/removable' %
2179 os.path.basename(device)).stdout.strip())
2180 return removable == 1
Helen Zhang17dae2b2014-11-11 09:25:52 -08002181
Otabek Kasimov77a40332020-10-20 15:40:03 -07002182 def is_boot_from_external_device(self):
2183 """Check if DUT is boot from external storage.
2184
2185 @return: True if DUT is boot from external storage.
2186 """
2187 boot_device = self.run('rootdev -s -d', ignore_status=True,
2188 timeout=60).stdout.strip()
2189 if not boot_device:
2190 logging.debug('Boot storage not detected on the host.')
2191 return False
2192 main_storage_cmd = ('. /usr/sbin/write_gpt.sh;'
2193 ' . /usr/share/misc/chromeos-common.sh;'
2194 ' load_base_vars; get_fixed_dst_drive')
2195 main_storage = self.run(main_storage_cmd,
2196 ignore_status=True,
2197 timeout=60).stdout.strip()
Otabek Kasimov723e8562020-12-08 13:29:34 -08002198 if not main_storage or boot_device != main_storage:
2199 logging.debug('Device booted from external storage storage.')
2200 return True
2201 logging.debug('Device booted from main storage.')
2202 return False
Helen Zhang17dae2b2014-11-11 09:25:52 -08002203
2204 def read_from_meminfo(self, key):
Dan Shi49ca0932014-11-14 11:22:27 -08002205 """Return the memory info from /proc/meminfo
Helen Zhang17dae2b2014-11-11 09:25:52 -08002206
2207 @param key: meminfo requested
2208
2209 @return the memory value as a string
2210
2211 """
Helen Zhang17dae2b2014-11-11 09:25:52 -08002212 meminfo = self.run('grep %s /proc/meminfo' % key).stdout.strip()
2213 logging.debug('%s', meminfo)
2214 return int(re.search(r'\d+', meminfo).group(0))
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002215
2216
Rohit Makasana98e696f2016-06-03 18:48:10 -07002217 def get_cpu_arch(self):
2218 """Returns CPU arch of the device.
2219
2220 @return CPU architecture of the DUT.
2221 """
Allen Li2c32d6b2017-02-03 15:28:10 -08002222 # Add CPUs by following logic in client/bin/utils.py.
Rohit Makasana98e696f2016-06-03 18:48:10 -07002223 if self.run("grep '^flags.*:.* lm .*' /proc/cpuinfo",
2224 ignore_status=True).stdout:
2225 return 'x86_64'
2226 if self.run("grep -Ei 'ARM|CPU implementer' /proc/cpuinfo",
2227 ignore_status=True).stdout:
2228 return 'arm'
2229 return 'i386'
2230
2231
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002232 def get_board_type(self):
2233 """
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002234 Get the DUT's device type / form factor from cros_config. It can be one
2235 of CHROMEBOX, CHROMEBASE, CHROMEBOOK, or CHROMEBIT.
Danny Chan471a8d12015-08-18 14:57:41 -07002236
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002237 @return form factor value from cros_config.
Rohit Makasana8a4923c2015-08-13 17:04:26 -07002238 """
Nikolai Artemiev5b5b6802021-05-12 12:56:43 +10002239
2240 device_type = self.run('cros_config /hardware-properties form-factor',
2241 ignore_status=True).stdout
2242 if device_type:
2243 return device_type
2244
2245 # TODO: remove lsb-release fallback once cros_config works everywhere
Danny Chan471a8d12015-08-18 14:57:41 -07002246 device_type = self.run('grep DEVICETYPE /etc/lsb-release',
2247 ignore_status=True).stdout
2248 if device_type:
Kalin Stoyanov524310b2015-08-21 16:24:04 -07002249 return device_type.split('=')[-1].strip()
Danny Chan471a8d12015-08-18 14:57:41 -07002250 return ''
Gilad Arnolda76bef02015-09-29 13:55:15 -07002251
2252
Rohit Makasanadf0a3a32017-06-30 13:55:18 -07002253 def get_arc_version(self):
2254 """Return ARC version installed on the DUT.
2255
2256 @returns ARC version as string if the CrOS build has ARC, else None.
2257 """
2258 arc_version = self.run('grep CHROMEOS_ARC_VERSION /etc/lsb-release',
2259 ignore_status=True).stdout
2260 if arc_version:
2261 return arc_version.split('=')[-1].strip()
2262 return None
2263
2264
Gilad Arnolda76bef02015-09-29 13:55:15 -07002265 def get_os_type(self):
2266 return 'cros'
Simran Basia5522a32015-10-06 11:01:24 -07002267
2268
Kevin Chenga2619dc2016-03-28 11:42:08 -07002269 def get_labels(self):
Kevin Chengf8660142016-08-12 10:17:41 -07002270 """Return the detected labels on the host."""
Kevin Chenga2619dc2016-03-28 11:42:08 -07002271 return self.labels.get_labels(self)
Garry Wang5e5538a2019-04-08 15:36:18 -07002272
2273
2274 def get_default_power_method(self):
2275 """
2276 Get the default power method for power_on/off/cycle() methods.
2277 @return POWER_CONTROL_RPM or POWER_CONTROL_CCD
2278 """
2279 if not self._default_power_method:
Garry Wang1a004aa2019-05-16 22:56:51 -07002280 self._default_power_method = self.POWER_CONTROL_RPM
Ruben Rodriguez Buchillon3eeeab32019-10-02 15:29:58 -07002281 if self.servo and self.servo.supports_built_in_pd_control():
2282 self._default_power_method = self.POWER_CONTROL_CCD
2283 else:
2284 logging.debug('Either servo is unitialized or the servo '
2285 'setup does not support pd controls. Falling '
2286 'back to default RPM method.')
Garry Wang5e5538a2019-04-08 15:36:18 -07002287 return self._default_power_method
Puthikorn Voravootivat4a054792019-12-13 16:44:17 -08002288
2289
2290 def find_usb_devices(self, idVendor, idProduct):
2291 """
2292 Get usb device sysfs name for specific device.
2293
2294 @param idVendor Vendor ID to search in sysfs directory.
2295 @param idProduct Product ID to search in sysfs directory.
2296
2297 @return Usb node names in /sys/bus/usb/drivers/usb/ that match.
2298 """
2299 # Look for matching file and cut at position 7 to get dir name.
2300 grep_cmd = 'grep {} /sys/bus/usb/drivers/usb/*/{} | cut -f 7 -d /'
2301
2302 vendor_cmd = grep_cmd.format(idVendor, 'idVendor')
2303 product_cmd = grep_cmd.format(idProduct, 'idProduct')
2304
2305 # Use uniq -d to print duplicate line from both command
2306 cmd = 'sort <({}) <({}) | uniq -d'.format(vendor_cmd, product_cmd)
2307
2308 return self.run(cmd, ignore_status=True).stdout.strip().split('\n')
2309
2310
2311 def bind_usb_device(self, usb_node):
2312 """
2313 Bind usb device
2314
2315 @param usb_node Node name in /sys/bus/usb/drivers/usb/
2316 """
2317 cmd = 'echo {} > /sys/bus/usb/drivers/usb/bind'.format(usb_node)
2318 self.run(cmd, ignore_status=True)
2319
2320
2321 def unbind_usb_device(self, usb_node):
2322 """
2323 Unbind usb device
2324
2325 @param usb_node Node name in /sys/bus/usb/drivers/usb/
2326 """
2327 cmd = 'echo {} > /sys/bus/usb/drivers/usb/unbind'.format(usb_node)
2328 self.run(cmd, ignore_status=True)
2329
2330
2331 def get_wlan_ip(self):
2332 """
2333 Get ip address of wlan interface.
2334
2335 @return ip address of wlan or empty string if wlan is not connected.
2336 """
2337 cmds = [
2338 'iw dev', # List wlan physical device
2339 'grep Interface', # Grep only interface name
2340 'cut -f 2 -d" "', # Cut the name part
2341 'xargs ifconfig', # Feed it to ifconfig to get ip
2342 'grep -oE "inet [0-9.]+"', # Grep only ipv4
2343 'cut -f 2 -d " "' # Cut the ip part
2344 ]
2345 return self.run(' | '.join(cmds), ignore_status=True).stdout.strip()
Puthikorn Voravootivatcd0dc9e2020-01-22 14:22:22 -08002346
2347 def connect_to_wifi(self, ssid, passphrase=None, security=None):
2348 """
2349 Connect to wifi network
2350
2351 @param ssid SSID of the wifi network.
2352 @param passphrase Passphrase of the wifi network. None if not existed.
2353 @param security Security of the wifi network. Default to "psk" if
2354 passphase is given without security. Possible values
2355 are "none", "psk", "802_1x".
2356
2357 @return True if succeed, False if not.
2358 """
2359 cmd = '/usr/local/autotest/cros/scripts/wifi connect ' + ssid
2360 if passphrase:
2361 cmd += ' ' + passphrase
2362 if security:
2363 cmd += ' ' + security
2364 return self.run(cmd, ignore_status=True).exit_status == 0
Otabek Kasimov6825b762020-06-23 23:42:44 -07002365
2366 def get_device_repair_state(self):
2367 """Get device repair state"""
2368 return self._device_repair_state
2369
Otabek Kasimov42506d02020-07-29 14:44:57 -07002370 def is_file_system_writable(self, testdirs=None):
2371 """Check is the file systems are writable.
2372
2373 The standard linux response to certain unexpected file system errors
2374 (including hardware errors in block devices) is to change the file
2375 system status to read-only. This checks that that hasn't happened.
2376
2377 @param testdirs: List of directories to check. If no data provided
2378 then '/mnt/stateful_partition' and '/var/tmp'
2379 directories will be checked.
2380
2381 @returns boolean whether file-system writable.
2382 """
2383 def _check_dir(testdir):
2384 # check if we can create a file
2385 filename = os.path.join(testdir, 'writable_my_test_file')
2386 command = 'touch %s && rm %s' % (filename, filename)
2387 rv = self.run(command=command,
2388 timeout=30,
2389 ignore_status=True)
2390 is_writable = rv.exit_status == 0
2391 if not is_writable:
2392 logging.info('Cannot create a file in "%s"!'
2393 ' Probably the FS is read-only', testdir)
2394 logging.info("FileSystem is not writable!")
2395 return False
2396 return True
2397
2398 if not testdirs or len(testdirs) == 0:
2399 # N.B. Order matters here: Encrypted stateful is loop-mounted
2400 # from a file in unencrypted stateful, so we don't test for
2401 # errors in encrypted stateful if unencrypted fails.
2402 testdirs = ['/mnt/stateful_partition', '/var/tmp']
2403
2404 for dir in testdirs:
2405 # loop will be stopped if any directory fill fail the check
2406 try:
2407 if not _check_dir(dir):
2408 return False
2409 except Exception as e:
2410 # here expected only timeout error, all other will
2411 # be catch by 'ignore_status=True'
2412 logging.debug('Fail to check %s to write in it', dir)
2413 return False
2414 return True
Garry Wang1a493d82020-08-31 21:01:19 -07002415
Dana Goyettec172b172020-07-29 16:26:15 -07002416 def blocking_sync(self, freeze_for_reset=False):
2417 """Sync root device and internal device, via script.
2418
2419 The actual calls end up logged by the run() call, since they're printed
2420 to stdout/stderr in the script.
2421
2422 @param freeze_for_reset: if True, prepare for reset by blocking writes
2423 (only if enable_fs_sync_fsfreeze=True)
2424 """
2425
2426 if freeze_for_reset and self.USE_FSFREEZE:
2427 logging.info('Blocking sync and freeze')
2428 elif freeze_for_reset:
2429 logging.info('Blocking sync for reset')
2430 else:
2431 logging.info('Blocking sync')
2432
2433 # client/bin is installed on the DUT as /usr/local/autotest/bin
2434 sync_cmd = '/usr/local/autotest/bin/fs_sync.py'
2435 if freeze_for_reset and self.USE_FSFREEZE:
2436 sync_cmd += ' --freeze'
2437 return self.run(sync_cmd)
2438
Garry Wanga2e78172020-09-09 23:49:07 -07002439 def set_health_profile_dut_state(self, state):
2440 if not self.health_profile:
2441 logging.debug('Device health profile is not initialized, skip'
2442 ' set dut state.')
2443 return
2444 reset_counters = state in profile_constants.STATES_NEED_RESET_COUNTER
2445 self.health_profile.update_dut_state(state, reset_counters)
Garry Wang53fc8f32020-09-18 13:30:08 -07002446
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07002447 def _set_servo_topology(self):
2448 """Set servo-topology info to the host-info."""
2449 logging.debug('Try to save servo topology to host-info.')
2450 if not self._servo_host:
Greg Edelstonff2665d2021-04-21 14:32:27 -06002451 logging.debug('Servo host is not initialized.')
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08002452 return
2453 if not self.is_servo_in_working_state():
2454 logging.debug('Is servo is not in working state then'
2455 ' update topology is not allowed.')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07002456 return
2457 if not self._servo_host.is_servo_topology_supported():
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08002458 logging.debug('Servo-topology is not supported.')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07002459 return
2460 servo_topology = self._servo_host.get_topology()
2461 if not servo_topology or servo_topology.is_empty():
Otabek Kasimovfe41e2d2021-02-14 20:48:52 -08002462 logging.debug('Servo topology is empty')
Otabek Kasimov382c3bb2020-10-28 13:22:45 -07002463 return
2464 servo_topology.save(self.host_info_store)