| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Takes in a test name and retrives all the output that the swarm server | |
| 7 has produced for tests with that name. This is expected to be called as a | |
| 8 build step.""" | |
| 9 | |
| 10 import os | |
| 11 import sys | |
| 12 | |
| 13 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 14 | |
| 15 from common import find_depot_tools # pylint: disable=W0611 | |
| 16 from common import gtest_utils | |
| 17 | |
| 18 from slave.swarming import swarming_utils | |
| 19 | |
| 20 # From depot tools/ | |
| 21 import fix_encoding | |
| 22 | |
| 23 | |
| 24 NO_OUTPUT_FOUND = ( | |
| 25 'No output produced by the test, it may have failed to run.\n' | |
| 26 'Showing all the output, including swarm specific output.\n' | |
| 27 '\n') | |
| 28 | |
| 29 | |
| 30 def gen_shard_output(result, gtest_parser): | |
| 31 """Returns output for swarm shard.""" | |
| 32 index = result['config_instance_index'] | |
| 33 machine_id = result['machine_id'] | |
| 34 machine_tag = result.get('machine_tag', 'unknown') | |
| 35 | |
| 36 header = ( | |
| 37 '\n' | |
| 38 '================================================================\n' | |
| 39 'Begin output from shard index %s (machine tag: %s, id: %s)\n' | |
| 40 '================================================================\n' | |
| 41 '\n') % (index, machine_tag, machine_id) | |
| 42 | |
| 43 # If we fail to get output, we should always mark it as an error. | |
| 44 if result['output']: | |
| 45 map(gtest_parser.ProcessLine, result['output'].splitlines()) | |
| 46 content = result['output'] | |
| 47 else: | |
| 48 content = NO_OUTPUT_FOUND | |
| 49 | |
| 50 test_exit_codes = (result['exit_codes'] or '1').split(',') | |
| 51 test_exit_code = max(int(i) for i in test_exit_codes) | |
| 52 test_exit_code = test_exit_code or int(not result['output']) | |
| 53 | |
| 54 footer = ( | |
| 55 '\n' | |
| 56 '================================================================\n' | |
| 57 'End output from shard index %s (machine tag: %s, id: %s). Return %d\n' | |
| 58 '================================================================\n' | |
| 59 ) % (index, machine_tag, machine_id, test_exit_code) | |
| 60 | |
| 61 return header + content + footer, test_exit_code | |
| 62 | |
| 63 | |
| 64 def gen_summary_output(failed_tests, exit_code, shards_remaining): | |
| 65 out = 'Summary for all the shards:\n' | |
| 66 if failed_tests: | |
| 67 plural = 's' if len(failed_tests) > 1 else '' | |
| 68 out += '%d test%s failed, listed below:\n' % (len(failed_tests), plural) | |
| 69 out += ''.join(' %s\n' % test for test in failed_tests) | |
| 70 | |
| 71 if shards_remaining: | |
| 72 out += 'Not all shards were executed.\n' | |
| 73 out += 'The following gtest shards weren\'t run:\n' | |
| 74 out += ''.join(' %d\n' % shard_id for shard_id in shards_remaining) | |
| 75 exit_code = exit_code or 1 | |
| 76 elif not failed_tests: | |
| 77 out += 'All tests passed.' | |
| 78 return out, exit_code | |
| 79 | |
| 80 | |
| 81 def GetSwarmResults( | |
| 82 swarm_get_results, swarm_base_url, test_keys, timeout, max_threads): | |
| 83 gtest_parser = gtest_utils.GTestLogParser() | |
| 84 exit_code = None | |
| 85 shards_remaining = range(len(test_keys)) | |
| 86 first_result = True | |
| 87 for index, result in swarm_get_results.yield_results( | |
| 88 swarm_base_url, test_keys, timeout, max_threads): | |
| 89 assert index == result['config_instance_index'] | |
| 90 if first_result and result['num_config_instances'] != len(test_keys): | |
| 91 # There are more test_keys than actual shards. | |
| 92 shards_remaining = shards_remaining[:result['num_config_instances']] | |
| 93 shards_remaining.remove(index) | |
| 94 first_result = False | |
| 95 output, test_exit_code = gen_shard_output(result, gtest_parser) | |
| 96 print output | |
| 97 exit_code = max(exit_code, test_exit_code) | |
| 98 | |
| 99 output, exit_code = gen_summary_output( | |
| 100 gtest_parser.FailedTests(), | |
| 101 exit_code, | |
| 102 shards_remaining) | |
| 103 print output | |
| 104 return exit_code | |
| 105 | |
| 106 | |
| 107 def main(): | |
| 108 client = swarming_utils.find_client(os.getcwd()) | |
| 109 if not client: | |
| 110 print >> sys.stderr, 'Failed to find swarm(ing)_client' | |
| 111 return 1 | |
| 112 | |
| 113 # TODO(maruel): Do not import, reproduce the same flags and forward to a | |
| 114 # subprocess.call() instead. | |
| 115 sys.path.insert(0, client) | |
| 116 import swarm_get_results # pylint: disable=F0401 | |
| 117 | |
| 118 parser, options, test_name = swarm_get_results.parse_args() | |
| 119 if not options.shards: | |
| 120 parser.error('The number of shards expected must be passed in.') | |
| 121 test_keys = swarm_get_results.get_test_keys( | |
| 122 options.url, test_name, options.timeout) | |
| 123 if not test_keys: | |
| 124 parser.error('No test keys to get results with.') | |
| 125 | |
| 126 options.shards = int(options.shards) | |
| 127 if options.shards == -1: | |
| 128 options.shards = len(test_keys) | |
| 129 elif len(test_keys) < options.shards: | |
| 130 print >> sys.stderr, ('Warning: Test should have %d shards, but only %d ' | |
| 131 'test keys were found' % (options.shards, | |
| 132 len(test_keys))) | |
| 133 | |
| 134 return GetSwarmResults( | |
| 135 swarm_get_results, options.url, test_keys, options.timeout, None) | |
| 136 | |
| 137 | |
| 138 if __name__ == '__main__': | |
| 139 fix_encoding.fix_encoding() | |
| 140 sys.exit(main()) | |
| OLD | NEW |