blob: c4d01c0f1acba07d78f9b4a6c989a58fbe645f94 [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
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -07008import autotest_lib.client.cros.faft.config
Vadim Bendeburybb731952012-12-05 17:30:36 -08009from autotest_lib.server import autotest, test
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080010
Craig Harrison91944552011-08-04 14:09:55 -070011
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070012class ServoTest(test.test):
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080013 """AutoTest test class to serve as a parent class for FAFT tests.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070014
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080015 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 Harrison2b6c6fc2011-06-23 10:34:02 -070019 """
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080020 version = 2
J. Richard Barnette67ccb872012-04-19 16:34:56 -070021
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -070022 config = autotest_lib.client.cros.faft.config.Config()
J. Richard Barnette67ccb872012-04-19 16:34:56 -070023
Tom Wai-Hong Tam54f4c582013-07-18 12:05:27 +080024 def initialize(self, host):
J. Richard Barnette67ccb872012-04-19 16:34:56 -070025 """Create a Servo object and install the dependency.
J. Richard Barnette67ccb872012-04-19 16:34:56 -070026 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080027 self.servo = host.servo
28 self.faft_client = None
29 self._client = host
30 self._ssh_tunnel = None
31 self._remote_process = None
Vic Yang8535e772013-05-02 09:07:25 +080032 self._local_port = None
J. Richard Barnette67ccb872012-04-19 16:34:56 -070033
Chrome Bot9a1137d2011-07-19 14:35:00 -070034 # Initializes dut, may raise AssertionError if pre-defined gpio
35 # sequence to set GPIO's fail. Autotest does not handle exception
36 # throwing in initialize and will cause a test to hang.
37 try:
38 self.servo.initialize_dut()
Chris Sosa33320a82011-10-24 14:28:32 -070039 except (AssertionError, xmlrpclib.Fault) as e:
Chrome Bot9a1137d2011-07-19 14:35:00 -070040 raise error.TestFail(e)
41
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080042 # Install faft_client dependency.
43 self._autotest_client = autotest.Autotest(self._client)
44 self._autotest_client.install()
45 self._launch_client()
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070046
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070047 def _ping_test(self, hostname, timeout=5):
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070048 """Verify whether a host responds to a ping.
49
Tom Wai-Hong Tam144202a2013-07-18 13:59:29 +080050 @param hostname: Hostname to ping.
51 @param timeout: Time in seconds to wait for a response.
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070052 """
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +080053 with open(os.devnull, 'w') as fnull:
54 return subprocess.call(
55 ['ping', '-c', '1', '-W', str(timeout), hostname],
56 stdout=fnull, stderr=fnull) == 0
Craig Harrison2b6c6fc2011-06-23 10:34:02 -070057
ctchang74816ac2012-08-31 11:12:43 +080058 def _sshd_test(self, hostname, timeout=5):
59 """Verify whether sshd is running in host.
60
Tom Wai-Hong Tam144202a2013-07-18 13:59:29 +080061 @param hostname: Hostname to verify.
62 @param timeout: Time in seconds to wait for a response.
ctchang74816ac2012-08-31 11:12:43 +080063 """
64 try:
65 sock = socket.create_connection((hostname, 22), timeout=timeout)
66 sock.close()
67 return True
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080068 except socket.timeout:
69 return False
ctchang74816ac2012-08-31 11:12:43 +080070 except socket.error:
Tom Wai-Hong Tam711a7aa2012-09-18 10:30:43 +080071 time.sleep(timeout)
ctchang74816ac2012-08-31 11:12:43 +080072 return False
73
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080074 def _launch_client(self):
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080075 """Launch a remote XML RPC connection on client with retrials.
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080076 """
77 retry = 3
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080078 while retry:
79 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080080 self._launch_client_once()
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080081 break
82 except AssertionError:
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080083 retry -= 1
Tom Wai-Hong Tamb3db3b22012-12-04 11:59:54 +080084 if retry:
85 logging.info('Retry again...')
86 time.sleep(5)
87 else:
88 raise
Tom Wai-Hong Tame97cb4a2012-11-22 09:52:46 +080089
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080090 def _launch_client_once(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080091 """Launch a remote process on client and set up an xmlrpc connection.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080092 """
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080093 if self._ssh_tunnel:
94 self._ssh_tunnel.terminate()
95 self._ssh_tunnel = None
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +080096
97 # Launch RPC server remotely.
J. Richard Barnette33aec9f2013-02-01 16:38:41 -080098 self._kill_remote_process()
99 self._launch_ssh_tunnel()
Vadim Bendebury692341e2012-12-18 15:24:38 -0800100
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700101 logging.info('Client command: %s', self.config.rpc_command)
102 logging.info("Logging to %s", self.config.rpc_logfile)
Vic Yanga3492372013-05-16 16:11:19 +0800103 full_cmd = ['ssh -n %s root@%s \'%s &> %s\'' % (
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700104 self.config.rpc_ssh_options, self._client.ip,
105 self.config.rpc_command, self.config.rpc_logfile)]
Vadim Bendebury89ec24e2012-12-17 12:54:18 -0800106 logging.info('Starting process %s', ' '.join(full_cmd))
Vic Yanga3492372013-05-16 16:11:19 +0800107 self._remote_process = subprocess.Popen(full_cmd, shell=True,
108 stdout=subprocess.PIPE,
109 stderr=subprocess.PIPE)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800110
111 # Connect to RPC object.
112 logging.info('Connecting to client RPC server...')
Vic Yang8535e772013-05-02 09:07:25 +0800113 remote_url = 'http://localhost:%s' % self._local_port
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800114 self.faft_client = xmlrpclib.ServerProxy(remote_url, allow_none=True)
Vic Yang3a7cf602012-11-07 17:28:39 +0800115 logging.info('Server proxy: %s', remote_url)
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800116
Craig Harrison91944552011-08-04 14:09:55 -0700117 # Poll for client RPC server to come online.
Vic Yang6c1dc152012-09-13 14:56:11 +0800118 timeout = 20
Craig Harrison91944552011-08-04 14:09:55 -0700119 succeed = False
Vic Yang3a7cf602012-11-07 17:28:39 +0800120 rpc_error = None
Craig Harrison91944552011-08-04 14:09:55 -0700121 while timeout > 0 and not succeed:
Vic Yang6c1dc152012-09-13 14:56:11 +0800122 time.sleep(1)
Vic Yanga3492372013-05-16 16:11:19 +0800123 if self._remote_process.poll() is not None:
124 # The SSH process is gone. Log stderr.
125 logging.error('Remote process died!')
126 sout, serr = self._remote_process.communicate()
127 logging.error('Stdout: %s', sout)
128 logging.error('Stderr: %s', serr)
129 break
130
Craig Harrison91944552011-08-04 14:09:55 -0700131 try:
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800132 self.faft_client.system.is_available()
Craig Harrison91944552011-08-04 14:09:55 -0700133 succeed = True
Vadim Bendebury692341e2012-12-18 15:24:38 -0800134 except (socket.error,
135 xmlrpclib.ProtocolError,
136 httplib.BadStatusLine) as e:
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700137 logging.info('caught: %s %s, tries left: %s',
138 repr(e), str(e), timeout)
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)
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700153 p = subprocess.Popen(
154 ['ssh -n -q %s root@%s \'cat %s\'' % (
155 self.config.rpc_ssh_options,
156 self._client.ip, self.config.rpc_logfile)],
157 shell=True, stdout=subprocess.PIPE)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800158 logging.info('Log of running remote %s:',
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700159 self.config.rpc_command_short)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800160 logging.info(p.communicate()[0])
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800161 assert succeed, 'Timed out connecting to client RPC server.'
Craig Harrison91944552011-08-04 14:09:55 -0700162
Vic Yang8bbc1c32012-09-13 11:47:44 +0800163 def wait_for_client(self, install_deps=False, timeout=100):
Craig Harrison91944552011-08-04 14:09:55 -0700164 """Wait for the client to come back online.
165
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800166 New remote processes will be launched if their used flags are enabled.
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +0800167
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700168 @param install_deps: If True, install Autotest dependency when ready.
169 @param 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
Tom Wai-Hong Tama9b225b2013-05-03 10:35:10 +0800185 def wait_for_client_offline(self, timeout=60, orig_boot_id=None):
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800186 """Wait for the client to come offline.
187
Yusuf Mohsinallyf3cb7642013-04-15 14:20:56 -0700188 @param timeout: Time in seconds to wait the client to come offline.
Tom Wai-Hong Tam12636062013-04-09 17:14:15 +0800189 @param orig_boot_id: A string containing the original boot id.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800190 """
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
Tom Wai-Hong Tam12636062013-04-09 17:14:15 +0800195
196 # As get_boot_id() requires DUT online. So we move the comparison here.
197 if timeout == 0 and orig_boot_id:
198 if self._client.get_boot_id() != orig_boot_id:
199 logging.warn('Reboot done very quickly.')
200 return
201
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800202 assert timeout, 'Timed out waiting for client offline.'
203 logging.info('Server: Client machine is offline.')
204
Vic Yang4e0d1f72012-05-24 15:11:11 +0800205 def kill_remote(self):
206 """Call remote cleanup and kill ssh."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800207 if self._remote_process and self._remote_process.poll() is None:
208 try:
209 self.faft_client.cleanup()
210 logging.info('Cleanup succeeded.')
211 except xmlrpclib.ProtocolError, e:
212 logging.info('Cleanup returned protocol error: ' + str(e))
Craig Harrison91944552011-08-04 14:09:55 -0700213 self._terminate_all_ssh()
214
Vic Yang4e0d1f72012-05-24 15:11:11 +0800215 def cleanup(self):
216 """Delete the Servo object, call remote cleanup, and kill ssh."""
Vic Yang4e0d1f72012-05-24 15:11:11 +0800217 self.kill_remote()
218
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800219 def _launch_ssh_tunnel(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800220 """Establish an ssh tunnel for connecting to the remote RPC server.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800221 """
Vic Yang8535e772013-05-02 09:07:25 +0800222 if self._local_port is None:
Vic Yang69249552013-05-09 01:58:11 +0800223 self._local_port = utils.get_unused_port()
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800224 if not self._ssh_tunnel or self._ssh_tunnel.poll() is not None:
225 self._ssh_tunnel = subprocess.Popen([
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800226 'ssh -N -n -q %s -L %s:localhost:%s root@%s' %
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700227 (self.config.rpc_ssh_options, self._local_port,
228 self.config.rpc_port, self._client.ip)], shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800229 assert self._ssh_tunnel.poll() is None, \
Vic Yang8535e772013-05-02 09:07:25 +0800230 'The SSH tunnel on port %d is not up.' % self._local_port
Craig Harrison91944552011-08-04 14:09:55 -0700231
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800232 def _kill_remote_process(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800233 """Ensure the remote process and local ssh process are terminated.
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800234 """
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700235 kill_cmd = 'pkill -f %s' % self.config.rpc_command_short
Tom Wai-Hong Tam4a257e52011-11-12 08:36:22 +0800236 subprocess.call(['ssh -n -q %s root@%s \'%s\'' %
Yusuf Mohsinallyf4664222013-08-14 12:59:16 -0700237 (self.config.rpc_ssh_options,
238 self._client.ip, kill_cmd)], shell=True)
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800239 if self._remote_process and self._remote_process.poll() is None:
240 self._remote_process.terminate()
Craig Harrison91944552011-08-04 14:09:55 -0700241
242 def _terminate_all_ssh(self):
Tom Wai-Hong Tambea57b32011-09-02 18:27:47 +0800243 """Terminate all ssh connections associated with remote processes."""
J. Richard Barnette33aec9f2013-02-01 16:38:41 -0800244 if self._ssh_tunnel and self._ssh_tunnel.poll() is None:
245 self._ssh_tunnel.terminate()
246 self._kill_remote_process()
247 self._ssh_tunnel = None