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 | |
20 cr_src = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', '..') | |
21 sys.path.append(os.path.join(cr_src, 'third_party')) | |
22 sys.path.append(os.path.join(cr_src, 'third_party', 'tlslite')) | |
23 sys.path.append(os.path.join(cr_src, 'third_party', 'pyftpdlib', 'src')) | |
24 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', | |
25 '..', 'net', 'tools', 'testserver')) | |
26 import testserver | |
27 | |
28 _test_servers = [] | |
29 | |
30 class SpawningServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
31 """A handler used to process http GET request. | |
32 """ | |
33 | |
34 def GetServerType(self, server_type): | |
35 """Returns the server type to use when starting the test server. | |
36 | |
37 This function translate the command-line argument into the appropriate | |
38 numerical constant. | |
39 # TODO(yfriedman): Do that translation! | |
40 """ | |
41 if server_type: | |
42 pass | |
43 return 0 | |
44 | |
45 def do_GET(self): | |
46 parsed_path = urlparse.urlparse(self.path) | |
47 action = parsed_path.path | |
48 params = urlparse.parse_qs(parsed_path.query, keep_blank_values=1) | |
49 logging.info('Action is: %s' % action) | |
50 if action == '/killserver': | |
51 # There should only ever be one test server at a time. This may do the | |
52 # wrong thing if we try and start multiple test servers. | |
53 _test_servers.pop().Stop() | |
54 elif action == '/start': | |
55 logging.info('Handling request to spawn a test webserver') | |
56 for param in params: | |
57 logging.info('%s=%s' % (param, params[param][0])) | |
58 s_type = 0 | |
59 doc_root = None | |
60 if 'server_type' in params: | |
61 s_type = self.GetServerType(params['server_type'][0]) | |
62 if 'doc_root' in params: | |
63 doc_root = params['doc_root'][0] | |
64 self.webserver_thread = threading.Thread( | |
65 target=self.SpawnTestWebServer, args=(s_type, doc_root)) | |
66 self.webserver_thread.setDaemon(True) | |
67 self.webserver_thread.start() | |
68 self.send_response(200, 'OK') | |
69 self.send_header('Content-type', 'text/html') | |
70 self.end_headers() | |
71 self.wfile.write('<html><head><title>started</title></head></html>') | |
72 logging.info('Returned OK!!!') | |
73 | |
74 def SpawnTestWebServer(self, s_type, doc_root): | |
75 class Options(object): | |
76 log_to_console = True | |
77 server_type = s_type | |
78 port = self.server.test_server_port | |
79 data_dir = doc_root or 'chrome/test/data' | |
80 file_root_url = '/files/' | |
81 cert = False | |
82 policy_keys = None | |
83 policy_user = None | |
84 startup_pipe = None | |
85 options = Options() | |
86 logging.info('Listening on %d, type %d, data_dir %s' % (options.port, | |
87 options.server_type, 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 """The class used to start/stop a http server. | |
94 """ | |
95 | |
96 def __init__(self, test_server_spawner_port, test_server_port): | |
97 logging.info('Creating new spawner %d', test_server_spawner_port) | |
98 self.server = testserver.StoppableHTTPServer(('', test_server_spawner_port), | |
99 SpawningServerRequestHandler) | |
100 self.port = test_server_spawner_port | |
101 self.server.test_server_port = test_server_port | |
102 | |
103 def Listen(self): | |
104 logging.info('Starting test server spawner') | |
105 self.server.serve_forever() | |
106 | |
107 def Start(self): | |
108 listener_thread = threading.Thread(target=self.Listen) | |
109 listener_thread.setDaemon(True) | |
110 listener_thread.start() | |
111 time.sleep(1) | |
112 | |
113 def Stop(self): | |
114 self.server.Stop() | |
OLD | NEW |