Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / testing / chromoting / browser_tests_launcher.py
blob2fceb7c658ba1ddf0f21701818922888ca3a8206
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 BROWSER_TEST_ID = 'browser_tests'
17 PROD_DIR_ID = '#PROD_DIR#'
18 HOST_HASH_VALUE = hashlib.md5(socket.gethostname()).hexdigest()
19 SUCCESS_INDICATOR = 'SUCCESS: all tests passed.'
20 NATIVE_MESSAGING_DIR = 'NativeMessagingHosts'
21 CRD_ID = 'chrome-remote-desktop' # Used in a few file/folder names
22 CHROMOTING_HOST_PATH = '/opt/google/chrome-remote-desktop/chrome-remote-desktop'
23 TEST_FAILURE = False
24 FAILING_TESTS = ''
25 HOST_READY_INDICATOR = 'Host ready to receive connections.'
28 def LaunchBTCommand(command):
29 global TEST_FAILURE, FAILING_TESTS
30 results = RunCommandInSubProcess(command)
32 # Check that the test passed.
33 if SUCCESS_INDICATOR not in results:
34 TEST_FAILURE = True
35 # Add this command-line to list of tests that failed.
36 FAILING_TESTS += command
39 def RunCommandInSubProcess(command):
40 """Creates a subprocess with command-line that is passed in.
42 Args:
43 command: The text of command to be executed.
44 Returns:
45 results: stdout contents of executing the command.
46 """
48 cmd_line = [command]
49 try:
50 results = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT,
51 shell=True)
52 except subprocess.CalledProcessError, e:
53 results = e.output
54 finally:
55 print results
56 return results
59 def TestMachineCleanup(user_profile_dir):
60 """Cleans up test machine so as not to impact other tests.
62 Args:
63 user_profile_dir: the user-profile folder used by Chromoting tests.
65 """
66 # Stop the host service.
67 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
69 # Cleanup any host logs.
70 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
72 # Remove the user-profile dir
73 if os.path.exists(user_profile_dir):
74 shutil.rmtree(user_profile_dir)
77 def InitialiseTestMachineForLinux(cfg_file):
78 """Sets up a Linux machine for connect-to-host browser-tests.
80 Copy over me2me host-config to expected locations.
81 By default, the Linux me2me host expects the host-config file to be under
82 $HOME/.config/chrome-remote-desktop
83 Its name is expected to have a hash that is specific to a machine.
85 Args:
86 cfg_file: location of test account's host-config file.
88 Raises:
89 Exception: if host did not start properly.
90 """
92 # First get home directory on current machine.
93 home_dir = expanduser('~')
94 default_config_file_location = os.path.join(home_dir, '.config', CRD_ID)
95 if os.path.exists(default_config_file_location):
96 shutil.rmtree(default_config_file_location)
97 os.makedirs(default_config_file_location)
99 # Copy over test host-config to expected location, with expected file-name.
100 # The file-name should contain a hash-value that is machine-specific.
101 default_config_file_name = 'host#%s.json' % HOST_HASH_VALUE
102 config_file_src = os.path.join(os.getcwd(), cfg_file)
103 shutil.copyfile(
104 config_file_src,
105 os.path.join(default_config_file_location, default_config_file_name))
107 # Make sure chromoting host is running.
108 if not RestartMe2MeHost():
109 # Host start failed. Don't run any tests.
110 raise Exception('Host restart failed.')
113 def RestartMe2MeHost():
114 """Stops and starts the Me2Me host on the test machine.
116 Waits to confirm that host is ready to receive connections before returning.
118 Returns:
119 True: if HOST_READY_INDICATOR is found in stdout, indicating host is ready.
120 False: if HOST_READY_INDICATOR not found in stdout.
123 # Stop chromoting host.
124 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
125 # Start chromoting host.
126 results = RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --start')
127 # Confirm that the start process completed, and we got:
128 # "Host ready to receive connections." in the log.
129 if HOST_READY_INDICATOR not in results:
130 return False
131 return True
134 def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
135 user_profile_dir):
136 """Sets up the Google Chrome user profile directory.
138 Delete the previous user profile directory if exists and create a new one.
139 This invalidates any state changes by the previous test so each test can start
140 with the same environment.
142 When a user launches the remoting web-app, the native messaging host process
143 is started. For this to work, this function places the me2me and it2me native
144 messaging host manifest files in a specific folder under the user-profile dir.
146 Args:
147 me2me_manifest_file: location of me2me native messaging host manifest file.
148 it2me_manifest_file: location of it2me native messaging host manifest file.
149 user_profile_dir: Chrome user-profile-directory.
151 native_messaging_folder = os.path.join(user_profile_dir, NATIVE_MESSAGING_DIR)
153 if os.path.exists(user_profile_dir):
154 shutil.rmtree(user_profile_dir)
155 os.makedirs(native_messaging_folder)
157 manifest_files = [me2me_manifest_file, it2me_manifest_file]
158 for manifest_file in manifest_files:
159 manifest_file_src = os.path.join(os.getcwd(), manifest_file)
160 manifest_file_dest = (
161 os.path.join(native_messaging_folder, os.path.basename(manifest_file)))
162 shutil.copyfile(manifest_file_src, manifest_file_dest)
165 def main(args):
167 InitialiseTestMachineForLinux(args.cfg_file)
169 with open(args.commands_file) as f:
170 for line in f:
171 # Reset the user profile directory to start each test with a clean slate.
172 SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
173 args.user_profile_dir)
175 # Replace the PROD_DIR value in the command-line with
176 # the passed in value.
177 line = line.replace(PROD_DIR_ID, args.prod_dir)
178 # Launch specified command line for test.
179 LaunchBTCommand(line)
180 # After each test, stop+start me2me host process.
181 if not RestartMe2MeHost():
182 # Host restart failed. Don't run any more tests.
183 raise Exception('Host restart failed.')
185 # All tests completed. Include host-logs in the test results.
186 host_log_contents = ''
187 # There should be only 1 log file, as we delete logs on test completion.
188 # Loop through matching files, just in case there are more.
189 for log_file in glob.glob('/tmp/chrome_remote_desktop_*'):
190 with open(log_file, 'r') as log:
191 host_log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (
192 log_file, log.read())
193 print host_log_contents
195 # Was there any test failure?
196 if TEST_FAILURE:
197 print '++++++++++AT LEAST 1 TEST FAILED++++++++++'
198 print FAILING_TESTS.rstrip('\n')
199 print '++++++++++++++++++++++++++++++++++++++++++'
200 raise Exception('At least one test failed.')
202 if __name__ == '__main__':
204 parser = argparse.ArgumentParser()
205 parser.add_argument('-f', '--commands_file',
206 help='path to file listing commands to be launched.')
207 parser.add_argument('-p', '--prod_dir',
208 help='path to folder having product and test binaries.')
209 parser.add_argument('-c', '--cfg_file',
210 help='path to test host config file.')
211 parser.add_argument('--me2me_manifest_file',
212 help='path to me2me host manifest file.')
213 parser.add_argument('--it2me_manifest_file',
214 help='path to it2me host manifest file.')
215 parser.add_argument(
216 '-u', '--user_profile_dir',
217 help='path to user-profile-dir, used by connect-to-host tests.')
218 command_line_args = parser.parse_args()
219 try:
220 main(command_line_args)
221 finally:
222 # Stop host and cleanup user-profile-dir.
223 TestMachineCleanup(command_line_args.user_profile_dir)