J. Richard Barnette | 24adbf4 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Dale Curtis | aa5eedb | 2011-08-23 16:18:52 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 5 | import logging |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 6 | import re |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 7 | import subprocess |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 8 | import time |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 9 | import xmlrpclib |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 10 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 11 | from autotest_lib.client.bin import utils |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import global_config, error |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib.cros import autoupdater |
| 14 | from autotest_lib.server import autoserv_parser |
| 15 | from autotest_lib.server import site_host_attributes |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 16 | from autotest_lib.server.cros import servo |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 17 | from autotest_lib.server.hosts import remote |
J. Richard Barnette | 24adbf4 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 18 | |
| 19 | |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 20 | RPM_FRONTEND_URI = global_config.global_config.get_config_value('CROS', |
| 21 | 'rpm_frontend_uri', type=str, default='') |
| 22 | |
| 23 | |
| 24 | class RemotePowerException(Exception): |
| 25 | """This is raised when we fail to set the state of the device's outlet.""" |
| 26 | pass |
| 27 | |
| 28 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 29 | def make_ssh_command(user='root', port=22, opts='', hosts_file=None, |
| 30 | connect_timeout=None, alive_interval=None): |
| 31 | """Override default make_ssh_command to use options tuned for Chrome OS. |
| 32 | |
| 33 | Tuning changes: |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 34 | - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH connection |
| 35 | failure. Consistency with remote_access.sh. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 36 | |
Dale Curtis | aa5eedb | 2011-08-23 16:18:52 -0700 | [diff] [blame] | 37 | - ServerAliveInterval=180; which causes SSH to ping connection every |
| 38 | 180 seconds. In conjunction with ServerAliveCountMax ensures that if the |
| 39 | connection dies, Autotest will bail out quickly. Originally tried 60 secs, |
| 40 | but saw frequent job ABORTS where the test completed successfully. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 41 | |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 42 | - ServerAliveCountMax=3; consistency with remote_access.sh. |
| 43 | |
| 44 | - ConnectAttempts=4; reduce flakiness in connection errors; consistency |
| 45 | with remote_access.sh. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 46 | |
| 47 | - UserKnownHostsFile=/dev/null; we don't care about the keys. Host keys |
| 48 | change with every new installation, don't waste memory/space saving them. |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 49 | |
| 50 | - SSH protocol forced to 2; needed for ServerAliveInterval. |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 51 | """ |
| 52 | base_command = ('/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no' |
| 53 | ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes' |
Chris Sosa | f7fcd6e | 2011-09-27 17:30:47 -0700 | [diff] [blame] | 54 | ' -o ConnectTimeout=30 -o ServerAliveInterval=180' |
| 55 | ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4' |
| 56 | ' -o Protocol=2 -l %s -p %d') |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 57 | return base_command % (opts, user, port) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class SiteHost(remote.RemoteHost): |
| 61 | """Chromium OS specific subclass of Host.""" |
| 62 | |
| 63 | _parser = autoserv_parser.autoserv_parser |
| 64 | |
| 65 | # Time to wait for new kernel to be marked successful. |
Chris Masone | 163cead | 2012-05-16 11:49:48 -0700 | [diff] [blame] | 66 | _KERNEL_UPDATE_TIMEOUT = 120 |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 67 | |
| 68 | # Ephemeral file to indicate that an update has just occurred. |
| 69 | _JUST_UPDATED_FLAG = '/tmp/just_updated' |
| 70 | |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 71 | # Timeout values associated with various Chrome OS state |
| 72 | # changes. In general, the timeouts are the maximum time to |
| 73 | # allow between some event X, and the time that the unit is |
| 74 | # on (or off) the network. Note that "time to get on the |
| 75 | # network" is typically longer than the time to complete the |
| 76 | # operation. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 77 | # |
| 78 | # TODO(jrbarnette): None of these times have been thoroughly |
| 79 | # tested empirically; if timeouts are a problem, increasing the |
| 80 | # time limit really might be the right answer. |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 81 | # |
| 82 | # SLEEP_TIMEOUT: Time to allow for suspend to memory. |
| 83 | # RESUME_TIMEOUT: Time to allow for resume after suspend. |
| 84 | # BOOT_TIMEOUT: Time to allow for boot from power off. Among |
| 85 | # other things, this includes time for the 30 second dev-mode |
| 86 | # screen delay, |
| 87 | # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device, |
| 88 | # including the 30 second dev-mode delay. |
| 89 | # SHUTDOWN_TIMEOUT: Time to allow to shut down. |
| 90 | # REBOOT_TIMEOUT: Combination of shutdown and reboot times. |
| 91 | |
| 92 | SLEEP_TIMEOUT = 2 |
| 93 | RESUME_TIMEOUT = 5 |
| 94 | BOOT_TIMEOUT = 45 |
| 95 | USB_BOOT_TIMEOUT = 150 |
| 96 | SHUTDOWN_TIMEOUT = 5 |
| 97 | REBOOT_TIMEOUT = SHUTDOWN_TIMEOUT + BOOT_TIMEOUT |
Chris Sosa | e146ed8 | 2012-09-19 17:58:36 -0700 | [diff] [blame] | 98 | LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine' |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 99 | RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-' |
| 100 | 'host[0-9]+') |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 101 | |
| 102 | |
J. Richard Barnette | 55fb806 | 2012-05-23 10:29:31 -0700 | [diff] [blame] | 103 | def _initialize(self, hostname, servo_host=None, servo_port=None, |
| 104 | *args, **dargs): |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 105 | """Initialize superclasses, and |self.servo|. |
| 106 | |
| 107 | For creating the host servo object, there are three |
| 108 | possibilities: First, if the host is a lab system known to |
| 109 | have a servo board, we connect to that servo unconditionally. |
| 110 | Second, if we're called from a control file that requires |
J. Richard Barnette | 55fb806 | 2012-05-23 10:29:31 -0700 | [diff] [blame] | 111 | servo features for testing, it will pass settings for |
| 112 | `servo_host`, `servo_port`, or both. If neither of these |
| 113 | cases apply, `self.servo` will be `None`. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 114 | |
| 115 | """ |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 116 | super(SiteHost, self)._initialize(hostname=hostname, |
| 117 | *args, **dargs) |
J. Richard Barnette | f085985 | 2012-08-20 14:55:50 -0700 | [diff] [blame] | 118 | # self.env is a dictionary of environment variable settings |
| 119 | # to be exported for commands run on the host. |
| 120 | # LIBC_FATAL_STDERR_ can be useful for diagnosing certain |
| 121 | # errors that might happen. |
| 122 | self.env['LIBC_FATAL_STDERR_'] = '1' |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 123 | self._xmlrpc_proxy_map = {} |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 124 | self.servo = servo.Servo.get_lab_servo(hostname) |
J. Richard Barnette | 55fb806 | 2012-05-23 10:29:31 -0700 | [diff] [blame] | 125 | if not self.servo: |
| 126 | # The Servo constructor generally doesn't accept 'None' |
| 127 | # for its parameters. |
| 128 | if servo_host is not None: |
| 129 | if servo_port is not None: |
| 130 | self.servo = servo.Servo(servo_host=servo_host, |
| 131 | servo_port=servo_port) |
| 132 | else: |
| 133 | self.servo = servo.Servo(servo_host=servo_host) |
| 134 | elif servo_port is not None: |
| 135 | self.servo = servo.Servo(servo_port=servo_port) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 136 | |
| 137 | |
Chris Sosa | a3ac215 | 2012-05-23 22:23:13 -0700 | [diff] [blame] | 138 | def machine_install(self, update_url=None, force_update=False, |
| 139 | local_devserver=False): |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 140 | if not update_url and self._parser.options.image: |
| 141 | update_url = self._parser.options.image |
| 142 | elif not update_url: |
| 143 | raise autoupdater.ChromiumOSError( |
| 144 | 'Update failed. No update URL provided.') |
| 145 | |
| 146 | # Attempt to update the system. |
Chris Sosa | a3ac215 | 2012-05-23 22:23:13 -0700 | [diff] [blame] | 147 | updater = autoupdater.ChromiumOSUpdater(update_url, host=self, |
| 148 | local_devserver=local_devserver) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 149 | if updater.run_update(force_update): |
| 150 | # Figure out active and inactive kernel. |
| 151 | active_kernel, inactive_kernel = updater.get_kernel_state() |
| 152 | |
| 153 | # Ensure inactive kernel has higher priority than active. |
| 154 | if (updater.get_kernel_priority(inactive_kernel) |
| 155 | < updater.get_kernel_priority(active_kernel)): |
| 156 | raise autoupdater.ChromiumOSError( |
| 157 | 'Update failed. The priority of the inactive kernel' |
| 158 | ' partition is less than that of the active kernel' |
| 159 | ' partition.') |
| 160 | |
Scott Zawalski | 2190200 | 2012-09-19 17:57:00 -0400 | [diff] [blame] | 161 | update_engine_log = '/var/log/update_engine.log' |
| 162 | logging.info('Dumping %s', update_engine_log) |
| 163 | self.run('cat %s' % update_engine_log) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 164 | # Updater has returned, successfully, reboot the host. |
| 165 | self.reboot(timeout=60, wait=True) |
Chris Sosa | e146ed8 | 2012-09-19 17:58:36 -0700 | [diff] [blame] | 166 | # Touch the lab machine file to leave a marker that distinguishes |
| 167 | # this image from other test images. |
| 168 | self.run('touch %s' % self.LAB_MACHINE_FILE) |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 169 | |
| 170 | # Following the reboot, verify the correct version. |
| 171 | updater.check_version() |
| 172 | |
| 173 | # Figure out newly active kernel. |
| 174 | new_active_kernel, _ = updater.get_kernel_state() |
| 175 | |
| 176 | # Ensure that previously inactive kernel is now the active kernel. |
| 177 | if new_active_kernel != inactive_kernel: |
| 178 | raise autoupdater.ChromiumOSError( |
| 179 | 'Update failed. New kernel partition is not active after' |
| 180 | ' boot.') |
| 181 | |
| 182 | host_attributes = site_host_attributes.HostAttributes(self.hostname) |
| 183 | if host_attributes.has_chromeos_firmware: |
| 184 | # Wait until tries == 0 and success, or until timeout. |
| 185 | utils.poll_for_condition( |
| 186 | lambda: (updater.get_kernel_tries(new_active_kernel) == 0 |
| 187 | and updater.get_kernel_success(new_active_kernel)), |
| 188 | exception=autoupdater.ChromiumOSError( |
| 189 | 'Update failed. Timed out waiting for system to mark' |
| 190 | ' new kernel as successful.'), |
| 191 | timeout=self._KERNEL_UPDATE_TIMEOUT, sleep_interval=5) |
| 192 | |
| 193 | # TODO(dalecurtis): Hack for R12 builds to make sure BVT runs of |
| 194 | # platform_Shutdown pass correctly. |
| 195 | if updater.update_version.startswith('0.12'): |
| 196 | self.reboot(timeout=60, wait=True) |
| 197 | |
| 198 | # Mark host as recently updated. Hosts are rebooted at the end of |
| 199 | # every test cycle which will remove the file. |
| 200 | self.run('touch %s' % self._JUST_UPDATED_FLAG) |
| 201 | |
| 202 | # Clean up any old autotest directories which may be lying around. |
| 203 | for path in global_config.global_config.get_config_value( |
| 204 | 'AUTOSERV', 'client_autodir_paths', type=list): |
| 205 | self.run('rm -rf ' + path) |
| 206 | |
| 207 | |
| 208 | def has_just_updated(self): |
| 209 | """Indicates whether the host was updated within this boot.""" |
| 210 | # Check for the existence of the just updated flag file. |
| 211 | return self.run( |
| 212 | '[ -f %s ] && echo T || echo F' |
| 213 | % self._JUST_UPDATED_FLAG).stdout.strip() == 'T' |
| 214 | |
| 215 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 216 | def close(self): |
| 217 | super(SiteHost, self).close() |
| 218 | self.xmlrpc_disconnect_all() |
| 219 | |
| 220 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 221 | def cleanup(self): |
| 222 | """Special cleanup method to make sure hosts always get power back.""" |
Chris Sosa | 9479fcd | 2012-10-09 13:44:22 -0700 | [diff] [blame] | 223 | super(SiteHost, self).cleanup() |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 224 | if self.has_power(): |
| 225 | self.power_on() |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 226 | |
| 227 | |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 228 | def reboot(self, **dargs): |
| 229 | """ |
| 230 | This function reboots the site host. The more generic |
| 231 | RemoteHost.reboot() performs sync and sleeps for 5 |
| 232 | seconds. This is not necessary for Chrome OS devices as the |
| 233 | sync should be finished in a short time during the reboot |
| 234 | command. |
| 235 | """ |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 236 | if 'reboot_cmd' not in dargs: |
| 237 | dargs['reboot_cmd'] = ('((reboot & sleep 10; reboot -f &)' |
| 238 | ' </dev/null >/dev/null 2>&1 &)') |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 239 | # Enable fastsync to avoid running extra sync commands before reboot. |
Tom Wai-Hong Tam | f5cd1d4 | 2012-08-13 12:04:08 +0800 | [diff] [blame] | 240 | if 'fastsync' not in dargs: |
| 241 | dargs['fastsync'] = True |
Yu-Ju Hong | a2be94a | 2012-07-31 09:48:52 -0700 | [diff] [blame] | 242 | super(SiteHost, self).reboot(**dargs) |
| 243 | |
| 244 | |
J. Richard Barnette | 45e93de | 2012-04-11 17:24:15 -0700 | [diff] [blame] | 245 | def verify_software(self): |
| 246 | """Ensure the stateful partition has space for Autotest and updates. |
| 247 | |
| 248 | Similar to what is done by AbstractSSH, except instead of checking the |
| 249 | Autotest installation path, just check the stateful partition. |
| 250 | |
| 251 | Checking the stateful partition is preferable in case it has been wiped, |
| 252 | resulting in an Autotest installation path which doesn't exist and isn't |
| 253 | writable. We still want to pass verify in this state since the partition |
| 254 | will be recovered with the next install. |
| 255 | """ |
| 256 | super(SiteHost, self).verify_software() |
| 257 | self.check_diskspace( |
| 258 | '/mnt/stateful_partition', |
| 259 | global_config.global_config.get_config_value( |
| 260 | 'SERVER', 'gb_diskspace_required', type=int, |
| 261 | default=20)) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 262 | |
| 263 | |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 264 | def xmlrpc_connect(self, command, port, cleanup=None): |
| 265 | """Connect to an XMLRPC server on the host. |
| 266 | |
| 267 | The `command` argument should be a simple shell command that |
| 268 | starts an XMLRPC server on the given `port`. The command |
| 269 | must not daemonize, and must terminate cleanly on SIGTERM. |
| 270 | The command is started in the background on the host, and a |
| 271 | local XMLRPC client for the server is created and returned |
| 272 | to the caller. |
| 273 | |
| 274 | Note that the process of creating an XMLRPC client makes no |
| 275 | attempt to connect to the remote server; the caller is |
| 276 | responsible for determining whether the server is running |
| 277 | correctly, and is ready to serve requests. |
| 278 | |
| 279 | @param command Shell command to start the server. |
| 280 | @param port Port number on which the server is expected to |
| 281 | be serving. |
| 282 | """ |
| 283 | self.xmlrpc_disconnect(port) |
| 284 | |
| 285 | # Chrome OS on the target closes down most external ports |
| 286 | # for security. We could open the port, but doing that |
| 287 | # would conflict with security tests that check that only |
| 288 | # expected ports are open. So, to get to the port on the |
| 289 | # target we use an ssh tunnel. |
| 290 | local_port = utils.get_unused_port() |
| 291 | tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port) |
| 292 | ssh_cmd = make_ssh_command(opts=tunnel_options) |
| 293 | tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname) |
| 294 | logging.debug('Full tunnel command: %s', tunnel_cmd) |
| 295 | tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True) |
| 296 | logging.debug('Started XMLRPC tunnel, local = %d' |
| 297 | ' remote = %d, pid = %d', |
| 298 | local_port, port, tunnel_proc.pid) |
| 299 | |
| 300 | # Start the server on the host. Redirection in the command |
| 301 | # below is necessary, because 'ssh' won't terminate until |
| 302 | # background child processes close stdin, stdout, and |
| 303 | # stderr. |
| 304 | remote_cmd = '( %s ) </dev/null >/dev/null 2>&1 & echo $!' % command |
| 305 | remote_pid = self.run(remote_cmd).stdout.rstrip('\n') |
| 306 | logging.debug('Started XMLRPC server on host %s, pid = %s', |
| 307 | self.hostname, remote_pid) |
| 308 | |
| 309 | self._xmlrpc_proxy_map[port] = (cleanup, tunnel_proc) |
| 310 | rpc_url = 'http://localhost:%d' % local_port |
| 311 | return xmlrpclib.ServerProxy(rpc_url, allow_none=True) |
| 312 | |
| 313 | |
| 314 | def xmlrpc_disconnect(self, port): |
| 315 | """Disconnect from an XMLRPC server on the host. |
| 316 | |
| 317 | Terminates the remote XMLRPC server previously started for |
| 318 | the given `port`. Also closes the local ssh tunnel created |
| 319 | for the connection to the host. This function does not |
| 320 | directly alter the state of a previously returned XMLRPC |
| 321 | client object; however disconnection will cause all |
| 322 | subsequent calls to methods on the object to fail. |
| 323 | |
| 324 | This function does nothing if requested to disconnect a port |
| 325 | that was not previously connected via `self.xmlrpc_connect()` |
| 326 | |
| 327 | @param port Port number passed to a previous call to |
| 328 | `xmlrpc_connect()` |
| 329 | """ |
| 330 | if port not in self._xmlrpc_proxy_map: |
| 331 | return |
| 332 | entry = self._xmlrpc_proxy_map[port] |
| 333 | remote_name = entry[0] |
| 334 | tunnel_proc = entry[1] |
| 335 | if remote_name: |
| 336 | # We use 'pkill' to find our target process rather than |
| 337 | # a PID, because the host may have rebooted since |
| 338 | # connecting, and we don't want to kill an innocent |
| 339 | # process with the same PID. |
| 340 | # |
| 341 | # 'pkill' helpfully exits with status 1 if no target |
| 342 | # process is found, for which run() will throw an |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 343 | # exception. We don't want that, so we the ignore |
J. Richard Barnette | 1d78b01 | 2012-05-15 13:56:30 -0700 | [diff] [blame] | 344 | # status. |
| 345 | self.run("pkill -f '%s'" % remote_name, ignore_status=True) |
| 346 | |
| 347 | if tunnel_proc.poll() is None: |
| 348 | tunnel_proc.terminate() |
| 349 | logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid) |
| 350 | else: |
| 351 | logging.debug('Tunnel pid %d terminated early, status %d', |
| 352 | tunnel_proc.pid, tunnel_proc.returncode) |
| 353 | del self._xmlrpc_proxy_map[port] |
| 354 | |
| 355 | |
| 356 | def xmlrpc_disconnect_all(self): |
| 357 | """Disconnect all known XMLRPC proxy ports.""" |
| 358 | for port in self._xmlrpc_proxy_map.keys(): |
| 359 | self.xmlrpc_disconnect(port) |
| 360 | |
| 361 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 362 | def _ping_is_up(self): |
| 363 | """Ping the host once, and return whether it responded.""" |
| 364 | return utils.ping(self.hostname, tries=1, deadline=1) == 0 |
| 365 | |
| 366 | |
| 367 | def _ping_wait_down(self, timeout): |
| 368 | """Wait until the host no longer responds to `ping`. |
| 369 | |
| 370 | @param timeout Minimum time to allow before declaring the |
| 371 | host to be non-responsive. |
| 372 | """ |
| 373 | |
| 374 | # This function is a slightly faster version of wait_down(). |
| 375 | # |
| 376 | # In AbstractSSHHost.wait_down(), `ssh` is used to determine |
| 377 | # whether the host is down. In some situations (mine, at |
| 378 | # least), `ssh` can take over a minute to determine that the |
| 379 | # host is down. The `ping` command answers the question |
| 380 | # faster, so we use that here instead. |
| 381 | # |
| 382 | # There is no equivalent for wait_up(), because a target that |
| 383 | # answers to `ping` won't necessarily respond to `ssh`. |
| 384 | end_time = time.time() + timeout |
| 385 | while time.time() <= end_time: |
| 386 | if not self._ping_is_up(): |
| 387 | return True |
| 388 | |
| 389 | # If the timeout is short relative to the run time of |
| 390 | # _ping_is_up(), we might be prone to false failures for |
| 391 | # lack of checking frequently enough. To be safe, we make |
| 392 | # one last check _after_ the deadline. |
| 393 | return not self._ping_is_up() |
| 394 | |
| 395 | |
| 396 | def test_wait_for_sleep(self): |
| 397 | """Wait for the client to enter low-power sleep mode. |
| 398 | |
| 399 | The test for "is asleep" can't distinguish a system that is |
| 400 | powered off; to confirm that the unit was asleep, it is |
| 401 | necessary to force resume, and then call |
| 402 | `test_wait_for_resume()`. |
| 403 | |
| 404 | This function is expected to be called from a test as part |
| 405 | of a sequence like the following: |
| 406 | |
| 407 | ~~~~~~~~ |
| 408 | boot_id = host.get_boot_id() |
| 409 | # trigger sleep on the host |
| 410 | host.test_wait_for_sleep() |
| 411 | # trigger resume on the host |
| 412 | host.test_wait_for_resume(boot_id) |
| 413 | ~~~~~~~~ |
| 414 | |
| 415 | @exception TestFail The host did not go to sleep within |
| 416 | the allowed time. |
| 417 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 418 | if not self._ping_wait_down(timeout=self.SLEEP_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 419 | raise error.TestFail( |
| 420 | 'client failed to sleep after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 421 | self.SLEEP_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 422 | |
| 423 | |
| 424 | def test_wait_for_resume(self, old_boot_id): |
| 425 | """Wait for the client to resume from low-power sleep mode. |
| 426 | |
| 427 | The `old_boot_id` parameter should be the value from |
| 428 | `get_boot_id()` obtained prior to entering sleep mode. A |
| 429 | `TestFail` exception is raised if the boot id changes. |
| 430 | |
| 431 | See @ref test_wait_for_sleep for more on this function's |
| 432 | usage. |
| 433 | |
| 434 | @param[in] old_boot_id A boot id value obtained before the |
| 435 | target host went to sleep. |
| 436 | |
| 437 | @exception TestFail The host did not respond within the |
| 438 | allowed time. |
| 439 | @exception TestFail The host responded, but the boot id test |
| 440 | indicated a reboot rather than a sleep |
| 441 | cycle. |
| 442 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 443 | if not self.wait_up(timeout=self.RESUME_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 444 | raise error.TestFail( |
| 445 | 'client failed to resume from sleep after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 446 | self.RESUME_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 447 | else: |
| 448 | new_boot_id = self.get_boot_id() |
| 449 | if new_boot_id != old_boot_id: |
| 450 | raise error.TestFail( |
| 451 | 'client rebooted, but sleep was expected' |
| 452 | ' (old boot %s, new boot %s)' |
| 453 | % (old_boot_id, new_boot_id)) |
| 454 | |
| 455 | |
| 456 | def test_wait_for_shutdown(self): |
| 457 | """Wait for the client to shut down. |
| 458 | |
| 459 | The test for "has shut down" can't distinguish a system that |
| 460 | is merely asleep; to confirm that the unit was down, it is |
| 461 | necessary to force boot, and then call test_wait_for_boot(). |
| 462 | |
| 463 | This function is expected to be called from a test as part |
| 464 | of a sequence like the following: |
| 465 | |
| 466 | ~~~~~~~~ |
| 467 | boot_id = host.get_boot_id() |
| 468 | # trigger shutdown on the host |
| 469 | host.test_wait_for_shutdown() |
| 470 | # trigger boot on the host |
| 471 | host.test_wait_for_boot(boot_id) |
| 472 | ~~~~~~~~ |
| 473 | |
| 474 | @exception TestFail The host did not shut down within the |
| 475 | allowed time. |
| 476 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 477 | if not self._ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 478 | raise error.TestFail( |
| 479 | 'client failed to shut down after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 480 | self.SHUTDOWN_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 481 | |
| 482 | |
| 483 | def test_wait_for_boot(self, old_boot_id=None): |
| 484 | """Wait for the client to boot from cold power. |
| 485 | |
| 486 | The `old_boot_id` parameter should be the value from |
| 487 | `get_boot_id()` obtained prior to shutting down. A |
| 488 | `TestFail` exception is raised if the boot id does not |
| 489 | change. The boot id test is omitted if `old_boot_id` is not |
| 490 | specified. |
| 491 | |
| 492 | See @ref test_wait_for_shutdown for more on this function's |
| 493 | usage. |
| 494 | |
| 495 | @param[in] old_boot_id A boot id value obtained before the |
| 496 | shut down. |
| 497 | |
| 498 | @exception TestFail The host did not respond within the |
| 499 | allowed time. |
| 500 | @exception TestFail The host responded, but the boot id test |
| 501 | indicated that there was no reboot. |
| 502 | """ |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 503 | if not self.wait_up(timeout=self.REBOOT_TIMEOUT): |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 504 | raise error.TestFail( |
| 505 | 'client failed to reboot after %d seconds' % |
J. Richard Barnette | eb69d72 | 2012-06-18 17:29:44 -0700 | [diff] [blame] | 506 | self.REBOOT_TIMEOUT) |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 507 | elif old_boot_id: |
| 508 | if self.get_boot_id() == old_boot_id: |
| 509 | raise error.TestFail( |
| 510 | 'client is back up, but did not reboot' |
| 511 | ' (boot %s)' % old_boot_id) |
Simran Basi | d5e5e27 | 2012-09-24 15:23:59 -0700 | [diff] [blame^] | 512 | |
| 513 | |
| 514 | @staticmethod |
| 515 | def check_for_rpm_support(hostname): |
| 516 | """For a given hostname, return whether or not it is powered by an RPM. |
| 517 | |
| 518 | @return None if this host does not follows the defined naming format |
| 519 | for RPM powered DUT's in the lab. If it does follow the format, |
| 520 | it returns a regular expression MatchObject instead. |
| 521 | """ |
| 522 | return re.match(SiteHost.RPM_HOSTNAME_REGEX, hostname) |
| 523 | |
| 524 | |
| 525 | def has_power(self): |
| 526 | """For this host, return whether or not it is powered by an RPM. |
| 527 | |
| 528 | @return True if this host is in the CROS lab and follows the defined |
| 529 | naming format. |
| 530 | """ |
| 531 | return SiteHost.check_for_rpm_support(self.hostname) |
| 532 | |
| 533 | |
| 534 | def _set_power(self, new_state): |
| 535 | client = xmlrpclib.ServerProxy(RPM_FRONTEND_URI, verbose=False) |
| 536 | if not client.queue_request(self.hostname, new_state): |
| 537 | raise RemotePowerException('Failed to change outlet status for' |
| 538 | 'host: %s to state: %s', self.hostname, |
| 539 | new_state) |
| 540 | |
| 541 | |
| 542 | def power_off(self): |
| 543 | self._set_power('OFF') |
| 544 | |
| 545 | |
| 546 | def power_on(self): |
| 547 | self._set_power('ON') |
| 548 | |
| 549 | |
| 550 | def power_cycle(self): |
| 551 | self._set_power('CYCLE') |