chromoting_test_utilities.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.
  4. """Utility script to run tests on the Chromoting bot."""
  5. from __future__ import print_function
  6. import hashlib
  7. import os
  8. from os.path import expanduser
  9. import re
  10. import shutil
  11. import socket
  12. import subprocess
  13. import psutil
  14. PROD_DIR_ID = '#PROD_DIR#'
  15. CRD_ID = 'chrome-remote-desktop' # Used in a few file/folder names
  16. HOST_READY_INDICATOR = 'Host ready to receive connections.'
  17. BROWSER_TEST_ID = 'browser_tests'
  18. HOST_HASH_VALUE = hashlib.md5(socket.gethostname()).hexdigest()
  19. NATIVE_MESSAGING_DIR = 'NativeMessagingHosts'
  20. # On a Swarming bot where these tests are executed, a temp folder is created
  21. # under which the files specified in an .isolate are copied. This temp folder
  22. # has a random name, which we'll store here for use later.
  23. # Note that the test-execution always starts from the testing/chromoting folder
  24. # under the temp folder.
  25. ISOLATE_CHROMOTING_HOST_PATH = 'remoting/host/linux/linux_me2me_host.py'
  26. ISOLATE_TEMP_FOLDER = os.path.abspath(os.path.join(os.getcwd(), '../..'))
  27. CHROMOTING_HOST_PATH = os.path.join(ISOLATE_TEMP_FOLDER,
  28. ISOLATE_CHROMOTING_HOST_PATH)
  29. MAX_RETRIES = 1
  30. class HostOperationFailedException(Exception):
  31. pass
  32. def RunCommandInSubProcess(command):
  33. """Creates a subprocess with command-line that is passed in.
  34. Args:
  35. command: The text of command to be executed.
  36. Returns:
  37. results: stdout contents of executing the command.
  38. """
  39. cmd_line = [command]
  40. try:
  41. print('Going to run:\n%s' % command)
  42. results = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT,
  43. shell=True)
  44. except subprocess.CalledProcessError as e:
  45. results = e.output
  46. finally:
  47. print(results)
  48. return results
  49. def TestMachineCleanup(user_profile_dir, host_logs=None):
  50. """Cleans up test machine so as not to impact other tests.
  51. Args:
  52. user_profile_dir: the user-profile folder used by Chromoting tests.
  53. host_logs: List of me2me host logs; these will be deleted.
  54. """
  55. # Stop the host service.
  56. RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
  57. # Cleanup any host logs.
  58. if host_logs:
  59. for host_log in host_logs:
  60. RunCommandInSubProcess('rm %s' % host_log)
  61. # Remove the user-profile dir
  62. if os.path.exists(user_profile_dir):
  63. shutil.rmtree(user_profile_dir)
  64. def InitialiseTestMachineForLinux(cfg_file):
  65. """Sets up a Linux machine for connect-to-host chromoting tests.
  66. Copy over me2me host-config to expected locations.
  67. By default, the Linux me2me host expects the host-config file to be under
  68. $HOME/.config/chrome-remote-desktop
  69. Its name is expected to have a hash that is specific to a machine.
  70. Args:
  71. cfg_file: location of test account's host-config file.
  72. Raises:
  73. Exception: if host did not start properly.
  74. """
  75. # First get home directory on current machine.
  76. home_dir = expanduser('~')
  77. default_config_file_location = os.path.join(home_dir, '.config', CRD_ID)
  78. if os.path.exists(default_config_file_location):
  79. shutil.rmtree(default_config_file_location)
  80. os.makedirs(default_config_file_location)
  81. # Copy over test host-config to expected location, with expected file-name.
  82. # The file-name should contain a hash-value that is machine-specific.
  83. default_config_file_name = 'host#%s.json' % HOST_HASH_VALUE
  84. config_file_src = os.path.join(os.getcwd(), cfg_file)
  85. shutil.copyfile(
  86. config_file_src,
  87. os.path.join(default_config_file_location, default_config_file_name))
  88. # Make sure chromoting host is running.
  89. RestartMe2MeHost()
  90. def RestartMe2MeHost():
  91. """Stops and starts the Me2Me host on the test machine.
  92. Launches the me2me start-host command, and parses the stdout of the execution
  93. to obtain the host log-file name.
  94. Returns:
  95. log_file: Host log file.
  96. Raises:
  97. Exception: If host-log does not contain string indicating host is ready.
  98. """
  99. # To start the host, we want to be in the temp-folder for this test execution.
  100. # Store the current folder to return back to it later.
  101. previous_directory = os.getcwd()
  102. os.chdir(ISOLATE_TEMP_FOLDER)
  103. # Stop chromoting host.
  104. RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --stop')
  105. # Start chromoting host.
  106. print('Starting chromoting host from %s' % CHROMOTING_HOST_PATH)
  107. results = RunCommandInSubProcess(CHROMOTING_HOST_PATH + ' --start')
  108. os.chdir(previous_directory)
  109. # Get log file from results of above command printed to stdout. Example:
  110. # Log file: /tmp/tmp0c3EcP/chrome_remote_desktop_20150929_101525_B0o89t
  111. start_of_host_log = results.index('Log file: ') + len('Log file: ')
  112. log_file = results[start_of_host_log:].rstrip()
  113. # Confirm that the start process completed, and we got:
  114. # "Host ready to receive connections." in the log.
  115. if HOST_READY_INDICATOR not in results:
  116. # Host start failed. Print out host-log. Don't run any tests.
  117. with open(log_file, 'r') as f:
  118. print(f.read())
  119. raise HostOperationFailedException('Host restart failed.')
  120. return log_file
  121. def CleanupUserProfileDir(args):
  122. SetupUserProfileDir(args.me2me_manifest_file, args.it2me_manifest_file,
  123. args.user_profile_dir)
  124. def SetupUserProfileDir(me2me_manifest_file, it2me_manifest_file,
  125. user_profile_dir):
  126. """Sets up the Google Chrome user profile directory.
  127. Delete the previous user profile directory if exists and create a new one.
  128. This invalidates any state changes by the previous test so each test can start
  129. with the same environment.
  130. When a user launches the remoting web-app, the native messaging host process
  131. is started. For this to work, this function places the me2me and it2me native
  132. messaging host manifest files in a specific folder under the user-profile dir.
  133. Args:
  134. me2me_manifest_file: location of me2me native messaging host manifest file.
  135. it2me_manifest_file: location of it2me native messaging host manifest file.
  136. user_profile_dir: Chrome user-profile-directory.
  137. """
  138. native_messaging_folder = os.path.join(user_profile_dir, NATIVE_MESSAGING_DIR)
  139. if os.path.exists(user_profile_dir):
  140. shutil.rmtree(user_profile_dir)
  141. os.makedirs(native_messaging_folder)
  142. manifest_files = [me2me_manifest_file, it2me_manifest_file]
  143. for manifest_file in manifest_files:
  144. manifest_file_src = os.path.join(os.getcwd(), manifest_file)
  145. manifest_file_dest = (
  146. os.path.join(native_messaging_folder, os.path.basename(manifest_file)))
  147. shutil.copyfile(manifest_file_src, manifest_file_dest)
  148. def PrintRunningProcesses():
  149. processes = psutil.get_process_list()
  150. processes = sorted(processes, key=lambda process: process.name)
  151. print('List of running processes:\n')
  152. for process in processes:
  153. print(process.name)
  154. def PrintHostLogContents(host_log_files=None):
  155. if host_log_files:
  156. host_log_contents = ''
  157. for log_file in sorted(host_log_files):
  158. with open(log_file, 'r') as log:
  159. host_log_contents += '\nHOST LOG %s\n CONTENTS:\n%s' % (
  160. log_file, log.read())
  161. print(host_log_contents)
  162. def TestCaseSetup(args):
  163. # Reset the user profile directory to start each test with a clean slate.
  164. CleanupUserProfileDir(args)
  165. # Stop+start me2me host process.
  166. return RestartMe2MeHost()
  167. def GetJidListFromTestResults(results):
  168. """Parse the output of a test execution to obtain the JID used by the test.
  169. Args:
  170. results: stdio contents of test execution.
  171. Returns:
  172. jids_used: List of JIDs used by test; empty list if not found.
  173. """
  174. # Reg-ex defining the JID information in the string being parsed.
  175. jid_re = '(Connecting to )(.*.gserviceaccount.com/chromoting.*)(. Local.*)'
  176. jids_used = []
  177. for line in results.split('\n'):
  178. match = re.search(jid_re, line)
  179. if match:
  180. jid_used = match.group(2)
  181. if jid_used not in jids_used:
  182. jids_used.append(jid_used)
  183. return jids_used
  184. def GetJidFromHostLog(host_log_file):
  185. """Parse the me2me host log to obtain the JID that the host registered.
  186. Args:
  187. host_log_file: path to host-log file that should be parsed for a JID.
  188. Returns:
  189. host_jid: host-JID if found in host-log, else None
  190. """
  191. host_jid = None
  192. with open(host_log_file, 'r') as log_file:
  193. for line in log_file:
  194. # The host JID will be recorded in a line saying 'Signaling
  195. # connected'.
  196. if 'Signaling connected. ' in line:
  197. components = line.split(':')
  198. host_jid = components[-1].lstrip()
  199. break
  200. return host_jid