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 26 matching lines...) Expand all Loading... |
37 device: Tests will run on the device of this ID. | 37 device: Tests will run on the device of this ID. |
38 tool: Name of the Valgrind tool. | 38 tool: Name of the Valgrind tool. |
39 build_type: 'Release' or 'Debug'. | 39 build_type: 'Release' or 'Debug'. |
40 push_deps: If True, push all dependencies to the device. | 40 push_deps: If True, push all dependencies to the device. |
41 cleanup_test_files: Whether or not to cleanup test files on device. | 41 cleanup_test_files: Whether or not to cleanup test files on device. |
42 """ | 42 """ |
43 self.device = device | 43 self.device = device |
44 self.adb = android_commands.AndroidCommands(device=device) | 44 self.adb = android_commands.AndroidCommands(device=device) |
45 self.tool = CreateTool(tool, self.adb) | 45 self.tool = CreateTool(tool, self.adb) |
46 self._http_server = None | 46 self._http_server = None |
| 47 self._forwarder = None |
47 self._forwarder_device_port = 8000 | 48 self._forwarder_device_port = 8000 |
48 self.forwarder_base_url = ('http://localhost:%d' % | 49 self.forwarder_base_url = ('http://localhost:%d' % |
49 self._forwarder_device_port) | 50 self._forwarder_device_port) |
50 self.flags = FlagChanger(self.adb) | 51 self.flags = FlagChanger(self.adb) |
51 self.flags.AddFlags(['--disable-fre']) | 52 self.flags.AddFlags(['--disable-fre']) |
52 self._spawning_server = None | 53 self._spawning_server = None |
| 54 self._spawner_forwarder = None |
53 # We will allocate port for test server spawner when calling method | 55 # We will allocate port for test server spawner when calling method |
54 # LaunchChromeTestServerSpawner and allocate port for test server when | 56 # LaunchChromeTestServerSpawner and allocate port for test server when |
55 # starting it in TestServerThread. | 57 # starting it in TestServerThread. |
56 self.test_server_spawner_port = 0 | 58 self.test_server_spawner_port = 0 |
57 self.test_server_port = 0 | 59 self.test_server_port = 0 |
58 self.build_type = build_type | 60 self.build_type = build_type |
59 self._push_deps = push_deps | 61 self._push_deps = push_deps |
60 self._cleanup_test_files = cleanup_test_files | 62 self._cleanup_test_files = cleanup_test_files |
61 | 63 |
62 def _PushTestServerPortInfoToDevice(self): | 64 def _PushTestServerPortInfoToDevice(self): |
(...skipping 18 matching lines...) Expand all Loading... |
81 def InstallTestPackage(self): | 83 def InstallTestPackage(self): |
82 """Installs the test package once before all tests are run.""" | 84 """Installs the test package once before all tests are run.""" |
83 pass | 85 pass |
84 | 86 |
85 def PushDataDeps(self): | 87 def PushDataDeps(self): |
86 """Push all data deps to device once before all tests are run.""" | 88 """Push all data deps to device once before all tests are run.""" |
87 pass | 89 pass |
88 | 90 |
89 def SetUp(self): | 91 def SetUp(self): |
90 """Run once before all tests are run.""" | 92 """Run once before all tests are run.""" |
| 93 Forwarder.KillDevice(self.adb, self.tool) |
91 self.InstallTestPackage() | 94 self.InstallTestPackage() |
92 push_size_before = self.adb.GetPushSizeInfo() | 95 push_size_before = self.adb.GetPushSizeInfo() |
93 if self._push_deps: | 96 if self._push_deps: |
94 logging.warning('Pushing data files to device.') | 97 logging.warning('Pushing data files to device.') |
95 self.PushDataDeps() | 98 self.PushDataDeps() |
96 push_size_after = self.adb.GetPushSizeInfo() | 99 push_size_after = self.adb.GetPushSizeInfo() |
97 logging.warning( | 100 logging.warning( |
98 'Total data: %0.3fMB' % | 101 'Total data: %0.3fMB' % |
99 ((push_size_after[0] - push_size_before[0]) / float(2 ** 20))) | 102 ((push_size_after[0] - push_size_before[0]) / float(2 ** 20))) |
100 logging.warning( | 103 logging.warning( |
(...skipping 17 matching lines...) Expand all Loading... |
118 port: port on which we want to the http server bind. | 121 port: port on which we want to the http server bind. |
119 extra_config_contents: Extra config contents for the HTTP server. | 122 extra_config_contents: Extra config contents for the HTTP server. |
120 """ | 123 """ |
121 self._http_server = lighttpd_server.LighttpdServer( | 124 self._http_server = lighttpd_server.LighttpdServer( |
122 document_root, port=port, extra_config_contents=extra_config_contents) | 125 document_root, port=port, extra_config_contents=extra_config_contents) |
123 if self._http_server.StartupHttpServer(): | 126 if self._http_server.StartupHttpServer(): |
124 logging.info('http server started: http://localhost:%s', | 127 logging.info('http server started: http://localhost:%s', |
125 self._http_server.port) | 128 self._http_server.port) |
126 else: | 129 else: |
127 logging.critical('Failed to start http server') | 130 logging.critical('Failed to start http server') |
128 self._ForwardPortsForHttpServer() | 131 self.StartForwarderForHttpServer() |
129 return (self._forwarder_device_port, self._http_server.port) | 132 return (self._forwarder_device_port, self._http_server.port) |
130 | 133 |
131 def _ForwardPorts(self, port_pairs): | 134 def _ForwardPort(self, port_pairs): |
132 """Forwards a port.""" | 135 """Creates a forwarder instance if needed and forward a port.""" |
133 Forwarder.Map(port_pairs, self.adb, self.build_type, self.tool) | 136 if not self._forwarder: |
| 137 self._forwarder = Forwarder(self.adb, self.build_type) |
| 138 self._forwarder.Run(port_pairs, self.tool) |
134 | 139 |
135 def _UnmapPorts(self, port_pairs): | |
136 """Unmap previously forwarded ports.""" | |
137 for (device_port, _) in port_pairs: | |
138 Forwarder.UnmapDevicePort(device_port, self.adb) | |
139 | |
140 # Deprecated: Use ForwardPorts instead. | |
141 def StartForwarder(self, port_pairs): | 140 def StartForwarder(self, port_pairs): |
142 """Starts TCP traffic forwarding for the given |port_pairs|. | 141 """Starts TCP traffic forwarding for the given |port_pairs|. |
143 | 142 |
144 Args: | 143 Args: |
145 host_port_pairs: A list of (device_port, local_port) tuples to forward. | 144 host_port_pairs: A list of (device_port, local_port) tuples to forward. |
146 """ | 145 """ |
147 self.ForwardPorts(port_pairs) | 146 self._ForwardPort(port_pairs) |
148 | 147 |
149 def _ForwardPortsForHttpServer(self): | 148 def StartForwarderForHttpServer(self): |
150 """Starts a forwarder for the HTTP server. | 149 """Starts a forwarder for the HTTP server. |
151 | 150 |
152 The forwarder forwards HTTP requests and responses between host and device. | 151 The forwarder forwards HTTP requests and responses between host and device. |
153 """ | 152 """ |
154 self._ForwardPorts([(self._forwarder_device_port, self._http_server.port)]) | 153 self._ForwardPort([(self._forwarder_device_port, self._http_server.port)]) |
| 154 |
| 155 def RestartHttpServerForwarderIfNecessary(self): |
| 156 """Restarts the forwarder if it's not open.""" |
| 157 # Checks to see if the http server port is being used. If not forwards the |
| 158 # request. |
| 159 # TODO(dtrainor): This is not always reliable because sometimes the port |
| 160 # will be left open even after the forwarder has been killed. |
| 161 if not ports.IsDevicePortUsed(self.adb, |
| 162 self._forwarder_device_port): |
| 163 self.StartForwarderForHttpServer() |
155 | 164 |
156 def ShutdownHelperToolsForTestSuite(self): | 165 def ShutdownHelperToolsForTestSuite(self): |
157 """Shuts down the server and the forwarder.""" | 166 """Shuts down the server and the forwarder.""" |
| 167 # Forwarders should be killed before the actual servers they're forwarding |
| 168 # to as they are clients potentially with open connections and to allow for |
| 169 # proper hand-shake/shutdown. |
| 170 Forwarder.KillDevice(self.adb, self.tool) |
| 171 if self._forwarder: |
| 172 self._forwarder.Close() |
158 if self._http_server: | 173 if self._http_server: |
159 self._UnmapPorts([(self._forwarder_device_port, self._http_server.port)]) | |
160 self._http_server.ShutdownHttpServer() | 174 self._http_server.ShutdownHttpServer() |
161 if self._spawning_server: | 175 if self._spawning_server: |
162 self._spawning_server.Stop() | 176 self._spawning_server.Stop() |
163 self.flags.Restore() | 177 self.flags.Restore() |
164 | 178 |
165 def CleanupSpawningServerState(self): | 179 def CleanupSpawningServerState(self): |
166 """Tells the spawning server to clean up any state. | 180 """Tells the spawning server to clean up any state. |
167 | 181 |
168 If the spawning server is reused for multiple tests, this should be called | 182 If the spawning server is reused for multiple tests, this should be called |
169 after each test to prevent tests affecting each other. | 183 after each test to prevent tests affecting each other. |
170 """ | 184 """ |
171 if self._spawning_server: | 185 if self._spawning_server: |
172 self._spawning_server.CleanupState() | 186 self._spawning_server.CleanupState() |
173 | 187 |
174 def LaunchChromeTestServerSpawner(self): | 188 def LaunchChromeTestServerSpawner(self): |
175 """Launches test server spawner.""" | 189 """Launches test server spawner.""" |
176 server_ready = False | 190 server_ready = False |
177 error_msgs = [] | 191 error_msgs = [] |
178 # TODO(pliard): deflake this function. The for loop should be removed as | 192 # TODO(pliard): deflake this function. The for loop should be removed as |
179 # well as IsHttpServerConnectable(). spawning_server.Start() should also | 193 # well as IsHttpServerConnectable(). spawning_server.Start() should also |
180 # block until the server is ready. | 194 # block until the server is ready. |
181 # Try 3 times to launch test spawner server. | 195 # Try 3 times to launch test spawner server. |
182 for i in xrange(0, 3): | 196 for i in xrange(0, 3): |
183 self.test_server_spawner_port = ports.AllocateTestServerPort() | 197 self.test_server_spawner_port = ports.AllocateTestServerPort() |
184 self._ForwardPorts( | 198 self._ForwardPort( |
185 [(self.test_server_spawner_port, self.test_server_spawner_port)]) | 199 [(self.test_server_spawner_port, self.test_server_spawner_port)]) |
186 self._spawning_server = SpawningServer(self.test_server_spawner_port, | 200 self._spawning_server = SpawningServer(self.test_server_spawner_port, |
187 self.adb, | 201 self.adb, |
188 self.tool, | 202 self.tool, |
| 203 self._forwarder, |
189 self.build_type) | 204 self.build_type) |
190 self._spawning_server.Start() | 205 self._spawning_server.Start() |
191 server_ready, error_msg = ports.IsHttpServerConnectable( | 206 server_ready, error_msg = ports.IsHttpServerConnectable( |
192 '127.0.0.1', self.test_server_spawner_port, path='/ping', | 207 '127.0.0.1', self.test_server_spawner_port, path='/ping', |
193 expected_read='ready') | 208 expected_read='ready') |
194 if server_ready: | 209 if server_ready: |
195 break | 210 break |
196 else: | 211 else: |
197 error_msgs.append(error_msg) | 212 error_msgs.append(error_msg) |
198 self._spawning_server.Stop() | 213 self._spawning_server.Stop() |
199 # Wait for 2 seconds then restart. | 214 # Wait for 2 seconds then restart. |
200 time.sleep(2) | 215 time.sleep(2) |
201 if not server_ready: | 216 if not server_ready: |
202 logging.error(';'.join(error_msgs)) | 217 logging.error(';'.join(error_msgs)) |
203 raise Exception('Can not start the test spawner server.') | 218 raise Exception('Can not start the test spawner server.') |
204 self._PushTestServerPortInfoToDevice() | 219 self._PushTestServerPortInfoToDevice() |
OLD | NEW |