Fold NativeViewport application into ViewManager application.
[chromium-blink-merge.git] / testing / chromoting / browser_tests_launcher.py
blob1c9652fb513c5c6b576aa214994a50c283a11ac0
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.'
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:
36 TEST_FAILURE = True
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.
44 Args:
45 command: The text of command to be executed.
46 Returns:
47 results: stdout contents of executing the command.
48 """
50 cmd_line = [command]
51 try:
52 results = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT,
53 shell=True)
54 except subprocess.CalledProcessError, e:
55 results = e.output
56 finally:
57 print results
58 return results
61 def TestMachineCleanup(user_profile_dir):
62 """Cleans up test machine so as not to impact other tests.
64 Args:
65 user_profile_dir: the user-profile folder used by Chromoting tests.
67 """
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.
87 Args:
88 cfg_file: location of test account's host-config file.
90 Raises:
91 Exception: if host did not start properly.
92 """
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)
105 shutil.copyfile(
106 config_file_src,
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.
120 Returns:
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:
132 return False
133 return True
136 def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
137 user_profile_dir):
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.
148 Args:
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:
173 print process.name
176 def main(args):
178 InitialiseTestMachineForLinux(args.cfg_file)
180 with open(args.commands_file) as f:
181 for line in 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?
210 if 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.')
229 parser.add_argument(
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()
233 try:
234 main(command_line_args)
235 finally:
236 # Stop host and cleanup user-profile-dir.
237 TestMachineCleanup(command_line_args.user_profile_dir)