xref: /aosp_15_r20/external/cronet/testing/chromoting/browser_tests_launcher.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2014 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6"""Utility script to launch browser-tests on the Chromoting bot."""
7
8from __future__ import print_function
9
10import argparse
11import time
12
13from chromoting_test_utilities import CleanupUserProfileDir
14from chromoting_test_utilities import GetJidFromHostLog
15from chromoting_test_utilities import GetJidListFromTestResults
16from chromoting_test_utilities import InitialiseTestMachineForLinux
17from chromoting_test_utilities import MAX_RETRIES
18from chromoting_test_utilities import PrintHostLogContents
19from chromoting_test_utilities import PROD_DIR_ID
20from chromoting_test_utilities import RunCommandInSubProcess
21from chromoting_test_utilities import TestCaseSetup
22from chromoting_test_utilities import TestMachineCleanup
23
24SUCCESS_INDICATOR = 'SUCCESS: all tests passed.'
25TEST_FAILURE = False
26FAILING_TESTS = ''
27BROWSER_NOT_STARTED_ERROR = (
28    'Still waiting for the following processes to finish')
29TIME_OUT_INDICATOR = '(TIMED OUT)'
30
31
32def LaunchBTCommand(args, command):
33  """Launches the specified browser-test command.
34
35    Retry if the execution failed because a browser-instance was not launched or
36    because the JID used did not match the host-JID.
37  Args:
38    args: Command line args, used for test-case startup tasks.
39    command: Browser-test command line.
40
41  Returns:
42    host_log_file_names: Array of host logs created for this command, including
43         retries.
44  """
45  global TEST_FAILURE, FAILING_TESTS
46  host_log_file_names = []
47
48  retries = 0
49  host_jid_mismatch = False
50  host_jid = None
51  while retries <= MAX_RETRIES:
52    # TestCaseSetup restarts the me2me host, and sets up user-profile dir.
53    # It returns the file-name of the me2me host log.
54    # If we are attempting to run this test because of a JID-mismatch, don't
55    # restart host.
56    if host_jid_mismatch:
57      # Cleanup user-profile directory, but don't restart host.
58      CleanupUserProfileDir(args)
59    else:
60      host_log_file_names.append(TestCaseSetup(args))
61      # Parse the me2me host log to obtain the JID that the host registered.
62      host_jid = GetJidFromHostLog(host_log_file_names[retries])
63
64    results = RunCommandInSubProcess(command)
65
66    # Get the JID used by this test to connect a remote-host, if any.
67    jids_used = GetJidListFromTestResults(results)
68
69    # Check for JID mismatch before checking for test success, so that we may
70    # record instances where a test passed despite a JID mismatch.
71    if jids_used and host_jid.rstrip() not in jids_used:
72      host_jid_mismatch = True
73      print('Host JID mismatch. JID in host log = %s.' % host_jid.rstrip())
74      print('Host JIDs used by test:')
75      for jid in jids_used:
76        print(jid)
77
78    if host_jid_mismatch:
79      # The JID for the remote-host did not match the JID that was used for this
80      # execution of the test. This happens because of a replication delay in
81      # updating all instances of the Chromoting Directory Server. To
82      # work-around this, sleep for 30s, which, based off a recent (08/2015)
83      # query for average replication delay for Chromoting, should be sufficient
84      # for the current JID value to have fully propagated.
85      retries += 1
86      time.sleep(30)
87      continue
88    if jids_used:
89      print('JID used by test matched me2me host JID: %s' % host_jid)
90    else:
91      # There wasn't a mismatch and no JIDs were returned. If no JIDs were
92      # returned, that means the test didn't use any JIDs, so there is nothing
93      # further for us to do.
94      pass
95
96    if SUCCESS_INDICATOR in results:
97      break
98
99    # Sometimes, during execution of browser-tests, a browser instance is
100    # not started and the test times out. See http://crbug/480025.
101    # To work around it, check if this execution failed owing to that
102    # problem and retry.
103    # There are 2 things to look for in the results:
104    # A line saying "Still waiting for the following processes to finish",
105    # and, because sometimes that line gets logged even if the test
106    # eventually passes, we'll also look for "(TIMED OUT)", before retrying.
107    if BROWSER_NOT_STARTED_ERROR in results and TIME_OUT_INDICATOR in results:
108      print('Browser-instance not started (http://crbug/480025). Retrying.')
109    else:
110      print('Test failed for unknown reason. Retrying.')
111
112    retries += 1
113
114  # Check that the test passed.
115  if SUCCESS_INDICATOR not in results:
116    TEST_FAILURE = True
117    # Add this command-line to list of tests that failed.
118    FAILING_TESTS += command
119
120  return host_log_file_names
121
122
123def main(args):
124
125  InitialiseTestMachineForLinux(args.cfg_file)
126
127  host_log_files = []
128  with open(args.commands_file) as f:
129    for line in f:
130      # Replace the PROD_DIR value in the command-line with
131      # the passed in value.
132      line = line.replace(PROD_DIR_ID, args.prod_dir)
133      # Launch specified command line for test.
134      host_log_files.extend(LaunchBTCommand(args, line))
135
136  # All tests completed. Include host-logs in the test results.
137  PrintHostLogContents(host_log_files)
138
139  return host_log_files
140
141if __name__ == '__main__':
142
143  parser = argparse.ArgumentParser()
144  parser.add_argument('-f', '--commands_file',
145                      help='path to file listing commands to be launched.')
146  parser.add_argument('-p', '--prod_dir',
147                      help='path to folder having product and test binaries.')
148  parser.add_argument('-c', '--cfg_file',
149                      help='path to test host config file.')
150  parser.add_argument('--me2me_manifest_file',
151                      help='path to me2me host manifest file.')
152  parser.add_argument('--it2me_manifest_file',
153                      help='path to it2me host manifest file.')
154  parser.add_argument(
155      '-u', '--user_profile_dir',
156      help='path to user-profile-dir, used by connect-to-host tests.')
157  command_line_args = parser.parse_args()
158  host_logs = ''
159  try:
160    host_logs = main(command_line_args)
161    if TEST_FAILURE:
162      print('++++++++++AT LEAST 1 TEST FAILED++++++++++')
163      print(FAILING_TESTS.rstrip('\n'))
164      print('++++++++++++++++++++++++++++++++++++++++++')
165      raise Exception('At least one test failed.')
166  finally:
167    # Stop host and cleanup user-profile-dir.
168    TestMachineCleanup(command_line_args.user_profile_dir, host_logs)
169