1# Copyright 2015 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"""Utility script to run chromoting test driver tests on the Chromoting bot.""" 5 6from __future__ import print_function 7 8import argparse 9 10from chromoting_test_utilities import GetJidFromHostLog 11from chromoting_test_utilities import InitialiseTestMachineForLinux 12from chromoting_test_utilities import MAX_RETRIES 13from chromoting_test_utilities import PrintHostLogContents 14from chromoting_test_utilities import PROD_DIR_ID 15from chromoting_test_utilities import RunCommandInSubProcess 16from chromoting_test_utilities import TestCaseSetup 17from chromoting_test_utilities import TestMachineCleanup 18 19TEST_ENVIRONMENT_TEAR_DOWN_INDICATOR = 'Global test environment tear-down' 20FAILED_INDICATOR = '[ FAILED ]' 21 22 23def LaunchCTDCommand(args, command): 24 """Launches the specified chromoting test driver command. 25 26 Args: 27 args: Command line args, used for test-case startup tasks. 28 command: Chromoting Test Driver command line. 29 Returns: 30 command, host_log_file_names: Tuple of: 31 "command" if there was a test-environment failure, or any failing test, and 32 list of host-log file-names. 33 """ 34 host_log_file_names = [] 35 36 host_log_file_names.append(TestCaseSetup(args)) 37 # Parse the me2me host log to obtain the JID that the host registered. 38 host_jid = GetJidFromHostLog(host_log_file_names[-1]) 39 40 if not host_jid: 41 # Host-JID not found in log. Let's not attempt to run this test. 42 print('Host-JID not found in log %s.' % host_log_file_names[-1]) 43 return '[Command failed]: %s, %s' % (command, host_log_file_names) 44 45 retries = 0 46 failed_tests_list = [] 47 # TODO(anandc): Remove this retry-logic once http://crbug/570840 is fixed. 48 while retries <= MAX_RETRIES: 49 # In order to ensure the host is online with the expected JID, pass in the 50 # jid obtained from the host-log as a command-line parameter. 51 command = command.replace('\n', '') + ' --hostjid=%s' % host_jid 52 53 results = RunCommandInSubProcess(command) 54 55 tear_down_index = results.find(TEST_ENVIRONMENT_TEAR_DOWN_INDICATOR) 56 if tear_down_index == -1: 57 # The test environment did not tear down. Something went horribly wrong. 58 return '[Command failed]: ' + command, host_log_file_names 59 60 end_results_list = results[tear_down_index:].split('\n') 61 test_failed = False 62 for result in end_results_list: 63 if result.startswith(FAILED_INDICATOR): 64 test_failed = True 65 if retries == MAX_RETRIES: 66 # Test failed and we have no more retries left. 67 failed_tests_list.append(result) 68 69 if test_failed: 70 retries += 1 71 else: 72 break 73 74 if failed_tests_list: 75 test_result = '[Command]: ' + command 76 # Note: Skipping the first one is intentional. 77 for i in range(1, len(failed_tests_list)): 78 test_result += ' ' + failed_tests_list[i] 79 return test_result, host_log_file_names 80 81 # All tests passed! 82 return '', host_log_file_names 83 84 85def main(args): 86 InitialiseTestMachineForLinux(args.cfg_file) 87 88 failed_tests = '' 89 host_log_files = [] 90 with open(args.commands_file) as f: 91 for line in f: 92 # Replace the PROD_DIR value in the command-line with 93 # the passed in value. 94 line = line.replace(PROD_DIR_ID, args.prod_dir) 95 # Launch specified command line for test. 96 test_results, log_files = LaunchCTDCommand(args, line) 97 failed_tests += test_results 98 host_log_files.extend(log_files) 99 100 # All tests completed. Include host-logs in the test results. 101 PrintHostLogContents(host_log_files) 102 103 return failed_tests, host_log_files 104 105 106if __name__ == '__main__': 107 parser = argparse.ArgumentParser() 108 parser.add_argument('-f', 109 '--commands_file', 110 help='path to file listing commands to be launched.') 111 parser.add_argument('-p', 112 '--prod_dir', 113 help='path to folder having product and test binaries.') 114 parser.add_argument('-c', '--cfg_file', help='path to test host config file.') 115 parser.add_argument('--me2me_manifest_file', 116 help='path to me2me host manifest file.') 117 parser.add_argument('--it2me_manifest_file', 118 help='path to it2me host manifest file.') 119 parser.add_argument( 120 '-u', 121 '--user_profile_dir', 122 help='path to user-profile-dir, used by connect-to-host tests.') 123 command_line_args = parser.parse_args() 124 host_logs = '' 125 failing_tests = '' 126 try: 127 failing_tests, host_logs = main(command_line_args) 128 if failing_tests: 129 print('++++++++++FAILED TESTS++++++++++') 130 print(failing_tests.rstrip('\n')) 131 print('++++++++++++++++++++++++++++++++') 132 raise Exception('At least one test failed.') 133 finally: 134 # Stop host and cleanup user-profile-dir. 135 TestMachineCleanup(command_line_args.user_profile_dir, host_logs) 136