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
= '/opt/google/chrome-remote-desktop/chrome-remote-desktop'
27 HOST_READY_INDICATOR
= 'Host ready to receive connections.'
30 def LaunchBTCommand(command
):
31 global TEST_FAILURE
, FAILING_TESTS
32 results
= RunCommandInSubProcess(command
)
34 # Check that the test passed.
35 if SUCCESS_INDICATOR
not in results
:
37 # Add this command-line to list of tests that failed.
38 FAILING_TESTS
+= command
41 def RunCommandInSubProcess(command
):
42 """Creates a subprocess with command-line that is passed in.
45 command: The text of command to be executed.
47 results: stdout contents of executing the command.
52 results
= subprocess
.check_output(cmd_line
, stderr
=subprocess
.STDOUT
,
54 except subprocess
.CalledProcessError
, e
:
61 def TestMachineCleanup(user_profile_dir
):
62 """Cleans up test machine so as not to impact other tests.
65 user_profile_dir: the user-profile folder used by Chromoting tests.
68 # Stop the host service.
69 RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --stop')
71 # Cleanup any host logs.
72 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
74 # Remove the user-profile dir
75 if os
.path
.exists(user_profile_dir
):
76 shutil
.rmtree(user_profile_dir
)
79 def InitialiseTestMachineForLinux(cfg_file
):
80 """Sets up a Linux machine for connect-to-host browser-tests.
82 Copy over me2me host-config to expected locations.
83 By default, the Linux me2me host expects the host-config file to be under
84 $HOME/.config/chrome-remote-desktop
85 Its name is expected to have a hash that is specific to a machine.
88 cfg_file: location of test account's host-config file.
91 Exception: if host did not start properly.
94 # First get home directory on current machine.
95 home_dir
= expanduser('~')
96 default_config_file_location
= os
.path
.join(home_dir
, '.config', CRD_ID
)
97 if os
.path
.exists(default_config_file_location
):
98 shutil
.rmtree(default_config_file_location
)
99 os
.makedirs(default_config_file_location
)
101 # Copy over test host-config to expected location, with expected file-name.
102 # The file-name should contain a hash-value that is machine-specific.
103 default_config_file_name
= 'host#%s.json' % HOST_HASH_VALUE
104 config_file_src
= os
.path
.join(os
.getcwd(), cfg_file
)
107 os
.path
.join(default_config_file_location
, default_config_file_name
))
109 # Make sure chromoting host is running.
110 if not RestartMe2MeHost():
111 # Host start failed. Don't run any tests.
112 raise Exception('Host restart failed.')
115 def RestartMe2MeHost():
116 """Stops and starts the Me2Me host on the test machine.
118 Waits to confirm that host is ready to receive connections before returning.
121 True: if HOST_READY_INDICATOR is found in stdout, indicating host is ready.
122 False: if HOST_READY_INDICATOR not found in stdout.
125 # Stop chromoting host.
126 RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --stop')
127 # Start chromoting host.
128 results
= RunCommandInSubProcess(CHROMOTING_HOST_PATH
+ ' --start')
129 # Confirm that the start process completed, and we got:
130 # "Host ready to receive connections." in the log.
131 if HOST_READY_INDICATOR
not in results
:
136 def SetupUserProfileDir(me2me_manifest_file
, it2me_manifest_file
,
138 """Sets up the Google Chrome user profile directory.
140 Delete the previous user profile directory if exists and create a new one.
141 This invalidates any state changes by the previous test so each test can start
142 with the same environment.
144 When a user launches the remoting web-app, the native messaging host process
145 is started. For this to work, this function places the me2me and it2me native
146 messaging host manifest files in a specific folder under the user-profile dir.
149 me2me_manifest_file: location of me2me native messaging host manifest file.
150 it2me_manifest_file: location of it2me native messaging host manifest file.
151 user_profile_dir: Chrome user-profile-directory.
153 native_messaging_folder
= os
.path
.join(user_profile_dir
, NATIVE_MESSAGING_DIR
)
155 if os
.path
.exists(user_profile_dir
):
156 shutil
.rmtree(user_profile_dir
)
157 os
.makedirs(native_messaging_folder
)
159 manifest_files
= [me2me_manifest_file
, it2me_manifest_file
]
160 for manifest_file
in manifest_files
:
161 manifest_file_src
= os
.path
.join(os
.getcwd(), manifest_file
)
162 manifest_file_dest
= (
163 os
.path
.join(native_messaging_folder
, os
.path
.basename(manifest_file
)))
164 shutil
.copyfile(manifest_file_src
, manifest_file_dest
)
167 def PrintRunningProcesses():
168 processes
= psutil
.get_process_list()
169 processes
= sorted(processes
, key
=lambda process
: process
.name
)
171 print 'List of running processes:\n'
172 for process
in processes
:
178 InitialiseTestMachineForLinux(args
.cfg_file
)
180 with
open(args
.commands_file
) as f
:
182 # Reset the user profile directory to start each test with a clean slate.
183 SetupUserProfileDir(args
.me2me_manifest_file
, args
.it2me_manifest_file
,
184 args
.user_profile_dir
)
186 # Replace the PROD_DIR value in the command-line with
187 # the passed in value.
188 line
= line
.replace(PROD_DIR_ID
, args
.prod_dir
)
189 # Launch specified command line for test.
190 LaunchBTCommand(line
)
191 # After each test, stop+start me2me host process.
192 if not RestartMe2MeHost():
193 # Host restart failed. Don't run any more tests.
194 raise Exception('Host restart failed.')
196 # Print list of currently running processes.
197 PrintRunningProcesses()
199 # All tests completed. Include host-logs in the test results.
200 host_log_contents
= ''
201 # There should be only 1 log file, as we delete logs on test completion.
202 # Loop through matching files, just in case there are more.
203 for log_file
in glob
.glob('/tmp/chrome_remote_desktop_*'):
204 with
open(log_file
, 'r') as log
:
205 host_log_contents
+= '\nHOST LOG %s\n CONTENTS:\n%s' % (
206 log_file
, log
.read())
207 print host_log_contents
209 # Was there any test failure?
211 print '++++++++++AT LEAST 1 TEST FAILED++++++++++'
212 print FAILING_TESTS
.rstrip('\n')
213 print '++++++++++++++++++++++++++++++++++++++++++'
214 raise Exception('At least one test failed.')
216 if __name__
== '__main__':
218 parser
= argparse
.ArgumentParser()
219 parser
.add_argument('-f', '--commands_file',
220 help='path to file listing commands to be launched.')
221 parser
.add_argument('-p', '--prod_dir',
222 help='path to folder having product and test binaries.')
223 parser
.add_argument('-c', '--cfg_file',
224 help='path to test host config file.')
225 parser
.add_argument('--me2me_manifest_file',
226 help='path to me2me host manifest file.')
227 parser
.add_argument('--it2me_manifest_file',
228 help='path to it2me host manifest file.')
230 '-u', '--user_profile_dir',
231 help='path to user-profile-dir, used by connect-to-host tests.')
232 command_line_args
= parser
.parse_args()
234 main(command_line_args
)
236 # Stop host and cleanup user-profile-dir.
237 TestMachineCleanup(command_line_args
.user_profile_dir
)