J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -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 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 5 | import logging |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 6 | import os |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 7 | import re |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 8 | import socket |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 9 | import subprocess |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 10 | import time |
| 11 | import xmlrpclib |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 12 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import error |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 14 | from autotest_lib.server import autotest, site_host_attributes, test |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 15 | from autotest_lib.server.cros import servo |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 16 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 17 | class ServoTest(test.test): |
| 18 | """AutoTest test class that creates and destroys a servo object. |
| 19 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 20 | Servo-based server side AutoTests can inherit from this object. |
| 21 | There are 2 remote clients supported: |
| 22 | If use_pyauto flag is True, a remote PyAuto client will be launched; |
| 23 | If use_faft flag is Ture, a remote FAFT client will be launched. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 24 | """ |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 25 | version = 2 |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 26 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 27 | # Exposes RPC access to a remote PyAuto client. |
| 28 | pyauto = None |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 29 | # Exposes RPC access to a remote FAFT client. |
| 30 | faft_client = None |
| 31 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 32 | # Autotest references to the client. |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 33 | _autotest_client = None |
| 34 | # Remote client info list. |
| 35 | _remote_infos = { |
| 36 | 'pyauto': { |
| 37 | # Used or not. |
| 38 | 'used': False, |
| 39 | # Reference name of RPC object in this class. |
| 40 | 'ref_name': 'pyauto', |
| 41 | # Port number of the remote RPC. |
| 42 | 'port': 9988, |
| 43 | # Client test for installing dependency. |
J. Richard Barnette | 2f83d71 | 2012-04-23 14:31:45 -0700 | [diff] [blame] | 44 | 'client_test': 'desktopui_PyAutoInstall', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 45 | # The remote command to be run. |
Scott Zawalski | 9a98556 | 2012-04-03 17:47:30 -0400 | [diff] [blame] | 46 | 'remote_command': 'python /usr/local/autotest/cros/remote_pyauto.py' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 47 | ' --no-http-server', |
| 48 | # The short form of remote command, used by pkill. |
Scott Zawalski | 9a98556 | 2012-04-03 17:47:30 -0400 | [diff] [blame] | 49 | 'remote_command_short': 'remote_pyauto', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 50 | # The remote process info. |
| 51 | 'remote_process': None, |
| 52 | # The ssh tunnel process info. |
| 53 | 'ssh_tunnel': None, |
| 54 | # Polling RPC function name for testing the server availability. |
| 55 | 'polling_rpc': 'IsLinux', |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 56 | # Additional SSH options. |
| 57 | 'ssh_config': '-o StrictHostKeyChecking=no ', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 58 | }, |
| 59 | 'faft': { |
| 60 | 'used': False, |
| 61 | 'ref_name': 'faft_client', |
| 62 | 'port': 9990, |
| 63 | 'client_test': 'firmware_FAFTClient', |
Tom Wai-Hong Tam | c1e0b9a | 2012-11-01 13:52:39 +0800 | [diff] [blame] | 64 | 'remote_command': '/usr/local/autotest/cros/faft_client.py', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 65 | 'remote_command_short': 'faft_client', |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 66 | 'remote_log_file': '/tmp/faft_client.log', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 67 | 'remote_process': None, |
| 68 | 'ssh_tunnel': None, |
| 69 | 'polling_rpc': 'is_available', |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 70 | 'ssh_config': '-o StrictHostKeyChecking=no ' |
| 71 | '-o UserKnownHostsFile=/dev/null ', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 72 | }, |
| 73 | } |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 74 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 75 | def _init_servo(self, host, cmdline_args): |
| 76 | """Initialize `self.servo`. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 77 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 78 | If the host has an attached servo object, use that. |
| 79 | Otherwise assume that there's a locally attached servo |
| 80 | device, and start servod on localhost. |
| 81 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 82 | """ |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 83 | if host.servo: |
| 84 | self.servo = host.servo |
| 85 | self._servo_is_local = False |
| 86 | return |
| 87 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 88 | # Assign default arguments for servo invocation. |
| 89 | args = { |
| 90 | 'servo_host': 'localhost', 'servo_port': 9999, |
Todd Broch | d50ac04 | 2012-03-19 16:58:02 -0700 | [diff] [blame] | 91 | 'xml_config': [], 'servo_vid': None, 'servo_pid': None, |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 92 | 'servo_serial': None, 'use_pyauto': False} |
| 93 | |
| 94 | # Parse arguments from AFE and override servo defaults above. |
| 95 | client_attributes = site_host_attributes.HostAttributes(host.hostname) |
| 96 | if hasattr(site_host_attributes, 'servo_serial'): |
| 97 | args['servo_serial'] = client_attributes.servo_serial |
| 98 | |
| 99 | # Parse arguments from command line and override previous AFE or servo |
| 100 | # defaults |
| 101 | for arg in cmdline_args: |
| 102 | match = re.search("^(\w+)=(.+)", arg) |
| 103 | if match: |
Tom Wai-Hong Tam | 5fc2c79 | 2011-11-03 13:05:39 +0800 | [diff] [blame] | 104 | key = match.group(1) |
| 105 | val = match.group(2) |
| 106 | # Support multiple xml_config by appending it to a list. |
| 107 | if key == 'xml_config': |
| 108 | args[key].append(val) |
| 109 | else: |
| 110 | args[key] = val |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 111 | |
Todd Broch | 0edf7e1 | 2012-06-14 09:36:15 -0700 | [diff] [blame] | 112 | self.servo = servo.Servo() |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 113 | self._servo_is_local = True |
| 114 | |
| 115 | |
| 116 | def _release_servo(self): |
| 117 | """Clean up `self.servo` if it is locally attached.""" |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 118 | self._servo_is_local = False |
| 119 | |
| 120 | |
| 121 | def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False): |
| 122 | """Create a Servo object and install the dependency. |
| 123 | |
| 124 | If use_pyauto/use_faft is True the PyAuto/FAFTClient dependency is |
| 125 | installed on the client and a remote PyAuto/FAFTClient server is |
| 126 | launched and connected. |
| 127 | """ |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 128 | # Initialize servotest args. |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 129 | self._client = host |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 130 | self._remote_infos['pyauto']['used'] = use_pyauto |
| 131 | self._remote_infos['faft']['used'] = use_faft |
| 132 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 133 | self._init_servo(host, cmdline_args) |
| 134 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 135 | # Initializes dut, may raise AssertionError if pre-defined gpio |
| 136 | # sequence to set GPIO's fail. Autotest does not handle exception |
| 137 | # throwing in initialize and will cause a test to hang. |
| 138 | try: |
| 139 | self.servo.initialize_dut() |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 140 | except (AssertionError, xmlrpclib.Fault) as e: |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 141 | self._release_servo() |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 142 | raise error.TestFail(e) |
| 143 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 144 | # Install PyAuto/FAFTClient dependency. |
| 145 | for info in self._remote_infos.itervalues(): |
| 146 | if info['used']: |
| 147 | if not self._autotest_client: |
| 148 | self._autotest_client = autotest.Autotest(self._client) |
| 149 | self._autotest_client.run_test(info['client_test']) |
| 150 | self.launch_client(info) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 151 | |
| 152 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 153 | def _ping_test(self, hostname, timeout=5): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 154 | """Verify whether a host responds to a ping. |
| 155 | |
| 156 | Args: |
| 157 | hostname: Hostname to ping. |
| 158 | timeout: Time in seconds to wait for a response. |
| 159 | """ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 160 | with open(os.devnull, 'w') as fnull: |
| 161 | return subprocess.call( |
| 162 | ['ping', '-c', '1', '-W', str(timeout), hostname], |
| 163 | stdout=fnull, stderr=fnull) == 0 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 164 | |
| 165 | |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 166 | def _sshd_test(self, hostname, timeout=5): |
| 167 | """Verify whether sshd is running in host. |
| 168 | |
| 169 | Args: |
| 170 | hostname: Hostname to verify. |
| 171 | timeout: Time in seconds to wait for a response. |
| 172 | """ |
| 173 | try: |
| 174 | sock = socket.create_connection((hostname, 22), timeout=timeout) |
| 175 | sock.close() |
| 176 | return True |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 177 | except socket.timeout: |
| 178 | return False |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 179 | except socket.error: |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 180 | time.sleep(timeout) |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 181 | return False |
| 182 | |
| 183 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 184 | def launch_client(self, info): |
| 185 | """Launch a remote process on client and set up an xmlrpc connection. |
| 186 | |
| 187 | Args: |
| 188 | info: A dict of remote info, see the definition of self._remote_infos. |
| 189 | """ |
| 190 | assert info['used'], \ |
| 191 | 'Remote %s dependency not installed.' % info['ref_name'] |
| 192 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
| 193 | self._launch_ssh_tunnel(info) |
| 194 | assert info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None, \ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 195 | 'The SSH tunnel is not up.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 196 | |
| 197 | # Launch RPC server remotely. |
| 198 | self._kill_remote_process(info) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 199 | logging.info('Client command: %s', info['remote_command']) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 200 | if 'remote_log_file' in info: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 201 | log_file = info['remote_log_file'] |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 202 | else: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 203 | log_file = '/dev/null' |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 204 | logging.info("Logging to %s", log_file) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 205 | info['remote_process'] = subprocess.Popen([ |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 206 | 'ssh -n -q %s root@%s \'%s &> %s\'' % (info['ssh_config'], |
| 207 | self._client.ip, info['remote_command'], log_file)], |
| 208 | shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 209 | |
| 210 | # Connect to RPC object. |
| 211 | logging.info('Connecting to client RPC server...') |
| 212 | remote_url = 'http://localhost:%s' % info['port'] |
| 213 | setattr(self, info['ref_name'], |
| 214 | xmlrpclib.ServerProxy(remote_url, allow_none=True)) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 215 | logging.info('Server proxy: %s', remote_url) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 216 | |
Tom Wai-Hong Tam | 8fd2a77 | 2012-11-06 16:14:05 +0800 | [diff] [blame] | 217 | # We found that the following RPC call retrial doesn't work all the |
| 218 | # time and causes timeout error happened. So add this delay to wait |
| 219 | # the client RPC server start-up as a work-around. |
| 220 | # TODO(waihong@chromium.org): Find the root cause why retrial not work. |
| 221 | time.sleep(5) |
| 222 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 223 | # Poll for client RPC server to come online. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 224 | timeout = 20 |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 225 | succeed = False |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 226 | rpc_error = None |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 227 | while timeout > 0 and not succeed: |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 228 | time.sleep(1) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 229 | try: |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 230 | remote_object = getattr(self, info['ref_name']) |
| 231 | polling_rpc = getattr(remote_object, info['polling_rpc']) |
| 232 | polling_rpc() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 233 | succeed = True |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 234 | except (socket.error, xmlrpclib.ProtocolError) as e: |
| 235 | # The client RPC server may not come online fast enough. Retry. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 236 | timeout -= 1 |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 237 | rpc_error = e |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 238 | |
| 239 | if not succeed: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 240 | if isinstance(rpc_error, xmlrpclib.ProtocolError): |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 241 | logging.info("A protocol error occurred") |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 242 | logging.info("URL: %s", rpc_error.url) |
| 243 | logging.info("HTTP/HTTPS headers: %s", rpc_error.headers) |
| 244 | logging.info("Error code: %d", rpc_error.errcode) |
| 245 | logging.info("Error message: %s", rpc_error.errmsg) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 246 | if 'remote_log_file' in info: |
| 247 | p = subprocess.Popen([ |
| 248 | 'ssh -n -q %s root@%s \'cat %s\'' % (info['ssh_config'], |
| 249 | self._client.ip, info['remote_log_file'])], shell=True, |
| 250 | stdout=subprocess.PIPE) |
Tom Wai-Hong Tam | c1e0b9a | 2012-11-01 13:52:39 +0800 | [diff] [blame] | 251 | logging.info('Log of running remote %s:', info['ref_name']) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 252 | logging.info(p.communicate()[0]) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 253 | assert succeed, 'Timed out connecting to client RPC server.' |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 254 | |
| 255 | |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 256 | def wait_for_client(self, install_deps=False, timeout=100): |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 257 | """Wait for the client to come back online. |
| 258 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 259 | New remote processes will be launched if their used flags are enabled. |
Tom Wai-Hong Tam | 7c17ff2 | 2011-10-26 09:44:09 +0800 | [diff] [blame] | 260 | |
| 261 | Args: |
| 262 | install_deps: If True, install the Autotest dependency when ready. |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 263 | timeout: Time in seconds to wait for the client SSH daemon to |
| 264 | come up. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 265 | """ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 266 | # Ensure old ssh connections are terminated. |
| 267 | self._terminate_all_ssh() |
| 268 | # Wait for the client to come up. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 269 | while timeout > 0 and not self._sshd_test(self._client.ip, timeout=2): |
| 270 | timeout -= 2 |
Vadim Bendebury | 69f047c | 2012-10-11 15:20:31 -0700 | [diff] [blame] | 271 | assert (timeout > 0), 'Timed out waiting for client to reboot.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 272 | logging.info('Server: Client machine is up.') |
| 273 | # Relaunch remote clients. |
| 274 | for name, info in self._remote_infos.iteritems(): |
| 275 | if info['used']: |
Tom Wai-Hong Tam | 7c17ff2 | 2011-10-26 09:44:09 +0800 | [diff] [blame] | 276 | if install_deps: |
| 277 | if not self._autotest_client: |
| 278 | self._autotest_client = autotest.Autotest(self._client) |
| 279 | self._autotest_client.run_test(info['client_test']) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 280 | self.launch_client(info) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame^] | 281 | logging.info('Server: Relaunched remote %s.', name) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 282 | |
| 283 | |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 284 | def wait_for_client_offline(self, timeout=60): |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 285 | """Wait for the client to come offline. |
| 286 | |
| 287 | Args: |
| 288 | timeout: Time in seconds to wait the client to come offline. |
| 289 | """ |
| 290 | # Wait for the client to come offline. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 291 | while timeout > 0 and self._ping_test(self._client.ip, timeout=1): |
Vic Yang | cf5d6fc | 2012-09-14 15:22:44 +0800 | [diff] [blame] | 292 | time.sleep(1) |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 293 | timeout -= 1 |
| 294 | assert timeout, 'Timed out waiting for client offline.' |
| 295 | logging.info('Server: Client machine is offline.') |
| 296 | |
| 297 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 298 | def kill_remote(self): |
| 299 | """Call remote cleanup and kill ssh.""" |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 300 | for info in self._remote_infos.itervalues(): |
| 301 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 302 | remote_object = getattr(self, info['ref_name']) |
Vadim Bendebury | 5b82cc5 | 2012-10-09 19:05:01 -0700 | [diff] [blame] | 303 | try: |
| 304 | remote_object.cleanup() |
| 305 | logging.info('Cleanup succeeded.') |
| 306 | except xmlrpclib.ProtocolError, e: |
| 307 | logging.info('Cleanup returned protocol error: ' + str(e)) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 308 | self._terminate_all_ssh() |
| 309 | |
| 310 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 311 | def cleanup(self): |
| 312 | """Delete the Servo object, call remote cleanup, and kill ssh.""" |
| 313 | self._release_servo() |
| 314 | self.kill_remote() |
| 315 | |
| 316 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 317 | def _launch_ssh_tunnel(self, info): |
| 318 | """Establish an ssh tunnel for connecting to the remote RPC server. |
| 319 | |
| 320 | Args: |
| 321 | info: A dict of remote info, see the definition of self._remote_infos. |
| 322 | """ |
| 323 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 324 | info['ssh_tunnel'] = subprocess.Popen([ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 325 | 'ssh -N -n -q %s -L %s:localhost:%s root@%s' % |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 326 | (info['ssh_config'], info['port'], info['port'], |
| 327 | self._client.ip)], shell=True) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 328 | |
| 329 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 330 | def _kill_remote_process(self, info): |
| 331 | """Ensure the remote process and local ssh process are terminated. |
| 332 | |
| 333 | Args: |
| 334 | info: A dict of remote info, see the definition of self._remote_infos. |
| 335 | """ |
| 336 | kill_cmd = 'pkill -f %s' % info['remote_command_short'] |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 337 | subprocess.call(['ssh -n -q %s root@%s \'%s\'' % |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 338 | (info['ssh_config'], self._client.ip, kill_cmd)], |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 339 | shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 340 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 341 | info['remote_process'].terminate() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 342 | |
| 343 | |
| 344 | def _terminate_all_ssh(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 345 | """Terminate all ssh connections associated with remote processes.""" |
| 346 | for info in self._remote_infos.itervalues(): |
| 347 | if info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None: |
| 348 | info['ssh_tunnel'].terminate() |
| 349 | self._kill_remote_process(info) |