blob: 1396ef7fd5ca507f47c83bbc030580d858c2de36 [file] [log] [blame]
J. Richard Barnette24adbf42012-04-11 15:04:53 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtisaa5eedb2011-08-23 16:18:52 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Aviv Keshet74c89a92013-02-04 15:18:30 -08005import functools
J. Richard Barnette1d78b012012-05-15 13:56:30 -07006import logging
Simran Basid5e5e272012-09-24 15:23:59 -07007import re
Christopher Wileyd78249a2013-03-01 13:05:31 -08008import socket
J. Richard Barnette1d78b012012-05-15 13:56:30 -07009import subprocess
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070010import time
J. Richard Barnette1d78b012012-05-15 13:56:30 -070011import xmlrpclib
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070012
J. Richard Barnette45e93de2012-04-11 17:24:15 -070013from autotest_lib.client.bin import utils
Richard Barnette0c73ffc2012-11-19 15:21:18 -080014from autotest_lib.client.common_lib import error
15from autotest_lib.client.common_lib import global_config
J. Richard Barnette45e93de2012-04-11 17:24:15 -070016from autotest_lib.client.common_lib.cros import autoupdater
Richard Barnette03a0c132012-11-05 12:40:35 -080017from autotest_lib.client.common_lib.cros import dev_server
Christopher Wileyd78249a2013-03-01 13:05:31 -080018from autotest_lib.client.common_lib.cros import retry
Richard Barnette82c35912012-11-20 10:09:10 -080019from autotest_lib.client.cros import constants
J. Richard Barnette45e93de2012-04-11 17:24:15 -070020from autotest_lib.server import autoserv_parser
Chris Sosaf4d43ff2012-10-30 11:21:05 -070021from autotest_lib.server import autotest
J. Richard Barnette45e93de2012-04-11 17:24:15 -070022from autotest_lib.server import site_host_attributes
J. Richard Barnette67ccb872012-04-19 16:34:56 -070023from autotest_lib.server.cros import servo
Scott Zawalski89c44dd2013-02-26 09:28:02 -050024from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
25from autotest_lib.server.cros.dynamic_suite import tools
J. Richard Barnette45e93de2012-04-11 17:24:15 -070026from autotest_lib.server.hosts import remote
Simran Basidcff4252012-11-20 16:13:20 -080027from autotest_lib.site_utils.rpm_control_system import rpm_client
Simran Basid5e5e272012-09-24 15:23:59 -070028
Richard Barnette82c35912012-11-20 10:09:10 -080029# Importing frontend.afe.models requires a full Autotest
30# installation (with the Django modules), not just the source
31# repository. Most developers won't have the full installation, so
32# the imports below will fail for them.
33#
34# The fix is to catch import exceptions, and set `models` to `None`
35# on failure. This has the side effect that
36# SiteHost._get_board_from_afe() will fail: That will manifest as
37# failures during Repair jobs leaving the DUT as "Repair Failed".
38# In practice, you can't test Repair jobs without a full
39# installation, so that kind of failure isn't expected.
40try:
J. Richard Barnette7214e0b2013-02-06 15:20:49 -080041 # pylint: disable=W0611
Richard Barnette82c35912012-11-20 10:09:10 -080042 from autotest_lib.frontend import setup_django_environment
43 from autotest_lib.frontend.afe import models
44except:
45 models = None
46
Simran Basid5e5e272012-09-24 15:23:59 -070047
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080048def _make_servo_hostname(hostname):
49 host_parts = hostname.split('.')
50 host_parts[0] = host_parts[0] + '-servo'
51 return '.'.join(host_parts)
52
53
54def _get_lab_servo(target_hostname):
55 """Instantiate a Servo for |target_hostname| in the lab.
56
57 Assuming that |target_hostname| is a device in the CrOS test
58 lab, create and return a Servo object pointed at the servo
59 attached to that DUT. The servo in the test lab is assumed
60 to already have servod up and running on it.
61
62 @param target_hostname: device whose servo we want to target.
63 @return an appropriately configured Servo instance.
64 """
65 servo_host = _make_servo_hostname(target_hostname)
66 if utils.host_is_in_lab_zone(servo_host):
67 try:
J. Richard Barnetted5f807a2013-02-11 16:51:00 -080068 return servo.Servo(servo_host=servo_host)
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080069 except: # pylint: disable=W0702
70 # TODO(jrbarnette): Long-term, if we can't get to
71 # a servo in the lab, we want to fail, so we should
72 # pass any exceptions along. Short-term, we're not
73 # ready to rely on servo, so we ignore failures.
74 pass
75 return None
76
77
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070078def make_ssh_command(user='root', port=22, opts='', hosts_file=None,
79 connect_timeout=None, alive_interval=None):
80 """Override default make_ssh_command to use options tuned for Chrome OS.
81
82 Tuning changes:
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070083 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH connection
84 failure. Consistency with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070085
Dale Curtisaa5eedb2011-08-23 16:18:52 -070086 - ServerAliveInterval=180; which causes SSH to ping connection every
87 180 seconds. In conjunction with ServerAliveCountMax ensures that if the
88 connection dies, Autotest will bail out quickly. Originally tried 60 secs,
89 but saw frequent job ABORTS where the test completed successfully.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070090
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070091 - ServerAliveCountMax=3; consistency with remote_access.sh.
92
93 - ConnectAttempts=4; reduce flakiness in connection errors; consistency
94 with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070095
96 - UserKnownHostsFile=/dev/null; we don't care about the keys. Host keys
97 change with every new installation, don't waste memory/space saving them.
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070098
99 - SSH protocol forced to 2; needed for ServerAliveInterval.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800100
101 @param user User name to use for the ssh connection.
102 @param port Port on the target host to use for ssh connection.
103 @param opts Additional options to the ssh command.
104 @param hosts_file Ignored.
105 @param connect_timeout Ignored.
106 @param alive_interval Ignored.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -0700107 """
108 base_command = ('/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no'
109 ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes'
Chris Sosaf7fcd6e2011-09-27 17:30:47 -0700110 ' -o ConnectTimeout=30 -o ServerAliveInterval=180'
111 ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4'
112 ' -o Protocol=2 -l %s -p %d')
Dale Curtiscb7bfaf2011-06-07 16:21:57 -0700113 return base_command % (opts, user, port)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700114
115
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800116
Aviv Keshet74c89a92013-02-04 15:18:30 -0800117def add_label_detector(label_function_list, label_list=None, label=None):
118 """Decorator used to group functions together into the provided list.
119 @param label_function_list: List of label detecting functions to add
120 decorated function to.
121 @param label_list: List of detectable labels to add detectable labels to.
122 (Default: None)
123 @param label: Label string that is detectable by this detection function
124 (Default: None)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800125 """
Simran Basic6f1f7a2012-10-16 10:47:46 -0700126 def add_func(func):
Aviv Keshet74c89a92013-02-04 15:18:30 -0800127 """
128 @param func: The function to be added as a detector.
129 """
130 label_function_list.append(func)
131 if label and label_list is not None:
132 label_list.append(label)
Simran Basic6f1f7a2012-10-16 10:47:46 -0700133 return func
134 return add_func
135
136
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700137class SiteHost(remote.RemoteHost):
138 """Chromium OS specific subclass of Host."""
139
140 _parser = autoserv_parser.autoserv_parser
141
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800142 # Time to wait for new kernel to be marked successful after
143 # auto update.
Chris Masone163cead2012-05-16 11:49:48 -0700144 _KERNEL_UPDATE_TIMEOUT = 120
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700145
Richard Barnette03a0c132012-11-05 12:40:35 -0800146 # Timeout values (in seconds) associated with various Chrome OS
147 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700148 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800149 # In general, a good rule of thumb is that the timeout can be up
150 # to twice the typical measured value on the slowest platform.
151 # The times here have not necessarily been empirically tested to
152 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700153 #
154 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800155 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
156 # time to restart the netwowrk.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700157 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800158 # other things, this must account for the 30 second dev-mode
159 # screen delay and time to start the network,
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700160 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800161 # including the 30 second dev-mode delay and time to start the
162 # network,
163 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700164 # REBOOT_TIMEOUT: Combination of shutdown and reboot times.
Richard Barnette03a0c132012-11-05 12:40:35 -0800165 # _INSTALL_TIMEOUT: Time to allow for chromeos-install.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700166
167 SLEEP_TIMEOUT = 2
168 RESUME_TIMEOUT = 5
169 BOOT_TIMEOUT = 45
170 USB_BOOT_TIMEOUT = 150
171 SHUTDOWN_TIMEOUT = 5
172 REBOOT_TIMEOUT = SHUTDOWN_TIMEOUT + BOOT_TIMEOUT
Richard Barnette03a0c132012-11-05 12:40:35 -0800173 _INSTALL_TIMEOUT = 240
174
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800175 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
176 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
177 _USB_POWER_TIMEOUT = 5
178 _POWER_CYCLE_TIMEOUT = 10
179
Richard Barnette03a0c132012-11-05 12:40:35 -0800180 _DEFAULT_SERVO_URL_FORMAT = ('/static/servo-images/'
181 '%(board)s_test_image.bin')
182
183 # TODO(jrbarnette): Servo repair is restricted to x86-alex,
184 # because the existing servo client code won't work on other
185 # boards. http://crosbug.com/36973
186 _SERVO_REPAIR_WHITELIST = [ 'x86-alex' ]
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800187
188
Richard Barnette82c35912012-11-20 10:09:10 -0800189 _RPM_RECOVERY_BOARDS = global_config.global_config.get_config_value('CROS',
190 'rpm_recovery_boards', type=str).split(',')
191
192 _MAX_POWER_CYCLE_ATTEMPTS = 6
193 _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
194 _RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-'
195 'host[0-9]+')
196 _LIGHTSENSOR_FILES = ['in_illuminance0_input',
197 'in_illuminance0_raw',
198 'illuminance0_input']
199 _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices'
200 _LABEL_FUNCTIONS = []
Aviv Keshet74c89a92013-02-04 15:18:30 -0800201 _DETECTABLE_LABELS = []
202 label_decorator = functools.partial(add_label_detector, _LABEL_FUNCTIONS,
203 _DETECTABLE_LABELS)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700204
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800205 # Constants used in ping_wait_up() and ping_wait_down().
206 #
207 # _PING_WAIT_COUNT is the approximate number of polling
208 # cycles to use when waiting for a host state change.
209 #
210 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
211 # for arguments to the internal _ping_wait_for_status()
212 # method.
213 _PING_WAIT_COUNT = 40
214 _PING_STATUS_DOWN = False
215 _PING_STATUS_UP = True
216
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800217 # Allowed values for the power_method argument.
218
219 # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods.
220 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
221 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
222 POWER_CONTROL_RPM = 'RPM'
223 POWER_CONTROL_SERVO = 'servoj10'
224 POWER_CONTROL_MANUAL = 'manual'
225
226 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
227 POWER_CONTROL_SERVO,
228 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800229
J. Richard Barnette964fba02012-10-24 17:34:29 -0700230 @staticmethod
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800231 def get_servo_arguments(args_dict):
232 """Extract servo options from `args_dict` and return the result.
233
234 Take the provided dictionary of argument options and return
235 a subset that represent standard arguments needed to
236 construct a servo object for a host. The intent is to
237 provide standard argument processing from run_remote_tests
238 for tests that require a servo to operate.
239
240 Recommended usage:
241 ~~~~~~~~
242 args_dict = utils.args_to_dict(args)
243 servo_args = hosts.SiteHost.get_servo_arguments(args_dict)
244 host = hosts.create_host(machine, servo_args=servo_args)
245 ~~~~~~~~
246
247 @param args_dict Dictionary from which to extract the servo
248 arguments.
249 """
J. Richard Barnette964fba02012-10-24 17:34:29 -0700250 servo_args = {}
251 for arg in ('servo_host', 'servo_port'):
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800252 if arg in args_dict:
253 servo_args[arg] = args_dict[arg]
J. Richard Barnette964fba02012-10-24 17:34:29 -0700254 return servo_args
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700255
J. Richard Barnette964fba02012-10-24 17:34:29 -0700256
257 def _initialize(self, hostname, servo_args=None, *args, **dargs):
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700258 """Initialize superclasses, and |self.servo|.
259
260 For creating the host servo object, there are three
261 possibilities: First, if the host is a lab system known to
262 have a servo board, we connect to that servo unconditionally.
263 Second, if we're called from a control file that requires
J. Richard Barnette55fb8062012-05-23 10:29:31 -0700264 servo features for testing, it will pass settings for
265 `servo_host`, `servo_port`, or both. If neither of these
266 cases apply, `self.servo` will be `None`.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700267
268 """
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700269 super(SiteHost, self)._initialize(hostname=hostname,
270 *args, **dargs)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700271 # self.env is a dictionary of environment variable settings
272 # to be exported for commands run on the host.
273 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
274 # errors that might happen.
275 self.env['LIBC_FATAL_STDERR_'] = '1'
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700276 self._xmlrpc_proxy_map = {}
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -0800277 self.servo = _get_lab_servo(hostname)
J. Richard Barnettead7da482012-10-30 16:46:52 -0700278 if not self.servo and servo_args is not None:
J. Richard Barnette964fba02012-10-24 17:34:29 -0700279 self.servo = servo.Servo(**servo_args)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700280
281
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500282 def get_repair_image_name(self):
283 """Generate a image_name from variables in the global config.
284
285 @returns a str of $board-version/$BUILD.
286
287 """
288 stable_version = global_config.global_config.get_config_value(
289 'CROS', 'stable_cros_version')
290 build_pattern = global_config.global_config.get_config_value(
291 'CROS', 'stable_build_pattern')
292 board = self._get_board_from_afe()
293 if board is None:
294 raise error.AutoservError('DUT has no board attribute, '
295 'cannot be repaired.')
296 return build_pattern % (board, stable_version)
297
298
299 def clear_cros_version_labels_and_job_repo_url(self):
300 """Clear cros_version labels and host attribute job_repo_url."""
301 host_model = models.Host.objects.get(hostname=self.hostname)
302 for label in host_model.labels.iterator():
303 if not label.name.startswith(ds_constants.VERSION_PREFIX):
304 continue
305 label = models.Label.smart_get(label.name)
306 label.host_set.remove(host_model)
307
308 host_model.set_or_delete_attribute('job_repo_url', None)
309
310
Chris Sosaa3ac2152012-05-23 22:23:13 -0700311 def machine_install(self, update_url=None, force_update=False,
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500312 local_devserver=False, repair=False):
313 """Install the DUT.
314
315 @param update_url: The url to use for the update
316 pattern: http://$devserver:###/update/$build
317 If update_url is None and repair is True we will install the
318 stable image listed in global_config under
319 CROS.stable_cros_version.
320 @param force_update: Force an update even if the version installed
321 is the same. Default:False
322 @param local_devserver: Used by run_remote_test to allow people to
323 use their local devserver. Default: False
324 @param repair: Whether or not we are in repair mode. This adds special
325 cases for repairing a machine like starting update_engine.
326 Setting repair to True sets force_update to True as well.
327 default: False
328 @raises autoupdater.ChromiumOSError
329
330 """
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700331 if not update_url and self._parser.options.image:
332 update_url = self._parser.options.image
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500333 elif not update_url and not repair:
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700334 raise autoupdater.ChromiumOSError(
335 'Update failed. No update URL provided.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500336 elif not update_url and repair:
337 image_name = self.get_repair_image_name()
338 devserver = dev_server.ImageServer.resolve(image_name)
339 logging.info('Staging repair build: %s', image_name)
340 devserver.trigger_download(image_name, synchronous=False)
341 self.clear_cros_version_labels_and_job_repo_url()
342 update_url = tools.image_url_pattern() % (devserver.url(),
343 image_name)
344
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700345
Chris Sosafab08082013-01-04 15:21:20 -0800346 # In case the system is in a bad state, we always reboot the machine
347 # before machine_install.
348 self.reboot(timeout=60, wait=True)
349
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500350 if repair:
351 self.run('stop update-engine; start update-engine')
352 force_update = True
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700353 # Attempt to update the system.
Chris Sosaa3ac2152012-05-23 22:23:13 -0700354 updater = autoupdater.ChromiumOSUpdater(update_url, host=self,
355 local_devserver=local_devserver)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700356 if updater.run_update(force_update):
357 # Figure out active and inactive kernel.
358 active_kernel, inactive_kernel = updater.get_kernel_state()
359
360 # Ensure inactive kernel has higher priority than active.
361 if (updater.get_kernel_priority(inactive_kernel)
362 < updater.get_kernel_priority(active_kernel)):
363 raise autoupdater.ChromiumOSError(
364 'Update failed. The priority of the inactive kernel'
365 ' partition is less than that of the active kernel'
366 ' partition.')
367
Scott Zawalski21902002012-09-19 17:57:00 -0400368 update_engine_log = '/var/log/update_engine.log'
369 logging.info('Dumping %s', update_engine_log)
370 self.run('cat %s' % update_engine_log)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800371 # Updater has returned successfully; reboot the host.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700372 self.reboot(timeout=60, wait=True)
Chris Sosae146ed82012-09-19 17:58:36 -0700373 # Touch the lab machine file to leave a marker that distinguishes
374 # this image from other test images.
Richard Barnette82c35912012-11-20 10:09:10 -0800375 self.run('touch %s' % self._LAB_MACHINE_FILE)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700376
377 # Following the reboot, verify the correct version.
378 updater.check_version()
379
380 # Figure out newly active kernel.
381 new_active_kernel, _ = updater.get_kernel_state()
382
383 # Ensure that previously inactive kernel is now the active kernel.
384 if new_active_kernel != inactive_kernel:
385 raise autoupdater.ChromiumOSError(
386 'Update failed. New kernel partition is not active after'
387 ' boot.')
388
389 host_attributes = site_host_attributes.HostAttributes(self.hostname)
390 if host_attributes.has_chromeos_firmware:
391 # Wait until tries == 0 and success, or until timeout.
392 utils.poll_for_condition(
393 lambda: (updater.get_kernel_tries(new_active_kernel) == 0
394 and updater.get_kernel_success(new_active_kernel)),
395 exception=autoupdater.ChromiumOSError(
396 'Update failed. Timed out waiting for system to mark'
397 ' new kernel as successful.'),
398 timeout=self._KERNEL_UPDATE_TIMEOUT, sleep_interval=5)
399
Simran Basi13fa1ba2013-03-04 10:56:47 -0800400 # Kick off the autoreboot script as the _LAB_MACHINE_FILE was
401 # missing on the first boot.
402 self.run('start autoreboot')
403
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700404 # Clean up any old autotest directories which may be lying around.
405 for path in global_config.global_config.get_config_value(
406 'AUTOSERV', 'client_autodir_paths', type=list):
407 self.run('rm -rf ' + path)
408
409
Richard Barnette82c35912012-11-20 10:09:10 -0800410 def _get_board_from_afe(self):
411 """Retrieve this host's board from its labels in the AFE.
412
413 Looks for a host label of the form "board:<board>", and
414 returns the "<board>" part of the label. `None` is returned
415 if there is not a single, unique label matching the pattern.
416
417 @returns board from label, or `None`.
418 """
419 host_model = models.Host.objects.get(hostname=self.hostname)
420 board_labels = filter(lambda l: l.name.startswith('board:'),
421 host_model.labels.all())
422 board_name = None
423 if len(board_labels) == 1:
424 board_name = board_labels[0].name.split(':', 1)[1]
425 elif len(board_labels) == 0:
426 logging.error('Host %s does not have a board label.',
427 self.hostname)
428 else:
429 logging.error('Host %s has multiple board labels.',
430 self.hostname)
431 return board_name
432
433
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500434 def _install_repair(self):
435 """Attempt to repair this host using upate-engine.
436
437 If the host is up, try installing the DUT with a stable
438 "repair" version of Chrome OS as defined in the global_config
439 under CROS.stable_cros_version.
440
441 @returns True if successful, False if update_engine failed.
442
443 """
444 if not self.is_up():
445 return False
446
447 logging.info('Attempting to reimage machine to repair image.')
448 try:
449 self.machine_install(repair=True)
450 except autoupdater.ChromiumOSError:
451 logging.info('Repair via install failed.')
452 return False
453
454 return True
455
456
Richard Barnette03a0c132012-11-05 12:40:35 -0800457 def _servo_repair(self, board):
458 """Attempt to repair this host using an attached Servo.
459
460 Re-install the OS on the DUT by 1) installing a test image
461 on a USB storage device attached to the Servo board,
462 2) booting that image in recovery mode, and then
463 3) installing the image.
464
465 """
466 server = dev_server.ImageServer.devserver_url_for_servo(board)
467 image = server + (self._DEFAULT_SERVO_URL_FORMAT %
468 { 'board': board })
469 self.servo.install_recovery_image(image)
470 if not self.wait_up(timeout=self.USB_BOOT_TIMEOUT):
471 raise error.AutoservError('DUT failed to boot from USB'
472 ' after %d seconds' %
473 self.USB_BOOT_TIMEOUT)
474 self.run('chromeos-install --yes',
475 timeout=self._INSTALL_TIMEOUT)
476 self.servo.power_long_press()
477 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
478 self.servo.power_short_press()
479 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
480 raise error.AutoservError('DUT failed to reboot installed '
481 'test image after %d seconds' %
482 self.BOOT_TIMEOUT)
483
484
Richard Barnette82c35912012-11-20 10:09:10 -0800485 def _powercycle_to_repair(self):
486 """Utilize the RPM Infrastructure to bring the host back up.
487
488 If the host is not up/repaired after the first powercycle we utilize
489 auto fallback to the last good install by powercycling and rebooting the
490 host 6 times.
491 """
492 logging.info('Attempting repair via RPM powercycle.')
493 failed_cycles = 0
494 self.power_cycle()
495 while not self.wait_up(timeout=self.BOOT_TIMEOUT):
496 failed_cycles += 1
497 if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS:
498 raise error.AutoservError('Powercycled host %s %d times; '
499 'device did not come back online.' %
500 (self.hostname, failed_cycles))
501 self.power_cycle()
502 if failed_cycles == 0:
503 logging.info('Powercycling was successful first time.')
504 else:
505 logging.info('Powercycling was successful after %d failures.',
506 failed_cycles)
507
508
509 def repair_full(self):
510 """Repair a host for repair level NO_PROTECTION.
511
512 This overrides the base class function for repair; it does
513 not call back to the parent class, but instead offers a
514 simplified implementation based on the capabilities in the
515 Chrome OS test lab.
516
517 Repair follows this sequence:
518 1. If the DUT passes `self.verify()`, do nothing.
519 2. If the DUT can be power-cycled via RPM, try to repair
520 by power-cycling.
521
522 As with the parent method, the last operation performed on
523 the DUT must be to call `self.verify()`; if that call fails,
524 the exception it raises is passed back to the caller.
525 """
526 try:
527 self.verify()
528 except:
529 host_board = self._get_board_from_afe()
Richard Barnette03a0c132012-11-05 12:40:35 -0800530 if host_board is None:
531 logging.error('host %s has no board; failing repair',
532 self.hostname)
Richard Barnette82c35912012-11-20 10:09:10 -0800533 raise
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500534
535 reimage_success = self._install_repair()
536 # TODO(scottz): All repair pathways should be executed until we've
537 # exhausted all options. Below we favor servo over powercycle when
538 # we really should be falling back to power if servo fails.
539 if (not reimage_success and self.servo and
Richard Barnette03a0c132012-11-05 12:40:35 -0800540 host_board in self._SERVO_REPAIR_WHITELIST):
541 self._servo_repair(host_board)
542 elif (self.has_power() and
543 host_board in self._RPM_RECOVERY_BOARDS):
544 self._powercycle_to_repair()
545 else:
546 logging.error('host %s has no servo and no RPM control; '
547 'failing repair', self.hostname)
548 raise
Richard Barnette82c35912012-11-20 10:09:10 -0800549 self.verify()
550
551
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700552 def close(self):
553 super(SiteHost, self).close()
554 self.xmlrpc_disconnect_all()
555
556
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700557 def cleanup(self):
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700558 client_at = autotest.Autotest(self)
Richard Barnette82c35912012-11-20 10:09:10 -0800559 self.run('rm -f %s' % constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500560 try:
561 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
562 '_clear_login_prompt_state')
563 self.run('restart ui')
564 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
565 '_wait_for_login_prompt')
Alex Millerf4517962013-02-25 15:03:02 -0800566 except (error.AutotestRunError, error.AutoservRunError):
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500567 logging.warn('Unable to restart ui, rebooting device.')
568 # Since restarting the UI fails fall back to normal Autotest
569 # cleanup routines, i.e. reboot the machine.
570 super(SiteHost, self).cleanup()
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700571
572
Simran Basi154f5582012-10-23 16:27:11 -0700573 # TODO (sbasi) crosbug.com/35656
574 # Renamed the sitehost cleanup method so we don't go down this pathway.
575 # def cleanup(self):
576 def cleanup_poweron(self):
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700577 """Special cleanup method to make sure hosts always get power back."""
Chris Sosa9479fcd2012-10-09 13:44:22 -0700578 super(SiteHost, self).cleanup()
Simran Basid5e5e272012-09-24 15:23:59 -0700579 if self.has_power():
Simran Basifd23fb22012-10-22 17:56:22 -0700580 try:
581 self.power_on()
Chris Sosafab08082013-01-04 15:21:20 -0800582 except rpm_client.RemotePowerException:
Simran Basifd23fb22012-10-22 17:56:22 -0700583 # If cleanup has completed but there was an issue with the RPM
584 # Infrastructure, log an error message rather than fail cleanup
585 logging.error('Failed to turn Power On for this host after '
586 'cleanup through the RPM Infrastructure.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700587
588
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700589 def reboot(self, **dargs):
590 """
591 This function reboots the site host. The more generic
592 RemoteHost.reboot() performs sync and sleeps for 5
593 seconds. This is not necessary for Chrome OS devices as the
594 sync should be finished in a short time during the reboot
595 command.
596 """
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800597 if 'reboot_cmd' not in dargs:
598 dargs['reboot_cmd'] = ('((reboot & sleep 10; reboot -f &)'
599 ' </dev/null >/dev/null 2>&1 &)')
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700600 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800601 if 'fastsync' not in dargs:
602 dargs['fastsync'] = True
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700603 super(SiteHost, self).reboot(**dargs)
604
605
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700606 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800607 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700608
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800609 Tests for the following conditions:
610 1. All conditions tested by the parent version of this
611 function.
612 2. Sufficient space in /mnt/stateful_partition.
613 3. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700614
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700615 """
616 super(SiteHost, self).verify_software()
617 self.check_diskspace(
618 '/mnt/stateful_partition',
619 global_config.global_config.get_config_value(
620 'SERVER', 'gb_diskspace_required', type=int,
621 default=20))
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800622 self.run('update_engine_client --status')
Scott Zawalskifbca4a92013-03-04 15:56:42 -0500623 # Makes sure python is present, loads and can use built in functions.
624 # We have seen cases where importing cPickle fails with undefined
625 # symbols in cPickle.so.
626 self.run('python -c "import cPickle"')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700627
628
Christopher Wileyd78249a2013-03-01 13:05:31 -0800629 def xmlrpc_connect(self, command, port, command_name=None,
630 ready_test_name=None, timeout_seconds=10):
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700631 """Connect to an XMLRPC server on the host.
632
633 The `command` argument should be a simple shell command that
634 starts an XMLRPC server on the given `port`. The command
635 must not daemonize, and must terminate cleanly on SIGTERM.
636 The command is started in the background on the host, and a
637 local XMLRPC client for the server is created and returned
638 to the caller.
639
640 Note that the process of creating an XMLRPC client makes no
641 attempt to connect to the remote server; the caller is
642 responsible for determining whether the server is running
643 correctly, and is ready to serve requests.
644
Christopher Wileyd78249a2013-03-01 13:05:31 -0800645 Optionally, the caller can pass ready_test_name, a string
646 containing the name of a method to call on the proxy. This
647 method should take no parameters and return successfully only
648 when the server is ready to process client requests. When
649 ready_test_name is set, xmlrpc_connect will block until the
650 proxy is ready, and throw a TestError if the server isn't
651 ready by timeout_seconds.
652
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700653 @param command Shell command to start the server.
654 @param port Port number on which the server is expected to
655 be serving.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800656 @param command_name String to use as input to `pkill` to
657 terminate the XMLRPC server on the host.
Christopher Wileyd78249a2013-03-01 13:05:31 -0800658 @param ready_test_name String containing the name of a
659 method defined on the XMLRPC server.
660 @param timeout_seconds Number of seconds to wait
661 for the server to become 'ready.' Will throw a
662 TestFail error if server is not ready in time.
663
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700664 """
665 self.xmlrpc_disconnect(port)
666
667 # Chrome OS on the target closes down most external ports
668 # for security. We could open the port, but doing that
669 # would conflict with security tests that check that only
670 # expected ports are open. So, to get to the port on the
671 # target we use an ssh tunnel.
672 local_port = utils.get_unused_port()
673 tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port)
674 ssh_cmd = make_ssh_command(opts=tunnel_options)
675 tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname)
676 logging.debug('Full tunnel command: %s', tunnel_cmd)
677 tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True)
678 logging.debug('Started XMLRPC tunnel, local = %d'
679 ' remote = %d, pid = %d',
680 local_port, port, tunnel_proc.pid)
681
682 # Start the server on the host. Redirection in the command
683 # below is necessary, because 'ssh' won't terminate until
684 # background child processes close stdin, stdout, and
685 # stderr.
686 remote_cmd = '( %s ) </dev/null >/dev/null 2>&1 & echo $!' % command
687 remote_pid = self.run(remote_cmd).stdout.rstrip('\n')
688 logging.debug('Started XMLRPC server on host %s, pid = %s',
689 self.hostname, remote_pid)
690
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800691 self._xmlrpc_proxy_map[port] = (command_name, tunnel_proc)
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700692 rpc_url = 'http://localhost:%d' % local_port
Christopher Wileyd78249a2013-03-01 13:05:31 -0800693 proxy = xmlrpclib.ServerProxy(rpc_url, allow_none=True)
694 if ready_test_name is not None:
695 @retry.retry((socket.error, xmlrpclib.ProtocolError),
696 timeout_min=timeout_seconds/60.0,
697 delay_sec=0.1)
698 def ready_test():
699 """ Call proxy.ready_test_name(). """
700 getattr(proxy, ready_test_name)()
701 successful = False
702 try:
703 logging.info('Waiting %d seconds for XMLRPC server '
704 'to start.', timeout_seconds)
705 ready_test()
706 successful = True
707 except retry.TimeoutException:
708 raise error.TestError('Unable to start XMLRPC server after '
709 '%d seconds.' % timeout_seconds)
710 finally:
711 if not successful:
712 logging.error('Failed to start XMLRPC server.')
713 self.xmlrpc_disconnect(port)
714 logging.info('XMLRPC server started successfully.')
715 return proxy
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700716
717 def xmlrpc_disconnect(self, port):
718 """Disconnect from an XMLRPC server on the host.
719
720 Terminates the remote XMLRPC server previously started for
721 the given `port`. Also closes the local ssh tunnel created
722 for the connection to the host. This function does not
723 directly alter the state of a previously returned XMLRPC
724 client object; however disconnection will cause all
725 subsequent calls to methods on the object to fail.
726
727 This function does nothing if requested to disconnect a port
728 that was not previously connected via `self.xmlrpc_connect()`
729
730 @param port Port number passed to a previous call to
731 `xmlrpc_connect()`
732 """
733 if port not in self._xmlrpc_proxy_map:
734 return
735 entry = self._xmlrpc_proxy_map[port]
736 remote_name = entry[0]
737 tunnel_proc = entry[1]
738 if remote_name:
739 # We use 'pkill' to find our target process rather than
740 # a PID, because the host may have rebooted since
741 # connecting, and we don't want to kill an innocent
742 # process with the same PID.
743 #
744 # 'pkill' helpfully exits with status 1 if no target
745 # process is found, for which run() will throw an
Simran Basid5e5e272012-09-24 15:23:59 -0700746 # exception. We don't want that, so we the ignore
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700747 # status.
748 self.run("pkill -f '%s'" % remote_name, ignore_status=True)
749
750 if tunnel_proc.poll() is None:
751 tunnel_proc.terminate()
752 logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid)
753 else:
754 logging.debug('Tunnel pid %d terminated early, status %d',
755 tunnel_proc.pid, tunnel_proc.returncode)
756 del self._xmlrpc_proxy_map[port]
757
758
759 def xmlrpc_disconnect_all(self):
760 """Disconnect all known XMLRPC proxy ports."""
761 for port in self._xmlrpc_proxy_map.keys():
762 self.xmlrpc_disconnect(port)
763
764
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800765 def _ping_check_status(self, status):
766 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700767
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800768 @param status Check the ping status against this value.
769 @return True iff `status` and the result of ping are the same
770 (i.e. both True or both False).
771
772 """
773 ping_val = utils.ping(self.hostname, tries=1, deadline=1)
774 return not (status ^ (ping_val == 0))
775
776 def _ping_wait_for_status(self, status, timeout):
777 """Wait for the host to have a given status (UP or DOWN).
778
779 Status is checked by polling. Polling will not last longer
780 than the number of seconds in `timeout`. The polling
781 interval will be long enough that only approximately
782 _PING_WAIT_COUNT polling cycles will be executed, subject
783 to a maximum interval of about one minute.
784
785 @param status Waiting will stop immediately if `ping` of the
786 host returns this status.
787 @param timeout Poll for at most this many seconds.
788 @return True iff the host status from `ping` matched the
789 requested status at the time of return.
790
791 """
792 # _ping_check_status() takes about 1 second, hence the
793 # "- 1" in the formula below.
794 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
795 end_time = time.time() + timeout
796 while time.time() <= end_time:
797 if self._ping_check_status(status):
798 return True
799 if poll_interval > 0:
800 time.sleep(poll_interval)
801
802 # The last thing we did was sleep(poll_interval), so it may
803 # have been too long since the last `ping`. Check one more
804 # time, just to be sure.
805 return self._ping_check_status(status)
806
807 def ping_wait_up(self, timeout):
808 """Wait for the host to respond to `ping`.
809
810 N.B. This method is not a reliable substitute for
811 `wait_up()`, because a host that responds to ping will not
812 necessarily respond to ssh. This method should only be used
813 if the target DUT can be considered functional even if it
814 can't be reached via ssh.
815
816 @param timeout Minimum time to allow before declaring the
817 host to be non-responsive.
818 @return True iff the host answered to ping before the timeout.
819
820 """
821 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700822
Andrew Bresticker678c0c72013-01-22 10:44:09 -0800823 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700824 """Wait until the host no longer responds to `ping`.
825
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800826 This function can be used as a slightly faster version of
827 `wait_down()`, by avoiding potentially long ssh timeouts.
828
829 @param timeout Minimum time to allow for the host to become
830 non-responsive.
831 @return True iff the host quit answering ping before the
832 timeout.
833
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700834 """
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800835 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700836
837 def test_wait_for_sleep(self):
838 """Wait for the client to enter low-power sleep mode.
839
840 The test for "is asleep" can't distinguish a system that is
841 powered off; to confirm that the unit was asleep, it is
842 necessary to force resume, and then call
843 `test_wait_for_resume()`.
844
845 This function is expected to be called from a test as part
846 of a sequence like the following:
847
848 ~~~~~~~~
849 boot_id = host.get_boot_id()
850 # trigger sleep on the host
851 host.test_wait_for_sleep()
852 # trigger resume on the host
853 host.test_wait_for_resume(boot_id)
854 ~~~~~~~~
855
856 @exception TestFail The host did not go to sleep within
857 the allowed time.
858 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -0800859 if not self.ping_wait_down(timeout=self.SLEEP_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700860 raise error.TestFail(
861 'client failed to sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700862 self.SLEEP_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700863
864
865 def test_wait_for_resume(self, old_boot_id):
866 """Wait for the client to resume from low-power sleep mode.
867
868 The `old_boot_id` parameter should be the value from
869 `get_boot_id()` obtained prior to entering sleep mode. A
870 `TestFail` exception is raised if the boot id changes.
871
872 See @ref test_wait_for_sleep for more on this function's
873 usage.
874
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800875 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700876 target host went to sleep.
877
878 @exception TestFail The host did not respond within the
879 allowed time.
880 @exception TestFail The host responded, but the boot id test
881 indicated a reboot rather than a sleep
882 cycle.
883 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700884 if not self.wait_up(timeout=self.RESUME_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700885 raise error.TestFail(
886 'client failed to resume from sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700887 self.RESUME_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700888 else:
889 new_boot_id = self.get_boot_id()
890 if new_boot_id != old_boot_id:
891 raise error.TestFail(
892 'client rebooted, but sleep was expected'
893 ' (old boot %s, new boot %s)'
894 % (old_boot_id, new_boot_id))
895
896
897 def test_wait_for_shutdown(self):
898 """Wait for the client to shut down.
899
900 The test for "has shut down" can't distinguish a system that
901 is merely asleep; to confirm that the unit was down, it is
902 necessary to force boot, and then call test_wait_for_boot().
903
904 This function is expected to be called from a test as part
905 of a sequence like the following:
906
907 ~~~~~~~~
908 boot_id = host.get_boot_id()
909 # trigger shutdown on the host
910 host.test_wait_for_shutdown()
911 # trigger boot on the host
912 host.test_wait_for_boot(boot_id)
913 ~~~~~~~~
914
915 @exception TestFail The host did not shut down within the
916 allowed time.
917 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -0800918 if not self.ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700919 raise error.TestFail(
920 'client failed to shut down after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700921 self.SHUTDOWN_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700922
923
924 def test_wait_for_boot(self, old_boot_id=None):
925 """Wait for the client to boot from cold power.
926
927 The `old_boot_id` parameter should be the value from
928 `get_boot_id()` obtained prior to shutting down. A
929 `TestFail` exception is raised if the boot id does not
930 change. The boot id test is omitted if `old_boot_id` is not
931 specified.
932
933 See @ref test_wait_for_shutdown for more on this function's
934 usage.
935
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800936 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700937 shut down.
938
939 @exception TestFail The host did not respond within the
940 allowed time.
941 @exception TestFail The host responded, but the boot id test
942 indicated that there was no reboot.
943 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700944 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700945 raise error.TestFail(
946 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700947 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700948 elif old_boot_id:
949 if self.get_boot_id() == old_boot_id:
950 raise error.TestFail(
951 'client is back up, but did not reboot'
952 ' (boot %s)' % old_boot_id)
Simran Basid5e5e272012-09-24 15:23:59 -0700953
954
955 @staticmethod
956 def check_for_rpm_support(hostname):
957 """For a given hostname, return whether or not it is powered by an RPM.
958
959 @return None if this host does not follows the defined naming format
960 for RPM powered DUT's in the lab. If it does follow the format,
961 it returns a regular expression MatchObject instead.
962 """
Richard Barnette82c35912012-11-20 10:09:10 -0800963 return re.match(SiteHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -0700964
965
966 def has_power(self):
967 """For this host, return whether or not it is powered by an RPM.
968
969 @return True if this host is in the CROS lab and follows the defined
970 naming format.
971 """
972 return SiteHost.check_for_rpm_support(self.hostname)
973
974
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800975 def _set_power(self, state, power_method):
976 """Sets the power to the host via RPM, Servo or manual.
977
978 @param state Specifies which power state to set to DUT
979 @param power_method Specifies which method of power control to
980 use. By default "RPM" will be used. Valid values
981 are the strings "RPM", "manual", "servoj10".
982
983 """
984 ACCEPTABLE_STATES = ['ON', 'OFF']
985
986 if state.upper() not in ACCEPTABLE_STATES:
987 raise error.TestError('State must be one of: %s.'
988 % (ACCEPTABLE_STATES,))
989
990 if power_method == self.POWER_CONTROL_SERVO:
991 logging.info('Setting servo port J10 to %s', state)
992 self.servo.set('prtctl3_pwren', state.lower())
993 time.sleep(self._USB_POWER_TIMEOUT)
994 elif power_method == self.POWER_CONTROL_MANUAL:
995 logging.info('You have %d seconds to set the AC power to %s.',
996 self._POWER_CYCLE_TIMEOUT, state)
997 time.sleep(self._POWER_CYCLE_TIMEOUT)
998 else:
999 if not self.has_power():
1000 raise error.TestFail('DUT does not have RPM connected.')
1001 rpm_client.set_power(self.hostname, state.upper())
Simran Basid5e5e272012-09-24 15:23:59 -07001002
1003
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001004 def power_off(self, power_method=POWER_CONTROL_RPM):
1005 """Turn off power to this host via RPM, Servo or manual.
1006
1007 @param power_method Specifies which method of power control to
1008 use. By default "RPM" will be used. Valid values
1009 are the strings "RPM", "manual", "servoj10".
1010
1011 """
1012 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07001013
1014
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001015 def power_on(self, power_method=POWER_CONTROL_RPM):
1016 """Turn on power to this host via RPM, Servo or manual.
1017
1018 @param power_method Specifies which method of power control to
1019 use. By default "RPM" will be used. Valid values
1020 are the strings "RPM", "manual", "servoj10".
1021
1022 """
1023 self._set_power('ON', power_method)
1024
1025
1026 def power_cycle(self, power_method=POWER_CONTROL_RPM):
1027 """Cycle power to this host by turning it OFF, then ON.
1028
1029 @param power_method Specifies which method of power control to
1030 use. By default "RPM" will be used. Valid values
1031 are the strings "RPM", "manual", "servoj10".
1032
1033 """
1034 if power_method in (self.POWER_CONTROL_SERVO,
1035 self.POWER_CONTROL_MANUAL):
1036 self.power_off(power_method=power_method)
1037 time.sleep(self._POWER_CYCLE_TIMEOUT)
1038 self.power_on(power_method=power_method)
1039 else:
1040 rpm_client.set_power(self.hostname, 'CYCLE')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001041
1042
1043 def get_platform(self):
1044 """Determine the correct platform label for this host.
1045
1046 @returns a string representing this host's platform.
1047 """
1048 crossystem = utils.Crossystem(self)
1049 crossystem.init()
1050 # Extract fwid value and use the leading part as the platform id.
1051 # fwid generally follow the format of {platform}.{firmware version}
1052 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
1053 platform = crossystem.fwid().split('.')[0].lower()
1054 # Newer platforms start with 'Google_' while the older ones do not.
1055 return platform.replace('google_', '')
1056
1057
Aviv Keshet74c89a92013-02-04 15:18:30 -08001058 @label_decorator()
Simran Basic6f1f7a2012-10-16 10:47:46 -07001059 def get_board(self):
1060 """Determine the correct board label for this host.
1061
1062 @returns a string representing this host's board.
1063 """
1064 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
1065 run_method=self.run)
1066 board = release_info['CHROMEOS_RELEASE_BOARD']
1067 # Devices in the lab generally have the correct board name but our own
1068 # development devices have {board_name}-signed-{key_type}. The board
1069 # name may also begin with 'x86-' which we need to keep.
1070 if 'x86' not in board:
1071 return 'board:%s' % board.split('-')[0]
1072 return 'board:%s' % '-'.join(board.split('-')[0:2])
1073
1074
Aviv Keshet74c89a92013-02-04 15:18:30 -08001075 @label_decorator('lightsensor')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001076 def has_lightsensor(self):
1077 """Determine the correct board label for this host.
1078
1079 @returns the string 'lightsensor' if this host has a lightsensor or
1080 None if it does not.
1081 """
1082 search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % (
Richard Barnette82c35912012-11-20 10:09:10 -08001083 self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES))
Simran Basic6f1f7a2012-10-16 10:47:46 -07001084 try:
1085 # Run the search cmd following the symlinks. Stderr_tee is set to
1086 # None as there can be a symlink loop, but the command will still
1087 # execute correctly with a few messages printed to stderr.
1088 self.run(search_cmd, stdout_tee=None, stderr_tee=None)
1089 return 'lightsensor'
1090 except error.AutoservRunError:
1091 # egrep exited with a return code of 1 meaning none of the possible
1092 # lightsensor files existed.
1093 return None
1094
1095
Aviv Keshet74c89a92013-02-04 15:18:30 -08001096 @label_decorator('bluetooth')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001097 def has_bluetooth(self):
1098 """Determine the correct board label for this host.
1099
1100 @returns the string 'bluetooth' if this host has bluetooth or
1101 None if it does not.
1102 """
1103 try:
1104 self.run('test -d /sys/class/bluetooth/hci0')
1105 # test exited with a return code of 0.
1106 return 'bluetooth'
1107 except error.AutoservRunError:
1108 # test exited with a return code 1 meaning the directory did not
1109 # exist.
1110 return None
1111
1112
1113 def get_labels(self):
1114 """Return a list of labels for this given host.
1115
1116 This is the main way to retrieve all the automatic labels for a host
1117 as it will run through all the currently implemented label functions.
1118 """
1119 labels = []
Richard Barnette82c35912012-11-20 10:09:10 -08001120 for label_function in self._LABEL_FUNCTIONS:
Simran Basic6f1f7a2012-10-16 10:47:46 -07001121 label = label_function(self)
1122 if label:
1123 labels.append(label)
1124 return labels