Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 6 | import re |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 7 | import subprocess |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 8 | import time |
| 9 | import xmlrpclib |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 10 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 12 | from autotest_lib.server import autotest, site_host_attributes, test |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 13 | import autotest_lib.server.cros.servo |
| 14 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 15 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 16 | class ServoTest(test.test): |
| 17 | """AutoTest test class that creates and destroys a servo object. |
| 18 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 19 | Servo-based server side AutoTests can inherit from this object. |
| 20 | There are 2 remote clients supported: |
| 21 | If use_pyauto flag is True, a remote PyAuto client will be launched; |
| 22 | 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] | 23 | """ |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 24 | version = 2 |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 25 | # Abstracts access to all Servo functions. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 26 | servo = None |
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. |
| 44 | 'client_test': 'desktopui_ServoPyAuto', |
| 45 | # The remote command to be run. |
| 46 | 'remote_command': 'python /usr/local/autotest/cros/servo_pyauto.py' |
| 47 | ' --no-http-server', |
| 48 | # The short form of remote command, used by pkill. |
| 49 | 'remote_command_short': 'servo_pyauto', |
| 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', |
| 56 | }, |
| 57 | 'faft': { |
| 58 | 'used': False, |
| 59 | 'ref_name': 'faft_client', |
| 60 | 'port': 9990, |
| 61 | 'client_test': 'firmware_FAFTClient', |
| 62 | 'remote_command': 'python /usr/local/autotest/cros/faft_client.py', |
| 63 | 'remote_command_short': 'faft_client', |
| 64 | 'remote_process': None, |
| 65 | 'ssh_tunnel': None, |
| 66 | 'polling_rpc': 'is_available', |
| 67 | }, |
| 68 | } |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 69 | |
| 70 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 71 | def initialize(self, host, cmdline_args, use_pyauto=False, use_faft=False): |
| 72 | """Create a Servo object and install the dependency. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 73 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 74 | If use_pyauto/use_faft is True the PyAuto/FAFTClient dependency is |
| 75 | installed on the client and a remote PyAuto/FAFTClient server is |
| 76 | launched and connected. |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 77 | """ |
| 78 | # Assign default arguments for servo invocation. |
| 79 | args = { |
| 80 | 'servo_host': 'localhost', 'servo_port': 9999, |
| 81 | 'xml_config': 'servo.xml', 'servo_vid': None, 'servo_pid': None, |
| 82 | 'servo_serial': None, 'use_pyauto': False} |
| 83 | |
| 84 | # Parse arguments from AFE and override servo defaults above. |
| 85 | client_attributes = site_host_attributes.HostAttributes(host.hostname) |
| 86 | if hasattr(site_host_attributes, 'servo_serial'): |
| 87 | args['servo_serial'] = client_attributes.servo_serial |
| 88 | |
| 89 | # Parse arguments from command line and override previous AFE or servo |
| 90 | # defaults |
| 91 | for arg in cmdline_args: |
| 92 | match = re.search("^(\w+)=(.+)", arg) |
| 93 | if match: |
| 94 | args[match.group(1)] = match.group(2) |
| 95 | |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 96 | # Initialize servotest args. |
| 97 | self._client = host; |
| 98 | self._remote_infos['pyauto']['used'] = use_pyauto |
| 99 | self._remote_infos['faft']['used'] = use_faft |
| 100 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 101 | self.servo = autotest_lib.server.cros.servo.Servo( |
| 102 | args['servo_host'], args['servo_port'], args['xml_config'], |
| 103 | args['servo_vid'], args['servo_pid'], args['servo_serial']) |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 104 | # Initializes dut, may raise AssertionError if pre-defined gpio |
| 105 | # sequence to set GPIO's fail. Autotest does not handle exception |
| 106 | # throwing in initialize and will cause a test to hang. |
| 107 | try: |
| 108 | self.servo.initialize_dut() |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 109 | except (AssertionError, xmlrpclib.Fault) as e: |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 110 | del self.servo |
| 111 | raise error.TestFail(e) |
| 112 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 113 | # Install PyAuto/FAFTClient dependency. |
| 114 | for info in self._remote_infos.itervalues(): |
| 115 | if info['used']: |
| 116 | if not self._autotest_client: |
| 117 | self._autotest_client = autotest.Autotest(self._client) |
| 118 | self._autotest_client.run_test(info['client_test']) |
| 119 | self.launch_client(info) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 120 | |
| 121 | |
| 122 | def assert_ping(self): |
| 123 | """Ping to assert that the device is up.""" |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 124 | assert self.ping_test(self._client.ip) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def assert_pingfail(self): |
| 128 | """Ping to assert that the device is down.""" |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 129 | assert not self.ping_test(self._client.ip) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 130 | |
| 131 | |
| 132 | def ping_test(self, hostname, timeout=5): |
| 133 | """Verify whether a host responds to a ping. |
| 134 | |
| 135 | Args: |
| 136 | hostname: Hostname to ping. |
| 137 | timeout: Time in seconds to wait for a response. |
| 138 | """ |
| 139 | return subprocess.call(['ping', '-c', '1', '-W', |
| 140 | str(timeout), hostname]) == 0 |
| 141 | |
| 142 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 143 | def launch_client(self, info): |
| 144 | """Launch a remote process on client and set up an xmlrpc connection. |
| 145 | |
| 146 | Args: |
| 147 | info: A dict of remote info, see the definition of self._remote_infos. |
| 148 | """ |
| 149 | assert info['used'], \ |
| 150 | 'Remote %s dependency not installed.' % info['ref_name'] |
| 151 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
| 152 | self._launch_ssh_tunnel(info) |
| 153 | assert info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None, \ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 154 | 'The SSH tunnel is not up.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 155 | |
| 156 | # Launch RPC server remotely. |
| 157 | self._kill_remote_process(info) |
| 158 | logging.info('Client command: %s' % info['remote_command']) |
| 159 | info['remote_process'] = subprocess.Popen([ |
Craig Harrison | 63e9c6a | 2011-08-10 17:13:57 -0700 | [diff] [blame] | 160 | 'ssh -o "StrictHostKeyChecking no" -n root@%s \'%s\'' % |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 161 | (self._client.ip, info['remote_command'])], shell=True) |
| 162 | |
| 163 | # Connect to RPC object. |
| 164 | logging.info('Connecting to client RPC server...') |
| 165 | remote_url = 'http://localhost:%s' % info['port'] |
| 166 | setattr(self, info['ref_name'], |
| 167 | xmlrpclib.ServerProxy(remote_url, allow_none=True)) |
| 168 | logging.info('Server proxy: %s' % remote_url) |
| 169 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 170 | # Poll for client RPC server to come online. |
| 171 | timeout = 10 |
| 172 | succeed = False |
| 173 | while timeout > 0 and not succeed: |
| 174 | time.sleep(2) |
| 175 | try: |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 176 | remote_object = getattr(self, info['ref_name']) |
| 177 | polling_rpc = getattr(remote_object, info['polling_rpc']) |
| 178 | polling_rpc() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 179 | succeed = True |
| 180 | except: |
| 181 | timeout -= 1 |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 182 | assert succeed, 'Timed out connecting to client RPC server.' |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def wait_for_client(self): |
| 186 | """Wait for the client to come back online. |
| 187 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 188 | New remote processes will be launched if their used flags are enabled. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 189 | """ |
| 190 | timeout = 10 |
| 191 | # Ensure old ssh connections are terminated. |
| 192 | self._terminate_all_ssh() |
| 193 | # Wait for the client to come up. |
| 194 | while timeout > 0 and not self.ping_test(self._client.ip): |
| 195 | time.sleep(5) |
| 196 | timeout -= 1 |
| 197 | assert timeout, 'Timed out waiting for client to reboot.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 198 | logging.info('Server: Client machine is up.') |
| 199 | # Relaunch remote clients. |
| 200 | for name, info in self._remote_infos.iteritems(): |
| 201 | if info['used']: |
| 202 | self.launch_client(info) |
| 203 | logging.info('Server: Relaunched remote %s.' % name) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 204 | |
| 205 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 206 | def cleanup(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 207 | """Delete the Servo object, call remote cleanup, and kill ssh.""" |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 208 | if self.servo: |
| 209 | del self.servo |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 210 | for info in self._remote_infos.itervalues(): |
| 211 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 212 | remote_object = getattr(self, info['ref_name']) |
| 213 | remote_object.cleanup() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 214 | self._terminate_all_ssh() |
| 215 | |
| 216 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 217 | def _launch_ssh_tunnel(self, info): |
| 218 | """Establish an ssh tunnel for connecting to the remote RPC server. |
| 219 | |
| 220 | Args: |
| 221 | info: A dict of remote info, see the definition of self._remote_infos. |
| 222 | """ |
| 223 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
| 224 | info['ssh_tunnel'] = subprocess.Popen(['ssh', '-N', '-n', '-L', |
| 225 | '%s:localhost:%s' % (info['port'], info['port']), |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 226 | 'root@%s' % self._client.ip]) |
| 227 | |
| 228 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 229 | def _kill_remote_process(self, info): |
| 230 | """Ensure the remote process and local ssh process are terminated. |
| 231 | |
| 232 | Args: |
| 233 | info: A dict of remote info, see the definition of self._remote_infos. |
| 234 | """ |
| 235 | kill_cmd = 'pkill -f %s' % info['remote_command_short'] |
Craig Harrison | 63e9c6a | 2011-08-10 17:13:57 -0700 | [diff] [blame] | 236 | subprocess.call(['ssh -n -o "StrictHostKeyChecking no" root@%s \'%s\'' % |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 237 | (self._client.ip, kill_cmd)], |
| 238 | shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 239 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 240 | info['remote_process'].terminate() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def _terminate_all_ssh(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 244 | """Terminate all ssh connections associated with remote processes.""" |
| 245 | for info in self._remote_infos.itervalues(): |
| 246 | if info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None: |
| 247 | info['ssh_tunnel'].terminate() |
| 248 | self._kill_remote_process(info) |