| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Base class for running tests on a single device.""" | |
| 6 | |
| 7 # TODO(jbudorick) Deprecate and remove this class and all subclasses after | |
| 8 # any relevant parts have been ported to the new environment + test instance | |
| 9 # model. | |
| 10 | |
| 11 import logging | |
| 12 | |
| 13 from devil.android import device_utils | |
| 14 from devil.android import forwarder | |
| 15 from devil.android import ports | |
| 16 from pylib.valgrind_tools import CreateTool | |
| 17 # TODO(frankf): Move this to pylib/utils | |
| 18 | |
| 19 | |
| 20 # A file on device to store ports of net test server. The format of the file is | |
| 21 # test-spawner-server-port:test-server-port | |
| 22 NET_TEST_SERVER_PORT_INFO_FILE = 'net-test-server-ports' | |
| 23 | |
| 24 | |
| 25 class BaseTestRunner(object): | |
| 26 """Base class for running tests on a single device.""" | |
| 27 | |
| 28 def __init__(self, device, tool): | |
| 29 """ | |
| 30 Args: | |
| 31 device: An instance of DeviceUtils that the tests will run on. | |
| 32 tool: Name of the Valgrind tool. | |
| 33 """ | |
| 34 assert isinstance(device, device_utils.DeviceUtils) | |
| 35 self.device = device | |
| 36 self.device_serial = self.device.adb.GetDeviceSerial() | |
| 37 self.tool = CreateTool(tool, self.device) | |
| 38 self._http_server = None | |
| 39 self._forwarder_device_port = 8000 | |
| 40 self.forwarder_base_url = ('http://localhost:%d' % | |
| 41 self._forwarder_device_port) | |
| 42 # We will allocate port for test server spawner when calling method | |
| 43 # LaunchChromeTestServerSpawner and allocate port for test server when | |
| 44 # starting it in TestServerThread. | |
| 45 self.test_server_spawner_port = 0 | |
| 46 self.test_server_port = 0 | |
| 47 | |
| 48 def _PushTestServerPortInfoToDevice(self): | |
| 49 """Pushes the latest port information to device.""" | |
| 50 self.device.WriteFile( | |
| 51 self.device.GetExternalStoragePath() + '/' + | |
| 52 NET_TEST_SERVER_PORT_INFO_FILE, | |
| 53 '%d:%d' % (self.test_server_spawner_port, self.test_server_port)) | |
| 54 | |
| 55 def RunTest(self, test): | |
| 56 """Runs a test. Needs to be overridden. | |
| 57 | |
| 58 Args: | |
| 59 test: A test to run. | |
| 60 | |
| 61 Returns: | |
| 62 Tuple containing: | |
| 63 (base_test_result.TestRunResults, tests to rerun or None) | |
| 64 """ | |
| 65 raise NotImplementedError | |
| 66 | |
| 67 def InstallTestPackage(self): | |
| 68 """Installs the test package once before all tests are run.""" | |
| 69 pass | |
| 70 | |
| 71 def SetUp(self): | |
| 72 """Run once before all tests are run.""" | |
| 73 self.InstallTestPackage() | |
| 74 | |
| 75 def TearDown(self): | |
| 76 """Run once after all tests are run.""" | |
| 77 self.ShutdownHelperToolsForTestSuite() | |
| 78 | |
| 79 def LaunchTestHttpServer(self, document_root, port=None, | |
| 80 extra_config_contents=None): | |
| 81 """Launches an HTTP server to serve HTTP tests. | |
| 82 | |
| 83 Args: | |
| 84 document_root: Document root of the HTTP server. | |
| 85 port: port on which we want to the http server bind. | |
| 86 extra_config_contents: Extra config contents for the HTTP server. | |
| 87 """ | |
| 88 import lighttpd_server | |
| 89 self._http_server = lighttpd_server.LighttpdServer( | |
| 90 document_root, port=port, extra_config_contents=extra_config_contents) | |
| 91 if self._http_server.StartupHttpServer(): | |
| 92 logging.info('http server started: http://localhost:%s', | |
| 93 self._http_server.port) | |
| 94 else: | |
| 95 logging.critical('Failed to start http server') | |
| 96 self._ForwardPortsForHttpServer() | |
| 97 return (self._forwarder_device_port, self._http_server.port) | |
| 98 | |
| 99 def _ForwardPorts(self, port_pairs): | |
| 100 """Forwards a port.""" | |
| 101 forwarder.Forwarder.Map(port_pairs, self.device, self.tool) | |
| 102 | |
| 103 def _UnmapPorts(self, port_pairs): | |
| 104 """Unmap previously forwarded ports.""" | |
| 105 for (device_port, _) in port_pairs: | |
| 106 forwarder.Forwarder.UnmapDevicePort(device_port, self.device) | |
| 107 | |
| 108 # Deprecated: Use ForwardPorts instead. | |
| 109 def StartForwarder(self, port_pairs): | |
| 110 """Starts TCP traffic forwarding for the given |port_pairs|. | |
| 111 | |
| 112 Args: | |
| 113 host_port_pairs: A list of (device_port, local_port) tuples to forward. | |
| 114 """ | |
| 115 self._ForwardPorts(port_pairs) | |
| 116 | |
| 117 def _ForwardPortsForHttpServer(self): | |
| 118 """Starts a forwarder for the HTTP server. | |
| 119 | |
| 120 The forwarder forwards HTTP requests and responses between host and device. | |
| 121 """ | |
| 122 self._ForwardPorts([(self._forwarder_device_port, self._http_server.port)]) | |
| 123 | |
| 124 def _RestartHttpServerForwarderIfNecessary(self): | |
| 125 """Restarts the forwarder if it's not open.""" | |
| 126 # Checks to see if the http server port is being used. If not forwards the | |
| 127 # request. | |
| 128 # TODO(dtrainor): This is not always reliable because sometimes the port | |
| 129 # will be left open even after the forwarder has been killed. | |
| 130 if not ports.IsDevicePortUsed(self.device, self._forwarder_device_port): | |
| 131 self._ForwardPortsForHttpServer() | |
| 132 | |
| 133 def ShutdownHelperToolsForTestSuite(self): | |
| 134 """Shuts down the server and the forwarder.""" | |
| 135 if self._http_server: | |
| 136 self._UnmapPorts([(self._forwarder_device_port, self._http_server.port)]) | |
| 137 self._http_server.ShutdownHttpServer() | |
| 138 | |
| OLD | NEW |