michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: import contextlib michael@0: import httplib michael@0: import logging michael@0: import os michael@0: import tempfile michael@0: import time michael@0: michael@0: import android_commands michael@0: import constants michael@0: from chrome_test_server_spawner import SpawningServer michael@0: import constants michael@0: from flag_changer import FlagChanger michael@0: from forwarder import Forwarder michael@0: import lighttpd_server michael@0: import ports michael@0: from valgrind_tools import CreateTool michael@0: michael@0: michael@0: # A file on device to store ports of net test server. The format of the file is michael@0: # test-spawner-server-port:test-server-port michael@0: NET_TEST_SERVER_PORT_INFO_FILE = 'net-test-server-ports' michael@0: michael@0: michael@0: class BaseTestRunner(object): michael@0: """Base class for running tests on a single device. michael@0: michael@0: A subclass should implement RunTests() with no parameter, so that calling michael@0: the Run() method will set up tests, run them and tear them down. michael@0: """ michael@0: michael@0: def __init__(self, device, tool, shard_index, build_type): michael@0: """ michael@0: Args: michael@0: device: Tests will run on the device of this ID. michael@0: shard_index: Index number of the shard on which the test suite will run. michael@0: build_type: 'Release' or 'Debug'. michael@0: """ michael@0: self.device = device michael@0: self.adb = android_commands.AndroidCommands(device=device) michael@0: self.tool = CreateTool(tool, self.adb) michael@0: self._http_server = None michael@0: self._forwarder = None michael@0: self._forwarder_device_port = 8000 michael@0: self.forwarder_base_url = ('http://localhost:%d' % michael@0: self._forwarder_device_port) michael@0: self.flags = FlagChanger(self.adb) michael@0: self.shard_index = shard_index michael@0: self.flags.AddFlags(['--disable-fre']) michael@0: self._spawning_server = None michael@0: self._spawner_forwarder = None michael@0: # We will allocate port for test server spawner when calling method michael@0: # LaunchChromeTestServerSpawner and allocate port for test server when michael@0: # starting it in TestServerThread. michael@0: self.test_server_spawner_port = 0 michael@0: self.test_server_port = 0 michael@0: self.build_type = build_type michael@0: michael@0: def _PushTestServerPortInfoToDevice(self): michael@0: """Pushes the latest port information to device.""" michael@0: self.adb.SetFileContents(self.adb.GetExternalStorage() + '/' + michael@0: NET_TEST_SERVER_PORT_INFO_FILE, michael@0: '%d:%d' % (self.test_server_spawner_port, michael@0: self.test_server_port)) michael@0: michael@0: def Run(self): michael@0: """Calls subclass functions to set up tests, run them and tear them down. michael@0: michael@0: Returns: michael@0: Test results returned from RunTests(). michael@0: """ michael@0: if not self.HasTests(): michael@0: return True michael@0: self.SetUp() michael@0: try: michael@0: return self.RunTests() michael@0: finally: michael@0: self.TearDown() michael@0: michael@0: def SetUp(self): michael@0: """Called before tests run.""" michael@0: pass michael@0: michael@0: def HasTests(self): michael@0: """Whether the test suite has tests to run.""" michael@0: return True michael@0: michael@0: def RunTests(self): michael@0: """Runs the tests. Need to be overridden.""" michael@0: raise NotImplementedError michael@0: michael@0: def TearDown(self): michael@0: """Called when tests finish running.""" michael@0: self.ShutdownHelperToolsForTestSuite() michael@0: michael@0: def CopyTestData(self, test_data_paths, dest_dir): michael@0: """Copies |test_data_paths| list of files/directories to |dest_dir|. michael@0: michael@0: Args: michael@0: test_data_paths: A list of files or directories relative to |dest_dir| michael@0: which should be copied to the device. The paths must exist in michael@0: |CHROME_DIR|. michael@0: dest_dir: Absolute path to copy to on the device. michael@0: """ michael@0: for p in test_data_paths: michael@0: self.adb.PushIfNeeded( michael@0: os.path.join(constants.CHROME_DIR, p), michael@0: os.path.join(dest_dir, p)) michael@0: michael@0: def LaunchTestHttpServer(self, document_root, port=None, michael@0: extra_config_contents=None): michael@0: """Launches an HTTP server to serve HTTP tests. michael@0: michael@0: Args: michael@0: document_root: Document root of the HTTP server. michael@0: port: port on which we want to the http server bind. michael@0: extra_config_contents: Extra config contents for the HTTP server. michael@0: """ michael@0: self._http_server = lighttpd_server.LighttpdServer( michael@0: document_root, port=port, extra_config_contents=extra_config_contents) michael@0: if self._http_server.StartupHttpServer(): michael@0: logging.info('http server started: http://localhost:%s', michael@0: self._http_server.port) michael@0: else: michael@0: logging.critical('Failed to start http server') michael@0: self.StartForwarderForHttpServer() michael@0: return (self._forwarder_device_port, self._http_server.port) michael@0: michael@0: def StartForwarder(self, port_pairs): michael@0: """Starts TCP traffic forwarding for the given |port_pairs|. michael@0: michael@0: Args: michael@0: host_port_pairs: A list of (device_port, local_port) tuples to forward. michael@0: """ michael@0: if self._forwarder: michael@0: self._forwarder.Close() michael@0: self._forwarder = Forwarder( michael@0: self.adb, port_pairs, self.tool, '127.0.0.1', self.build_type) michael@0: michael@0: def StartForwarderForHttpServer(self): michael@0: """Starts a forwarder for the HTTP server. michael@0: michael@0: The forwarder forwards HTTP requests and responses between host and device. michael@0: """ michael@0: self.StartForwarder([(self._forwarder_device_port, self._http_server.port)]) michael@0: michael@0: def RestartHttpServerForwarderIfNecessary(self): michael@0: """Restarts the forwarder if it's not open.""" michael@0: # Checks to see if the http server port is being used. If not forwards the michael@0: # request. michael@0: # TODO(dtrainor): This is not always reliable because sometimes the port michael@0: # will be left open even after the forwarder has been killed. michael@0: if not ports.IsDevicePortUsed(self.adb, michael@0: self._forwarder_device_port): michael@0: self.StartForwarderForHttpServer() michael@0: michael@0: def ShutdownHelperToolsForTestSuite(self): michael@0: """Shuts down the server and the forwarder.""" michael@0: # Forwarders should be killed before the actual servers they're forwarding michael@0: # to as they are clients potentially with open connections and to allow for michael@0: # proper hand-shake/shutdown. michael@0: if self._forwarder or self._spawner_forwarder: michael@0: # Kill all forwarders on the device and then kill the process on the host michael@0: # (if it exists) michael@0: self.adb.KillAll('device_forwarder') michael@0: if self._forwarder: michael@0: self._forwarder.Close() michael@0: if self._spawner_forwarder: michael@0: self._spawner_forwarder.Close() michael@0: if self._http_server: michael@0: self._http_server.ShutdownHttpServer() michael@0: if self._spawning_server: michael@0: self._spawning_server.Stop() michael@0: self.flags.Restore() michael@0: michael@0: def LaunchChromeTestServerSpawner(self): michael@0: """Launches test server spawner.""" michael@0: server_ready = False michael@0: error_msgs = [] michael@0: # Try 3 times to launch test spawner server. michael@0: for i in xrange(0, 3): michael@0: # Do not allocate port for test server here. We will allocate michael@0: # different port for individual test in TestServerThread. michael@0: self.test_server_spawner_port = ports.AllocateTestServerPort() michael@0: self._spawning_server = SpawningServer(self.test_server_spawner_port, michael@0: self.adb, michael@0: self.tool, michael@0: self.build_type) michael@0: self._spawning_server.Start() michael@0: server_ready, error_msg = ports.IsHttpServerConnectable( michael@0: '127.0.0.1', self.test_server_spawner_port, path='/ping', michael@0: expected_read='ready') michael@0: if server_ready: michael@0: break michael@0: else: michael@0: error_msgs.append(error_msg) michael@0: self._spawning_server.Stop() michael@0: # Wait for 2 seconds then restart. michael@0: time.sleep(2) michael@0: if not server_ready: michael@0: logging.error(';'.join(error_msgs)) michael@0: raise Exception('Can not start the test spawner server.') michael@0: self._PushTestServerPortInfoToDevice() michael@0: self._spawner_forwarder = Forwarder( michael@0: self.adb, michael@0: [(self.test_server_spawner_port, self.test_server_spawner_port)], michael@0: self.tool, '127.0.0.1', self.build_type)