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