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