1 # Copyright (C) 2011 Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 """Base class used to start servers used by the layout tests.""" 30 31 import errno 32 import logging 33 import socket 34 import sys 35 import tempfile 36 import time 37 38 39 _log = logging.getLogger(__name__) 40 41 42 class ServerError(Exception): 43 pass 44 45 46 class ServerBase(object): 47 """A skeleton class for starting and stopping servers used by the layout tests.""" 48 49 def __init__(self, port_obj, output_dir): 50 self._port_obj = port_obj 51 self._executive = port_obj._executive 52 self._filesystem = port_obj._filesystem 53 self._output_dir = output_dir 54 55 # We need a non-checkout-dependent place to put lock files, etc. We 56 # don't use the Python default on the Mac because it defaults to a 57 # randomly-generated directory under /var/folders and no one would ever 58 # look there. 59 tmpdir = tempfile.gettempdir() 60 if port_obj.host.platform.is_mac(): 61 tmpdir = '/tmp' 62 63 self._runtime_path = self._filesystem.join(tmpdir, "WebKit") 64 self._filesystem.maybe_make_directory(self._runtime_path) 65 66 # Subclasses must override these fields. 67 self._name = '<virtual>' 68 self._log_prefixes = tuple() 69 self._mappings = {} 70 self._pid_file = None 71 self._start_cmd = None 72 73 # Subclasses may override these fields. 74 self._env = None 75 self._stdout = self._executive.PIPE 76 self._stderr = self._executive.PIPE 77 self._process = None 78 self._pid = None 79 80 def start(self): 81 """Starts the server. It is an error to start an already started server. 82 83 This method also stops any stale servers started by a previous instance.""" 84 assert not self._pid, '%s server is already running' % self._name 85 86 # Stop any stale servers left over from previous instances. 87 if self._filesystem.exists(self._pid_file): 88 try: 89 self._pid = int(self._filesystem.read_text_file(self._pid_file)) 90 self._stop_running_server() 91 except (ValueError, UnicodeDecodeError): 92 # These could be raised if the pid file is corrupt. 93 self._remove_pid_file() 94 self._pid = None 95 96 self._remove_stale_logs() 97 self._prepare_config() 98 self._check_that_all_ports_are_available() 99 100 self._pid = self._spawn_process() 101 102 if self._wait_for_action(self._is_server_running_on_all_ports): 103 _log.debug("%s successfully started (pid = %d)" % (self._name, self._pid)) 104 else: 105 self._stop_running_server() 106 raise ServerError('Failed to start %s server' % self._name) 107 108 def stop(self): 109 """Stops the server. Stopping a server that isn't started is harmless.""" 110 actual_pid = None 111 try: 112 if self._filesystem.exists(self._pid_file): 113 try: 114 actual_pid = int(self._filesystem.read_text_file(self._pid_file)) 115 except (ValueError, UnicodeDecodeError): 116 # These could be raised if the pid file is corrupt. 117 pass 118 if not self._pid: 119 self._pid = actual_pid 120 121 if not self._pid: 122 return 123 124 if not actual_pid: 125 _log.warning('Failed to stop %s: pid file is missing' % self._name) 126 return 127 if self._pid != actual_pid: 128 _log.warning('Failed to stop %s: pid file contains %d, not %d' % 129 (self._name, actual_pid, self._pid)) 130 # Try to kill the existing pid, anyway, in case it got orphaned. 131 self._executive.kill_process(self._pid) 132 self._pid = None 133 return 134 135 _log.debug("Attempting to shut down %s server at pid %d" % (self._name, self._pid)) 136 self._stop_running_server() 137 _log.debug("%s server at pid %d stopped" % (self._name, self._pid)) 138 self._pid = None 139 finally: 140 # Make sure we delete the pid file no matter what happens. 141 self._remove_pid_file() 142 143 def _prepare_config(self): 144 """This routine can be overridden by subclasses to do any sort 145 of initialization required prior to starting the server that may fail.""" 146 pass 147 148 def _remove_stale_logs(self): 149 """This routine can be overridden by subclasses to try and remove logs 150 left over from a prior run. This routine should log warnings if the 151 files cannot be deleted, but should not fail unless failure to 152 delete the logs will actually cause start() to fail.""" 153 # Sometimes logs are open in other processes but they should clear eventually. 154 for log_prefix in self._log_prefixes: 155 try: 156 self._remove_log_files(self._output_dir, log_prefix) 157 except OSError, e: 158 _log.warning('Failed to remove old %s %s files' % (self._name, log_prefix)) 159 160 def _spawn_process(self): 161 _log.debug('Starting %s server, cmd="%s"' % (self._name, self._start_cmd)) 162 process = self._executive.popen(self._start_cmd, env=self._env, stdout=self._stdout, stderr=self._stderr) 163 pid = process.pid 164 self._filesystem.write_text_file(self._pid_file, str(pid)) 165 return pid 166 167 def _stop_running_server(self): 168 self._wait_for_action(self._check_and_kill) 169 if self._filesystem.exists(self._pid_file): 170 self._filesystem.remove(self._pid_file) 171 172 def _check_and_kill(self): 173 if self._executive.check_running_pid(self._pid): 174 host = self._port_obj.host 175 self._executive.kill_process(self._pid) 176 return False 177 return True 178 179 def _remove_pid_file(self): 180 if self._filesystem.exists(self._pid_file): 181 self._filesystem.remove(self._pid_file) 182 183 def _remove_log_files(self, folder, starts_with): 184 files = self._filesystem.listdir(folder) 185 for file in files: 186 if file.startswith(starts_with): 187 full_path = self._filesystem.join(folder, file) 188 self._filesystem.remove(full_path) 189 190 def _wait_for_action(self, action, wait_secs=20.0, sleep_secs=1.0): 191 """Repeat the action for wait_sec or until it succeeds, sleeping for sleep_secs 192 in between each attempt. Returns whether it succeeded.""" 193 start_time = time.time() 194 while time.time() - start_time < wait_secs: 195 if action(): 196 return True 197 _log.debug("Waiting for action: %s" % action) 198 time.sleep(sleep_secs) 199 200 return False 201 202 def _is_server_running_on_all_ports(self): 203 """Returns whether the server is running on all the desired ports.""" 204 if not self._executive.check_running_pid(self._pid): 205 _log.debug("Server isn't running at all") 206 raise ServerError("Server exited") 207 208 for mapping in self._mappings: 209 s = socket.socket() 210 port = mapping['port'] 211 try: 212 s.connect(('localhost', port)) 213 _log.debug("Server running on %d" % port) 214 except IOError, e: 215 if e.errno not in (errno.ECONNREFUSED, errno.ECONNRESET): 216 raise 217 _log.debug("Server NOT running on %d: %s" % (port, e)) 218 return False 219 finally: 220 s.close() 221 return True 222 223 def _check_that_all_ports_are_available(self): 224 for mapping in self._mappings: 225 s = socket.socket() 226 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 227 port = mapping['port'] 228 try: 229 s.bind(('localhost', port)) 230 except IOError, e: 231 if e.errno in (errno.EALREADY, errno.EADDRINUSE): 232 raise ServerError('Port %d is already in use.' % port) 233 elif sys.platform == 'win32' and e.errno in (errno.WSAEACCES,): # pylint: disable=E1101 234 raise ServerError('Port %d is already in use.' % port) 235 else: 236 raise 237 finally: 238 s.close() 239