OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import os |
| 7 import os.path |
| 8 import subprocess |
| 9 |
| 10 from pylib import constants |
| 11 from pylib import flag_changer |
| 12 |
| 13 _HOST = '127.0.0.1' |
| 14 |
| 15 _SYNC_TEST_SERVER_PATH = 'sync/tools/testserver/sync_testserver.py' |
| 16 |
| 17 # NOTE: when adding or modifying these lines, omit any leading slashes! |
| 18 # Otherwise os.path.join() will (correctly) treat them as absolute paths |
| 19 # instead of relative paths, and will do nothing. |
| 20 PYTHONPATH_DIRS = [ |
| 21 'net/tools/testserver/', |
| 22 'third_party/', |
| 23 'third_party/pyftpdlib/src/', |
| 24 'third_party/pywebsocket/src', |
| 25 'third_party/tlslite/', |
| 26 ] |
| 27 |
| 28 # Python files in these directories are generated as part of the build. |
| 29 # These dirs are located in out/(Debug|Release) directory. |
| 30 # The correct path is determined based on the build type. E.g. out/Debug for |
| 31 # debug builds and out/Release for release builds. |
| 32 GENERATED_PYTHONPATH_DIRS = [ |
| 33 'pyproto/sync/protocol/', |
| 34 'pyproto/' |
| 35 ] |
| 36 |
| 37 |
| 38 def setUpTestSyncServer(adb, shard_index, command_line_file): |
| 39 return __setUpTestServer(adb, shard_index, |
| 40 constants.TEST_SYNC_SERVER_PORT, |
| 41 command_line_file, |
| 42 '--sync-url=http://%s:%d/chromiumsync', |
| 43 [], |
| 44 _SYNC_TEST_SERVER_PATH) |
| 45 |
| 46 def setUpTestSearchByImageServer(adb, shard_index): |
| 47 return __setUpTestServer( |
| 48 adb, shard_index, |
| 49 constants.TEST_SEARCH_BY_IMAGE_SERVER_PORT, |
| 50 '--host-resolver-rules="MAP * %s:%d, EXCLUDE localhost"', |
| 51 [], |
| 52 _NET_TEST_SERVER_PATH) |
| 53 |
| 54 def __setUpTestServer(adb, shard_index, test_server_port, command_line_file, |
| 55 chrome_flags_to_append, test_server_flags, |
| 56 test_server_path): |
| 57 """Sets up a server. |
| 58 |
| 59 Args: |
| 60 adb: AndroidCommands object. |
| 61 shard_index: Index of the current shard. |
| 62 test_server_port: port to run the test server on. |
| 63 chrome_flags_to_append: additional flags to post to Chrome. |
| 64 test_server_flags: flags to use when starting the test server. |
| 65 test_server_path: the path (relative to the root Chrome dir) of the server |
| 66 |
| 67 Returns: |
| 68 A tuple where the first element is the command line flags used by the |
| 69 tests, and the second is the mock server that was setup. |
| 70 """ |
| 71 port = test_server_port + shard_index |
| 72 flags = flag_changer.FlagChanger(adb, command_line_file) |
| 73 flags.AddFlags([chrome_flags_to_append % (_HOST, port)]) |
| 74 |
| 75 chrome_dir = os.getcwd() |
| 76 |
| 77 # Make dirs into a list of absolute paths. |
| 78 abs_dirs = [os.path.join(chrome_dir, d) for d in PYTHONPATH_DIRS] |
| 79 # Add the generated python files to the path |
| 80 abs_dirs.extend([os.path.join(chrome_dir, 'out', constants.GetBuildType(), d) |
| 81 for d in GENERATED_PYTHONPATH_DIRS]) |
| 82 # Note the colon after $PYTHONPATH. This appends the list of PYTHONPATH_DIRS |
| 83 # to the existing PYTHONPATH. |
| 84 python_path = '$PYTHONPATH:' + ':'.join(abs_dirs) |
| 85 |
| 86 # NOTE: A separate python process is used to simplify getting the right system |
| 87 # path for finding includes. |
| 88 server_flags = ['python', os.path.join(chrome_dir, test_server_path), |
| 89 '--log-to-console', ('--host=%s' % _HOST), |
| 90 ('--port=%d' % port)] |
| 91 server_flags.extend(test_server_flags) |
| 92 test_server = subprocess.Popen(server_flags, env={'PYTHONPATH': python_path}) |
| 93 return (port, flags, test_server) |
| 94 |
| 95 |
| 96 def tearDown(setup_params): |
| 97 flags, test_server = setup_params |
| 98 flags.Restore() |
| 99 test_server.kill() |
OLD | NEW |