Add ICU message format support
[chromium-blink-merge.git] / testing / chromoting / browser_tests_launcher.py
blobd39d2910e3e70ae330e210206d3c250147603c27
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."""
7 import argparse
8 import glob
9 import hashlib
10 import os
11 from os.path import expanduser
12 import shutil
13 import socket
14 import subprocess
16 import psutil
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'
25 TEST_FAILURE = False
26 FAILING_TESTS = ''
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)'
31 MAX_RETRIES = 1
34 def LaunchBTCommand(args, command):
35 """Launches the specified browser-test command.
37 If the execution failed because a browser-instance was not launched, retry
38 once.
39 Args:
40 args: Command line args, used for test-case startup tasks.
41 command: Browser-test command line.
42 """
43 global TEST_FAILURE, FAILING_TESTS
45 retries = 0
46 while retries <= MAX_RETRIES:
47 TestCaseSetup(args)
48 results = RunCommandInSubProcess(command)
50 if SUCCESS_INDICATOR in results:
51 # Test passed.
52 break
54 # Sometimes, during execution of browser-tests, a browser instance is
55 # not started and the test times out. See http://crbug/480025.
56 # To work around it, check if this execution failed owing to that
57 # problem and retry.
58 # There are 2 things to look for in the results:
59 # A line saying "Still waiting for the following processes to finish",
60 # and, because sometimes that line gets logged even if the test
61 # eventually passes, we'll also look for "(TIMED OUT)", before retrying.
62 if not (
63 BROWSER_NOT_STARTED_ERROR in results and TIME_OUT_INDICATOR in results):
64 # Test failed for some other reason. Let's not retry.
65 break
66 retries += 1
68 # Check that the test passed.
69 if SUCCESS_INDICATOR not in results:
70 TEST_FAILURE = True
71 # Add this command-line to list of tests that failed.
72 FAILING_TESTS += command
75 def RunCommandInSubProcess(command):
76 """Creates a subprocess with command-line that is passed in.
78 Args:
79 command: The text of command to be executed.
80 Returns:
81 results: stdout contents of executing the command.
82 """
84 cmd_line = [command]
85 try:
86 results = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT,
87 shell=True)
88 except subprocess.CalledProcessError, e:
89 results = e.output
90 finally:
91 print results
92 return results
95 def TestMachineCleanup(user_profile_dir):
96 """Cleans up test machine so as not to impact other tests.
98 Args:
99 user_profile_dir: the user-profile folder used by Chromoting tests.
102 # Stop the host service.
103 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
105 # Cleanup any host logs.
106 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
108 # Remove the user-profile dir
109 if os.path.exists(user_profile_dir):
110 shutil.rmtree(user_profile_dir)
113 def InitialiseTestMachineForLinux(cfg_file):
114 """Sets up a Linux machine for connect-to-host browser-tests.
116 Copy over me2me host-config to expected locations.
117 By default, the Linux me2me host expects the host-config file to be under
118 $HOME/.config/chrome-remote-desktop
119 Its name is expected to have a hash that is specific to a machine.
121 Args:
122 cfg_file: location of test account's host-config file.
124 Raises:
125 Exception: if host did not start properly.
128 # First get home directory on current machine.
129 home_dir = expanduser('~')
130 default_config_file_location = os.path.join(home_dir, '.config', CRD_ID)
131 if os.path.exists(default_config_file_location):
132 shutil.rmtree(default_config_file_location)
133 os.makedirs(default_config_file_location)
135 # Copy over test host-config to expected location, with expected file-name.
136 # The file-name should contain a hash-value that is machine-specific.
137 default_config_file_name = 'host#%s.json' % HOST_HASH_VALUE
138 config_file_src = os.path.join(os.getcwd(), cfg_file)
139 shutil.copyfile(
140 config_file_src,
141 os.path.join(default_config_file_location, default_config_file_name))
143 # Make sure chromoting host is running.
144 if not RestartMe2MeHost():
145 # Host start failed. Don't run any tests.
146 raise Exception('Host restart failed.')
149 def RestartMe2MeHost():
150 """Stops and starts the Me2Me host on the test machine.
152 Waits to confirm that host is ready to receive connections before returning.
154 Returns:
155 True: if HOST_READY_INDICATOR is found in stdout, indicating host is ready.
156 False: if HOST_READY_INDICATOR not found in stdout.
159 # Stop chromoting host.
160 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
161 # Start chromoting host.
162 results = RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --start')
163 # Confirm that the start process completed, and we got:
164 # "Host ready to receive connections." in the log.
165 if HOST_READY_INDICATOR not in results:
166 return False
167 return True
170 def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
171 user_profile_dir):
172 """Sets up the Google Chrome user profile directory.
174 Delete the previous user profile directory if exists and create a new one.
175 This invalidates any state changes by the previous test so each test can start
176 with the same environment.
178 When a user launches the remoting web-app, the native messaging host process
179 is started. For this to work, this function places the me2me and it2me native
180 messaging host manifest files in a specific folder under the user-profile dir.
182 Args:
183 me2me_manifest_file: location of me2me native messaging host manifest file.
184 it2me_manifest_file: location of it2me native messaging host manifest file.
185 user_profile_dir: Chrome user-profile-directory.
187 native_messaging_folder = os.path.join(user_profile_dir, NATIVE_MESSAGING_DIR)
189 if os.path.exists(user_profile_dir):
190 shutil.rmtree(user_profile_dir)
191 os.makedirs(native_messaging_folder)
193 manifest_files = [me2me_manifest_file, it2me_manifest_file]
194 for manifest_file in manifest_files:
195 manifest_file_src = os.path.join(os.getcwd(), manifest_file)
196 manifest_file_dest = (
197 os.path.join(native_messaging_folder, os.path.basename(manifest_file)))
198 shutil.copyfile(manifest_file_src, manifest_file_dest)
201 def PrintRunningProcesses():
202 processes = psutil.get_process_list()
203 processes = sorted(processes, key=lambda process: process.name)
205 print 'List of running processes:\n'
206 for process in processes:
207 print process.name
210 def TestCaseSetup(args):
211 # Stop+start me2me host process.
212 if not RestartMe2MeHost():
213 # Host restart failed. Don't run any more tests.
214 raise Exception('Host restart failed.')
216 # Reset the user profile directory to start each test with a clean slate.
217 SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
218 args.user_profile_dir)
221 def main(args):
223 InitialiseTestMachineForLinux(args.cfg_file)
225 with open(args.commands_file) as f:
226 for line in f:
227 # Replace the PROD_DIR value in the command-line with
228 # the passed in value.
229 line = line.replace(PROD_DIR_ID, args.prod_dir)
230 # Launch specified command line for test.
231 LaunchBTCommand(args, line)
233 # All tests completed. Include host-logs in the test results.
234 host_log_contents = ''
235 # There should be only 1 log file, as we delete logs on test completion.
236 # Loop through matching files, just in case there are more.
237 for log_file in glob.glob('/tmp/chrome_remote_desktop_*'):
238 with open(log_file, 'r') as log:
239 host_log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (
240 log_file, log.read())
241 print host_log_contents
243 # Was there any test failure?
244 if TEST_FAILURE:
245 print '++++++++++AT LEAST 1 TEST FAILED++++++++++'
246 print FAILING_TESTS.rstrip('\n')
247 print '++++++++++++++++++++++++++++++++++++++++++'
248 raise Exception('At least one test failed.')
250 if __name__ == '__main__':
252 parser = argparse.ArgumentParser()
253 parser.add_argument('-f', '--commands_file',
254 help='path to file listing commands to be launched.')
255 parser.add_argument('-p', '--prod_dir',
256 help='path to folder having product and test binaries.')
257 parser.add_argument('-c', '--cfg_file',
258 help='path to test host config file.')
259 parser.add_argument('--me2me_manifest_file',
260 help='path to me2me host manifest file.')
261 parser.add_argument('--it2me_manifest_file',
262 help='path to it2me host manifest file.')
263 parser.add_argument(
264 '-u', '--user_profile_dir',
265 help='path to user-profile-dir, used by connect-to-host tests.')
266 command_line_args = parser.parse_args()
267 try:
268 main(command_line_args)
269 finally:
270 # Stop host and cleanup user-profile-dir.
271 TestMachineCleanup(command_line_args.user_profile_dir)