OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 """Base class for running tests on a single device.""" | 5 """Base class for running tests on a single device.""" |
6 | 6 |
7 import contextlib | 7 import contextlib |
8 import httplib | 8 import httplib |
9 import logging | 9 import logging |
10 import os | 10 import os |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 | 24 |
25 # A file on device to store ports of net test server. The format of the file is | 25 # A file on device to store ports of net test server. The format of the file is |
26 # test-spawner-server-port:test-server-port | 26 # test-spawner-server-port:test-server-port |
27 NET_TEST_SERVER_PORT_INFO_FILE = 'net-test-server-ports' | 27 NET_TEST_SERVER_PORT_INFO_FILE = 'net-test-server-ports' |
28 | 28 |
29 | 29 |
30 class BaseTestRunner(object): | 30 class BaseTestRunner(object): |
31 """Base class for running tests on a single device.""" | 31 """Base class for running tests on a single device.""" |
32 | 32 |
33 def __init__(self, device, tool, build_type, push_deps=True, | 33 def __init__(self, device, tool, push_deps=True, cleanup_test_files=False): |
34 cleanup_test_files=False): | |
35 """ | 34 """ |
36 Args: | 35 Args: |
37 device: Tests will run on the device of this ID. | 36 device: Tests will run on the device of this ID. |
38 tool: Name of the Valgrind tool. | 37 tool: Name of the Valgrind tool. |
39 build_type: 'Release' or 'Debug'. | |
40 push_deps: If True, push all dependencies to the device. | 38 push_deps: If True, push all dependencies to the device. |
41 cleanup_test_files: Whether or not to cleanup test files on device. | 39 cleanup_test_files: Whether or not to cleanup test files on device. |
42 """ | 40 """ |
43 self.device = device | 41 self.device = device |
44 self.adb = android_commands.AndroidCommands(device=device) | 42 self.adb = android_commands.AndroidCommands(device=device) |
45 self.tool = CreateTool(tool, self.adb) | 43 self.tool = CreateTool(tool, self.adb) |
46 self._http_server = None | 44 self._http_server = None |
47 self._forwarder_device_port = 8000 | 45 self._forwarder_device_port = 8000 |
48 self.forwarder_base_url = ('http://localhost:%d' % | 46 self.forwarder_base_url = ('http://localhost:%d' % |
49 self._forwarder_device_port) | 47 self._forwarder_device_port) |
50 self.flags = FlagChanger(self.adb) | 48 self.flags = FlagChanger(self.adb) |
51 self.flags.AddFlags(['--disable-fre']) | 49 self.flags.AddFlags(['--disable-fre']) |
52 self._spawning_server = None | 50 self._spawning_server = None |
53 # We will allocate port for test server spawner when calling method | 51 # We will allocate port for test server spawner when calling method |
54 # LaunchChromeTestServerSpawner and allocate port for test server when | 52 # LaunchChromeTestServerSpawner and allocate port for test server when |
55 # starting it in TestServerThread. | 53 # starting it in TestServerThread. |
56 self.test_server_spawner_port = 0 | 54 self.test_server_spawner_port = 0 |
57 self.test_server_port = 0 | 55 self.test_server_port = 0 |
58 self.build_type = build_type | |
59 self._push_deps = push_deps | 56 self._push_deps = push_deps |
60 self._cleanup_test_files = cleanup_test_files | 57 self._cleanup_test_files = cleanup_test_files |
61 | 58 |
62 def _PushTestServerPortInfoToDevice(self): | 59 def _PushTestServerPortInfoToDevice(self): |
63 """Pushes the latest port information to device.""" | 60 """Pushes the latest port information to device.""" |
64 self.adb.SetFileContents(self.adb.GetExternalStorage() + '/' + | 61 self.adb.SetFileContents(self.adb.GetExternalStorage() + '/' + |
65 NET_TEST_SERVER_PORT_INFO_FILE, | 62 NET_TEST_SERVER_PORT_INFO_FILE, |
66 '%d:%d' % (self.test_server_spawner_port, | 63 '%d:%d' % (self.test_server_spawner_port, |
67 self.test_server_port)) | 64 self.test_server_port)) |
68 | 65 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 if self._http_server.StartupHttpServer(): | 120 if self._http_server.StartupHttpServer(): |
124 logging.info('http server started: http://localhost:%s', | 121 logging.info('http server started: http://localhost:%s', |
125 self._http_server.port) | 122 self._http_server.port) |
126 else: | 123 else: |
127 logging.critical('Failed to start http server') | 124 logging.critical('Failed to start http server') |
128 self._ForwardPortsForHttpServer() | 125 self._ForwardPortsForHttpServer() |
129 return (self._forwarder_device_port, self._http_server.port) | 126 return (self._forwarder_device_port, self._http_server.port) |
130 | 127 |
131 def _ForwardPorts(self, port_pairs): | 128 def _ForwardPorts(self, port_pairs): |
132 """Forwards a port.""" | 129 """Forwards a port.""" |
133 Forwarder.Map(port_pairs, self.adb, self.build_type, self.tool) | 130 Forwarder.Map(port_pairs, self.adb, constants.GetBuildType(), self.tool) |
134 | 131 |
135 def _UnmapPorts(self, port_pairs): | 132 def _UnmapPorts(self, port_pairs): |
136 """Unmap previously forwarded ports.""" | 133 """Unmap previously forwarded ports.""" |
137 for (device_port, _) in port_pairs: | 134 for (device_port, _) in port_pairs: |
138 Forwarder.UnmapDevicePort(device_port, self.adb) | 135 Forwarder.UnmapDevicePort(device_port, self.adb) |
139 | 136 |
140 # Deprecated: Use ForwardPorts instead. | 137 # Deprecated: Use ForwardPorts instead. |
141 def StartForwarder(self, port_pairs): | 138 def StartForwarder(self, port_pairs): |
142 """Starts TCP traffic forwarding for the given |port_pairs|. | 139 """Starts TCP traffic forwarding for the given |port_pairs|. |
143 | 140 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 # TODO(pliard): deflake this function. The for loop should be removed as | 184 # TODO(pliard): deflake this function. The for loop should be removed as |
188 # well as IsHttpServerConnectable(). spawning_server.Start() should also | 185 # well as IsHttpServerConnectable(). spawning_server.Start() should also |
189 # block until the server is ready. | 186 # block until the server is ready. |
190 # Try 3 times to launch test spawner server. | 187 # Try 3 times to launch test spawner server. |
191 for i in xrange(0, 3): | 188 for i in xrange(0, 3): |
192 self.test_server_spawner_port = ports.AllocateTestServerPort() | 189 self.test_server_spawner_port = ports.AllocateTestServerPort() |
193 self._ForwardPorts( | 190 self._ForwardPorts( |
194 [(self.test_server_spawner_port, self.test_server_spawner_port)]) | 191 [(self.test_server_spawner_port, self.test_server_spawner_port)]) |
195 self._spawning_server = SpawningServer(self.test_server_spawner_port, | 192 self._spawning_server = SpawningServer(self.test_server_spawner_port, |
196 self.adb, | 193 self.adb, |
197 self.tool, | 194 self.tool) |
198 self.build_type) | |
199 self._spawning_server.Start() | 195 self._spawning_server.Start() |
200 server_ready, error_msg = ports.IsHttpServerConnectable( | 196 server_ready, error_msg = ports.IsHttpServerConnectable( |
201 '127.0.0.1', self.test_server_spawner_port, path='/ping', | 197 '127.0.0.1', self.test_server_spawner_port, path='/ping', |
202 expected_read='ready') | 198 expected_read='ready') |
203 if server_ready: | 199 if server_ready: |
204 break | 200 break |
205 else: | 201 else: |
206 error_msgs.append(error_msg) | 202 error_msgs.append(error_msg) |
207 self._spawning_server.Stop() | 203 self._spawning_server.Stop() |
208 # Wait for 2 seconds then restart. | 204 # Wait for 2 seconds then restart. |
209 time.sleep(2) | 205 time.sleep(2) |
210 if not server_ready: | 206 if not server_ready: |
211 logging.error(';'.join(error_msgs)) | 207 logging.error(';'.join(error_msgs)) |
212 raise Exception('Can not start the test spawner server.') | 208 raise Exception('Can not start the test spawner server.') |
213 self._PushTestServerPortInfoToDevice() | 209 self._PushTestServerPortInfoToDevice() |
OLD | NEW |