OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Host driven test server controller. |
| 6 |
| 7 This class controls the startup and shutdown of a python driven test server that |
| 8 runs in a separate process. |
| 9 |
| 10 The server starts up automatically when the object is created. |
| 11 |
| 12 After it starts up, it is possible to retreive the hostname it started on |
| 13 through accessing the member field |host| and the port name through |port|. |
| 14 |
| 15 For shutting down the server, call TearDown(). |
| 16 """ |
| 17 |
| 18 import subprocess |
| 19 import os |
| 20 import os.path |
| 21 |
| 22 from pylib import constants |
| 23 |
| 24 # NOTE: when adding or modifying these lines, omit any leading slashes! |
| 25 # Otherwise os.path.join() will (correctly) treat them as absolute paths |
| 26 # instead of relative paths, and will do nothing. |
| 27 _PYTHONPATH_DIRS = [ |
| 28 'net/tools/testserver/', |
| 29 'third_party/', |
| 30 'third_party/pyftpdlib/src/', |
| 31 'third_party/pywebsocket/src', |
| 32 'third_party/tlslite/', |
| 33 ] |
| 34 |
| 35 # Python files in these directories are generated as part of the build. |
| 36 # These dirs are located in out/(Debug|Release) directory. |
| 37 # The correct path is determined based on the build type. E.g. out/Debug for |
| 38 # debug builds and out/Release for release builds. |
| 39 _GENERATED_PYTHONPATH_DIRS = [ |
| 40 'pyproto/sync/protocol/', |
| 41 'pyproto/' |
| 42 ] |
| 43 |
| 44 _TEST_SERVER_HOST = '127.0.0.1' |
| 45 # Paths for supported test server executables. |
| 46 TEST_NET_SERVER_PATH = 'net/tools/testserver/testserver.py' |
| 47 TEST_SYNC_SERVER_PATH = 'sync/tools/testserver/sync_testserver.py' |
| 48 |
| 49 |
| 50 class TestServer(object): |
| 51 """Sets up a host driven test server on the host machine. |
| 52 |
| 53 For shutting down the server, call TearDown(). |
| 54 """ |
| 55 |
| 56 def __init__(self, shard_index, test_server_port, test_server_path): |
| 57 """Sets up a Python driven test server on the host machine. |
| 58 |
| 59 Args: |
| 60 shard_index: Index of the current shard. |
| 61 test_server_port: Port to run the test server on. This is multiplexed with |
| 62 the shard index. To retrieve the real port access the |
| 63 member variable |port|. |
| 64 test_server_path: The path (relative to the root src dir) of the server |
| 65 """ |
| 66 self.host = _TEST_SERVER_HOST |
| 67 self.port = test_server_port + shard_index |
| 68 |
| 69 src_dir = constants.DIR_SOURCE_ROOT |
| 70 # Make dirs into a list of absolute paths. |
| 71 abs_dirs = [os.path.join(src_dir, d) for d in _PYTHONPATH_DIRS] |
| 72 # Add the generated python files to the path |
| 73 abs_dirs.extend([os.path.join(src_dir, 'out', constants.GetBuildType(), d) |
| 74 for d in _GENERATED_PYTHONPATH_DIRS]) |
| 75 current_python_path = os.environ.get('PYTHONPATH') |
| 76 extra_python_path = ':'.join(abs_dirs) |
| 77 if current_python_path: |
| 78 python_path = current_python_path + ':' + extra_python_path |
| 79 else: |
| 80 python_path = extra_python_path |
| 81 |
| 82 # NOTE: A separate python process is used to simplify getting the right |
| 83 # system path for finding includes. |
| 84 cmd = ['python', os.path.join(src_dir, test_server_path), |
| 85 '--log-to-console', |
| 86 ('--host=%s' % self.host), |
| 87 ('--port=%d' % self.port)] |
| 88 self._test_server_process = subprocess.Popen( |
| 89 cmd, env={'PYTHONPATH': python_path}) |
| 90 |
| 91 def TearDown(self): |
| 92 self._test_server_process.kill() |
OLD | NEW |