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