blob: 904d41943e26c3ee9df5fd6eb0939e278e864920 [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
Chrome Bot9a1137d2011-07-19 14:35:00 -07007from autotest_lib.client.common_lib import error
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
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080021 _PORT = 9990
22 _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
Vadim Bendeburybb731952012-12-05 17:30:36 -080028 def initialize(self, host, _, use_pyauto=False, use_faft=False):
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 # TODO(jrbarnette): Part of the incomplete refactoring:
32 # assert here that there are no legacy callers passing
33 # parameters for functionality that's been deprecated and
34 # removed.
35 assert use_faft and not use_pyauto
Chris Sosa33320a82011-10-24 14:28:32 -070036
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080037 self.servo = host.servo
38 self.faft_client = None
39 self._client = host
40 self._ssh_tunnel = None
41 self._remote_process = None
J. Richard Barnette67ccb872012-04-19 16:34:56 -070042
Chrome Bot9a1137d2011-07-19 14:35:00 -070043 # Initializes dut, may raise AssertionError if pre-defined gpio
44 # sequence to set GPIO's fail. Autotest does not handle exception
45 # throwing in initialize and will cause a test to hang.
46 try:
47 self.servo.initialize_dut()
Chris Sosa33320a82011-10-24 14:28:32 -070048 except (AssertionError, xmlrpclib.Fault) as e:
Chrome Bot9a1137d2011-07-19 14:35:00 -070049 raise error.TestFail(e)
50
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080051 # Install faft_client dependency.
52 self._autotest_client = autotest.Autotest(self._client)
53 self._autotest_client.install()
54 self._launch_client()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070055
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070056 def _ping_test(self, hostname, timeout=5):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070057 """Verify whether a host responds to a ping.
58
59 Args:
60 hostname: Hostname to ping.
61 timeout: Time in seconds to wait for a response.
62 """
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +080063 with open(os.devnull, 'w') as fnull:
64 return subprocess.call(
65 ['ping', '-c', '1', '-W', str(timeout), hostname],
66 stdout=fnull, stderr=fnull) == 0
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070067
ctchang74816ac2012-08-31 11:12:43 +080068 def _sshd_test(self, hostname, timeout=5):
69 """Verify whether sshd is running in host.
70
71 Args:
72 hostname: Hostname to verify.
73 timeout: Time in seconds to wait for a response.
74 """
75 try:
76 sock = socket.create_connection((hostname, 22), timeout=timeout)
77 sock.close()
78 return True
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080079 except socket.timeout:
80 return False
ctchang74816ac2012-08-31 11:12:43 +080081 except socket.error:
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080082 time.sleep(timeout)
ctchang74816ac2012-08-31 11:12:43 +080083 return False
84
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080085 def _launch_client(self):
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080086 """Launch a remote XML RPC connection on client with retrials.
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080087 """
88 retry = 3
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080089 while retry:
90 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080091 self._launch_client_once()
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080092 break
93 except AssertionError:
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080094 retry -= 1
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080095 if retry:
96 logging.info('Retry again...')
97 time.sleep(5)
98 else:
99 raise
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +0800100
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800101 def _launch_client_once(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800102 """Launch a remote process on client and set up an xmlrpc connection.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800103 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800104 if self._ssh_tunnel:
105 self._ssh_tunnel.terminate()
106 self._ssh_tunnel = None
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800107
108 # Launch RPC server remotely.
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800109 self._kill_remote_process()
110 self._launch_ssh_tunnel()
Vadim Bendebury692341e2012-12-18 15:24:38 -0800111
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800112 logging.info('Client command: %s', self._REMOTE_COMMAND)
113 logging.info("Logging to %s", self._REMOTE_LOG_FILE)
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800114 full_cmd = ['ssh -n -q %s root@%s \'%s &> %s\'' % (
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800115 self._SSH_CONFIG, self._client.ip,
116 self._REMOTE_COMMAND, self._REMOTE_LOG_FILE)]
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800117 logging.info('Starting process %s', ' '.join(full_cmd))
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800118 self._remote_process = subprocess.Popen(full_cmd, shell=True)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800119
120 # Connect to RPC object.
121 logging.info('Connecting to client RPC server...')
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800122 remote_url = 'http://localhost:%s' % self._PORT
123 self.faft_client = xmlrpclib.ServerProxy(remote_url, allow_none=True)
Vic Yang3a7cf602012-11-07 17:28:39 +0800124 logging.info('Server proxy: %s', remote_url)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800125
Craig Harrison91944552011-08-04 14:09:55 -0700126 # Poll for client RPC server to come online.
Vic Yang6c1dc152012-09-13 14:56:11 +0800127 timeout = 20
Craig Harrison91944552011-08-04 14:09:55 -0700128 succeed = False
Vic Yang3a7cf602012-11-07 17:28:39 +0800129 rpc_error = None
Craig Harrison91944552011-08-04 14:09:55 -0700130 while timeout > 0 and not succeed:
Vic Yang6c1dc152012-09-13 14:56:11 +0800131 time.sleep(1)
Craig Harrison91944552011-08-04 14:09:55 -0700132 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800133 self.faft_client.system.is_available()
Craig Harrison91944552011-08-04 14:09:55 -0700134 succeed = True
Vadim Bendebury692341e2012-12-18 15:24:38 -0800135 except (socket.error,
136 xmlrpclib.ProtocolError,
137 httplib.BadStatusLine) as e:
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800138 logging.info('caught exception %s', e)
Tom Wai-Hong Tamac35f082012-11-06 15:00:35 +0800139 # The client RPC server may not come online fast enough. Retry.
Craig Harrison91944552011-08-04 14:09:55 -0700140 timeout -= 1
Vic Yang3a7cf602012-11-07 17:28:39 +0800141 rpc_error = e
Vadim Bendebury692341e2012-12-18 15:24:38 -0800142 except:
143 logging.error('Unexpected error: %s', sys.exc_info()[0])
144 raise
Vic Yangf8fd4542012-08-01 11:37:46 +0800145
146 if not succeed:
Vic Yang3a7cf602012-11-07 17:28:39 +0800147 if isinstance(rpc_error, xmlrpclib.ProtocolError):
Tom Wai-Hong Tamac35f082012-11-06 15:00:35 +0800148 logging.info("A protocol error occurred")
Vic Yang3a7cf602012-11-07 17:28:39 +0800149 logging.info("URL: %s", rpc_error.url)
150 logging.info("HTTP/HTTPS headers: %s", rpc_error.headers)
151 logging.info("Error code: %d", rpc_error.errcode)
152 logging.info("Error message: %s", rpc_error.errmsg)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800153 p = subprocess.Popen([
154 'ssh -n -q %s root@%s \'cat %s\'' % (self._SSH_CONFIG,
155 self._client.ip, self._REMOTE_LOG_FILE)], shell=True,
156 stdout=subprocess.PIPE)
157 logging.info('Log of running remote %s:',
158 self._REMOTE_COMMAND_SHORT)
159 logging.info(p.communicate()[0])
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800160 assert succeed, 'Timed out connecting to client RPC server.'
Craig Harrison91944552011-08-04 14:09:55 -0700161
Vic Yang8bbc1c32012-09-13 11:47:44 +0800162 def wait_for_client(self, install_deps=False, timeout=100):
Craig Harrison91944552011-08-04 14:09:55 -0700163 """Wait for the client to come back online.
164
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800165 New remote processes will be launched if their used flags are enabled.
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +0800166
167 Args:
168 install_deps: If True, install the Autotest dependency when ready.
Vic Yang8bbc1c32012-09-13 11:47:44 +0800169 timeout: Time in seconds to wait for the client SSH daemon to
170 come up.
Craig Harrison91944552011-08-04 14:09:55 -0700171 """
Craig Harrison91944552011-08-04 14:09:55 -0700172 # Ensure old ssh connections are terminated.
173 self._terminate_all_ssh()
174 # Wait for the client to come up.
Vic Yang6c1dc152012-09-13 14:56:11 +0800175 while timeout > 0 and not self._sshd_test(self._client.ip, timeout=2):
176 timeout -= 2
Vadim Bendebury69f047c2012-10-11 15:20:31 -0700177 assert (timeout > 0), 'Timed out waiting for client to reboot.'
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800178 logging.info('Server: Client machine is up.')
179 # Relaunch remote clients.
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800180 if install_deps:
181 self._autotest_client.install()
182 self._launch_client()
183 logging.info('Server: Relaunched remote %s.', 'faft')
Craig Harrison91944552011-08-04 14:09:55 -0700184
Vic Yang8bbc1c32012-09-13 11:47:44 +0800185 def wait_for_client_offline(self, timeout=60):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800186 """Wait for the client to come offline.
187
188 Args:
189 timeout: Time in seconds to wait the client to come offline.
190 """
191 # Wait for the client to come offline.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700192 while timeout > 0 and self._ping_test(self._client.ip, timeout=1):
Vic Yangcf5d6fc2012-09-14 15:22:44 +0800193 time.sleep(1)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800194 timeout -= 1
195 assert timeout, 'Timed out waiting for client offline.'
196 logging.info('Server: Client machine is offline.')
197
Vic Yang4e0d1f72012-05-24 15:11:11 +0800198 def kill_remote(self):
199 """Call remote cleanup and kill ssh."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800200 if self._remote_process and self._remote_process.poll() is None:
201 try:
202 self.faft_client.cleanup()
203 logging.info('Cleanup succeeded.')
204 except xmlrpclib.ProtocolError, e:
205 logging.info('Cleanup returned protocol error: ' + str(e))
Craig Harrison91944552011-08-04 14:09:55 -0700206 self._terminate_all_ssh()
207
Vic Yang4e0d1f72012-05-24 15:11:11 +0800208 def cleanup(self):
209 """Delete the Servo object, call remote cleanup, and kill ssh."""
Vic Yang4e0d1f72012-05-24 15:11:11 +0800210 self.kill_remote()
211
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800212 def _launch_ssh_tunnel(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800213 """Establish an ssh tunnel for connecting to the remote RPC server.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800214 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800215 if not self._ssh_tunnel or self._ssh_tunnel.poll() is not None:
216 self._ssh_tunnel = subprocess.Popen([
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800217 'ssh -N -n -q %s -L %s:localhost:%s root@%s' %
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800218 (self._SSH_CONFIG, self._PORT, self._PORT,
Tom Wai-Hong Tame2c66122011-10-25 17:21:35 +0800219 self._client.ip)], shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800220 assert self._ssh_tunnel.poll() is None, \
221 'The SSH tunnel on port %d is not up.' % self._PORT
Craig Harrison91944552011-08-04 14:09:55 -0700222
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800223 def _kill_remote_process(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800224 """Ensure the remote process and local ssh process are terminated.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800225 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800226 kill_cmd = 'pkill -f %s' % self._REMOTE_COMMAND_SHORT
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800227 subprocess.call(['ssh -n -q %s root@%s \'%s\'' %
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800228 (self._SSH_CONFIG, self._client.ip, kill_cmd)],
Craig Harrison91944552011-08-04 14:09:55 -0700229 shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800230 if self._remote_process and self._remote_process.poll() is None:
231 self._remote_process.terminate()
Craig Harrison91944552011-08-04 14:09:55 -0700232
233 def _terminate_all_ssh(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800234 """Terminate all ssh connections associated with remote processes."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800235 if self._ssh_tunnel and self._ssh_tunnel.poll() is None:
236 self._ssh_tunnel.terminate()
237 self._kill_remote_process()
238 self._ssh_tunnel = None