OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """A "Test Server Spawner" that handles killing/stopping per-test test servers. | 5 """A "Test Server Spawner" that handles killing/stopping per-test test servers. |
6 | 6 |
7 It's used to accept requests from the device to spawn and kill instances of the | 7 It's used to accept requests from the device to spawn and kill instances of the |
8 chrome test server on the host. | 8 chrome test server on the host. |
9 """ | 9 """ |
10 | 10 |
11 import BaseHTTPServer | 11 import BaseHTTPServer |
12 import json | 12 import json |
13 import logging | 13 import logging |
14 import os | 14 import os |
15 import select | 15 import select |
16 import struct | 16 import struct |
17 import subprocess | 17 import subprocess |
18 import sys | 18 import sys |
19 import threading | 19 import threading |
20 import time | 20 import time |
21 import urlparse | 21 import urlparse |
22 | 22 |
23 import constants | 23 import constants |
24 import ports | 24 import ports |
25 | 25 |
| 26 from pylib.forwarder import Forwarder |
26 | 27 |
27 # Path that are needed to import necessary modules when launching a testserver. | 28 # Path that are needed to import necessary modules when launching a testserver. |
28 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' | 29 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' |
29 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), | 30 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), |
30 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), | 31 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), |
31 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', | 32 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', |
32 'src'), | 33 'src'), |
33 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), | 34 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), |
34 os.path.join(constants.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) | 35 os.path.join(constants.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) |
35 | 36 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 raise NotImplementedError('Unknown server type: %s' % server_type) | 93 raise NotImplementedError('Unknown server type: %s' % server_type) |
93 if server_type == 'udpecho': | 94 if server_type == 'udpecho': |
94 raise Exception('Please do not run UDP echo tests because we do not have ' | 95 raise Exception('Please do not run UDP echo tests because we do not have ' |
95 'a UDP forwarder tool.') | 96 'a UDP forwarder tool.') |
96 return SERVER_TYPES[server_type] | 97 return SERVER_TYPES[server_type] |
97 | 98 |
98 | 99 |
99 class TestServerThread(threading.Thread): | 100 class TestServerThread(threading.Thread): |
100 """A thread to run the test server in a separate process.""" | 101 """A thread to run the test server in a separate process.""" |
101 | 102 |
102 def __init__(self, ready_event, arguments, adb, tool, forwarder, build_type): | 103 def __init__(self, ready_event, arguments, adb, tool, build_type): |
103 """Initialize TestServerThread with the following argument. | 104 """Initialize TestServerThread with the following argument. |
104 | 105 |
105 Args: | 106 Args: |
106 ready_event: event which will be set when the test server is ready. | 107 ready_event: event which will be set when the test server is ready. |
107 arguments: dictionary of arguments to run the test server. | 108 arguments: dictionary of arguments to run the test server. |
108 adb: instance of AndroidCommands. | 109 adb: instance of AndroidCommands. |
109 tool: instance of runtime error detection tool. | 110 tool: instance of runtime error detection tool. |
110 forwarder: instance of Forwarder. | |
111 build_type: 'Release' or 'Debug'. | 111 build_type: 'Release' or 'Debug'. |
112 """ | 112 """ |
113 threading.Thread.__init__(self) | 113 threading.Thread.__init__(self) |
114 self.wait_event = threading.Event() | 114 self.wait_event = threading.Event() |
115 self.stop_flag = False | 115 self.stop_flag = False |
116 self.ready_event = ready_event | 116 self.ready_event = ready_event |
117 self.ready_event.clear() | 117 self.ready_event.clear() |
118 self.arguments = arguments | 118 self.arguments = arguments |
119 self.adb = adb | 119 self.adb = adb |
120 self.tool = tool | 120 self.tool = tool |
121 self.test_server_process = None | 121 self.test_server_process = None |
122 self.is_ready = False | 122 self.is_ready = False |
123 self.host_port = self.arguments['port'] | 123 self.host_port = self.arguments['port'] |
124 assert isinstance(self.host_port, int) | 124 assert isinstance(self.host_port, int) |
125 self._test_server_forwarder = forwarder | |
126 # The forwarder device port now is dynamically allocated. | 125 # The forwarder device port now is dynamically allocated. |
127 self.forwarder_device_port = 0 | 126 self.forwarder_device_port = 0 |
128 # Anonymous pipe in order to get port info from test server. | 127 # Anonymous pipe in order to get port info from test server. |
129 self.pipe_in = None | 128 self.pipe_in = None |
130 self.pipe_out = None | 129 self.pipe_out = None |
131 self.command_line = [] | 130 self.command_line = [] |
132 self.build_type = build_type | 131 self.build_type = build_type |
133 | 132 |
134 def _WaitToStartAndGetPortFromTestServer(self): | 133 def _WaitToStartAndGetPortFromTestServer(self): |
135 """Waits for the Python test server to start and gets the port it is using. | 134 """Waits for the Python test server to start and gets the port it is using. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 command = [os.path.join(command, 'net', 'tools', 'testserver', | 232 command = [os.path.join(command, 'net', 'tools', 'testserver', |
234 'testserver.py')] + self.command_line | 233 'testserver.py')] + self.command_line |
235 logging.info('Running: %s', command) | 234 logging.info('Running: %s', command) |
236 self.process = subprocess.Popen(command) | 235 self.process = subprocess.Popen(command) |
237 if self.process: | 236 if self.process: |
238 if self.pipe_out: | 237 if self.pipe_out: |
239 self.is_ready = self._WaitToStartAndGetPortFromTestServer() | 238 self.is_ready = self._WaitToStartAndGetPortFromTestServer() |
240 else: | 239 else: |
241 self.is_ready = _CheckPortStatus(self.host_port, True) | 240 self.is_ready = _CheckPortStatus(self.host_port, True) |
242 if self.is_ready: | 241 if self.is_ready: |
243 self._test_server_forwarder.Run([(0, self.host_port)], self.tool) | 242 Forwarder.Map([(0, self.host_port)], self.adb, self.build_type, self.tool) |
244 # Check whether the forwarder is ready on the device. | 243 # Check whether the forwarder is ready on the device. |
245 self.is_ready = False | 244 self.is_ready = False |
246 device_port = self._test_server_forwarder.DevicePortForHostPort( | 245 device_port = Forwarder.DevicePortForHostPort(self.host_port) |
247 self.host_port) | |
248 if device_port and _CheckDevicePortStatus(self.adb, device_port): | 246 if device_port and _CheckDevicePortStatus(self.adb, device_port): |
249 self.is_ready = True | 247 self.is_ready = True |
250 self.forwarder_device_port = device_port | 248 self.forwarder_device_port = device_port |
251 # Wake up the request handler thread. | 249 # Wake up the request handler thread. |
252 self.ready_event.set() | 250 self.ready_event.set() |
253 # Keep thread running until Stop() gets called. | 251 # Keep thread running until Stop() gets called. |
254 _WaitUntil(lambda: self.stop_flag, max_attempts=sys.maxint) | 252 _WaitUntil(lambda: self.stop_flag, max_attempts=sys.maxint) |
255 if self.process.poll() is None: | 253 if self.process.poll() is None: |
256 self.process.kill() | 254 self.process.kill() |
257 self._test_server_forwarder.UnmapDevicePort(self.forwarder_device_port) | 255 Forwarder.UnmapDevicePort(self.forwarder_device_port, self.adb) |
258 self.process = None | 256 self.process = None |
259 self.is_ready = False | 257 self.is_ready = False |
260 if self.pipe_out: | 258 if self.pipe_out: |
261 os.close(self.pipe_in) | 259 os.close(self.pipe_in) |
262 os.close(self.pipe_out) | 260 os.close(self.pipe_out) |
263 self.pipe_in = None | 261 self.pipe_in = None |
264 self.pipe_out = None | 262 self.pipe_out = None |
265 logging.info('Test-server has died.') | 263 logging.info('Test-server has died.') |
266 self.wait_event.set() | 264 self.wait_event.set() |
267 | 265 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 logging.info(content_length) | 315 logging.info(content_length) |
318 test_server_argument_json = self.rfile.read(content_length) | 316 test_server_argument_json = self.rfile.read(content_length) |
319 logging.info(test_server_argument_json) | 317 logging.info(test_server_argument_json) |
320 assert not self.server.test_server_instance | 318 assert not self.server.test_server_instance |
321 ready_event = threading.Event() | 319 ready_event = threading.Event() |
322 self.server.test_server_instance = TestServerThread( | 320 self.server.test_server_instance = TestServerThread( |
323 ready_event, | 321 ready_event, |
324 json.loads(test_server_argument_json), | 322 json.loads(test_server_argument_json), |
325 self.server.adb, | 323 self.server.adb, |
326 self.server.tool, | 324 self.server.tool, |
327 self.server.forwarder, | |
328 self.server.build_type) | 325 self.server.build_type) |
329 self.server.test_server_instance.setDaemon(True) | 326 self.server.test_server_instance.setDaemon(True) |
330 self.server.test_server_instance.start() | 327 self.server.test_server_instance.start() |
331 ready_event.wait() | 328 ready_event.wait() |
332 if self.server.test_server_instance.is_ready: | 329 if self.server.test_server_instance.is_ready: |
333 self._SendResponse(200, 'OK', {}, json.dumps( | 330 self._SendResponse(200, 'OK', {}, json.dumps( |
334 {'port': self.server.test_server_instance.forwarder_device_port, | 331 {'port': self.server.test_server_instance.forwarder_device_port, |
335 'message': 'started'})) | 332 'message': 'started'})) |
336 logging.info('Test server is running on port: %d.', | 333 logging.info('Test server is running on port: %d.', |
337 self.server.test_server_instance.host_port) | 334 self.server.test_server_instance.host_port) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 self._SendResponse(200, 'OK', {}, 'ready') | 382 self._SendResponse(200, 'OK', {}, 'ready') |
386 logging.info('Handled ping request and sent response.') | 383 logging.info('Handled ping request and sent response.') |
387 else: | 384 else: |
388 self._SendResponse(400, 'Unknown request', {}, '') | 385 self._SendResponse(400, 'Unknown request', {}, '') |
389 logging.info('Encounter unknown request: %s.', action) | 386 logging.info('Encounter unknown request: %s.', action) |
390 | 387 |
391 | 388 |
392 class SpawningServer(object): | 389 class SpawningServer(object): |
393 """The class used to start/stop a http server.""" | 390 """The class used to start/stop a http server.""" |
394 | 391 |
395 def __init__(self, test_server_spawner_port, adb, tool, forwarder, | 392 def __init__(self, test_server_spawner_port, adb, tool, build_type): |
396 build_type): | |
397 logging.info('Creating new spawner on port: %d.', test_server_spawner_port) | 393 logging.info('Creating new spawner on port: %d.', test_server_spawner_port) |
398 self.server = BaseHTTPServer.HTTPServer(('', test_server_spawner_port), | 394 self.server = BaseHTTPServer.HTTPServer(('', test_server_spawner_port), |
399 SpawningServerRequestHandler) | 395 SpawningServerRequestHandler) |
400 self.server.adb = adb | 396 self.server.adb = adb |
401 self.server.tool = tool | 397 self.server.tool = tool |
402 self.server.forwarder = forwarder | |
403 self.server.test_server_instance = None | 398 self.server.test_server_instance = None |
404 self.server.build_type = build_type | 399 self.server.build_type = build_type |
405 | 400 |
406 def _Listen(self): | 401 def _Listen(self): |
407 logging.info('Starting test server spawner') | 402 logging.info('Starting test server spawner') |
408 self.server.serve_forever() | 403 self.server.serve_forever() |
409 | 404 |
410 def Start(self): | 405 def Start(self): |
411 """Starts the test server spawner.""" | 406 """Starts the test server spawner.""" |
412 listener_thread = threading.Thread(target=self._Listen) | 407 listener_thread = threading.Thread(target=self._Listen) |
(...skipping 10 matching lines...) Expand all Loading... |
423 | 418 |
424 def CleanupState(self): | 419 def CleanupState(self): |
425 """Cleans up the spawning server state. | 420 """Cleans up the spawning server state. |
426 | 421 |
427 This should be called if the test server spawner is reused, | 422 This should be called if the test server spawner is reused, |
428 to avoid sharing the test server instance. | 423 to avoid sharing the test server instance. |
429 """ | 424 """ |
430 if self.server.test_server_instance: | 425 if self.server.test_server_instance: |
431 self.server.test_server_instance.Stop() | 426 self.server.test_server_instance.Stop() |
432 self.server.test_server_instance = None | 427 self.server.test_server_instance = None |
OLD | NEW |