| 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 """Traces each test cases of a google-test executable individually and generates | |
| 7 or updates an .isolate file. | |
| 8 | |
| 9 If the trace hangs up, you can still take advantage of the traces up to the | |
| 10 points it got to by Ctrl-C'ing out, then running isolate.py merge -r | |
| 11 out/release/foo_test.isolated. the reason it works is because both isolate.py | |
| 12 and isolate_test_cases.py uses the same filename for the trace by default. | |
| 13 """ | |
| 14 | |
| 15 import logging | |
| 16 import os | |
| 17 import subprocess | |
| 18 import sys | |
| 19 | |
| 20 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 21 if not ROOT_DIR in sys.path: | |
| 22 sys.path.insert(0, ROOT_DIR) | |
| 23 | |
| 24 import isolate | |
| 25 import run_test_cases | |
| 26 import trace_inputs | |
| 27 import trace_test_cases | |
| 28 | |
| 29 from utils import tools | |
| 30 | |
| 31 | |
| 32 def isolate_test_cases( | |
| 33 cmd, test_cases, jobs, isolated_file, isolate_file, | |
| 34 root_dir, reldir, variables, trace_blacklist): | |
| 35 assert os.path.isabs(root_dir) and os.path.isdir(root_dir), root_dir | |
| 36 | |
| 37 logname = isolated_file + '.log' | |
| 38 basename = isolated_file.rsplit('.', 1)[0] | |
| 39 cwd_dir = os.path.join(root_dir, reldir) | |
| 40 # Do the actual tracing. | |
| 41 results = trace_test_cases.trace_test_cases( | |
| 42 cmd, cwd_dir, test_cases, jobs, logname) | |
| 43 api = trace_inputs.get_api() | |
| 44 blacklist = trace_inputs.gen_blacklist(trace_blacklist) | |
| 45 logs = dict( | |
| 46 (i.pop('trace'), i) for i in api.parse_log(logname, blacklist, None)) | |
| 47 exception = None | |
| 48 try: | |
| 49 inputs = [] | |
| 50 for items in results: | |
| 51 item = items[-1] | |
| 52 assert item['valid'] | |
| 53 # Load the results; | |
| 54 log_dict = logs[item['tracename']] | |
| 55 if log_dict.get('exception'): | |
| 56 exception = exception or log_dict['exception'] | |
| 57 logging.error('Got exception: %s', exception) | |
| 58 continue | |
| 59 files = log_dict['results'].strip_root(root_dir).files | |
| 60 tracked, touched = isolate.split_touched(files) | |
| 61 value = isolate.generate_isolate( | |
| 62 tracked, | |
| 63 [], | |
| 64 touched, | |
| 65 root_dir, | |
| 66 variables, | |
| 67 reldir, | |
| 68 blacklist) | |
| 69 # item['test_case'] could be an invalid file name. | |
| 70 out = basename + '.' + item['tracename'] + '.isolate' | |
| 71 with open(out, 'w') as f: | |
| 72 isolate.pretty_print(value, f) | |
| 73 inputs.append(out) | |
| 74 | |
| 75 # Merges back. Note that it is possible to blow up the command line | |
| 76 # argument length here but writing the files is still useful. Convert to | |
| 77 # importing the module instead if necessary. | |
| 78 merge_cmd = [ | |
| 79 sys.executable, | |
| 80 os.path.join(ROOT_DIR, 'isolate_merge.py'), | |
| 81 isolate_file, | |
| 82 '-o', isolate_file, | |
| 83 ] | |
| 84 merge_cmd.extend(inputs) | |
| 85 logging.info(merge_cmd) | |
| 86 proc = subprocess.Popen(merge_cmd) | |
| 87 proc.communicate() | |
| 88 return proc.returncode | |
| 89 finally: | |
| 90 if exception: | |
| 91 raise exception[0], exception[1], exception[2] | |
| 92 | |
| 93 | |
| 94 def test_xvfb(command, rel_dir): | |
| 95 """Calls back ourself if not running inside Xvfb and it's on the command line | |
| 96 to run. | |
| 97 | |
| 98 Otherwise the X session will die while trying to start too many Xvfb | |
| 99 instances. | |
| 100 """ | |
| 101 if os.environ.get('_CHROMIUM_INSIDE_XVFB') == '1': | |
| 102 return | |
| 103 for index, item in enumerate(command): | |
| 104 if item.endswith('xvfb.py'): | |
| 105 # Note this has inside knowledge about src/testing/xvfb.py. | |
| 106 print('Restarting itself under Xvfb') | |
| 107 prefix = command[index:index+2] | |
| 108 prefix[0] = os.path.normpath(os.path.join(rel_dir, prefix[0])) | |
| 109 prefix[1] = os.path.normpath(os.path.join(rel_dir, prefix[1])) | |
| 110 cmd = tools.fix_python_path(prefix + sys.argv) | |
| 111 sys.exit(subprocess.call(cmd)) | |
| 112 | |
| 113 | |
| 114 def safely_load_isolated(parser, options): | |
| 115 """Loads a .isolated.state to extract the executable information. | |
| 116 | |
| 117 Returns the CompleteState instance, the command and the list of test cases. | |
| 118 """ | |
| 119 config = isolate.CompleteState.load_files(options.isolated) | |
| 120 logging.debug( | |
| 121 'root_dir: %s relative_cwd: %s isolated: %s', | |
| 122 config.root_dir, config.saved_state.relative_cwd, options.isolated) | |
| 123 reldir = os.path.join(config.root_dir, config.saved_state.relative_cwd) | |
| 124 command = config.saved_state.command | |
| 125 test_cases = [] | |
| 126 if command: | |
| 127 command = tools.fix_python_path(command) | |
| 128 test_xvfb(command, reldir) | |
| 129 test_cases = parser.process_gtest_options(command, reldir, options) | |
| 130 return config, command, test_cases | |
| 131 | |
| 132 | |
| 133 def main(): | |
| 134 """CLI frontend to validate arguments.""" | |
| 135 tools.disable_buffering() | |
| 136 parser = run_test_cases.OptionParserTestCases( | |
| 137 usage='%prog <options> --isolated <.isolated>') | |
| 138 parser.format_description = lambda *_: parser.description | |
| 139 isolate.add_variable_option(parser) | |
| 140 isolate.add_trace_option(parser) | |
| 141 | |
| 142 # TODO(maruel): Add support for options.timeout. | |
| 143 parser.remove_option('--timeout') | |
| 144 | |
| 145 options, args = parser.parse_args() | |
| 146 if args: | |
| 147 parser.error('Unsupported arg: %s' % args) | |
| 148 isolate.parse_isolated_option(parser, options, os.getcwd(), True) | |
| 149 isolate.parse_variable_option(options) | |
| 150 | |
| 151 try: | |
| 152 config, command, test_cases = safely_load_isolated(parser, options) | |
| 153 if not command: | |
| 154 parser.error('A command must be defined') | |
| 155 if not test_cases: | |
| 156 parser.error('No test case to run with command: %s' % ' '.join(command)) | |
| 157 | |
| 158 config.saved_state.variables.update(options.variables) | |
| 159 return isolate_test_cases( | |
| 160 command, | |
| 161 test_cases, | |
| 162 options.jobs, | |
| 163 config.isolated_filepath, | |
| 164 config.saved_state.isolate_filepath, | |
| 165 config.root_dir, | |
| 166 config.saved_state.relative_cwd, | |
| 167 config.saved_state.variables, | |
| 168 options.trace_blacklist) | |
| 169 except isolate.ExecutionError, e: | |
| 170 print >> sys.stderr, str(e) | |
| 171 return 1 | |
| 172 | |
| 173 | |
| 174 if __name__ == '__main__': | |
| 175 sys.exit(main()) | |
| OLD | NEW |