OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Provides a convenient wrapper for spawning a test lighttpd instance. | 7 """Provides a convenient wrapper for spawning a test lighttpd instance. |
8 | 8 |
9 Usage: | 9 Usage: |
10 lighttpd_server PATH_TO_DOC_ROOT | 10 lighttpd_server PATH_TO_DOC_ROOT |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 self.extra_config_contents = extra_config_contents | 57 self.extra_config_contents = extra_config_contents |
58 self.config_path = config_path or self._Mktmp('config') | 58 self.config_path = config_path or self._Mktmp('config') |
59 self.error_log = error_log or self._Mktmp('error_log') | 59 self.error_log = error_log or self._Mktmp('error_log') |
60 self.access_log = access_log or self._Mktmp('access_log') | 60 self.access_log = access_log or self._Mktmp('access_log') |
61 self.pid_file = self._Mktmp('pid_file') | 61 self.pid_file = self._Mktmp('pid_file') |
62 self.process = None | 62 self.process = None |
63 | 63 |
64 def _Mktmp(self, name): | 64 def _Mktmp(self, name): |
65 return os.path.join(self.temp_dir, name) | 65 return os.path.join(self.temp_dir, name) |
66 | 66 |
67 @staticmethod | 67 def _GetRandomPort(self): |
68 def _GetRandomPort(): | |
69 # The ports of test server is arranged in constants.py. | 68 # The ports of test server is arranged in constants.py. |
70 return random.randint(constants.LIGHTTPD_RANDOM_PORT_FIRST, | 69 return random.randint(constants.LIGHTTPD_RANDOM_PORT_FIRST, |
71 constants.LIGHTTPD_RANDOM_PORT_LAST) | 70 constants.LIGHTTPD_RANDOM_PORT_LAST) |
72 | 71 |
73 def StartupHttpServer(self): | 72 def StartupHttpServer(self): |
74 """Starts up a http server with specified document root and port.""" | 73 """Starts up a http server with specified document root and port.""" |
75 # If we want a specific port, make sure no one else is listening on it. | 74 # If we want a specific port, make sure no one else is listening on it. |
76 if self.fixed_port: | 75 if self.fixed_port: |
77 self._KillProcessListeningOnPort(self.fixed_port) | 76 self._KillProcessListeningOnPort(self.fixed_port) |
78 while True: | 77 while True: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 # Check for server startup error messages | 138 # Check for server startup error messages |
140 ix = self.process.expect([pexpect.TIMEOUT, pexpect.EOF, '.+'], | 139 ix = self.process.expect([pexpect.TIMEOUT, pexpect.EOF, '.+'], |
141 timeout=timeout) | 140 timeout=timeout) |
142 if ix == 2: # stdout spew from the server | 141 if ix == 2: # stdout spew from the server |
143 server_msg += self.process.match.group(0) | 142 server_msg += self.process.match.group(0) |
144 elif ix == 1: # EOF -- server has quit so giveup. | 143 elif ix == 1: # EOF -- server has quit so giveup. |
145 client_error = client_error or 'Server exited' | 144 client_error = client_error or 'Server exited' |
146 break | 145 break |
147 return (client_error or 'Timeout', server_msg) | 146 return (client_error or 'Timeout', server_msg) |
148 | 147 |
149 @staticmethod | 148 def _KillProcessListeningOnPort(self, port): |
150 def _KillProcessListeningOnPort(port): | |
151 """Checks if there is a process listening on port number |port| and | 149 """Checks if there is a process listening on port number |port| and |
152 terminates it if found. | 150 terminates it if found. |
153 | 151 |
154 Args: | 152 Args: |
155 port: Port number to check. | 153 port: Port number to check. |
156 """ | 154 """ |
157 if subprocess.call(['fuser', '-kv', '%d/tcp' % port]) == 0: | 155 if subprocess.call(['fuser', '-kv', '%d/tcp' % port]) == 0: |
158 # Give the process some time to terminate and check that it is gone. | 156 # Give the process some time to terminate and check that it is gone. |
159 time.sleep(2) | 157 time.sleep(2) |
160 assert subprocess.call(['fuser', '-v', '%d/tcp' % port]) != 0, \ | 158 assert subprocess.call(['fuser', '-v', '%d/tcp' % port]) != 0, \ |
161 'Unable to kill process listening on port %d.' % port | 159 'Unable to kill process listening on port %d.' % port |
162 | 160 |
163 @staticmethod | 161 def _GetDefaultBaseConfig(self): |
164 def _GetDefaultBaseConfig(): | |
165 return """server.tag = "%(server_tag)s" | 162 return """server.tag = "%(server_tag)s" |
166 server.modules = ( "mod_access", | 163 server.modules = ( "mod_access", |
167 "mod_accesslog", | 164 "mod_accesslog", |
168 "mod_alias", | 165 "mod_alias", |
169 "mod_cgi", | 166 "mod_cgi", |
170 "mod_rewrite" ) | 167 "mod_rewrite" ) |
171 | 168 |
172 # default document root required | 169 # default document root required |
173 #server.document-root = "." | 170 #server.document-root = "." |
174 | 171 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 raw_input('Server running at http://127.0.0.1:%s -' | 244 raw_input('Server running at http://127.0.0.1:%s -' |
248 ' press Enter to exit it.' % server.port) | 245 ' press Enter to exit it.' % server.port) |
249 else: | 246 else: |
250 print 'Server exit code:', server.process.exitstatus | 247 print 'Server exit code:', server.process.exitstatus |
251 finally: | 248 finally: |
252 server.ShutdownHttpServer() | 249 server.ShutdownHttpServer() |
253 | 250 |
254 | 251 |
255 if __name__ == '__main__': | 252 if __name__ == '__main__': |
256 sys.exit(main(sys.argv)) | 253 sys.exit(main(sys.argv)) |
OLD | NEW |