1 # Copyright (c) 2014 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.
6 """Utility script to launch browser-tests on the Chromoting bot."""
11 from os
.path
import expanduser
18 BROWSER_TEST_ID
= 'browser_tests'
19 PROD_DIR_ID
= '#PROD_DIR#'
20 HOST_HASH_VALUE
= hashlib
.md5(socket
.gethostname()).hexdigest()
21 SUCCESS_INDICATOR
= 'SUCCESS: all tests passed.'
22 NATIVE_MESSAGING_DIR
= 'NativeMessagingHosts'
23 CRD_ID
= 'chrome-remote-desktop' # Used in a few file/folder names
24 CHROMOTING_HOST_PATH
= './remoting/host/linux/linux_me2me_host.py'
27 HOST_READY_INDICATOR
= 'Host ready to receive connections.'
28 BROWSER_NOT_STARTED_ERROR
= (
29 'Still waiting for the following processes to finish')
30 TIME_OUT_INDICATOR
= '(TIMED OUT)'
32 # On a Swarming bot where these tests are executed, a temp folder is created
33 # under which the files specified in an .isolate are copied. This temp folder
34 # has a random name, which we'll store here for use later.
35 # Note that the test-execution always starts from the testing/chromoting folder
36 # under the temp folder.
37 ISOLATE_TEMP_FOLDER
= os
.path
.abspath(os
.path
.join(os
.getcwd(), '../..'))
40 def LaunchBTCommand(args
, command
):
41 """Launches the specified browser-test command.
43 If the execution failed because a browser-instance was not launched, retry
46 args: Command line args, used for test-case startup tasks.
47 command: Browser-test command line.
49 global TEST_FAILURE
, FAILING_TESTS
52 while retries
<= MAX_RETRIES
:
54 results
= RunCommandInSubProcess(command
)
56 if SUCCESS_INDICATOR
in results
:
60 # Sometimes, during execution of browser-tests, a browser instance is
61 # not started and the test times out. See http://crbug/480025.
62 # To work around it, check if this execution failed owing to that
64 # There are 2 things to look for in the results:
65 # A line saying "Still waiting for the following processes to finish",
66 # and, because sometimes that line gets logged even if the test
67 # eventually passes, we'll also look for "(TIMED OUT)", before retrying.
69 BROWSER_NOT_STARTED_ERROR
in results
and TIME_OUT_INDICATOR
in results
):
70 # Test failed for some other reason. Let's not retry.
74 # Check that the test passed.
75 if SUCCESS_INDICATOR
not in results
:
77 # Add this command-line to list of tests that failed.
78 FAILING_TESTS
+= command
81 def RunCommandInSubProcess(command
):
82 """Creates a subprocess with command-line that is passed in.
85 command: The text of command to be executed.
87 results: stdout contents of executing the command.
92 results
= subprocess
.check_output(cmd_line
, stderr
=subprocess
.STDOUT
,
94 except subprocess
.CalledProcessError
, e
:
101 def TestMachineCleanup(user_profile_dir
):
102 """Cleans up test machine so as not to impact other tests.
105 user_profile_dir: the user-profile folder used by Chromoting tests.
108 # Stop the host service.
109 RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --stop')
111 # Cleanup any host logs.
112 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
114 # Remove the user-profile dir
115 if os
.path
.exists(user_profile_dir
):
116 shutil
.rmtree(user_profile_dir
)
119 def InitialiseTestMachineForLinux(cfg_file
):
120 """Sets up a Linux machine for connect-to-host browser-tests.
122 Copy over me2me host-config to expected locations.
123 By default, the Linux me2me host expects the host-config file to be under
124 $HOME/.config/chrome-remote-desktop
125 Its name is expected to have a hash that is specific to a machine.
128 cfg_file: location of test account's host-config file.
131 Exception: if host did not start properly.
134 # First get home directory on current machine.
135 home_dir
= expanduser('~')
136 default_config_file_location
= os
.path
.join(home_dir
, '.config', CRD_ID
)
137 if os
.path
.exists(default_config_file_location
):
138 shutil
.rmtree(default_config_file_location
)
139 os
.makedirs(default_config_file_location
)
141 # Copy over test host-config to expected location, with expected file-name.
142 # The file-name should contain a hash-value that is machine-specific.
143 default_config_file_name
= 'host#%s.json' % HOST_HASH_VALUE
144 config_file_src
= os
.path
.join(os
.getcwd(), cfg_file
)
147 os
.path
.join(default_config_file_location
, default_config_file_name
))
149 # Make sure chromoting host is running.
150 if not RestartMe2MeHost():
151 # Host start failed. Don't run any tests.
152 raise Exception('Host restart failed.')
155 def RestartMe2MeHost():
156 """Stops and starts the Me2Me host on the test machine.
158 Waits to confirm that host is ready to receive connections before returning.
161 True: if HOST_READY_INDICATOR is found in stdout, indicating host is ready.
162 False: if HOST_READY_INDICATOR not found in stdout.
165 # To start the host, we want to be in the temp-folder for this test execution.
166 # Store the current folder to return back to it later.
167 previous_directory
= os
.getcwd()
168 os
.chdir(ISOLATE_TEMP_FOLDER
)
170 # Stop chromoting host.
171 RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --stop')
172 # Start chromoting host.
173 results
= RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --start')
175 os
.chdir(previous_directory
)
176 # Confirm that the start process completed, and we got:
177 # "Host ready to receive connections." in the log.
178 if HOST_READY_INDICATOR
not in results
:
183 def SetupUserProfileDir(me2me_manifest_file
, it2me_manifest_file
,
185 """Sets up the Google Chrome user profile directory.
187 Delete the previous user profile directory if exists and create a new one.
188 This invalidates any state changes by the previous test so each test can start
189 with the same environment.
191 When a user launches the remoting web-app, the native messaging host process
192 is started. For this to work, this function places the me2me and it2me native
193 messaging host manifest files in a specific folder under the user-profile dir.
196 me2me_manifest_file: location of me2me native messaging host manifest file.
197 it2me_manifest_file: location of it2me native messaging host manifest file.
198 user_profile_dir: Chrome user-profile-directory.
200 native_messaging_folder
= os
.path
.join(user_profile_dir
, NATIVE_MESSAGING_DIR
)
202 if os
.path
.exists(user_profile_dir
):
203 shutil
.rmtree(user_profile_dir
)
204 os
.makedirs(native_messaging_folder
)
206 manifest_files
= [me2me_manifest_file
, it2me_manifest_file
]
207 for manifest_file
in manifest_files
:
208 manifest_file_src
= os
.path
.join(os
.getcwd(), manifest_file
)
209 manifest_file_dest
= (
210 os
.path
.join(native_messaging_folder
, os
.path
.basename(manifest_file
)))
211 shutil
.copyfile(manifest_file_src
, manifest_file_dest
)
214 def PrintRunningProcesses():
215 processes
= psutil
.get_process_list()
216 processes
= sorted(processes
, key
=lambda process
: process
.name
)
218 print 'List of running processes:\n'
219 for process
in processes
:
223 def TestCaseSetup(args
):
224 # Stop+start me2me host process.
225 if not RestartMe2MeHost():
226 # Host restart failed. Don't run any more tests.
227 raise Exception('Host restart failed.')
229 # Reset the user profile directory to start each test with a clean slate.
230 SetupUserProfileDir(args
.me2me_manifest_file
, args
.it2me_manifest_file
,
231 args
.user_profile_dir
)
236 InitialiseTestMachineForLinux(args
.cfg_file
)
238 with
open(args
.commands_file
) as f
:
240 # Replace the PROD_DIR value in the command-line with
241 # the passed in value.
242 line
= line
.replace(PROD_DIR_ID
, args
.prod_dir
)
243 # Launch specified command line for test.
244 LaunchBTCommand(args
, line
)
246 # All tests completed. Include host-logs in the test results.
247 host_log_contents
= ''
248 for log_file
in glob
.glob('/tmp/chrome_remote_desktop_*'):
249 with
open(log_file
, 'r') as log
:
250 host_log_contents
+= '\nHOST LOG %s\n CONTENTS:\n%s' % (
251 log_file
, log
.read())
252 print host_log_contents
255 print '++++++++++AT LEAST 1 TEST FAILED++++++++++'
256 print FAILING_TESTS
.rstrip('\n')
257 print '++++++++++++++++++++++++++++++++++++++++++'
258 raise Exception('At least one test failed.')
260 if __name__
== '__main__':
262 parser
= argparse
.ArgumentParser()
263 parser
.add_argument('-f', '--commands_file',
264 help='path to file listing commands to be launched.')
265 parser
.add_argument('-p', '--prod_dir',
266 help='path to folder having product and test binaries.')
267 parser
.add_argument('-c', '--cfg_file',
268 help='path to test host config file.')
269 parser
.add_argument('--me2me_manifest_file',
270 help='path to me2me host manifest file.')
271 parser
.add_argument('--it2me_manifest_file',
272 help='path to it2me host manifest file.')
274 '-u', '--user_profile_dir',
275 help='path to user-profile-dir, used by connect-to-host tests.')
276 command_line_args
= parser
.parse_args()
278 main(command_line_args
)
280 # Stop host and cleanup user-profile-dir.
281 TestMachineCleanup(command_line_args
.user_profile_dir
)