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