Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011 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 logging | |
| 7 import os | |
| 8 | |
| 9 import android_commands | |
| 10 from chrome_test_server_spawner import SpawningServer | |
| 11 from flag_changer import FlagChanger | |
| 12 import lighttpd_server | |
| 13 import run_tests_helper | |
| 14 | |
| 15 FORWARDER_PATH = '/data/local/tmp/forwarder' | |
| 16 # These ports must match up with the constants in net/test/test_server.cc | |
| 17 TEST_SERVER_SPAWNER_PORT = 8001 | |
| 18 TEST_SERVER_PORT = 8002 | |
| 19 TEST_SYNC_SERVER_PORT = 8003 | |
| 20 | |
| 21 | |
| 22 class BaseTestRunner(object): | |
| 23 """Base class for running tests on a single device.""" | |
| 24 | |
| 25 def __init__(self, device): | |
| 26 """ | |
| 27 Args: | |
| 28 device: Tests will run on the device of this ID. | |
| 29 """ | |
| 30 self.device = device | |
| 31 self.adb = android_commands.AndroidCommands(device=device) | |
| 32 # Synchronize date/time between host and device. Otherwise same file on | |
| 33 # host and device may have different timestamp which may cause | |
| 34 # AndroidCommands.PushIfNeeded failed, or a test which may compare timestamp | |
| 35 # got from http head and local time could be failed. | |
| 36 self.adb.SynchronizeDateTime() | |
| 37 self._http_server = None | |
| 38 self._forwarder = None | |
| 39 self._spawning_server = None | |
| 40 self._spawner_forwarder = None | |
| 41 self._forwarder_device_port = 8000 | |
| 42 self._forwarder_base_url = 'http://localhost:%d' % self._forwarder_device_po rt | |
|
Nirnimesh
2011/10/24 18:02:34
80+ chars
michaelbai
2011/10/24 18:33:51
Done.
| |
| 43 self._flags = FlagChanger(self.adb) | |
| 44 | |
| 45 def RunTests(self): | |
| 46 # TODO(bulach): this should actually do SetUp / RunTestsInternal / TearDown. | |
| 47 # Refactor the various subclasses to expose a RunTestsInternal without | |
| 48 # any params. | |
| 49 raise NotImplementedError | |
| 50 | |
| 51 def SetUp(self): | |
| 52 """Called before tests run.""" | |
| 53 pass | |
| 54 | |
| 55 def TearDown(self): | |
| 56 """Called when tests finish running.""" | |
| 57 self.ShutdownHelperToolsForTestSuite() | |
| 58 | |
| 59 def CopyTestData(self, test_data_paths, dest_dir): | |
| 60 """Copies |test_data_paths| list of files/directories to |dest_dir|. | |
| 61 | |
| 62 Args: | |
| 63 test_data_paths: A list of files or directories relative to |dest_dir| | |
| 64 which should be copied to the device. The paths must exist in | |
| 65 |CHROME_DIR|. | |
| 66 dest_dir: Absolute path to copy to on the device. | |
| 67 """ | |
| 68 for p in test_data_paths: | |
| 69 self.adb.PushIfNeeded( | |
| 70 os.path.join(run_tests_helper.CHROME_DIR, p), | |
| 71 os.path.join(dest_dir, p)) | |
| 72 | |
| 73 def LaunchTestHttpServer(self, document_root, extra_config_contents=None): | |
| 74 """Launches an HTTP server to serve HTTP tests. | |
| 75 | |
| 76 Args: | |
| 77 document_root: Document root of the HTTP server. | |
| 78 extra_config_contents: Extra config contents for the HTTP server. | |
| 79 """ | |
| 80 self._http_server = lighttpd_server.LighttpdServer( | |
| 81 document_root, extra_config_contents=extra_config_contents) | |
| 82 if self._http_server.StartupHttpServer(): | |
| 83 logging.info('http server started: http://localhost:%s', | |
| 84 self._http_server.port) | |
| 85 else: | |
| 86 logging.critical('Failed to start http server') | |
| 87 # Root access needed to make the forwarder executable work. | |
| 88 self.adb.EnableAdbRoot() | |
| 89 self.StartForwarderForHttpServer() | |
| 90 | |
| 91 def StartForwarderForHttpServer(self): | |
| 92 """Starts a forwarder for the HTTP server. | |
| 93 | |
| 94 The forwarder forwards HTTP requests and responses between host and device. | |
| 95 """ | |
| 96 # Sometimes the forwarder device port may be already used. We have to kill | |
| 97 # all forwarder processes to ensure that the forwarder can be started since | |
| 98 # currently we can not associate the specified port to related pid. | |
| 99 # TODO(yfriedman/wangxianzhu): This doesn't work as most of the time the | |
| 100 # port is in use but the forwarder is already dead. Killing all forwarders | |
| 101 # is overly destructive and breaks other tests which make use of forwarders. | |
| 102 # if IsDevicePortUsed(self.adb, self._forwarder_device_port): | |
| 103 # self.adb.KillAll('forwarder') | |
| 104 self._forwarder = run_tests_helper.ForwardDevicePorts( | |
| 105 self.adb, [(self._forwarder_device_port, self._http_server.port)]) | |
| 106 | |
| 107 def RestartHttpServerForwarderIfNecessary(self): | |
| 108 """Restarts the forwarder if it's not open.""" | |
| 109 # Checks to see if the http server port is being used. If not forwards the | |
| 110 # request. | |
| 111 # TODO(dtrainor): This is not always reliable because sometimes the port | |
| 112 # will be left open even after the forwarder has been killed. | |
| 113 if not run_tests_helper.IsDevicePortUsed(self.adb, | |
| 114 self._forwarder_device_port): | |
| 115 self.StartForwarderForHttpServer() | |
| 116 | |
| 117 def ShutdownHelperToolsForTestSuite(self): | |
| 118 """Shuts down the server and the forwarder.""" | |
| 119 # Forwarders should be killed before the actual servers they're forwarding | |
| 120 # to as they are clients potentially with open connections and to allow for | |
| 121 # proper hand-shake/shutdown. | |
| 122 if self._forwarder or self._spawner_forwarder: | |
| 123 # Kill all forwarders on the device and then kill the process on the host | |
| 124 # (if it exists) | |
| 125 self.adb.KillAll('forwarder') | |
| 126 if self._forwarder: | |
| 127 self._forwarder.kill() | |
| 128 if self._spawner_forwarder: | |
| 129 self._spawner_forwarder.kill() | |
| 130 if self._http_server: | |
| 131 self._http_server.ShutdownHttpServer() | |
| 132 if self._spawning_server: | |
| 133 self._spawning_server.Stop() | |
| 134 self._flags.Restore() | |
| 135 | |
| 136 def LaunchChromeTestServerSpawner(self): | |
| 137 """Launches test server spawner.""" | |
| 138 self._spawning_server = SpawningServer(TEST_SERVER_SPAWNER_PORT, | |
| 139 TEST_SERVER_PORT) | |
| 140 self._spawning_server.Start() | |
| 141 # TODO(yfriedman): Ideally we'll only try to start up a port forwarder if | |
| 142 # there isn't one already running but for now we just get an error message | |
| 143 # and the existing forwarder still works. | |
| 144 self._spawner_forwarder = run_tests_helper.ForwardDevicePorts( | |
| 145 self.adb, [(TEST_SERVER_SPAWNER_PORT, TEST_SERVER_SPAWNER_PORT), | |
| 146 (TEST_SERVER_PORT, TEST_SERVER_PORT)]) | |
| OLD | NEW |