cc: Make VideoResourceUpdater use CopyToResource instead of SetPixels.
[chromium-blink-merge.git] / testing / chromoting / browser_tests_launcher.py
bloba265f56c389a283a7f66940d35f589f79c38819f
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():
137 parser = argparse.ArgumentParser()
138 parser.add_argument('-f', '--commands_file',
139 help='path to file listing commands to be launched.')
140 parser.add_argument('-p', '--prod_dir',
141 help='path to folder having product and test binaries.')
142 parser.add_argument('-c', '--cfg_file',
143 help='path to test host config file.')
144 parser.add_argument('--me2me_manifest_file',
145 help='path to me2me host manifest file.')
146 parser.add_argument('--it2me_manifest_file',
147 help='path to it2me host manifest file.')
148 parser.add_argument(
149 '-u', '--user_profile_dir',
150 help='path to user-profile-dir, used by connect-to-host tests.')
152 args = parser.parse_args()
154 InitialiseTestMachineForLinux(args.cfg_file)
156 with open(args.commands_file) as f:
157 for line in f:
158 # Reset the user profile directory to start each test with a clean slate.
159 SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
160 args.user_profile_dir)
162 # Replace the PROD_DIR value in the command-line with
163 # the passed in value.
164 line = line.replace(PROD_DIR_ID, args.prod_dir)
165 LaunchBTCommand(line)
167 # Was there any test failure?
168 if TEST_FAILURE:
169 # Obtain contents of Chromoting host logs.
170 log_contents = ''
171 # There should be only 1 log file, as we delete logs on test completion.
172 # Loop through matching files, just in case there are more.
173 for log_file in glob.glob('/tmp/chrome_remote_desktop_*'):
174 with open(log_file, 'r') as log:
175 log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (log_file, log.read())
176 print log_contents
177 raise Exception('At least one test failed.')
179 # Now, stop host, and cleanup user-profile-dir
180 TestCleanUp(args.user_profile_dir)
182 if __name__ == '__main__':
183 main()