| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import json | 6 import json |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 import subprocess | 10 import subprocess |
| 11 import sys | 11 import sys |
| 12 import tempfile | 12 import tempfile |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | 15 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 ROOT_DIR = os.path.dirname(BASE_DIR) | 16 GOOGLETEST_DIR = os.path.dirname(BASE_DIR) |
| 17 ROOT_DIR = os.path.dirname(GOOGLETEST_DIR) |
| 17 sys.path.insert(0, ROOT_DIR) | 18 sys.path.insert(0, ROOT_DIR) |
| 18 sys.path.insert(0, os.path.join(BASE_DIR, 'gtest_fake')) | 19 sys.path.insert(0, os.path.join(BASE_DIR, 'gtest_fake')) |
| 19 | 20 |
| 20 import trace_inputs | 21 import trace_inputs |
| 21 import gtest_fake_base | 22 import gtest_fake_base |
| 22 | 23 |
| 23 FILE_PATH = os.path.realpath(unicode(os.path.abspath(__file__))) | 24 FILE_PATH = os.path.realpath(unicode(os.path.abspath(__file__))) |
| 24 TARGET_UTIL_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_base.py') | 25 TARGET_UTIL_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_base.py') |
| 25 TARGET_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_fail.py') | 26 TARGET_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_fail.py') |
| 26 | 27 |
| 27 | 28 |
| 28 class TraceTestCases(unittest.TestCase): | 29 class TraceTestCases(unittest.TestCase): |
| 29 def setUp(self): | 30 def setUp(self): |
| 30 self.temp_file = None | 31 self.temp_file = None |
| 31 | 32 |
| 32 self.initial_cwd = ROOT_DIR | 33 self.initial_cwd = GOOGLETEST_DIR |
| 33 if sys.platform == 'win32': | 34 if sys.platform == 'win32': |
| 34 # Windows has no kernel mode concept of current working directory. | 35 # Windows has no kernel mode concept of current working directory. |
| 35 self.initial_cwd = None | 36 self.initial_cwd = None |
| 36 | 37 |
| 37 # There's 2 kinds of references to python, self.executable, | 38 # There's 2 kinds of references to python, self.executable, |
| 38 # self.real_executable. It depends how python was started and on which OS. | 39 # self.real_executable. It depends how python was started and on which OS. |
| 39 self.executable = unicode(sys.executable) | 40 self.executable = unicode(sys.executable) |
| 40 if sys.platform == 'darwin': | 41 if sys.platform == 'darwin': |
| 41 # /usr/bin/python is a thunk executable that decides which version of | 42 # /usr/bin/python is a thunk executable that decides which version of |
| 42 # python gets executed. | 43 # python gets executed. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 63 standardMsg = '%r not greater than %r' % (a, b) | 64 standardMsg = '%r not greater than %r' % (a, b) |
| 64 self.fail(msg or standardMsg) | 65 self.fail(msg or standardMsg) |
| 65 | 66 |
| 66 def test_simple(self): | 67 def test_simple(self): |
| 67 file_handle, self.temp_file = tempfile.mkstemp( | 68 file_handle, self.temp_file = tempfile.mkstemp( |
| 68 prefix='trace_test_cases_test') | 69 prefix='trace_test_cases_test') |
| 69 os.close(file_handle) | 70 os.close(file_handle) |
| 70 | 71 |
| 71 cmd = [ | 72 cmd = [ |
| 72 sys.executable, | 73 sys.executable, |
| 73 os.path.join(ROOT_DIR, 'trace_test_cases.py'), | 74 os.path.join(GOOGLETEST_DIR, 'trace_test_cases.py'), |
| 74 # Forces 4 parallel jobs. | 75 # Forces 4 parallel jobs. |
| 75 '--jobs', '4', | 76 '--jobs', '4', |
| 76 '--out', self.temp_file, | 77 '--out', self.temp_file, |
| 77 ] | 78 ] |
| 78 if VERBOSE: | 79 if VERBOSE: |
| 79 cmd.extend(['-v'] * 3) | 80 cmd.extend(['-v'] * 3) |
| 80 cmd.append(TARGET_PATH) | 81 cmd.append(TARGET_PATH) |
| 81 logging.debug(' '.join(cmd)) | 82 logging.debug(' '.join(cmd)) |
| 82 proc = subprocess.Popen( | 83 proc = subprocess.Popen( |
| 83 cmd, | 84 cmd, |
| 84 stdout=subprocess.PIPE, stderr=subprocess.PIPE, | 85 stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 85 universal_newlines=True, | 86 universal_newlines=True, |
| 86 cwd=ROOT_DIR) | 87 cwd=GOOGLETEST_DIR) |
| 87 out, err = proc.communicate() or ('', '') # pylint is confused. | 88 out, err = proc.communicate() or ('', '') # pylint is confused. |
| 88 self.assertEqual(0, proc.returncode, (out, err)) | 89 self.assertEqual(0, proc.returncode, (out, err)) |
| 89 lines = out.splitlines() | 90 lines = out.splitlines() |
| 90 expected_out_re = [ | 91 expected_out_re = [ |
| 91 r'Tracing\.\.\.', | 92 r'Tracing\.\.\.', |
| 92 r'\[1/4\] +\d+\.\d\ds .+', | 93 r'\[1/4\] +\d+\.\d\ds .+', |
| 93 r'\[2/4\] +\d+\.\d\ds .+', | 94 r'\[2/4\] +\d+\.\d\ds .+', |
| 94 r'\[3/4\] +\d+\.\d\ds .+', | 95 r'\[3/4\] +\d+\.\d\ds .+', |
| 95 r'\[4/4\] +\d+\.\d\ds .+', | 96 r'\[4/4\] +\d+\.\d\ds .+', |
| 96 r'Reading trace logs\.\.\.', | 97 r'Reading trace logs\.\.\.', |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 self.assertEqual(expected_output, output, repr(output)) | 142 self.assertEqual(expected_output, output, repr(output)) |
| 142 | 143 |
| 143 expected_trace = { | 144 expected_trace = { |
| 144 u'root': { | 145 u'root': { |
| 145 u'children': [], | 146 u'children': [], |
| 146 u'command': [ | 147 u'command': [ |
| 147 self.executable, TARGET_PATH, '--gtest_filter=' + test_case, | 148 self.executable, TARGET_PATH, '--gtest_filter=' + test_case, |
| 148 ], | 149 ], |
| 149 u'executable': trace_inputs.get_native_path_case( | 150 u'executable': trace_inputs.get_native_path_case( |
| 150 unicode(self.executable)), | 151 unicode(self.executable)), |
| 151 u'initial_cwd': ROOT_DIR, | 152 u'initial_cwd': GOOGLETEST_DIR, |
| 152 }, | 153 }, |
| 153 } | 154 } |
| 154 if sys.platform == 'win32': | 155 if sys.platform == 'win32': |
| 155 expected_trace['root']['initial_cwd'] = None | 156 expected_trace['root']['initial_cwd'] = None |
| 156 self.assertGreater(actual['trace']['root'].pop('pid'), 1) | 157 self.assertGreater(actual['trace']['root'].pop('pid'), 1) |
| 157 self.assertGreater(len(actual['trace']['root'].pop('files')), 10) | 158 self.assertGreater(len(actual['trace']['root'].pop('files')), 10) |
| 158 self.assertEqual(expected_trace, actual['trace']) | 159 self.assertEqual(expected_trace, actual['trace']) |
| 159 | 160 |
| 160 | 161 |
| 161 if __name__ == '__main__': | 162 if __name__ == '__main__': |
| 162 VERBOSE = '-v' in sys.argv | 163 VERBOSE = '-v' in sys.argv |
| 163 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | 164 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
| 164 # Necessary for the dtrace logger to work around execve() hook. See | 165 # Necessary for the dtrace logger to work around execve() hook. See |
| 165 # trace_inputs.py for more details. | 166 # trace_inputs.py for more details. |
| 166 os.environ['TRACE_INPUTS_DTRACE_ENABLE_EXECVE'] = '1' | 167 os.environ['TRACE_INPUTS_DTRACE_ENABLE_EXECVE'] = '1' |
| 167 unittest.main() | 168 unittest.main() |
| OLD | NEW |