blob: da73cb1d7a3ba2d27aa3569d80ec156f9036b56c [file] [log] [blame]
J. Richard Barnette67ccb872012-04-19 16:34:56 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Vadim Bendebury692341e2012-12-18 15:24:38 -08005import httplib, logging, os, socket, subprocess, sys, time, xmlrpclib
Craig Harrison2b6c6fc2011-06-23 10:34:02 -07006
Vic Yang69249552013-05-09 01:58:11 +08007from autotest_lib.client.common_lib import error, utils
Vadim Bendeburybb731952012-12-05 17:30:36 -08008from autotest_lib.server import autotest, test
J. Richard Barnette33aec9f2013-02-01 16:38:41 -08009
Craig Harrison91944552011-08-04 14:09:55 -070010
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070011class ServoTest(test.test):
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080012 """AutoTest test class to serve as a parent class for FAFT tests.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070013
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080014 TODO(jrbarnette): This class is a legacy, reflecting
15 refactoring that has begun but not completed. The long term
16 plan is to move all function here into FAFT specific classes.
17 http://crosbug.com/33305.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070018 """
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080019 version = 2
J. Richard Barnette67ccb872012-04-19 16:34:56 -070020
Vic Yang8535e772013-05-02 09:07:25 +080021 _REMOTE_PORT = 9990
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080022 _REMOTE_COMMAND = '/usr/local/autotest/cros/faft_client.py'
23 _REMOTE_COMMAND_SHORT = 'faft_client'
24 _REMOTE_LOG_FILE = '/tmp/faft_client.log'
25 _SSH_CONFIG = ('-o StrictHostKeyChecking=no '
26 '-o UserKnownHostsFile=/dev/null ')
J. Richard Barnette67ccb872012-04-19 16:34:56 -070027
Tom Wai-Hong Tam54f4c582013-07-18 12:05:27 +080028 def initialize(self, host):
J. Richard Barnette67ccb872012-04-19 16:34:56 -070029 """Create a Servo object and install the dependency.
J. Richard Barnette67ccb872012-04-19 16:34:56 -070030 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080031 self.servo = host.servo
32 self.faft_client = None
33 self._client = host
34 self._ssh_tunnel = None
35 self._remote_process = None
Vic Yang8535e772013-05-02 09:07:25 +080036 self._local_port = None
J. Richard Barnette67ccb872012-04-19 16:34:56 -070037
Chrome Bot9a1137d2011-07-19 14:35:00 -070038 # Initializes dut, may raise AssertionError if pre-defined gpio
39 # sequence to set GPIO's fail. Autotest does not handle exception
40 # throwing in initialize and will cause a test to hang.
41 try:
42 self.servo.initialize_dut()
Chris Sosa33320a82011-10-24 14:28:32 -070043 except (AssertionError, xmlrpclib.Fault) as e:
Chrome Bot9a1137d2011-07-19 14:35:00 -070044 raise error.TestFail(e)
45
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080046 # Install faft_client dependency.
47 self._autotest_client = autotest.Autotest(self._client)
48 self._autotest_client.install()
49 self._launch_client()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070050
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070051 def _ping_test(self, hostname, timeout=5):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070052 """Verify whether a host responds to a ping.
53
Tom Wai-Hong Tam144202a2013-07-18 13:59:29 +080054 @param hostname: Hostname to ping.
55 @param timeout: Time in seconds to wait for a response.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070056 """
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +080057 with open(os.devnull, 'w') as fnull:
58 return subprocess.call(
59 ['ping', '-c', '1', '-W', str(timeout), hostname],
60 stdout=fnull, stderr=fnull) == 0
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070061
ctchang74816ac2012-08-31 11:12:43 +080062 def _sshd_test(self, hostname, timeout=5):
63 """Verify whether sshd is running in host.
64
Tom Wai-Hong Tam144202a2013-07-18 13:59:29 +080065 @param hostname: Hostname to verify.
66 @param timeout: Time in seconds to wait for a response.
ctchang74816ac2012-08-31 11:12:43 +080067 """
68 try:
69 sock = socket.create_connection((hostname, 22), timeout=timeout)
70 sock.close()
71 return True
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080072 except socket.timeout:
73 return False
ctchang74816ac2012-08-31 11:12:43 +080074 except socket.error:
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080075 time.sleep(timeout)
ctchang74816ac2012-08-31 11:12:43 +080076 return False
77
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080078 def _launch_client(self):
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080079 """Launch a remote XML RPC connection on client with retrials.
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080080 """
81 retry = 3
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080082 while retry:
83 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080084 self._launch_client_once()
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080085 break
86 except AssertionError:
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080087 retry -= 1
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080088 if retry:
89 logging.info('Retry again...')
90 time.sleep(5)
91 else:
92 raise
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080093
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080094 def _launch_client_once(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080095 """Launch a remote process on client and set up an xmlrpc connection.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080096 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080097 if self._ssh_tunnel:
98 self._ssh_tunnel.terminate()
99 self._ssh_tunnel = None
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800100
101 # Launch RPC server remotely.
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800102 self._kill_remote_process()
103 self._launch_ssh_tunnel()
Vadim Bendebury692341e2012-12-18 15:24:38 -0800104
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800105 logging.info('Client command: %s', self._REMOTE_COMMAND)
106 logging.info("Logging to %s", self._REMOTE_LOG_FILE)
Vic Yanga3492372013-05-16 16:11:19 +0800107 full_cmd = ['ssh -n %s root@%s \'%s &> %s\'' % (
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800108 self._SSH_CONFIG, self._client.ip,
109 self._REMOTE_COMMAND, self._REMOTE_LOG_FILE)]
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800110 logging.info('Starting process %s', ' '.join(full_cmd))
Vic Yanga3492372013-05-16 16:11:19 +0800111 self._remote_process = subprocess.Popen(full_cmd, shell=True,
112 stdout=subprocess.PIPE,
113 stderr=subprocess.PIPE)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800114
115 # Connect to RPC object.
116 logging.info('Connecting to client RPC server...')
Vic Yang8535e772013-05-02 09:07:25 +0800117 remote_url = 'http://localhost:%s' % self._local_port
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800118 self.faft_client = xmlrpclib.ServerProxy(remote_url, allow_none=True)
Vic Yang3a7cf602012-11-07 17:28:39 +0800119 logging.info('Server proxy: %s', remote_url)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800120
Craig Harrison91944552011-08-04 14:09:55 -0700121 # Poll for client RPC server to come online.
Vic Yang6c1dc152012-09-13 14:56:11 +0800122 timeout = 20
Craig Harrison91944552011-08-04 14:09:55 -0700123 succeed = False
Vic Yang3a7cf602012-11-07 17:28:39 +0800124 rpc_error = None
Craig Harrison91944552011-08-04 14:09:55 -0700125 while timeout > 0 and not succeed:
Vic Yang6c1dc152012-09-13 14:56:11 +0800126 time.sleep(1)
Vic Yanga3492372013-05-16 16:11:19 +0800127 if self._remote_process.poll() is not None:
128 # The SSH process is gone. Log stderr.
129 logging.error('Remote process died!')
130 sout, serr = self._remote_process.communicate()
131 logging.error('Stdout: %s', sout)
132 logging.error('Stderr: %s', serr)
133 break
134
Craig Harrison91944552011-08-04 14:09:55 -0700135 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800136 self.faft_client.system.is_available()
Craig Harrison91944552011-08-04 14:09:55 -0700137 succeed = True
Vadim Bendebury692341e2012-12-18 15:24:38 -0800138 except (socket.error,
139 xmlrpclib.ProtocolError,
140 httplib.BadStatusLine) as e:
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700141 logging.info('caught: %s %s, tries left: %s',
142 repr(e), str(e), timeout)
Tom Wai-Hong Tamac35f082012-11-06 15:00:35 +0800143 # The client RPC server may not come online fast enough. Retry.
Craig Harrison91944552011-08-04 14:09:55 -0700144 timeout -= 1
Vic Yang3a7cf602012-11-07 17:28:39 +0800145 rpc_error = e
Vadim Bendebury692341e2012-12-18 15:24:38 -0800146 except:
147 logging.error('Unexpected error: %s', sys.exc_info()[0])
148 raise
Vic Yangf8fd4542012-08-01 11:37:46 +0800149
150 if not succeed:
Vic Yang3a7cf602012-11-07 17:28:39 +0800151 if isinstance(rpc_error, xmlrpclib.ProtocolError):
Tom Wai-Hong Tamac35f082012-11-06 15:00:35 +0800152 logging.info("A protocol error occurred")
Vic Yang3a7cf602012-11-07 17:28:39 +0800153 logging.info("URL: %s", rpc_error.url)
154 logging.info("HTTP/HTTPS headers: %s", rpc_error.headers)
155 logging.info("Error code: %d", rpc_error.errcode)
156 logging.info("Error message: %s", rpc_error.errmsg)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800157 p = subprocess.Popen([
158 'ssh -n -q %s root@%s \'cat %s\'' % (self._SSH_CONFIG,
159 self._client.ip, self._REMOTE_LOG_FILE)], shell=True,
160 stdout=subprocess.PIPE)
161 logging.info('Log of running remote %s:',
162 self._REMOTE_COMMAND_SHORT)
163 logging.info(p.communicate()[0])
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800164 assert succeed, 'Timed out connecting to client RPC server.'
Craig Harrison91944552011-08-04 14:09:55 -0700165
Vic Yang8bbc1c32012-09-13 11:47:44 +0800166 def wait_for_client(self, install_deps=False, timeout=100):
Craig Harrison91944552011-08-04 14:09:55 -0700167 """Wait for the client to come back online.
168
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800169 New remote processes will be launched if their used flags are enabled.
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +0800170
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700171 @param install_deps: If True, install Autotest dependency when ready.
172 @param timeout: Time in seconds to wait for the client SSH daemon to
173 come up.
Craig Harrison91944552011-08-04 14:09:55 -0700174 """
Craig Harrison91944552011-08-04 14:09:55 -0700175 # Ensure old ssh connections are terminated.
176 self._terminate_all_ssh()
177 # Wait for the client to come up.
Vic Yang6c1dc152012-09-13 14:56:11 +0800178 while timeout > 0 and not self._sshd_test(self._client.ip, timeout=2):
179 timeout -= 2
Vadim Bendebury69f047c2012-10-11 15:20:31 -0700180 assert (timeout > 0), 'Timed out waiting for client to reboot.'
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800181 logging.info('Server: Client machine is up.')
182 # Relaunch remote clients.
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800183 if install_deps:
184 self._autotest_client.install()
185 self._launch_client()
186 logging.info('Server: Relaunched remote %s.', 'faft')
Craig Harrison91944552011-08-04 14:09:55 -0700187
Tom Wai-Hong Tama9b225b2013-05-03 10:35:10 +0800188 def wait_for_client_offline(self, timeout=60, orig_boot_id=None):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800189 """Wait for the client to come offline.
190
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700191 @param timeout: Time in seconds to wait the client to come offline.
Tom Wai-Hong Tam12636062013-04-09 17:14:15 +0800192 @param orig_boot_id: A string containing the original boot id.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800193 """
194 # Wait for the client to come offline.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700195 while timeout > 0 and self._ping_test(self._client.ip, timeout=1):
Vic Yangcf5d6fc2012-09-14 15:22:44 +0800196 time.sleep(1)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800197 timeout -= 1
Tom Wai-Hong Tam12636062013-04-09 17:14:15 +0800198
199 # As get_boot_id() requires DUT online. So we move the comparison here.
200 if timeout == 0 and orig_boot_id:
201 if self._client.get_boot_id() != orig_boot_id:
202 logging.warn('Reboot done very quickly.')
203 return
204
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800205 assert timeout, 'Timed out waiting for client offline.'
206 logging.info('Server: Client machine is offline.')
207
Vic Yang4e0d1f72012-05-24 15:11:11 +0800208 def kill_remote(self):
209 """Call remote cleanup and kill ssh."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800210 if self._remote_process and self._remote_process.poll() is None:
211 try:
212 self.faft_client.cleanup()
213 logging.info('Cleanup succeeded.')
214 except xmlrpclib.ProtocolError, e:
215 logging.info('Cleanup returned protocol error: ' + str(e))
Craig Harrison91944552011-08-04 14:09:55 -0700216 self._terminate_all_ssh()
217
Vic Yang4e0d1f72012-05-24 15:11:11 +0800218 def cleanup(self):
219 """Delete the Servo object, call remote cleanup, and kill ssh."""
Vic Yang4e0d1f72012-05-24 15:11:11 +0800220 self.kill_remote()
221
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800222 def _launch_ssh_tunnel(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800223 """Establish an ssh tunnel for connecting to the remote RPC server.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800224 """
Vic Yang8535e772013-05-02 09:07:25 +0800225 if self._local_port is None:
Vic Yang69249552013-05-09 01:58:11 +0800226 self._local_port = utils.get_unused_port()
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800227 if not self._ssh_tunnel or self._ssh_tunnel.poll() is not None:
228 self._ssh_tunnel = subprocess.Popen([
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800229 'ssh -N -n -q %s -L %s:localhost:%s root@%s' %
Vic Yang8535e772013-05-02 09:07:25 +0800230 (self._SSH_CONFIG, self._local_port, self._REMOTE_PORT,
Tom Wai-Hong Tame2c66122011-10-25 17:21:35 +0800231 self._client.ip)], shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800232 assert self._ssh_tunnel.poll() is None, \
Vic Yang8535e772013-05-02 09:07:25 +0800233 'The SSH tunnel on port %d is not up.' % self._local_port
Craig Harrison91944552011-08-04 14:09:55 -0700234
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800235 def _kill_remote_process(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800236 """Ensure the remote process and local ssh process are terminated.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800237 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800238 kill_cmd = 'pkill -f %s' % self._REMOTE_COMMAND_SHORT
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800239 subprocess.call(['ssh -n -q %s root@%s \'%s\'' %
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800240 (self._SSH_CONFIG, self._client.ip, kill_cmd)],
Craig Harrison91944552011-08-04 14:09:55 -0700241 shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800242 if self._remote_process and self._remote_process.poll() is None:
243 self._remote_process.terminate()
Craig Harrison91944552011-08-04 14:09:55 -0700244
245 def _terminate_all_ssh(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800246 """Terminate all ssh connections associated with remote processes."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800247 if self._ssh_tunnel and self._ssh_tunnel.poll() is None:
248 self._ssh_tunnel.terminate()
249 self._kill_remote_process()
250 self._ssh_tunnel = None