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 def _GetRandomPort(self): | 67 @staticmethod |
| 68 def _GetRandomPort(): |
68 # The ports of test server is arranged in constants.py. | 69 # The ports of test server is arranged in constants.py. |
69 return random.randint(constants.LIGHTTPD_RANDOM_PORT_FIRST, | 70 return random.randint(constants.LIGHTTPD_RANDOM_PORT_FIRST, |
70 constants.LIGHTTPD_RANDOM_PORT_LAST) | 71 constants.LIGHTTPD_RANDOM_PORT_LAST) |
71 | 72 |
72 def StartupHttpServer(self): | 73 def StartupHttpServer(self): |
73 """Starts up a http server with specified document root and port.""" | 74 """Starts up a http server with specified document root and port.""" |
74 # If we want a specific port, make sure no one else is listening on it. | 75 # If we want a specific port, make sure no one else is listening on it. |
75 if self.fixed_port: | 76 if self.fixed_port: |
76 self._KillProcessListeningOnPort(self.fixed_port) | 77 self._KillProcessListeningOnPort(self.fixed_port) |
77 while True: | 78 while True: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 # Check for server startup error messages | 139 # Check for server startup error messages |
139 ix = self.process.expect([pexpect.TIMEOUT, pexpect.EOF, '.+'], | 140 ix = self.process.expect([pexpect.TIMEOUT, pexpect.EOF, '.+'], |
140 timeout=timeout) | 141 timeout=timeout) |
141 if ix == 2: # stdout spew from the server | 142 if ix == 2: # stdout spew from the server |
142 server_msg += self.process.match.group(0) | 143 server_msg += self.process.match.group(0) |
143 elif ix == 1: # EOF -- server has quit so giveup. | 144 elif ix == 1: # EOF -- server has quit so giveup. |
144 client_error = client_error or 'Server exited' | 145 client_error = client_error or 'Server exited' |
145 break | 146 break |
146 return (client_error or 'Timeout', server_msg) | 147 return (client_error or 'Timeout', server_msg) |
147 | 148 |
148 def _KillProcessListeningOnPort(self, port): | 149 @staticmethod |
| 150 def _KillProcessListeningOnPort(port): |
149 """Checks if there is a process listening on port number |port| and | 151 """Checks if there is a process listening on port number |port| and |
150 terminates it if found. | 152 terminates it if found. |
151 | 153 |
152 Args: | 154 Args: |
153 port: Port number to check. | 155 port: Port number to check. |
154 """ | 156 """ |
155 if subprocess.call(['fuser', '-kv', '%d/tcp' % port]) == 0: | 157 if subprocess.call(['fuser', '-kv', '%d/tcp' % port]) == 0: |
156 # Give the process some time to terminate and check that it is gone. | 158 # Give the process some time to terminate and check that it is gone. |
157 time.sleep(2) | 159 time.sleep(2) |
158 assert subprocess.call(['fuser', '-v', '%d/tcp' % port]) != 0, \ | 160 assert subprocess.call(['fuser', '-v', '%d/tcp' % port]) != 0, \ |
159 'Unable to kill process listening on port %d.' % port | 161 'Unable to kill process listening on port %d.' % port |
160 | 162 |
161 def _GetDefaultBaseConfig(self): | 163 @staticmethod |
| 164 def _GetDefaultBaseConfig(): |
162 return """server.tag = "%(server_tag)s" | 165 return """server.tag = "%(server_tag)s" |
163 server.modules = ( "mod_access", | 166 server.modules = ( "mod_access", |
164 "mod_accesslog", | 167 "mod_accesslog", |
165 "mod_alias", | 168 "mod_alias", |
166 "mod_cgi", | 169 "mod_cgi", |
167 "mod_rewrite" ) | 170 "mod_rewrite" ) |
168 | 171 |
169 # default document root required | 172 # default document root required |
170 #server.document-root = "." | 173 #server.document-root = "." |
171 | 174 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 raw_input('Server running at http://127.0.0.1:%s -' | 247 raw_input('Server running at http://127.0.0.1:%s -' |
245 ' press Enter to exit it.' % server.port) | 248 ' press Enter to exit it.' % server.port) |
246 else: | 249 else: |
247 print 'Server exit code:', server.process.exitstatus | 250 print 'Server exit code:', server.process.exitstatus |
248 finally: | 251 finally: |
249 server.ShutdownHttpServer() | 252 server.ShutdownHttpServer() |
250 | 253 |
251 | 254 |
252 if __name__ == '__main__': | 255 if __name__ == '__main__': |
253 sys.exit(main(sys.argv)) | 256 sys.exit(main(sys.argv)) |
OLD | NEW |