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 | |
Vadim Bendebury | 692341e | 2012-12-18 15:24:38 -0800 | [diff] [blame] | 5 | import httplib, logging, os, socket, subprocess, sys, time, xmlrpclib |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 6 | import SocketServer |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 7 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import error |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 9 | from autotest_lib.server import autotest, test |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 10 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 11 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 12 | class ServoTest(test.test): |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 13 | """AutoTest test class to serve as a parent class for FAFT tests. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 14 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 15 | TODO(jrbarnette): This class is a legacy, reflecting |
| 16 | refactoring that has begun but not completed. The long term |
| 17 | plan is to move all function here into FAFT specific classes. |
| 18 | http://crosbug.com/33305. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 19 | """ |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 20 | version = 2 |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 21 | |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 22 | _REMOTE_PORT = 9990 |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 23 | _REMOTE_COMMAND = '/usr/local/autotest/cros/faft_client.py' |
| 24 | _REMOTE_COMMAND_SHORT = 'faft_client' |
| 25 | _REMOTE_LOG_FILE = '/tmp/faft_client.log' |
| 26 | _SSH_CONFIG = ('-o StrictHostKeyChecking=no ' |
| 27 | '-o UserKnownHostsFile=/dev/null ') |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 28 | |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 29 | def initialize(self, host, _, use_pyauto=False, use_faft=False): |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 30 | """Create a Servo object and install the dependency. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 31 | """ |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 32 | # TODO(jrbarnette): Part of the incomplete refactoring: |
| 33 | # assert here that there are no legacy callers passing |
| 34 | # parameters for functionality that's been deprecated and |
| 35 | # removed. |
| 36 | assert use_faft and not use_pyauto |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 37 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 38 | self.servo = host.servo |
| 39 | self.faft_client = None |
| 40 | self._client = host |
| 41 | self._ssh_tunnel = None |
| 42 | self._remote_process = None |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 43 | self._local_port = None |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 44 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 45 | # Initializes dut, may raise AssertionError if pre-defined gpio |
| 46 | # sequence to set GPIO's fail. Autotest does not handle exception |
| 47 | # throwing in initialize and will cause a test to hang. |
| 48 | try: |
| 49 | self.servo.initialize_dut() |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 50 | except (AssertionError, xmlrpclib.Fault) as e: |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 51 | raise error.TestFail(e) |
| 52 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 53 | # Install faft_client dependency. |
| 54 | self._autotest_client = autotest.Autotest(self._client) |
| 55 | self._autotest_client.install() |
| 56 | self._launch_client() |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 57 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 58 | def _ping_test(self, hostname, timeout=5): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 59 | """Verify whether a host responds to a ping. |
| 60 | |
| 61 | Args: |
| 62 | hostname: Hostname to ping. |
| 63 | timeout: Time in seconds to wait for a response. |
| 64 | """ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 65 | with open(os.devnull, 'w') as fnull: |
| 66 | return subprocess.call( |
| 67 | ['ping', '-c', '1', '-W', str(timeout), hostname], |
| 68 | stdout=fnull, stderr=fnull) == 0 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 69 | |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 70 | def _sshd_test(self, hostname, timeout=5): |
| 71 | """Verify whether sshd is running in host. |
| 72 | |
| 73 | Args: |
| 74 | hostname: Hostname to verify. |
| 75 | timeout: Time in seconds to wait for a response. |
| 76 | """ |
| 77 | try: |
| 78 | sock = socket.create_connection((hostname, 22), timeout=timeout) |
| 79 | sock.close() |
| 80 | return True |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 81 | except socket.timeout: |
| 82 | return False |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 83 | except socket.error: |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 84 | time.sleep(timeout) |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 85 | return False |
| 86 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 87 | def _launch_client(self): |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 88 | """Launch a remote XML RPC connection on client with retrials. |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 89 | """ |
| 90 | retry = 3 |
Tom Wai-Hong Tam | b3db3b2 | 2012-12-04 11:59:54 +0800 | [diff] [blame] | 91 | while retry: |
| 92 | try: |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 93 | self._launch_client_once() |
Tom Wai-Hong Tam | b3db3b2 | 2012-12-04 11:59:54 +0800 | [diff] [blame] | 94 | break |
| 95 | except AssertionError: |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 96 | retry -= 1 |
Tom Wai-Hong Tam | b3db3b2 | 2012-12-04 11:59:54 +0800 | [diff] [blame] | 97 | if retry: |
| 98 | logging.info('Retry again...') |
| 99 | time.sleep(5) |
| 100 | else: |
| 101 | raise |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 102 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 103 | def _launch_client_once(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 104 | """Launch a remote process on client and set up an xmlrpc connection. |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 105 | """ |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 106 | if self._ssh_tunnel: |
| 107 | self._ssh_tunnel.terminate() |
| 108 | self._ssh_tunnel = None |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 109 | |
| 110 | # Launch RPC server remotely. |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 111 | self._kill_remote_process() |
| 112 | self._launch_ssh_tunnel() |
Vadim Bendebury | 692341e | 2012-12-18 15:24:38 -0800 | [diff] [blame] | 113 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 114 | logging.info('Client command: %s', self._REMOTE_COMMAND) |
| 115 | logging.info("Logging to %s", self._REMOTE_LOG_FILE) |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 116 | full_cmd = ['ssh -n -q %s root@%s \'%s &> %s\'' % ( |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 117 | self._SSH_CONFIG, self._client.ip, |
| 118 | self._REMOTE_COMMAND, self._REMOTE_LOG_FILE)] |
Vadim Bendebury | 89ec24e | 2012-12-17 12:54:18 -0800 | [diff] [blame] | 119 | logging.info('Starting process %s', ' '.join(full_cmd)) |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 120 | self._remote_process = subprocess.Popen(full_cmd, shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 121 | |
| 122 | # Connect to RPC object. |
| 123 | logging.info('Connecting to client RPC server...') |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 124 | remote_url = 'http://localhost:%s' % self._local_port |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 125 | self.faft_client = xmlrpclib.ServerProxy(remote_url, allow_none=True) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 126 | logging.info('Server proxy: %s', remote_url) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 127 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 128 | # Poll for client RPC server to come online. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 129 | timeout = 20 |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 130 | succeed = False |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 131 | rpc_error = None |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 132 | while timeout > 0 and not succeed: |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 133 | time.sleep(1) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 134 | try: |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 135 | self.faft_client.system.is_available() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 136 | succeed = True |
Vadim Bendebury | 692341e | 2012-12-18 15:24:38 -0800 | [diff] [blame] | 137 | except (socket.error, |
| 138 | xmlrpclib.ProtocolError, |
| 139 | httplib.BadStatusLine) as e: |
Yusuf Mohsinally | f3cb764 | 2013-04-15 14:20:56 -0700 | [diff] [blame] | 140 | logging.info('caught: %s %s, tries left: %s', |
| 141 | repr(e), str(e), timeout) |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 142 | # The client RPC server may not come online fast enough. Retry. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 143 | timeout -= 1 |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 144 | rpc_error = e |
Vadim Bendebury | 692341e | 2012-12-18 15:24:38 -0800 | [diff] [blame] | 145 | except: |
| 146 | logging.error('Unexpected error: %s', sys.exc_info()[0]) |
| 147 | raise |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 148 | |
| 149 | if not succeed: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 150 | if isinstance(rpc_error, xmlrpclib.ProtocolError): |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 151 | logging.info("A protocol error occurred") |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 152 | logging.info("URL: %s", rpc_error.url) |
| 153 | logging.info("HTTP/HTTPS headers: %s", rpc_error.headers) |
| 154 | logging.info("Error code: %d", rpc_error.errcode) |
| 155 | logging.info("Error message: %s", rpc_error.errmsg) |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 156 | p = subprocess.Popen([ |
| 157 | 'ssh -n -q %s root@%s \'cat %s\'' % (self._SSH_CONFIG, |
| 158 | self._client.ip, self._REMOTE_LOG_FILE)], shell=True, |
| 159 | stdout=subprocess.PIPE) |
| 160 | logging.info('Log of running remote %s:', |
| 161 | self._REMOTE_COMMAND_SHORT) |
| 162 | logging.info(p.communicate()[0]) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 163 | assert succeed, 'Timed out connecting to client RPC server.' |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 164 | |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 165 | def wait_for_client(self, install_deps=False, timeout=100): |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 166 | """Wait for the client to come back online. |
| 167 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 168 | 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] | 169 | |
Yusuf Mohsinally | f3cb764 | 2013-04-15 14:20:56 -0700 | [diff] [blame] | 170 | @param install_deps: If True, install Autotest dependency when ready. |
| 171 | @param timeout: Time in seconds to wait for the client SSH daemon to |
| 172 | come up. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 173 | """ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 174 | # Ensure old ssh connections are terminated. |
| 175 | self._terminate_all_ssh() |
| 176 | # Wait for the client to come up. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 177 | while timeout > 0 and not self._sshd_test(self._client.ip, timeout=2): |
| 178 | timeout -= 2 |
Vadim Bendebury | 69f047c | 2012-10-11 15:20:31 -0700 | [diff] [blame] | 179 | assert (timeout > 0), 'Timed out waiting for client to reboot.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 180 | logging.info('Server: Client machine is up.') |
| 181 | # Relaunch remote clients. |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 182 | if install_deps: |
| 183 | self._autotest_client.install() |
| 184 | self._launch_client() |
| 185 | logging.info('Server: Relaunched remote %s.', 'faft') |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 186 | |
Tom Wai-Hong Tam | a9b225b | 2013-05-03 10:35:10 +0800 | [diff] [blame] | 187 | def wait_for_client_offline(self, timeout=60, orig_boot_id=None): |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 188 | """Wait for the client to come offline. |
| 189 | |
Yusuf Mohsinally | f3cb764 | 2013-04-15 14:20:56 -0700 | [diff] [blame] | 190 | @param timeout: Time in seconds to wait the client to come offline. |
Tom Wai-Hong Tam | 1263606 | 2013-04-09 17:14:15 +0800 | [diff] [blame] | 191 | @param orig_boot_id: A string containing the original boot id. |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 192 | """ |
| 193 | # Wait for the client to come offline. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 194 | while timeout > 0 and self._ping_test(self._client.ip, timeout=1): |
Vic Yang | cf5d6fc | 2012-09-14 15:22:44 +0800 | [diff] [blame] | 195 | time.sleep(1) |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 196 | timeout -= 1 |
Tom Wai-Hong Tam | 1263606 | 2013-04-09 17:14:15 +0800 | [diff] [blame] | 197 | |
| 198 | # As get_boot_id() requires DUT online. So we move the comparison here. |
| 199 | if timeout == 0 and orig_boot_id: |
| 200 | if self._client.get_boot_id() != orig_boot_id: |
| 201 | logging.warn('Reboot done very quickly.') |
| 202 | return |
| 203 | |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 204 | assert timeout, 'Timed out waiting for client offline.' |
| 205 | logging.info('Server: Client machine is offline.') |
| 206 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 207 | def kill_remote(self): |
| 208 | """Call remote cleanup and kill ssh.""" |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 209 | if self._remote_process and self._remote_process.poll() is None: |
| 210 | try: |
| 211 | self.faft_client.cleanup() |
| 212 | logging.info('Cleanup succeeded.') |
| 213 | except xmlrpclib.ProtocolError, e: |
| 214 | logging.info('Cleanup returned protocol error: ' + str(e)) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 215 | self._terminate_all_ssh() |
| 216 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 217 | def cleanup(self): |
| 218 | """Delete the Servo object, call remote cleanup, and kill ssh.""" |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 219 | self.kill_remote() |
| 220 | |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 221 | def _find_unused_port(self): |
| 222 | """Returns an unused TCP port.""" |
| 223 | server = SocketServer.TCPServer(('localhost', 0), |
| 224 | SocketServer.BaseRequestHandler) |
| 225 | _, port = server.server_address |
| 226 | return port |
| 227 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 228 | def _launch_ssh_tunnel(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 229 | """Establish an ssh tunnel for connecting to the remote RPC server. |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 230 | """ |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 231 | if self._local_port is None: |
| 232 | self._local_port = self._find_unused_port() |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 233 | if not self._ssh_tunnel or self._ssh_tunnel.poll() is not None: |
| 234 | self._ssh_tunnel = subprocess.Popen([ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 235 | 'ssh -N -n -q %s -L %s:localhost:%s root@%s' % |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 236 | (self._SSH_CONFIG, self._local_port, self._REMOTE_PORT, |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 237 | self._client.ip)], shell=True) |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 238 | assert self._ssh_tunnel.poll() is None, \ |
Vic Yang | 8535e77 | 2013-05-02 09:07:25 +0800 | [diff] [blame] | 239 | 'The SSH tunnel on port %d is not up.' % self._local_port |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 240 | |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 241 | def _kill_remote_process(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 242 | """Ensure the remote process and local ssh process are terminated. |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 243 | """ |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 244 | kill_cmd = 'pkill -f %s' % self._REMOTE_COMMAND_SHORT |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 245 | subprocess.call(['ssh -n -q %s root@%s \'%s\'' % |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 246 | (self._SSH_CONFIG, self._client.ip, kill_cmd)], |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 247 | shell=True) |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 248 | if self._remote_process and self._remote_process.poll() is None: |
| 249 | self._remote_process.terminate() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 250 | |
| 251 | def _terminate_all_ssh(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 252 | """Terminate all ssh connections associated with remote processes.""" |
J. Richard Barnette | 33aec9f | 2013-02-01 16:38:41 -0800 | [diff] [blame] | 253 | if self._ssh_tunnel and self._ssh_tunnel.poll() is None: |
| 254 | self._ssh_tunnel.terminate() |
| 255 | self._kill_remote_process() |
| 256 | self._ssh_tunnel = None |