Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / testing / chromoting / browser_tests_launcher.py
blobda7bbaca22084560f9c11455955241d0616eff6a
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
26 def LaunchBTCommand(command):
27 global TEST_FAILURE
28 results = RunCommandInSubProcess(command)
30 # Check that the test passed.
31 if SUCCESS_INDICATOR not in results:
32 TEST_FAILURE = True
35 def RunCommandInSubProcess(command):
36 """Creates a subprocess with command-line that is passed in.
38 Args:
39 command: The text of command to be executed.
40 Returns:
41 results: stdout contents of executing the command.
42 """
44 cmd_line = [command]
45 try:
46 p = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, shell=True)
47 results, error = p.communicate()
48 except subprocess.CalledProcessError, e:
49 raise Exception('Exception %s running command %s\nError: %s' %
50 (e, command, error))
51 else:
52 print results
53 return results
56 def TestCleanUp(user_profile_dir):
57 """Cleans up test machine so as not to impact other tests.
59 Args:
60 user_profile_dir: the user-profile folder used by Chromoting tests.
62 """
63 # Stop the host service.
64 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
66 # Cleanup any host logs.
67 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
69 # Remove the user-profile dir
70 if os.path.exists(user_profile_dir):
71 shutil.rmtree(user_profile_dir)
74 def InitialiseTestMachineForLinux(cfg_file):
75 """Sets up a Linux machine for connect-to-host browser-tests.
77 Copy over me2me host-config to expected locations.
78 By default, the Linux me2me host expects the host-config file to be under
79 $HOME/.config/chrome-remote-desktop
80 Its name is expected to have a hash that is specific to a machine.
82 Args:
83 cfg_file: location of test account's host-config file.
84 """
86 # First get home directory on current machine.
87 home_dir = expanduser('~')
88 default_config_file_location = os.path.join(home_dir, '.config', CRD_ID)
89 if os.path.exists(default_config_file_location):
90 shutil.rmtree(default_config_file_location)
91 os.makedirs(default_config_file_location)
93 # Copy over test host-config to expected location, with expected file-name.
94 # The file-name should contain a hash-value that is machine-specific.
95 default_config_file_name = 'host#%s.json' % HOST_HASH_VALUE
96 config_file_src = os.path.join(os.getcwd(), cfg_file)
97 shutil.copyfile(
98 config_file_src,
99 os.path.join(default_config_file_location, default_config_file_name))
101 # Finally, start chromoting host.
102 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --start')
105 def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
106 user_profile_dir):
107 """Sets up the Google Chrome user profile directory.
109 Delete the previous user profile directory if exists and create a new one.
110 This invalidates any state changes by the previous test so each test can start
111 with the same environment.
113 When a user launches the remoting web-app, the native messaging host process
114 is started. For this to work, this function places the me2me and it2me native
115 messaging host manifest files in a specific folder under the user-profile dir.
117 Args:
118 me2me_manifest_file: location of me2me native messaging host manifest file.
119 it2me_manifest_file: location of it2me native messaging host manifest file.
120 user_profile_dir: Chrome user-profile-directory.
122 native_messaging_folder = os.path.join(user_profile_dir, NATIVE_MESSAGING_DIR)
124 if os.path.exists(user_profile_dir):
125 shutil.rmtree(user_profile_dir)
126 os.makedirs(native_messaging_folder)
128 manifest_files = [me2me_manifest_file, it2me_manifest_file]
129 for manifest_file in manifest_files:
130 manifest_file_src = os.path.join(os.getcwd(), manifest_file)
131 manifest_file_dest = (
132 os.path.join(native_messaging_folder, os.path.basename(manifest_file)))
133 shutil.copyfile(manifest_file_src, manifest_file_dest)
136 def main(args):
138 InitialiseTestMachineForLinux(args.cfg_file)
140 with open(args.commands_file) as f:
141 for line in f:
142 # Reset the user profile directory to start each test with a clean slate.
143 SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
144 args.user_profile_dir)
146 # Replace the PROD_DIR value in the command-line with
147 # the passed in value.
148 line = line.replace(PROD_DIR_ID, args.prod_dir)
149 LaunchBTCommand(line)
151 # All tests completed. Include host-logs in the test results.
152 host_log_contents = ''
153 # There should be only 1 log file, as we delete logs on test completion.
154 # Loop through matching files, just in case there are more.
155 for log_file in glob.glob('/tmp/chrome_remote_desktop_*'):
156 with open(log_file, 'r') as log:
157 host_log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (
158 log_file, log.read())
159 print host_log_contents
161 # Was there any test failure?
162 if TEST_FAILURE:
163 raise Exception('At least one test failed.')
165 if __name__ == '__main__':
167 parser = argparse.ArgumentParser()
168 parser.add_argument('-f', '--commands_file',
169 help='path to file listing commands to be launched.')
170 parser.add_argument('-p', '--prod_dir',
171 help='path to folder having product and test binaries.')
172 parser.add_argument('-c', '--cfg_file',
173 help='path to test host config file.')
174 parser.add_argument('--me2me_manifest_file',
175 help='path to me2me host manifest file.')
176 parser.add_argument('--it2me_manifest_file',
177 help='path to it2me host manifest file.')
178 parser.add_argument(
179 '-u', '--user_profile_dir',
180 help='path to user-profile-dir, used by connect-to-host tests.')
181 command_line_args = parser.parse_args()
182 try:
183 main(command_line_args)
184 finally:
185 # Stop host and cleanup user-profile-dir.
186 TestCleanUp(command_line_args.user_profile_dir)