blob: 742d443ce78bb55134487308f2f3f47b2c4ceb8c [file] [log] [blame]
J. Richard Barnette24adbf42012-04-11 15:04:53 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtisaa5eedb2011-08-23 16:18:52 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Aviv Keshet74c89a92013-02-04 15:18:30 -08005import functools
J. Richard Barnette1d78b012012-05-15 13:56:30 -07006import logging
Dan Shi0f466e82013-02-22 15:44:58 -08007import os
Simran Basid5e5e272012-09-24 15:23:59 -07008import re
Christopher Wileyd78249a2013-03-01 13:05:31 -08009import socket
J. Richard Barnette1d78b012012-05-15 13:56:30 -070010import subprocess
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070011import time
J. Richard Barnette1d78b012012-05-15 13:56:30 -070012import xmlrpclib
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070013
J. Richard Barnette45e93de2012-04-11 17:24:15 -070014from autotest_lib.client.bin import utils
Richard Barnette0c73ffc2012-11-19 15:21:18 -080015from autotest_lib.client.common_lib import error
16from autotest_lib.client.common_lib import global_config
J. Richard Barnette45e93de2012-04-11 17:24:15 -070017from autotest_lib.client.common_lib.cros import autoupdater
Richard Barnette03a0c132012-11-05 12:40:35 -080018from autotest_lib.client.common_lib.cros import dev_server
Christopher Wileyd78249a2013-03-01 13:05:31 -080019from autotest_lib.client.common_lib.cros import retry
Richard Barnette82c35912012-11-20 10:09:10 -080020from autotest_lib.client.cros import constants
J. Richard Barnette45e93de2012-04-11 17:24:15 -070021from autotest_lib.server import autoserv_parser
Chris Sosaf4d43ff2012-10-30 11:21:05 -070022from autotest_lib.server import autotest
J. Richard Barnette45e93de2012-04-11 17:24:15 -070023from autotest_lib.server import site_host_attributes
J. Richard Barnette67ccb872012-04-19 16:34:56 -070024from autotest_lib.server.cros import servo
Scott Zawalski89c44dd2013-02-26 09:28:02 -050025from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
26from autotest_lib.server.cros.dynamic_suite import tools
J. Richard Barnette45e93de2012-04-11 17:24:15 -070027from autotest_lib.server.hosts import remote
Simran Basidcff4252012-11-20 16:13:20 -080028from autotest_lib.site_utils.rpm_control_system import rpm_client
Simran Basid5e5e272012-09-24 15:23:59 -070029
Richard Barnette82c35912012-11-20 10:09:10 -080030# Importing frontend.afe.models requires a full Autotest
31# installation (with the Django modules), not just the source
32# repository. Most developers won't have the full installation, so
33# the imports below will fail for them.
34#
35# The fix is to catch import exceptions, and set `models` to `None`
36# on failure. This has the side effect that
37# SiteHost._get_board_from_afe() will fail: That will manifest as
38# failures during Repair jobs leaving the DUT as "Repair Failed".
39# In practice, you can't test Repair jobs without a full
40# installation, so that kind of failure isn't expected.
41try:
J. Richard Barnette7214e0b2013-02-06 15:20:49 -080042 # pylint: disable=W0611
Richard Barnette82c35912012-11-20 10:09:10 -080043 from autotest_lib.frontend import setup_django_environment
44 from autotest_lib.frontend.afe import models
45except:
46 models = None
47
Simran Basid5e5e272012-09-24 15:23:59 -070048
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080049def _make_servo_hostname(hostname):
50 host_parts = hostname.split('.')
51 host_parts[0] = host_parts[0] + '-servo'
52 return '.'.join(host_parts)
53
54
55def _get_lab_servo(target_hostname):
56 """Instantiate a Servo for |target_hostname| in the lab.
57
58 Assuming that |target_hostname| is a device in the CrOS test
59 lab, create and return a Servo object pointed at the servo
60 attached to that DUT. The servo in the test lab is assumed
61 to already have servod up and running on it.
62
63 @param target_hostname: device whose servo we want to target.
64 @return an appropriately configured Servo instance.
65 """
66 servo_host = _make_servo_hostname(target_hostname)
67 if utils.host_is_in_lab_zone(servo_host):
68 try:
J. Richard Barnetted5f807a2013-02-11 16:51:00 -080069 return servo.Servo(servo_host=servo_host)
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080070 except: # pylint: disable=W0702
71 # TODO(jrbarnette): Long-term, if we can't get to
72 # a servo in the lab, we want to fail, so we should
73 # pass any exceptions along. Short-term, we're not
74 # ready to rely on servo, so we ignore failures.
75 pass
76 return None
77
78
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070079def make_ssh_command(user='root', port=22, opts='', hosts_file=None,
80 connect_timeout=None, alive_interval=None):
81 """Override default make_ssh_command to use options tuned for Chrome OS.
82
83 Tuning changes:
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070084 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH connection
85 failure. Consistency with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070086
Dale Curtisaa5eedb2011-08-23 16:18:52 -070087 - ServerAliveInterval=180; which causes SSH to ping connection every
88 180 seconds. In conjunction with ServerAliveCountMax ensures that if the
89 connection dies, Autotest will bail out quickly. Originally tried 60 secs,
90 but saw frequent job ABORTS where the test completed successfully.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070091
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070092 - ServerAliveCountMax=3; consistency with remote_access.sh.
93
94 - ConnectAttempts=4; reduce flakiness in connection errors; consistency
95 with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070096
97 - UserKnownHostsFile=/dev/null; we don't care about the keys. Host keys
98 change with every new installation, don't waste memory/space saving them.
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070099
100 - SSH protocol forced to 2; needed for ServerAliveInterval.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800101
102 @param user User name to use for the ssh connection.
103 @param port Port on the target host to use for ssh connection.
104 @param opts Additional options to the ssh command.
105 @param hosts_file Ignored.
106 @param connect_timeout Ignored.
107 @param alive_interval Ignored.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -0700108 """
109 base_command = ('/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no'
110 ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes'
Chris Sosaf7fcd6e2011-09-27 17:30:47 -0700111 ' -o ConnectTimeout=30 -o ServerAliveInterval=180'
112 ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4'
113 ' -o Protocol=2 -l %s -p %d')
Dale Curtiscb7bfaf2011-06-07 16:21:57 -0700114 return base_command % (opts, user, port)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700115
116
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800117
Aviv Keshet74c89a92013-02-04 15:18:30 -0800118def add_label_detector(label_function_list, label_list=None, label=None):
119 """Decorator used to group functions together into the provided list.
120 @param label_function_list: List of label detecting functions to add
121 decorated function to.
122 @param label_list: List of detectable labels to add detectable labels to.
123 (Default: None)
124 @param label: Label string that is detectable by this detection function
125 (Default: None)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800126 """
Simran Basic6f1f7a2012-10-16 10:47:46 -0700127 def add_func(func):
Aviv Keshet74c89a92013-02-04 15:18:30 -0800128 """
129 @param func: The function to be added as a detector.
130 """
131 label_function_list.append(func)
132 if label and label_list is not None:
133 label_list.append(label)
Simran Basic6f1f7a2012-10-16 10:47:46 -0700134 return func
135 return add_func
136
137
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700138class SiteHost(remote.RemoteHost):
139 """Chromium OS specific subclass of Host."""
140
141 _parser = autoserv_parser.autoserv_parser
142
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800143 # Time to wait for new kernel to be marked successful after
144 # auto update.
Chris Masone163cead2012-05-16 11:49:48 -0700145 _KERNEL_UPDATE_TIMEOUT = 120
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700146
Richard Barnette03a0c132012-11-05 12:40:35 -0800147 # Timeout values (in seconds) associated with various Chrome OS
148 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700149 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800150 # In general, a good rule of thumb is that the timeout can be up
151 # to twice the typical measured value on the slowest platform.
152 # The times here have not necessarily been empirically tested to
153 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700154 #
155 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800156 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
157 # time to restart the netwowrk.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700158 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800159 # other things, this must account for the 30 second dev-mode
160 # screen delay and time to start the network,
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700161 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800162 # including the 30 second dev-mode delay and time to start the
163 # network,
164 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700165 # REBOOT_TIMEOUT: Combination of shutdown and reboot times.
Richard Barnette03a0c132012-11-05 12:40:35 -0800166 # _INSTALL_TIMEOUT: Time to allow for chromeos-install.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700167
168 SLEEP_TIMEOUT = 2
169 RESUME_TIMEOUT = 5
170 BOOT_TIMEOUT = 45
171 USB_BOOT_TIMEOUT = 150
172 SHUTDOWN_TIMEOUT = 5
173 REBOOT_TIMEOUT = SHUTDOWN_TIMEOUT + BOOT_TIMEOUT
Richard Barnette03a0c132012-11-05 12:40:35 -0800174 _INSTALL_TIMEOUT = 240
175
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800176 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
177 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
178 _USB_POWER_TIMEOUT = 5
179 _POWER_CYCLE_TIMEOUT = 10
180
Richard Barnette03a0c132012-11-05 12:40:35 -0800181 _DEFAULT_SERVO_URL_FORMAT = ('/static/servo-images/'
182 '%(board)s_test_image.bin')
183
184 # TODO(jrbarnette): Servo repair is restricted to x86-alex,
185 # because the existing servo client code won't work on other
186 # boards. http://crosbug.com/36973
187 _SERVO_REPAIR_WHITELIST = [ 'x86-alex' ]
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800188
189
Richard Barnette82c35912012-11-20 10:09:10 -0800190 _RPM_RECOVERY_BOARDS = global_config.global_config.get_config_value('CROS',
191 'rpm_recovery_boards', type=str).split(',')
192
193 _MAX_POWER_CYCLE_ATTEMPTS = 6
194 _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
195 _RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-'
196 'host[0-9]+')
197 _LIGHTSENSOR_FILES = ['in_illuminance0_input',
198 'in_illuminance0_raw',
199 'illuminance0_input']
200 _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices'
201 _LABEL_FUNCTIONS = []
Aviv Keshet74c89a92013-02-04 15:18:30 -0800202 _DETECTABLE_LABELS = []
203 label_decorator = functools.partial(add_label_detector, _LABEL_FUNCTIONS,
204 _DETECTABLE_LABELS)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700205
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800206 # Constants used in ping_wait_up() and ping_wait_down().
207 #
208 # _PING_WAIT_COUNT is the approximate number of polling
209 # cycles to use when waiting for a host state change.
210 #
211 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
212 # for arguments to the internal _ping_wait_for_status()
213 # method.
214 _PING_WAIT_COUNT = 40
215 _PING_STATUS_DOWN = False
216 _PING_STATUS_UP = True
217
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800218 # Allowed values for the power_method argument.
219
220 # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods.
221 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
222 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
223 POWER_CONTROL_RPM = 'RPM'
224 POWER_CONTROL_SERVO = 'servoj10'
225 POWER_CONTROL_MANUAL = 'manual'
226
227 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
228 POWER_CONTROL_SERVO,
229 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800230
J. Richard Barnette964fba02012-10-24 17:34:29 -0700231 @staticmethod
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800232 def get_servo_arguments(args_dict):
233 """Extract servo options from `args_dict` and return the result.
234
235 Take the provided dictionary of argument options and return
236 a subset that represent standard arguments needed to
237 construct a servo object for a host. The intent is to
238 provide standard argument processing from run_remote_tests
239 for tests that require a servo to operate.
240
241 Recommended usage:
242 ~~~~~~~~
243 args_dict = utils.args_to_dict(args)
244 servo_args = hosts.SiteHost.get_servo_arguments(args_dict)
245 host = hosts.create_host(machine, servo_args=servo_args)
246 ~~~~~~~~
247
248 @param args_dict Dictionary from which to extract the servo
249 arguments.
250 """
J. Richard Barnette964fba02012-10-24 17:34:29 -0700251 servo_args = {}
252 for arg in ('servo_host', 'servo_port'):
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800253 if arg in args_dict:
254 servo_args[arg] = args_dict[arg]
J. Richard Barnette964fba02012-10-24 17:34:29 -0700255 return servo_args
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700256
J. Richard Barnette964fba02012-10-24 17:34:29 -0700257
258 def _initialize(self, hostname, servo_args=None, *args, **dargs):
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700259 """Initialize superclasses, and |self.servo|.
260
261 For creating the host servo object, there are three
262 possibilities: First, if the host is a lab system known to
263 have a servo board, we connect to that servo unconditionally.
264 Second, if we're called from a control file that requires
J. Richard Barnette55fb8062012-05-23 10:29:31 -0700265 servo features for testing, it will pass settings for
266 `servo_host`, `servo_port`, or both. If neither of these
267 cases apply, `self.servo` will be `None`.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700268
269 """
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700270 super(SiteHost, self)._initialize(hostname=hostname,
271 *args, **dargs)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700272 # self.env is a dictionary of environment variable settings
273 # to be exported for commands run on the host.
274 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
275 # errors that might happen.
276 self.env['LIBC_FATAL_STDERR_'] = '1'
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700277 self._xmlrpc_proxy_map = {}
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -0800278 self.servo = _get_lab_servo(hostname)
J. Richard Barnettead7da482012-10-30 16:46:52 -0700279 if not self.servo and servo_args is not None:
J. Richard Barnette964fba02012-10-24 17:34:29 -0700280 self.servo = servo.Servo(**servo_args)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700281
282
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500283 def get_repair_image_name(self):
284 """Generate a image_name from variables in the global config.
285
286 @returns a str of $board-version/$BUILD.
287
288 """
289 stable_version = global_config.global_config.get_config_value(
290 'CROS', 'stable_cros_version')
291 build_pattern = global_config.global_config.get_config_value(
292 'CROS', 'stable_build_pattern')
293 board = self._get_board_from_afe()
294 if board is None:
295 raise error.AutoservError('DUT has no board attribute, '
296 'cannot be repaired.')
297 return build_pattern % (board, stable_version)
298
299
300 def clear_cros_version_labels_and_job_repo_url(self):
301 """Clear cros_version labels and host attribute job_repo_url."""
302 host_model = models.Host.objects.get(hostname=self.hostname)
303 for label in host_model.labels.iterator():
304 if not label.name.startswith(ds_constants.VERSION_PREFIX):
305 continue
Dan Shi0f466e82013-02-22 15:44:58 -0800306
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500307 label.host_set.remove(host_model)
308
309 host_model.set_or_delete_attribute('job_repo_url', None)
310
311
Dan Shi0f466e82013-02-22 15:44:58 -0800312 def _try_stateful_update(self, update_url, force_update, updater):
313 """Try to use stateful update to initialize DUT.
314
315 When DUT is already running the same version that machine_install
316 tries to install, stateful update is a much faster way to clean up
317 the DUT for testing, compared to a full reimage. It is implemeted
318 by calling autoupdater.run_update, but skipping updating root, as
319 updating the kernel is time consuming and not necessary.
320
321 @param update_url: url of the image.
322 @param force_update: Set to True to update the image even if the DUT
323 is running the same version.
324 @param updater: ChromiumOSUpdater instance used to update the DUT.
325 @returns: True if the DUT was updated with stateful update.
326
327 """
328 if not updater.check_version():
329 return False
330 if not force_update:
331 logging.info('Canceling stateful update because the new and '
332 'old versions are the same.')
333 return False
334 # Following folders should be rebuilt after stateful update.
335 # A test file is used to confirm each folder gets rebuilt after
336 # the stateful update.
337 folders_to_check = ['/var', '/home', '/mnt/stateful_partition']
338 test_file = '.test_file_to_be_deleted'
339 for folder in folders_to_check:
340 touch_path = os.path.join(folder, test_file)
341 self.run('touch %s' % touch_path)
342
343 if not updater.run_update(force_update=True, update_root=False):
344 return False
345
346 # Reboot to complete stateful update.
347 self.reboot(timeout=60, wait=True)
348 check_file_cmd = 'test -f %s; echo $?'
349 for folder in folders_to_check:
350 test_file_path = os.path.join(folder, test_file)
351 result = self.run(check_file_cmd % test_file_path,
352 ignore_status=True)
353 if result.exit_status == 1:
354 return False
355 return True
356
357
358 def _post_update_processing(self, updater, inactive_kernel=None):
359 """After the DUT is updated, confirm machine_install succeeded.
360
361 @param updater: ChromiumOSUpdater instance used to update the DUT.
362 @param inactive_kernel: kernel state of inactive kernel before reboot.
363
364 """
365
366 # Touch the lab machine file to leave a marker that distinguishes
367 # this image from other test images.
368 self.run('touch %s' % self._LAB_MACHINE_FILE)
369
370 # Kick off the autoreboot script as the _LAB_MACHINE_FILE was
371 # missing on the first boot.
372 self.run('start autoreboot')
373
374 # Following the reboot, verify the correct version.
375 if not updater.check_version():
376 # Print out crossystem to make it easier to debug the rollback.
377 logging.debug('Dumping partition table.')
378 self.host.run('cgpt show $(rootdev -s -d)')
379 logging.debug('Dumping crossystem for firmware debugging.')
380 self.host.run('crossystem --all')
381 logging.error('Expected Chromium OS version: %s. '
382 'Found Chromium OS %s',
383 self.update_version, updater.get_build_id())
384 raise ChromiumOSError('Updater failed on host %s' %
385 self.host.hostname)
386
387 # Figure out newly active kernel.
388 new_active_kernel, _ = updater.get_kernel_state()
389
390 # Ensure that previously inactive kernel is now the active kernel.
391 if inactive_kernel and new_active_kernel != inactive_kernel:
392 raise autoupdater.ChromiumOSError(
393 'Update failed. New kernel partition is not active after'
394 ' boot.')
395
396 host_attributes = site_host_attributes.HostAttributes(self.hostname)
397 if host_attributes.has_chromeos_firmware:
398 # Wait until tries == 0 and success, or until timeout.
399 utils.poll_for_condition(
400 lambda: (updater.get_kernel_tries(new_active_kernel) == 0
401 and updater.get_kernel_success(new_active_kernel)),
402 exception=autoupdater.ChromiumOSError(
403 'Update failed. Timed out waiting for system to mark'
404 ' new kernel as successful.'),
405 timeout=self._KERNEL_UPDATE_TIMEOUT, sleep_interval=5)
406
407
Chris Sosaa3ac2152012-05-23 22:23:13 -0700408 def machine_install(self, update_url=None, force_update=False,
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500409 local_devserver=False, repair=False):
410 """Install the DUT.
411
Dan Shi0f466e82013-02-22 15:44:58 -0800412 Use stateful update if the DUT is already running the same build.
413 Stateful update does not update kernel and tends to run much faster
414 than a full reimage. If the DUT is running a different build, or it
415 failed to do a stateful update, full update, including kernel update,
416 will be applied to the DUT.
417
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500418 @param update_url: The url to use for the update
419 pattern: http://$devserver:###/update/$build
420 If update_url is None and repair is True we will install the
421 stable image listed in global_config under
422 CROS.stable_cros_version.
423 @param force_update: Force an update even if the version installed
424 is the same. Default:False
425 @param local_devserver: Used by run_remote_test to allow people to
426 use their local devserver. Default: False
427 @param repair: Whether or not we are in repair mode. This adds special
428 cases for repairing a machine like starting update_engine.
429 Setting repair to True sets force_update to True as well.
430 default: False
431 @raises autoupdater.ChromiumOSError
432
433 """
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700434 if not update_url and self._parser.options.image:
435 update_url = self._parser.options.image
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500436 elif not update_url and not repair:
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700437 raise autoupdater.ChromiumOSError(
438 'Update failed. No update URL provided.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500439 elif not update_url and repair:
440 image_name = self.get_repair_image_name()
441 devserver = dev_server.ImageServer.resolve(image_name)
442 logging.info('Staging repair build: %s', image_name)
443 devserver.trigger_download(image_name, synchronous=False)
444 self.clear_cros_version_labels_and_job_repo_url()
445 update_url = tools.image_url_pattern() % (devserver.url(),
446 image_name)
447
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500448 if repair:
Dan Shi0f466e82013-02-22 15:44:58 -0800449 # In case the system is in a bad state, we always reboot the machine
450 # before machine_install.
451 self.reboot(timeout=60, wait=True)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500452 self.run('stop update-engine; start update-engine')
453 force_update = True
Dan Shi0f466e82013-02-22 15:44:58 -0800454
Chris Sosaa3ac2152012-05-23 22:23:13 -0700455 updater = autoupdater.ChromiumOSUpdater(update_url, host=self,
Dan Shi0f466e82013-02-22 15:44:58 -0800456 local_devserver=local_devserver)
457 updated = False
458 # If the DUT is already running the same build, try stateful update
459 # first. Stateful update does not update kernel and tends to run much
460 # faster than a full reimage.
461 try:
462 updated = self._try_stateful_update(update_url, force_update,
463 updater)
464 if updated:
465 logging.info('DUT is updated with stateful update.')
466 except Exception as e:
467 logging.exception(e)
468 logging.warn('Failed to stateful update DUT, force to update.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700469
Dan Shi0f466e82013-02-22 15:44:58 -0800470 inactive_kernel = None
471 # Do a full update if stateful update is not applicable or failed.
472 if not updated:
473 # In case the system is in a bad state, we always reboot the
474 # machine before machine_install.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700475 self.reboot(timeout=60, wait=True)
Dan Shi0f466e82013-02-22 15:44:58 -0800476 if updater.run_update(force_update):
477 updated = True
478 # Figure out active and inactive kernel.
479 active_kernel, inactive_kernel = updater.get_kernel_state()
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700480
Dan Shi0f466e82013-02-22 15:44:58 -0800481 # Ensure inactive kernel has higher priority than active.
482 if (updater.get_kernel_priority(inactive_kernel)
483 < updater.get_kernel_priority(active_kernel)):
484 raise autoupdater.ChromiumOSError(
485 'Update failed. The priority of the inactive kernel'
486 ' partition is less than that of the active kernel'
487 ' partition.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700488
Dan Shi0f466e82013-02-22 15:44:58 -0800489 update_engine_log = '/var/log/update_engine.log'
490 logging.info('Dumping %s', update_engine_log)
491 self.run('cat %s' % update_engine_log)
492 # Updater has returned successfully; reboot the host.
493 self.reboot(timeout=60, wait=True)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700494
Dan Shi0f466e82013-02-22 15:44:58 -0800495 if updated:
496 self._post_update_processing(updater, inactive_kernel)
Simran Basi13fa1ba2013-03-04 10:56:47 -0800497
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700498 # Clean up any old autotest directories which may be lying around.
499 for path in global_config.global_config.get_config_value(
500 'AUTOSERV', 'client_autodir_paths', type=list):
501 self.run('rm -rf ' + path)
502
503
Richard Barnette82c35912012-11-20 10:09:10 -0800504 def _get_board_from_afe(self):
505 """Retrieve this host's board from its labels in the AFE.
506
507 Looks for a host label of the form "board:<board>", and
508 returns the "<board>" part of the label. `None` is returned
509 if there is not a single, unique label matching the pattern.
510
511 @returns board from label, or `None`.
512 """
513 host_model = models.Host.objects.get(hostname=self.hostname)
514 board_labels = filter(lambda l: l.name.startswith('board:'),
515 host_model.labels.all())
516 board_name = None
517 if len(board_labels) == 1:
518 board_name = board_labels[0].name.split(':', 1)[1]
519 elif len(board_labels) == 0:
520 logging.error('Host %s does not have a board label.',
521 self.hostname)
522 else:
523 logging.error('Host %s has multiple board labels.',
524 self.hostname)
525 return board_name
526
527
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500528 def _install_repair(self):
529 """Attempt to repair this host using upate-engine.
530
531 If the host is up, try installing the DUT with a stable
532 "repair" version of Chrome OS as defined in the global_config
533 under CROS.stable_cros_version.
534
535 @returns True if successful, False if update_engine failed.
536
537 """
538 if not self.is_up():
539 return False
540
541 logging.info('Attempting to reimage machine to repair image.')
542 try:
543 self.machine_install(repair=True)
544 except autoupdater.ChromiumOSError:
545 logging.info('Repair via install failed.')
546 return False
547
548 return True
549
550
Richard Barnette03a0c132012-11-05 12:40:35 -0800551 def _servo_repair(self, board):
552 """Attempt to repair this host using an attached Servo.
553
554 Re-install the OS on the DUT by 1) installing a test image
555 on a USB storage device attached to the Servo board,
556 2) booting that image in recovery mode, and then
557 3) installing the image.
558
559 """
560 server = dev_server.ImageServer.devserver_url_for_servo(board)
561 image = server + (self._DEFAULT_SERVO_URL_FORMAT %
562 { 'board': board })
563 self.servo.install_recovery_image(image)
564 if not self.wait_up(timeout=self.USB_BOOT_TIMEOUT):
565 raise error.AutoservError('DUT failed to boot from USB'
566 ' after %d seconds' %
567 self.USB_BOOT_TIMEOUT)
568 self.run('chromeos-install --yes',
569 timeout=self._INSTALL_TIMEOUT)
570 self.servo.power_long_press()
571 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
572 self.servo.power_short_press()
573 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
574 raise error.AutoservError('DUT failed to reboot installed '
575 'test image after %d seconds' %
576 self.BOOT_TIMEOUT)
577
578
Richard Barnette82c35912012-11-20 10:09:10 -0800579 def _powercycle_to_repair(self):
580 """Utilize the RPM Infrastructure to bring the host back up.
581
582 If the host is not up/repaired after the first powercycle we utilize
583 auto fallback to the last good install by powercycling and rebooting the
584 host 6 times.
585 """
586 logging.info('Attempting repair via RPM powercycle.')
587 failed_cycles = 0
588 self.power_cycle()
589 while not self.wait_up(timeout=self.BOOT_TIMEOUT):
590 failed_cycles += 1
591 if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS:
592 raise error.AutoservError('Powercycled host %s %d times; '
593 'device did not come back online.' %
594 (self.hostname, failed_cycles))
595 self.power_cycle()
596 if failed_cycles == 0:
597 logging.info('Powercycling was successful first time.')
598 else:
599 logging.info('Powercycling was successful after %d failures.',
600 failed_cycles)
601
602
603 def repair_full(self):
604 """Repair a host for repair level NO_PROTECTION.
605
606 This overrides the base class function for repair; it does
607 not call back to the parent class, but instead offers a
608 simplified implementation based on the capabilities in the
609 Chrome OS test lab.
610
611 Repair follows this sequence:
612 1. If the DUT passes `self.verify()`, do nothing.
613 2. If the DUT can be power-cycled via RPM, try to repair
614 by power-cycling.
615
616 As with the parent method, the last operation performed on
617 the DUT must be to call `self.verify()`; if that call fails,
618 the exception it raises is passed back to the caller.
619 """
620 try:
621 self.verify()
622 except:
623 host_board = self._get_board_from_afe()
Richard Barnette03a0c132012-11-05 12:40:35 -0800624 if host_board is None:
625 logging.error('host %s has no board; failing repair',
626 self.hostname)
Richard Barnette82c35912012-11-20 10:09:10 -0800627 raise
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500628
629 reimage_success = self._install_repair()
630 # TODO(scottz): All repair pathways should be executed until we've
631 # exhausted all options. Below we favor servo over powercycle when
632 # we really should be falling back to power if servo fails.
633 if (not reimage_success and self.servo and
Richard Barnette03a0c132012-11-05 12:40:35 -0800634 host_board in self._SERVO_REPAIR_WHITELIST):
635 self._servo_repair(host_board)
636 elif (self.has_power() and
637 host_board in self._RPM_RECOVERY_BOARDS):
638 self._powercycle_to_repair()
639 else:
640 logging.error('host %s has no servo and no RPM control; '
641 'failing repair', self.hostname)
642 raise
Richard Barnette82c35912012-11-20 10:09:10 -0800643 self.verify()
644
645
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700646 def close(self):
647 super(SiteHost, self).close()
648 self.xmlrpc_disconnect_all()
649
650
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700651 def cleanup(self):
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700652 client_at = autotest.Autotest(self)
Richard Barnette82c35912012-11-20 10:09:10 -0800653 self.run('rm -f %s' % constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500654 try:
655 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
656 '_clear_login_prompt_state')
657 self.run('restart ui')
658 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
659 '_wait_for_login_prompt')
Alex Millerf4517962013-02-25 15:03:02 -0800660 except (error.AutotestRunError, error.AutoservRunError):
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500661 logging.warn('Unable to restart ui, rebooting device.')
662 # Since restarting the UI fails fall back to normal Autotest
663 # cleanup routines, i.e. reboot the machine.
664 super(SiteHost, self).cleanup()
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700665
666
Simran Basi154f5582012-10-23 16:27:11 -0700667 # TODO (sbasi) crosbug.com/35656
668 # Renamed the sitehost cleanup method so we don't go down this pathway.
669 # def cleanup(self):
670 def cleanup_poweron(self):
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700671 """Special cleanup method to make sure hosts always get power back."""
Chris Sosa9479fcd2012-10-09 13:44:22 -0700672 super(SiteHost, self).cleanup()
Simran Basid5e5e272012-09-24 15:23:59 -0700673 if self.has_power():
Simran Basifd23fb22012-10-22 17:56:22 -0700674 try:
675 self.power_on()
Chris Sosafab08082013-01-04 15:21:20 -0800676 except rpm_client.RemotePowerException:
Simran Basifd23fb22012-10-22 17:56:22 -0700677 # If cleanup has completed but there was an issue with the RPM
678 # Infrastructure, log an error message rather than fail cleanup
679 logging.error('Failed to turn Power On for this host after '
680 'cleanup through the RPM Infrastructure.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700681
682
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700683 def reboot(self, **dargs):
684 """
685 This function reboots the site host. The more generic
686 RemoteHost.reboot() performs sync and sleeps for 5
687 seconds. This is not necessary for Chrome OS devices as the
688 sync should be finished in a short time during the reboot
689 command.
690 """
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800691 if 'reboot_cmd' not in dargs:
692 dargs['reboot_cmd'] = ('((reboot & sleep 10; reboot -f &)'
693 ' </dev/null >/dev/null 2>&1 &)')
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700694 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800695 if 'fastsync' not in dargs:
696 dargs['fastsync'] = True
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700697 super(SiteHost, self).reboot(**dargs)
698
699
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700700 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800701 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700702
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800703 Tests for the following conditions:
704 1. All conditions tested by the parent version of this
705 function.
706 2. Sufficient space in /mnt/stateful_partition.
707 3. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700708
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700709 """
710 super(SiteHost, self).verify_software()
711 self.check_diskspace(
712 '/mnt/stateful_partition',
713 global_config.global_config.get_config_value(
714 'SERVER', 'gb_diskspace_required', type=int,
715 default=20))
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800716 self.run('update_engine_client --status')
Scott Zawalskifbca4a92013-03-04 15:56:42 -0500717 # Makes sure python is present, loads and can use built in functions.
718 # We have seen cases where importing cPickle fails with undefined
719 # symbols in cPickle.so.
720 self.run('python -c "import cPickle"')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700721
722
Christopher Wileyd78249a2013-03-01 13:05:31 -0800723 def xmlrpc_connect(self, command, port, command_name=None,
724 ready_test_name=None, timeout_seconds=10):
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700725 """Connect to an XMLRPC server on the host.
726
727 The `command` argument should be a simple shell command that
728 starts an XMLRPC server on the given `port`. The command
729 must not daemonize, and must terminate cleanly on SIGTERM.
730 The command is started in the background on the host, and a
731 local XMLRPC client for the server is created and returned
732 to the caller.
733
734 Note that the process of creating an XMLRPC client makes no
735 attempt to connect to the remote server; the caller is
736 responsible for determining whether the server is running
737 correctly, and is ready to serve requests.
738
Christopher Wileyd78249a2013-03-01 13:05:31 -0800739 Optionally, the caller can pass ready_test_name, a string
740 containing the name of a method to call on the proxy. This
741 method should take no parameters and return successfully only
742 when the server is ready to process client requests. When
743 ready_test_name is set, xmlrpc_connect will block until the
744 proxy is ready, and throw a TestError if the server isn't
745 ready by timeout_seconds.
746
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700747 @param command Shell command to start the server.
748 @param port Port number on which the server is expected to
749 be serving.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800750 @param command_name String to use as input to `pkill` to
751 terminate the XMLRPC server on the host.
Christopher Wileyd78249a2013-03-01 13:05:31 -0800752 @param ready_test_name String containing the name of a
753 method defined on the XMLRPC server.
754 @param timeout_seconds Number of seconds to wait
755 for the server to become 'ready.' Will throw a
756 TestFail error if server is not ready in time.
757
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700758 """
759 self.xmlrpc_disconnect(port)
760
761 # Chrome OS on the target closes down most external ports
762 # for security. We could open the port, but doing that
763 # would conflict with security tests that check that only
764 # expected ports are open. So, to get to the port on the
765 # target we use an ssh tunnel.
766 local_port = utils.get_unused_port()
767 tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port)
768 ssh_cmd = make_ssh_command(opts=tunnel_options)
769 tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname)
770 logging.debug('Full tunnel command: %s', tunnel_cmd)
771 tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True)
772 logging.debug('Started XMLRPC tunnel, local = %d'
773 ' remote = %d, pid = %d',
774 local_port, port, tunnel_proc.pid)
775
776 # Start the server on the host. Redirection in the command
777 # below is necessary, because 'ssh' won't terminate until
778 # background child processes close stdin, stdout, and
779 # stderr.
780 remote_cmd = '( %s ) </dev/null >/dev/null 2>&1 & echo $!' % command
781 remote_pid = self.run(remote_cmd).stdout.rstrip('\n')
782 logging.debug('Started XMLRPC server on host %s, pid = %s',
783 self.hostname, remote_pid)
784
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800785 self._xmlrpc_proxy_map[port] = (command_name, tunnel_proc)
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700786 rpc_url = 'http://localhost:%d' % local_port
Christopher Wileyd78249a2013-03-01 13:05:31 -0800787 proxy = xmlrpclib.ServerProxy(rpc_url, allow_none=True)
788 if ready_test_name is not None:
789 @retry.retry((socket.error, xmlrpclib.ProtocolError),
790 timeout_min=timeout_seconds/60.0,
791 delay_sec=0.1)
792 def ready_test():
793 """ Call proxy.ready_test_name(). """
794 getattr(proxy, ready_test_name)()
795 successful = False
796 try:
797 logging.info('Waiting %d seconds for XMLRPC server '
798 'to start.', timeout_seconds)
799 ready_test()
800 successful = True
801 except retry.TimeoutException:
802 raise error.TestError('Unable to start XMLRPC server after '
803 '%d seconds.' % timeout_seconds)
804 finally:
805 if not successful:
806 logging.error('Failed to start XMLRPC server.')
807 self.xmlrpc_disconnect(port)
808 logging.info('XMLRPC server started successfully.')
809 return proxy
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700810
811 def xmlrpc_disconnect(self, port):
812 """Disconnect from an XMLRPC server on the host.
813
814 Terminates the remote XMLRPC server previously started for
815 the given `port`. Also closes the local ssh tunnel created
816 for the connection to the host. This function does not
817 directly alter the state of a previously returned XMLRPC
818 client object; however disconnection will cause all
819 subsequent calls to methods on the object to fail.
820
821 This function does nothing if requested to disconnect a port
822 that was not previously connected via `self.xmlrpc_connect()`
823
824 @param port Port number passed to a previous call to
825 `xmlrpc_connect()`
826 """
827 if port not in self._xmlrpc_proxy_map:
828 return
829 entry = self._xmlrpc_proxy_map[port]
830 remote_name = entry[0]
831 tunnel_proc = entry[1]
832 if remote_name:
833 # We use 'pkill' to find our target process rather than
834 # a PID, because the host may have rebooted since
835 # connecting, and we don't want to kill an innocent
836 # process with the same PID.
837 #
838 # 'pkill' helpfully exits with status 1 if no target
839 # process is found, for which run() will throw an
Simran Basid5e5e272012-09-24 15:23:59 -0700840 # exception. We don't want that, so we the ignore
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700841 # status.
842 self.run("pkill -f '%s'" % remote_name, ignore_status=True)
843
844 if tunnel_proc.poll() is None:
845 tunnel_proc.terminate()
846 logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid)
847 else:
848 logging.debug('Tunnel pid %d terminated early, status %d',
849 tunnel_proc.pid, tunnel_proc.returncode)
850 del self._xmlrpc_proxy_map[port]
851
852
853 def xmlrpc_disconnect_all(self):
854 """Disconnect all known XMLRPC proxy ports."""
855 for port in self._xmlrpc_proxy_map.keys():
856 self.xmlrpc_disconnect(port)
857
858
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800859 def _ping_check_status(self, status):
860 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700861
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800862 @param status Check the ping status against this value.
863 @return True iff `status` and the result of ping are the same
864 (i.e. both True or both False).
865
866 """
867 ping_val = utils.ping(self.hostname, tries=1, deadline=1)
868 return not (status ^ (ping_val == 0))
869
870 def _ping_wait_for_status(self, status, timeout):
871 """Wait for the host to have a given status (UP or DOWN).
872
873 Status is checked by polling. Polling will not last longer
874 than the number of seconds in `timeout`. The polling
875 interval will be long enough that only approximately
876 _PING_WAIT_COUNT polling cycles will be executed, subject
877 to a maximum interval of about one minute.
878
879 @param status Waiting will stop immediately if `ping` of the
880 host returns this status.
881 @param timeout Poll for at most this many seconds.
882 @return True iff the host status from `ping` matched the
883 requested status at the time of return.
884
885 """
886 # _ping_check_status() takes about 1 second, hence the
887 # "- 1" in the formula below.
888 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
889 end_time = time.time() + timeout
890 while time.time() <= end_time:
891 if self._ping_check_status(status):
892 return True
893 if poll_interval > 0:
894 time.sleep(poll_interval)
895
896 # The last thing we did was sleep(poll_interval), so it may
897 # have been too long since the last `ping`. Check one more
898 # time, just to be sure.
899 return self._ping_check_status(status)
900
901 def ping_wait_up(self, timeout):
902 """Wait for the host to respond to `ping`.
903
904 N.B. This method is not a reliable substitute for
905 `wait_up()`, because a host that responds to ping will not
906 necessarily respond to ssh. This method should only be used
907 if the target DUT can be considered functional even if it
908 can't be reached via ssh.
909
910 @param timeout Minimum time to allow before declaring the
911 host to be non-responsive.
912 @return True iff the host answered to ping before the timeout.
913
914 """
915 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700916
Andrew Bresticker678c0c72013-01-22 10:44:09 -0800917 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700918 """Wait until the host no longer responds to `ping`.
919
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800920 This function can be used as a slightly faster version of
921 `wait_down()`, by avoiding potentially long ssh timeouts.
922
923 @param timeout Minimum time to allow for the host to become
924 non-responsive.
925 @return True iff the host quit answering ping before the
926 timeout.
927
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700928 """
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800929 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700930
931 def test_wait_for_sleep(self):
932 """Wait for the client to enter low-power sleep mode.
933
934 The test for "is asleep" can't distinguish a system that is
935 powered off; to confirm that the unit was asleep, it is
936 necessary to force resume, and then call
937 `test_wait_for_resume()`.
938
939 This function is expected to be called from a test as part
940 of a sequence like the following:
941
942 ~~~~~~~~
943 boot_id = host.get_boot_id()
944 # trigger sleep on the host
945 host.test_wait_for_sleep()
946 # trigger resume on the host
947 host.test_wait_for_resume(boot_id)
948 ~~~~~~~~
949
950 @exception TestFail The host did not go to sleep within
951 the allowed time.
952 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -0800953 if not self.ping_wait_down(timeout=self.SLEEP_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700954 raise error.TestFail(
955 'client failed to sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700956 self.SLEEP_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700957
958
959 def test_wait_for_resume(self, old_boot_id):
960 """Wait for the client to resume from low-power sleep mode.
961
962 The `old_boot_id` parameter should be the value from
963 `get_boot_id()` obtained prior to entering sleep mode. A
964 `TestFail` exception is raised if the boot id changes.
965
966 See @ref test_wait_for_sleep for more on this function's
967 usage.
968
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800969 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700970 target host went to sleep.
971
972 @exception TestFail The host did not respond within the
973 allowed time.
974 @exception TestFail The host responded, but the boot id test
975 indicated a reboot rather than a sleep
976 cycle.
977 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700978 if not self.wait_up(timeout=self.RESUME_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700979 raise error.TestFail(
980 'client failed to resume from sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700981 self.RESUME_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700982 else:
983 new_boot_id = self.get_boot_id()
984 if new_boot_id != old_boot_id:
985 raise error.TestFail(
986 'client rebooted, but sleep was expected'
987 ' (old boot %s, new boot %s)'
988 % (old_boot_id, new_boot_id))
989
990
991 def test_wait_for_shutdown(self):
992 """Wait for the client to shut down.
993
994 The test for "has shut down" can't distinguish a system that
995 is merely asleep; to confirm that the unit was down, it is
996 necessary to force boot, and then call test_wait_for_boot().
997
998 This function is expected to be called from a test as part
999 of a sequence like the following:
1000
1001 ~~~~~~~~
1002 boot_id = host.get_boot_id()
1003 # trigger shutdown on the host
1004 host.test_wait_for_shutdown()
1005 # trigger boot on the host
1006 host.test_wait_for_boot(boot_id)
1007 ~~~~~~~~
1008
1009 @exception TestFail The host did not shut down within the
1010 allowed time.
1011 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001012 if not self.ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001013 raise error.TestFail(
1014 'client failed to shut down after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001015 self.SHUTDOWN_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001016
1017
1018 def test_wait_for_boot(self, old_boot_id=None):
1019 """Wait for the client to boot from cold power.
1020
1021 The `old_boot_id` parameter should be the value from
1022 `get_boot_id()` obtained prior to shutting down. A
1023 `TestFail` exception is raised if the boot id does not
1024 change. The boot id test is omitted if `old_boot_id` is not
1025 specified.
1026
1027 See @ref test_wait_for_shutdown for more on this function's
1028 usage.
1029
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001030 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001031 shut down.
1032
1033 @exception TestFail The host did not respond within the
1034 allowed time.
1035 @exception TestFail The host responded, but the boot id test
1036 indicated that there was no reboot.
1037 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001038 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001039 raise error.TestFail(
1040 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001041 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001042 elif old_boot_id:
1043 if self.get_boot_id() == old_boot_id:
1044 raise error.TestFail(
1045 'client is back up, but did not reboot'
1046 ' (boot %s)' % old_boot_id)
Simran Basid5e5e272012-09-24 15:23:59 -07001047
1048
1049 @staticmethod
1050 def check_for_rpm_support(hostname):
1051 """For a given hostname, return whether or not it is powered by an RPM.
1052
1053 @return None if this host does not follows the defined naming format
1054 for RPM powered DUT's in the lab. If it does follow the format,
1055 it returns a regular expression MatchObject instead.
1056 """
Richard Barnette82c35912012-11-20 10:09:10 -08001057 return re.match(SiteHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001058
1059
1060 def has_power(self):
1061 """For this host, return whether or not it is powered by an RPM.
1062
1063 @return True if this host is in the CROS lab and follows the defined
1064 naming format.
1065 """
1066 return SiteHost.check_for_rpm_support(self.hostname)
1067
1068
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001069 def _set_power(self, state, power_method):
1070 """Sets the power to the host via RPM, Servo or manual.
1071
1072 @param state Specifies which power state to set to DUT
1073 @param power_method Specifies which method of power control to
1074 use. By default "RPM" will be used. Valid values
1075 are the strings "RPM", "manual", "servoj10".
1076
1077 """
1078 ACCEPTABLE_STATES = ['ON', 'OFF']
1079
1080 if state.upper() not in ACCEPTABLE_STATES:
1081 raise error.TestError('State must be one of: %s.'
1082 % (ACCEPTABLE_STATES,))
1083
1084 if power_method == self.POWER_CONTROL_SERVO:
1085 logging.info('Setting servo port J10 to %s', state)
1086 self.servo.set('prtctl3_pwren', state.lower())
1087 time.sleep(self._USB_POWER_TIMEOUT)
1088 elif power_method == self.POWER_CONTROL_MANUAL:
1089 logging.info('You have %d seconds to set the AC power to %s.',
1090 self._POWER_CYCLE_TIMEOUT, state)
1091 time.sleep(self._POWER_CYCLE_TIMEOUT)
1092 else:
1093 if not self.has_power():
1094 raise error.TestFail('DUT does not have RPM connected.')
1095 rpm_client.set_power(self.hostname, state.upper())
Simran Basid5e5e272012-09-24 15:23:59 -07001096
1097
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001098 def power_off(self, power_method=POWER_CONTROL_RPM):
1099 """Turn off power to this host via RPM, Servo or manual.
1100
1101 @param power_method Specifies which method of power control to
1102 use. By default "RPM" will be used. Valid values
1103 are the strings "RPM", "manual", "servoj10".
1104
1105 """
1106 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07001107
1108
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001109 def power_on(self, power_method=POWER_CONTROL_RPM):
1110 """Turn on power to this host via RPM, Servo or manual.
1111
1112 @param power_method Specifies which method of power control to
1113 use. By default "RPM" will be used. Valid values
1114 are the strings "RPM", "manual", "servoj10".
1115
1116 """
1117 self._set_power('ON', power_method)
1118
1119
1120 def power_cycle(self, power_method=POWER_CONTROL_RPM):
1121 """Cycle power to this host by turning it OFF, then ON.
1122
1123 @param power_method Specifies which method of power control to
1124 use. By default "RPM" will be used. Valid values
1125 are the strings "RPM", "manual", "servoj10".
1126
1127 """
1128 if power_method in (self.POWER_CONTROL_SERVO,
1129 self.POWER_CONTROL_MANUAL):
1130 self.power_off(power_method=power_method)
1131 time.sleep(self._POWER_CYCLE_TIMEOUT)
1132 self.power_on(power_method=power_method)
1133 else:
1134 rpm_client.set_power(self.hostname, 'CYCLE')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001135
1136
1137 def get_platform(self):
1138 """Determine the correct platform label for this host.
1139
1140 @returns a string representing this host's platform.
1141 """
1142 crossystem = utils.Crossystem(self)
1143 crossystem.init()
1144 # Extract fwid value and use the leading part as the platform id.
1145 # fwid generally follow the format of {platform}.{firmware version}
1146 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
1147 platform = crossystem.fwid().split('.')[0].lower()
1148 # Newer platforms start with 'Google_' while the older ones do not.
1149 return platform.replace('google_', '')
1150
1151
Aviv Keshet74c89a92013-02-04 15:18:30 -08001152 @label_decorator()
Simran Basic6f1f7a2012-10-16 10:47:46 -07001153 def get_board(self):
1154 """Determine the correct board label for this host.
1155
1156 @returns a string representing this host's board.
1157 """
1158 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
1159 run_method=self.run)
1160 board = release_info['CHROMEOS_RELEASE_BOARD']
1161 # Devices in the lab generally have the correct board name but our own
1162 # development devices have {board_name}-signed-{key_type}. The board
1163 # name may also begin with 'x86-' which we need to keep.
1164 if 'x86' not in board:
1165 return 'board:%s' % board.split('-')[0]
1166 return 'board:%s' % '-'.join(board.split('-')[0:2])
1167
1168
Aviv Keshet74c89a92013-02-04 15:18:30 -08001169 @label_decorator('lightsensor')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001170 def has_lightsensor(self):
1171 """Determine the correct board label for this host.
1172
1173 @returns the string 'lightsensor' if this host has a lightsensor or
1174 None if it does not.
1175 """
1176 search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % (
Richard Barnette82c35912012-11-20 10:09:10 -08001177 self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES))
Simran Basic6f1f7a2012-10-16 10:47:46 -07001178 try:
1179 # Run the search cmd following the symlinks. Stderr_tee is set to
1180 # None as there can be a symlink loop, but the command will still
1181 # execute correctly with a few messages printed to stderr.
1182 self.run(search_cmd, stdout_tee=None, stderr_tee=None)
1183 return 'lightsensor'
1184 except error.AutoservRunError:
1185 # egrep exited with a return code of 1 meaning none of the possible
1186 # lightsensor files existed.
1187 return None
1188
1189
Aviv Keshet74c89a92013-02-04 15:18:30 -08001190 @label_decorator('bluetooth')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001191 def has_bluetooth(self):
1192 """Determine the correct board label for this host.
1193
1194 @returns the string 'bluetooth' if this host has bluetooth or
1195 None if it does not.
1196 """
1197 try:
1198 self.run('test -d /sys/class/bluetooth/hci0')
1199 # test exited with a return code of 0.
1200 return 'bluetooth'
1201 except error.AutoservRunError:
1202 # test exited with a return code 1 meaning the directory did not
1203 # exist.
1204 return None
1205
1206
1207 def get_labels(self):
1208 """Return a list of labels for this given host.
1209
1210 This is the main way to retrieve all the automatic labels for a host
1211 as it will run through all the currently implemented label functions.
1212 """
1213 labels = []
Richard Barnette82c35912012-11-20 10:09:10 -08001214 for label_function in self._LABEL_FUNCTIONS:
Simran Basic6f1f7a2012-10-16 10:47:46 -07001215 label = label_function(self)
1216 if label:
1217 labels.append(label)
1218 return labels