Roll src/third_party/WebKit a05b987:7eb2976 (svn 202510:202511)
[chromium-blink-merge.git] / testing / chromoting / chromoting_test_utilities.py
blobea183f1732938a9c1358c8c6007445472d9e2137
1 # Copyright 2015 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.
5 """Utility script to run tests on the Chromoting bot."""
7 import glob
8 import hashlib
9 import os
10 from os.path import expanduser
11 import shutil
12 import socket
13 import subprocess
15 import psutil
17 PROD_DIR_ID = '#PROD_DIR#'
18 CRD_ID = 'chrome-remote-desktop' # Used in a few file/folder names
19 CHROMOTING_HOST_PATH = './remoting/host/linux/linux_me2me_host.py'
20 HOST_READY_INDICATOR = 'Host ready to receive connections.'
21 BROWSER_TEST_ID = 'browser_tests'
22 HOST_HASH_VALUE = hashlib.md5(socket.gethostname()).hexdigest()
23 NATIVE_MESSAGING_DIR = 'NativeMessagingHosts'
24 # On a Swarming bot where these tests are executed, a temp folder is created
25 # under which the files specified in an .isolate are copied. This temp folder
26 # has a random name, which we'll store here for use later.
27 # Note that the test-execution always starts from the testing/chromoting folder
28 # under the temp folder.
29 ISOLATE_TEMP_FOLDER = os.path.abspath(os.path.join(os.getcwd(), '../..'))
32 def RunCommandInSubProcess(command):
33 """Creates a subprocess with command-line that is passed in.
35 Args:
36 command: The text of command to be executed.
37 Returns:
38 results: stdout contents of executing the command.
39 """
41 cmd_line = [command]
42 try:
43 results = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT,
44 shell=True)
45 except subprocess.CalledProcessError, e:
46 results = e.output
47 finally:
48 print results
49 return results
52 def TestMachineCleanup(user_profile_dir):
53 """Cleans up test machine so as not to impact other tests.
55 Args:
56 user_profile_dir: the user-profile folder used by Chromoting tests.
57 """
58 # Stop the host service.
59 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
61 # Cleanup any host logs.
62 RunCommandInSubProcess('rm /tmp/chrome_remote_desktop_*')
64 # Remove the user-profile dir
65 if os.path.exists(user_profile_dir):
66 shutil.rmtree(user_profile_dir)
69 def InitialiseTestMachineForLinux(cfg_file):
70 """Sets up a Linux machine for connect-to-host chromoting tests.
72 Copy over me2me host-config to expected locations.
73 By default, the Linux me2me host expects the host-config file to be under
74 $HOME/.config/chrome-remote-desktop
75 Its name is expected to have a hash that is specific to a machine.
77 Args:
78 cfg_file: location of test account's host-config file.
80 Raises:
81 Exception: if host did not start properly.
82 """
84 # First get home directory on current machine.
85 home_dir = expanduser('~')
86 default_config_file_location = os.path.join(home_dir, '.config', CRD_ID)
87 if os.path.exists(default_config_file_location):
88 shutil.rmtree(default_config_file_location)
89 os.makedirs(default_config_file_location)
91 # Copy over test host-config to expected location, with expected file-name.
92 # The file-name should contain a hash-value that is machine-specific.
93 default_config_file_name = 'host#%s.json' % HOST_HASH_VALUE
94 config_file_src = os.path.join(os.getcwd(), cfg_file)
95 shutil.copyfile(
96 config_file_src,
97 os.path.join(default_config_file_location, default_config_file_name))
99 # Make sure chromoting host is running.
100 if not RestartMe2MeHost():
101 # Host start failed. Don't run any tests.
102 raise Exception('Host restart failed.')
105 def RestartMe2MeHost():
106 """Stops and starts the Me2Me host on the test machine.
108 Waits to confirm that host is ready to receive connections before returning.
110 Returns:
111 True: if HOST_READY_INDICATOR is found in stdout, indicating host is ready.
112 False: if HOST_READY_INDICATOR not found in stdout.
115 # To start the host, we want to be in the temp-folder for this test execution.
116 # Store the current folder to return back to it later.
117 previous_directory = os.getcwd()
118 os.chdir(ISOLATE_TEMP_FOLDER)
120 # Stop chromoting host.
121 RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
122 # Start chromoting host.
123 results = RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --start')
125 os.chdir(previous_directory)
126 # Confirm that the start process completed, and we got:
127 # "Host ready to receive connections." in the log.
128 if HOST_READY_INDICATOR not in results:
129 return False
130 return True
133 def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
134 user_profile_dir):
135 """Sets up the Google Chrome user profile directory.
137 Delete the previous user profile directory if exists and create a new one.
138 This invalidates any state changes by the previous test so each test can start
139 with the same environment.
141 When a user launches the remoting web-app, the native messaging host process
142 is started. For this to work, this function places the me2me and it2me native
143 messaging host manifest files in a specific folder under the user-profile dir.
145 Args:
146 me2me_manifest_file: location of me2me native messaging host manifest file.
147 it2me_manifest_file: location of it2me native messaging host manifest file.
148 user_profile_dir: Chrome user-profile-directory.
150 native_messaging_folder = os.path.join(user_profile_dir, NATIVE_MESSAGING_DIR)
152 if os.path.exists(user_profile_dir):
153 shutil.rmtree(user_profile_dir)
154 os.makedirs(native_messaging_folder)
156 manifest_files = [me2me_manifest_file, it2me_manifest_file]
157 for manifest_file in manifest_files:
158 manifest_file_src = os.path.join(os.getcwd(), manifest_file)
159 manifest_file_dest = (
160 os.path.join(native_messaging_folder, os.path.basename(manifest_file)))
161 shutil.copyfile(manifest_file_src, manifest_file_dest)
164 def PrintRunningProcesses():
165 processes = psutil.get_process_list()
166 processes = sorted(processes, key=lambda process: process.name)
168 print 'List of running processes:\n'
169 for process in processes:
170 print process.name
173 def PrintHostLogContents():
174 host_log_contents = ''
175 for log_file in sorted(glob.glob('/tmp/chrome_remote_desktop_*')):
176 with open(log_file, 'r') as log:
177 host_log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (
178 log_file, log.read())
179 print host_log_contents
182 def TestCaseSetup(args):
183 # Stop+start me2me host process.
184 if not RestartMe2MeHost():
185 # Host restart failed. Don't run any more tests.
186 raise Exception('Host restart failed.')
188 # Reset the user profile directory to start each test with a clean slate.
189 SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
190 args.user_profile_dir)