| 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 # pylint: disable=W0702 | 10 # pylint: disable=W0702 |
| 11 | 11 |
| 12 import BaseHTTPServer | 12 import BaseHTTPServer |
| 13 import json | 13 import json |
| 14 import logging | 14 import logging |
| 15 import os | 15 import os |
| 16 import select | 16 import select |
| 17 import struct | 17 import struct |
| 18 import subprocess | 18 import subprocess |
| 19 import sys | 19 import sys |
| 20 import threading | 20 import threading |
| 21 import time | 21 import time |
| 22 import urlparse | 22 import urlparse |
| 23 | 23 |
| 24 from devil.android import forwarder | 24 from devil.android import forwarder |
| 25 from devil.android import ports | 25 from devil.android import ports |
| 26 | 26 |
| 27 from pylib import constants | 27 from pylib import constants |
| 28 from pylib.constants import host_paths |
| 28 | 29 |
| 29 | 30 |
| 30 # Path that are needed to import necessary modules when launching a testserver. | 31 # Path that are needed to import necessary modules when launching a testserver. |
| 31 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' | 32 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s:%s:%s' |
| 32 % (os.path.join(constants.DIR_SOURCE_ROOT, 'third_party'), | 33 % (os.path.join(host_paths.DIR_SOURCE_ROOT, 'third_party'), |
| 33 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), | 34 os.path.join(host_paths.DIR_SOURCE_ROOT, 'third_party', 'tlslite'), |
| 34 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', | 35 os.path.join(host_paths.DIR_SOURCE_ROOT, 'third_party', 'pyftpdlib', |
| 35 'src'), | 36 'src'), |
| 36 os.path.join(constants.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), | 37 os.path.join(host_paths.DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'), |
| 37 os.path.join(constants.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) | 38 os.path.join(host_paths.DIR_SOURCE_ROOT, 'sync', 'tools', 'testserver'))) |
| 38 | 39 |
| 39 | 40 |
| 40 SERVER_TYPES = { | 41 SERVER_TYPES = { |
| 41 'http': '', | 42 'http': '', |
| 42 'ftp': '-f', | 43 'ftp': '-f', |
| 43 'sync': '', # Sync uses its own script, and doesn't take a server type arg. | 44 'sync': '', # Sync uses its own script, and doesn't take a server type arg. |
| 44 'tcpecho': '--tcp-echo', | 45 'tcpecho': '--tcp-echo', |
| 45 'udpecho': '--udp-echo', | 46 'udpecho': '--udp-echo', |
| 46 } | 47 } |
| 47 | 48 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if fd != self.pipe_out: | 210 if fd != self.pipe_out: |
| 210 try: | 211 try: |
| 211 os.close(fd) | 212 os.close(fd) |
| 212 except: | 213 except: |
| 213 pass | 214 pass |
| 214 | 215 |
| 215 def run(self): | 216 def run(self): |
| 216 logging.info('Start running the thread!') | 217 logging.info('Start running the thread!') |
| 217 self.wait_event.clear() | 218 self.wait_event.clear() |
| 218 self._GenerateCommandLineArguments() | 219 self._GenerateCommandLineArguments() |
| 219 command = constants.DIR_SOURCE_ROOT | 220 command = host_paths.DIR_SOURCE_ROOT |
| 220 if self.arguments['server-type'] == 'sync': | 221 if self.arguments['server-type'] == 'sync': |
| 221 command = [os.path.join(command, 'sync', 'tools', 'testserver', | 222 command = [os.path.join(command, 'sync', 'tools', 'testserver', |
| 222 'sync_testserver.py')] + self.command_line | 223 'sync_testserver.py')] + self.command_line |
| 223 else: | 224 else: |
| 224 command = [os.path.join(command, 'net', 'tools', 'testserver', | 225 command = [os.path.join(command, 'net', 'tools', 'testserver', |
| 225 'testserver.py')] + self.command_line | 226 'testserver.py')] + self.command_line |
| 226 logging.info('Running: %s', command) | 227 logging.info('Running: %s', command) |
| 227 | 228 |
| 228 # Disable PYTHONUNBUFFERED because it has a bad interaction with the | 229 # Disable PYTHONUNBUFFERED because it has a bad interaction with the |
| 229 # testserver. Remove once this interaction is fixed. | 230 # testserver. Remove once this interaction is fixed. |
| 230 unbuf = os.environ.pop('PYTHONUNBUFFERED', None) | 231 unbuf = os.environ.pop('PYTHONUNBUFFERED', None) |
| 231 | 232 |
| 232 # Pass DIR_SOURCE_ROOT as the child's working directory so that relative | 233 # Pass DIR_SOURCE_ROOT as the child's working directory so that relative |
| 233 # paths in the arguments are resolved correctly. | 234 # paths in the arguments are resolved correctly. |
| 234 self.process = subprocess.Popen( | 235 self.process = subprocess.Popen( |
| 235 command, preexec_fn=self._CloseUnnecessaryFDsForTestServerProcess, | 236 command, preexec_fn=self._CloseUnnecessaryFDsForTestServerProcess, |
| 236 cwd=constants.DIR_SOURCE_ROOT) | 237 cwd=host_paths.DIR_SOURCE_ROOT) |
| 237 if unbuf: | 238 if unbuf: |
| 238 os.environ['PYTHONUNBUFFERED'] = unbuf | 239 os.environ['PYTHONUNBUFFERED'] = unbuf |
| 239 if self.process: | 240 if self.process: |
| 240 if self.pipe_out: | 241 if self.pipe_out: |
| 241 self.is_ready = self._WaitToStartAndGetPortFromTestServer() | 242 self.is_ready = self._WaitToStartAndGetPortFromTestServer() |
| 242 else: | 243 else: |
| 243 self.is_ready = _CheckPortNotAvailable(self.host_port) | 244 self.is_ready = _CheckPortNotAvailable(self.host_port) |
| 244 if self.is_ready: | 245 if self.is_ready: |
| 245 forwarder.Forwarder.Map([(0, self.host_port)], self.device, self.tool) | 246 forwarder.Forwarder.Map([(0, self.host_port)], self.device, self.tool) |
| 246 # Check whether the forwarder is ready on the device. | 247 # Check whether the forwarder is ready on the device. |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 421 |
| 421 def CleanupState(self): | 422 def CleanupState(self): |
| 422 """Cleans up the spawning server state. | 423 """Cleans up the spawning server state. |
| 423 | 424 |
| 424 This should be called if the test server spawner is reused, | 425 This should be called if the test server spawner is reused, |
| 425 to avoid sharing the test server instance. | 426 to avoid sharing the test server instance. |
| 426 """ | 427 """ |
| 427 if self.server.test_server_instance: | 428 if self.server.test_server_instance: |
| 428 self.server.test_server_instance.Stop() | 429 self.server.test_server_instance.Stop() |
| 429 self.server.test_server_instance = None | 430 self.server.test_server_instance = None |
| OLD | NEW |