| 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 import json | |
| 7 import logging | |
| 8 import os | |
| 9 import re | |
| 10 import subprocess | |
| 11 import sys | |
| 12 import tempfile | |
| 13 import unittest | |
| 14 | |
| 15 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 16 ROOT_DIR = os.path.dirname(BASE_DIR) | |
| 17 sys.path.insert(0, ROOT_DIR) | |
| 18 sys.path.insert(0, os.path.join(BASE_DIR, 'gtest_fake')) | |
| 19 | |
| 20 import trace_inputs | |
| 21 import gtest_fake_base | |
| 22 | |
| 23 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_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_fail.py') | |
| 26 | |
| 27 | |
| 28 class TraceTestCases(unittest.TestCase): | |
| 29 def setUp(self): | |
| 30 self.temp_file = None | |
| 31 | |
| 32 self.initial_cwd = ROOT_DIR | |
| 33 if sys.platform == 'win32': | |
| 34 # Windows has no kernel mode concept of current working directory. | |
| 35 self.initial_cwd = None | |
| 36 | |
| 37 # 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.executable = unicode(sys.executable) | |
| 40 if sys.platform == 'darwin': | |
| 41 # /usr/bin/python is a thunk executable that decides which version of | |
| 42 # python gets executed. | |
| 43 suffix = '.'.join(map(str, sys.version_info[0:2])) | |
| 44 if os.access(self.executable + suffix, os.X_OK): | |
| 45 # So it'll look like /usr/bin/python2.7 | |
| 46 self.executable += suffix | |
| 47 | |
| 48 self.real_executable = trace_inputs.get_native_path_case(self.executable) | |
| 49 # Make sure there's no environment variable that could do side effects. | |
| 50 os.environ.pop('GTEST_SHARD_INDEX', '') | |
| 51 os.environ.pop('GTEST_TOTAL_SHARDS', '') | |
| 52 | |
| 53 def tearDown(self): | |
| 54 if self.temp_file: | |
| 55 os.remove(self.temp_file) | |
| 56 | |
| 57 def assertGreater(self, a, b, msg=None): | |
| 58 """Just like self.assertTrue(a > b), but with a nicer default message. | |
| 59 | |
| 60 Added to support python 2.6. | |
| 61 """ | |
| 62 if not a > b: | |
| 63 standardMsg = '%r not greater than %r' % (a, b) | |
| 64 self.fail(msg or standardMsg) | |
| 65 | |
| 66 def test_simple(self): | |
| 67 file_handle, self.temp_file = tempfile.mkstemp( | |
| 68 prefix='trace_test_cases_test') | |
| 69 os.close(file_handle) | |
| 70 | |
| 71 cmd = [ | |
| 72 sys.executable, | |
| 73 os.path.join(ROOT_DIR, 'trace_test_cases.py'), | |
| 74 # Forces 4 parallel jobs. | |
| 75 '--jobs', '4', | |
| 76 '--out', self.temp_file, | |
| 77 ] | |
| 78 if VERBOSE: | |
| 79 cmd.extend(['-v'] * 3) | |
| 80 cmd.append(TARGET_PATH) | |
| 81 logging.debug(' '.join(cmd)) | |
| 82 proc = subprocess.Popen( | |
| 83 cmd, | |
| 84 stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
| 85 universal_newlines=True, | |
| 86 cwd=ROOT_DIR) | |
| 87 out, err = proc.communicate() or ('', '') # pylint is confused. | |
| 88 self.assertEqual(0, proc.returncode, (out, err)) | |
| 89 lines = out.splitlines() | |
| 90 expected_out_re = [ | |
| 91 r'Tracing\.\.\.', | |
| 92 r'\[1/4\] +\d+\.\d\ds .+', | |
| 93 r'\[2/4\] +\d+\.\d\ds .+', | |
| 94 r'\[3/4\] +\d+\.\d\ds .+', | |
| 95 r'\[4/4\] +\d+\.\d\ds .+', | |
| 96 r'Reading trace logs\.\.\.', | |
| 97 ] | |
| 98 self.assertEqual(len(expected_out_re), len(lines), lines) | |
| 99 for index in range(len(expected_out_re)): | |
| 100 self.assertTrue( | |
| 101 re.match('^%s$' % expected_out_re[index], lines[index]), | |
| 102 '%d: %s\n%r\n%s' % ( | |
| 103 index, expected_out_re[index], lines[index], out)) | |
| 104 # Junk is printed on win32. | |
| 105 if sys.platform != 'win32' and not VERBOSE: | |
| 106 self.assertEqual('', err) | |
| 107 | |
| 108 with open(self.temp_file, 'r') as f: | |
| 109 content = f.read() | |
| 110 try: | |
| 111 result = json.loads(content) | |
| 112 except: | |
| 113 print repr(content) | |
| 114 raise | |
| 115 | |
| 116 test_cases = { | |
| 117 'Baz.Fail': 1, | |
| 118 'Foo.Bar1': 0, | |
| 119 'Foo.Bar2': 0, | |
| 120 'Foo.Bar3': 0, | |
| 121 } | |
| 122 self.assertEqual(dict, result.__class__) | |
| 123 self.assertEqual(sorted(test_cases), sorted(result)) | |
| 124 for index, test_case in enumerate(sorted(result)): | |
| 125 actual = result[test_case] | |
| 126 self.assertEqual( | |
| 127 [u'duration', u'output', u'returncode', u'trace'], sorted(actual)) | |
| 128 self.assertGreater(actual['duration'], 0.0000001) | |
| 129 self.assertEqual(test_cases[test_case], actual['returncode']) | |
| 130 expected_output = ( | |
| 131 'Note: Google Test filter = %s\n' % test_case + | |
| 132 '\n' + | |
| 133 gtest_fake_base.get_test_output(test_case, 'Fail' in test_case) + | |
| 134 '\n' + | |
| 135 gtest_fake_base.get_footer(1, 1) + | |
| 136 '\n') | |
| 137 # On Windows, actual['output'] is unprocessed so it will contain CRLF. | |
| 138 output = actual['output'] | |
| 139 if sys.platform == 'win32': | |
| 140 output = output.replace('\r\n', '\n') | |
| 141 self.assertEqual(expected_output, output, repr(output)) | |
| 142 | |
| 143 expected_trace = { | |
| 144 u'root': { | |
| 145 u'children': [], | |
| 146 u'command': [ | |
| 147 self.executable, TARGET_PATH, '--gtest_filter=' + test_case, | |
| 148 ], | |
| 149 u'executable': trace_inputs.get_native_path_case( | |
| 150 unicode(self.executable)), | |
| 151 u'initial_cwd': ROOT_DIR, | |
| 152 }, | |
| 153 } | |
| 154 if sys.platform == 'win32': | |
| 155 expected_trace['root']['initial_cwd'] = None | |
| 156 self.assertGreater(actual['trace']['root'].pop('pid'), 1) | |
| 157 self.assertGreater(len(actual['trace']['root'].pop('files')), 10) | |
| 158 self.assertEqual(expected_trace, actual['trace']) | |
| 159 | |
| 160 | |
| 161 if __name__ == '__main__': | |
| 162 VERBOSE = '-v' in sys.argv | |
| 163 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | |
| 164 # Necessary for the dtrace logger to work around execve() hook. See | |
| 165 # trace_inputs.py for more details. | |
| 166 os.environ['TRACE_INPUTS_DTRACE_ENABLE_EXECVE'] = '1' | |
| 167 unittest.main() | |
| OLD | NEW |