| OLD | NEW |
| (Empty) |
| 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 """A class to start/stop the apache http server used by layout tests.""" | |
| 30 | |
| 31 | |
| 32 import logging | |
| 33 import os | |
| 34 import re | |
| 35 import socket | |
| 36 import sys | |
| 37 | |
| 38 from webkitpy.layout_tests.servers import http_server_base | |
| 39 | |
| 40 | |
| 41 _log = logging.getLogger(__name__) | |
| 42 | |
| 43 | |
| 44 class LayoutTestApacheHttpd(http_server_base.HttpServerBase): | |
| 45 def __init__(self, port_obj, output_dir, additional_dirs=None, number_of_ser
vers=None): | |
| 46 """Args: | |
| 47 port_obj: handle to the platform-specific routines | |
| 48 output_dir: the absolute path to the layout test result directory | |
| 49 """ | |
| 50 http_server_base.HttpServerBase.__init__(self, port_obj, number_of_serve
rs) | |
| 51 # We use the name "httpd" instead of "apache" to make our paths (e.g. th
e pid file: /tmp/WebKit/httpd.pid) | |
| 52 # match old-run-webkit-tests: https://bugs.webkit.org/show_bug.cgi?id=63
956 | |
| 53 self._name = 'httpd' | |
| 54 self._mappings = [{'port': 8000}, | |
| 55 {'port': 8080}, | |
| 56 {'port': 8443, 'sslcert': True}] | |
| 57 self._output_dir = output_dir | |
| 58 self._filesystem.maybe_make_directory(output_dir) | |
| 59 | |
| 60 self._pid_file = self._filesystem.join(self._runtime_path, '%s.pid' % se
lf._name) | |
| 61 | |
| 62 test_dir = self._port_obj.layout_tests_dir() | |
| 63 js_test_resources_dir = self._filesystem.join(test_dir, "resources") | |
| 64 media_resources_dir = self._filesystem.join(test_dir, "media") | |
| 65 mime_types_path = self._filesystem.join(test_dir, "http", "conf", "mime.
types") | |
| 66 cert_file = self._filesystem.join(test_dir, "http", "conf", "webkit-http
d.pem") | |
| 67 access_log = self._filesystem.join(output_dir, "access_log.txt") | |
| 68 error_log = self._filesystem.join(output_dir, "error_log.txt") | |
| 69 document_root = self._filesystem.join(test_dir, "http", "tests") | |
| 70 | |
| 71 executable = self._port_obj.path_to_apache() | |
| 72 | |
| 73 start_cmd = [executable, | |
| 74 '-f', "\"%s\"" % self._port_obj.path_to_apache_config_file(), | |
| 75 '-C', "\'DocumentRoot \"%s\"\'" % document_root, | |
| 76 '-c', "\'Alias /js-test-resources \"%s\"'" % js_test_resources_dir, | |
| 77 '-c', "\'Alias /media-resources \"%s\"'" % media_resources_dir, | |
| 78 '-c', "\'TypesConfig \"%s\"\'" % mime_types_path, | |
| 79 '-c', "\'CustomLog \"%s\" common\'" % access_log, | |
| 80 '-c', "\'ErrorLog \"%s\"\'" % error_log, | |
| 81 '-C', "\'User \"%s\"\'" % os.environ.get("USERNAME", os.environ.get(
"USER", "")), | |
| 82 '-c', "\'PidFile %s'" % self._pid_file, | |
| 83 '-k', "start"] | |
| 84 | |
| 85 enable_ipv6 = self._port_obj.http_server_supports_ipv6() | |
| 86 # Perform part of the checks Apache's APR does when trying to listen to | |
| 87 # a specific host/port. This allows us to avoid trying to listen to | |
| 88 # IPV6 addresses when it fails on Apache. APR itself tries to call | |
| 89 # getaddrinfo() again without AI_ADDRCONFIG if the first call fails | |
| 90 # with EBADFLAGS, but that is not how it normally fails in our use | |
| 91 # cases, so ignore that for now. | |
| 92 # See https://bugs.webkit.org/show_bug.cgi?id=98602#c7 | |
| 93 try: | |
| 94 socket.getaddrinfo('::1', 0, 0, 0, 0, socket.AI_ADDRCONFIG) | |
| 95 except: | |
| 96 enable_ipv6 = False | |
| 97 | |
| 98 for mapping in self._mappings: | |
| 99 port = mapping['port'] | |
| 100 | |
| 101 start_cmd += ['-C', "\'Listen 127.0.0.1:%d\'" % port] | |
| 102 | |
| 103 # We listen to both IPv4 and IPv6 loop-back addresses, but ignore | |
| 104 # requests to 8000 from random users on network. | |
| 105 # See https://bugs.webkit.org/show_bug.cgi?id=37104 | |
| 106 if enable_ipv6: | |
| 107 start_cmd += ['-C', "\'Listen [::1]:%d\'" % port] | |
| 108 | |
| 109 if additional_dirs: | |
| 110 for alias, path in additional_dirs.iteritems(): | |
| 111 start_cmd += ['-c', "\'Alias %s \"%s\"\'" % (alias, path), | |
| 112 # Disable CGI handler for additional dirs. | |
| 113 '-c', "\'<Location %s>\'" % alias, | |
| 114 '-c', "\'RemoveHandler .cgi .pl\'", | |
| 115 '-c', "\'</Location>\'"] | |
| 116 | |
| 117 if self._number_of_servers: | |
| 118 start_cmd += ['-c', "\'StartServers %d\'" % self._number_of_servers, | |
| 119 '-c', "\'MinSpareServers %d\'" % self._number_of_serve
rs, | |
| 120 '-c', "\'MaxSpareServers %d\'" % self._number_of_serve
rs] | |
| 121 | |
| 122 stop_cmd = [executable, | |
| 123 '-f', "\"%s\"" % self._port_obj.path_to_apache_config_file(), | |
| 124 '-c', "\'PidFile %s'" % self._pid_file, | |
| 125 '-k', "stop"] | |
| 126 | |
| 127 start_cmd.extend(['-c', "\'SSLCertificateFile %s\'" % cert_file]) | |
| 128 # Join the string here so that Cygwin/Windows and Mac/Linux | |
| 129 # can use the same code. Otherwise, we could remove the single | |
| 130 # quotes above and keep cmd as a sequence. | |
| 131 # FIXME: It's unclear if this is still needed. | |
| 132 self._start_cmd = " ".join(start_cmd) | |
| 133 self._stop_cmd = " ".join(stop_cmd) | |
| 134 | |
| 135 def _spawn_process(self): | |
| 136 _log.debug('Starting %s server, cmd="%s"' % (self._name, str(self._start
_cmd))) | |
| 137 retval, err = self._run(self._start_cmd) | |
| 138 if retval or len(err): | |
| 139 raise http_server_base.ServerError('Failed to start %s: %s' % (self.
_name, err)) | |
| 140 | |
| 141 # For some reason apache isn't guaranteed to have created the pid file b
efore | |
| 142 # the process exits, so we wait a little while longer. | |
| 143 if not self._wait_for_action(lambda: self._filesystem.exists(self._pid_f
ile)): | |
| 144 raise http_server_base.ServerError('Failed to start %s: no pid file
found' % self._name) | |
| 145 | |
| 146 return int(self._filesystem.read_text_file(self._pid_file)) | |
| 147 | |
| 148 def _stop_running_server(self): | |
| 149 # If apache was forcefully killed, the pid file will not have been delet
ed, so check | |
| 150 # that the process specified by the pid_file no longer exists before del
eting the file. | |
| 151 if self._pid and not self._executive.check_running_pid(self._pid): | |
| 152 self._filesystem.remove(self._pid_file) | |
| 153 return | |
| 154 | |
| 155 retval, err = self._run(self._stop_cmd) | |
| 156 if retval or len(err): | |
| 157 raise http_server_base.ServerError('Failed to stop %s: %s' % (self._
name, err)) | |
| 158 | |
| 159 # For some reason apache isn't guaranteed to have actually stopped after | |
| 160 # the stop command returns, so we wait a little while longer for the | |
| 161 # pid file to be removed. | |
| 162 if not self._wait_for_action(lambda: not self._filesystem.exists(self._p
id_file)): | |
| 163 raise http_server_base.ServerError('Failed to stop %s: pid file stil
l exists' % self._name) | |
| 164 | |
| 165 def _run(self, cmd): | |
| 166 # Use shell=True because we join the arguments into a string for | |
| 167 # the sake of Window/Cygwin and it needs quoting that breaks | |
| 168 # shell=False. | |
| 169 # FIXME: We should not need to be joining shell arguments into strings. | |
| 170 # shell=True is a trail of tears. | |
| 171 # Note: Not thread safe: http://bugs.python.org/issue2320 | |
| 172 process = self._executive.popen(cmd, shell=True, stderr=self._executive.
PIPE) | |
| 173 process.wait() | |
| 174 retval = process.returncode | |
| 175 err = process.stderr.read() | |
| 176 return (retval, err) | |
| OLD | NEW |