Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2011 The Chromium 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 | |
| 5 """A "Test Server Spawner" that handles killing/stopping per-test test servers. | |
| 6 | |
| 7 It's used to accept requests from the device to spawn and kill instances of the | |
| 8 chrome test server on the host. | |
| 9 """ | |
| 10 | |
| 11 import BaseHTTPServer | |
| 12 import logging | |
| 13 import os | |
| 14 import sys | |
| 15 import threading | |
| 16 import time | |
| 17 import urlparse | |
| 18 | |
| 19 # Path that are needed to import testserver | |
|
Nirnimesh
2011/10/21 08:16:15
cr_src = os.path.join(os.path.abspath(os.path.dirn
michaelbai
2011/10/21 21:08:41
Done.
| |
| 20 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | |
| 21 '..', 'third_party')) | |
| 22 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | |
| 23 '..', 'third_party', 'tlslite')) | |
| 24 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | |
| 25 '..', 'third_party', 'pyftpdlib', 'src')) | |
| 26 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | |
| 27 '..', 'net', 'tools', 'testserver')) | |
| 28 import testserver | |
| 29 | |
| 30 _test_servers = [] | |
|
Nirnimesh
2011/10/21 08:16:15
why global?
michaelbai
2011/10/21 21:08:41
It seemed that it used to grantee only one running
| |
| 31 | |
| 32 class SpawningServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
|
Nirnimesh
2011/10/21 08:16:15
docstring please
michaelbai
2011/10/21 21:08:41
Done.
| |
| 33 def GetServerType(self, server_type): | |
| 34 """Returns the server type to use when starting the test server. | |
| 35 | |
| 36 This function translate the command-line argument into the appropriate | |
| 37 numerical constant. | |
| 38 # TODO(yfriedman): Do that translation! | |
| 39 """ | |
| 40 if server_type: | |
| 41 pass | |
| 42 return 0 | |
| 43 | |
| 44 def do_GET(self): | |
| 45 parsed_path = urlparse.urlparse(self.path) | |
| 46 action = parsed_path.path | |
| 47 params = urlparse.parse_qs(parsed_path.query, keep_blank_values=1) | |
| 48 logging.info('Action is: %s' % action) | |
| 49 if action == '/killserver': | |
| 50 # There should only ever be one test server at a time. This may do the | |
| 51 # wrong thing if we try and start multiple test servers. | |
| 52 _test_servers.pop().Stop() | |
| 53 elif action == '/start': | |
| 54 logging.info('Handling request to spawn a test webserver') | |
| 55 for param in params: | |
| 56 logging.info('%s=%s' % (param, params[param][0])) | |
| 57 s_type = 0 | |
| 58 doc_root = None | |
| 59 if 'server_type' in params: | |
| 60 s_type = self.GetServerType(params['server_type'][0]) | |
| 61 if 'doc_root' in params: | |
| 62 doc_root = params['doc_root'][0] | |
| 63 self.webserver_thread = threading.Thread( | |
| 64 target=self.SpawnTestWebServer, args=(s_type, doc_root)) | |
| 65 self.webserver_thread.setDaemon(True) | |
| 66 self.webserver_thread.start() | |
| 67 self.send_response(200, 'OK') | |
| 68 self.send_header('Content-type', 'text/html') | |
| 69 self.end_headers() | |
| 70 self.wfile.write('<html><head><title>started</title></head></html>') | |
| 71 logging.info('Returned OK!!!') | |
| 72 | |
| 73 def SpawnTestWebServer(self, s_type, doc_root): | |
| 74 class Options(object): | |
| 75 log_to_console = True | |
| 76 server_type = s_type | |
| 77 port = self.server.test_server_port | |
| 78 data_dir = doc_root or 'chrome/test/data' | |
| 79 file_root_url = '/files/' | |
| 80 cert = False | |
| 81 policy_keys = None | |
| 82 policy_user = None | |
| 83 startup_pipe = None | |
| 84 options = Options() | |
| 85 print 'Listening on %d, type %d, data_dir %s' % (options.port, | |
|
Nirnimesh
2011/10/21 08:16:15
logging.info()?
michaelbai
2011/10/21 21:08:41
Done.
| |
| 86 options.server_type, | |
| 87 options.data_dir) | |
| 88 testserver.main(options, None, server_list=_test_servers) | |
| 89 logging.info('Test-server has died.') | |
| 90 | |
| 91 | |
| 92 class SpawningServer(object): | |
| 93 def __init__(self, test_server_spawner_port, test_server_port): | |
| 94 logging.info('Creating new spawner %d', test_server_spawner_port) | |
| 95 self.server = testserver.StoppableHTTPServer(('', test_server_spawner_port), | |
| 96 SpawningServerRequestHandler) | |
| 97 self.port = test_server_spawner_port | |
| 98 self.server.test_server_port = test_server_port | |
| 99 | |
| 100 def Listen(self): | |
| 101 logging.info('Starting test server spawner') | |
| 102 self.server.serve_forever() | |
| 103 | |
| 104 def Start(self): | |
| 105 listener_thread = threading.Thread(target=self.Listen) | |
| 106 listener_thread.setDaemon(True) | |
| 107 listener_thread.start() | |
| 108 time.sleep(1) | |
| 109 | |
| 110 def Stop(self): | |
| 111 self.server.Stop() | |
| OLD | NEW |