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 hashlib | |
7 import json | |
8 import logging | |
9 import os | |
10 import re | |
11 import shutil | |
12 import subprocess | |
13 import sys | |
14 import tempfile | |
15 import unittest | |
16 | |
17 GOOGLETEST_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
18 ROOT_DIR = os.path.dirname(GOOGLETEST_DIR) | |
19 | |
20 sys.path.insert(0, GOOGLETEST_DIR) | |
21 sys.path.insert(0, ROOT_DIR) | |
22 | |
23 import isolate | |
24 import trace_test_cases | |
25 from utils import file_path | |
26 | |
27 | |
28 class IsolateTestCases(unittest.TestCase): | |
29 def setUp(self): | |
30 self.tempdir = 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 = file_path.get_native_path_case( | |
49 self.executable) | |
50 # Make sure there's no environment variable that could cause side effects. | |
51 os.environ.pop('GTEST_SHARD_INDEX', '') | |
52 os.environ.pop('GTEST_TOTAL_SHARDS', '') | |
53 | |
54 def tearDown(self): | |
55 if self.tempdir: | |
56 if VERBOSE: | |
57 # If -v is used, this means the user wants to do further analisys on | |
58 # the data. | |
59 print('Leaking %s' % self.tempdir) | |
60 else: | |
61 shutil.rmtree(self.tempdir) | |
62 | |
63 def _copy(self, root, *relpath): | |
64 relpath = os.path.join(*relpath) | |
65 shutil.copy( | |
66 os.path.join(root, relpath), | |
67 os.path.join(self.tempdir, relpath)) | |
68 | |
69 def test_simple(self): | |
70 # Create a directory and re-use tests/gtest_fake/gtest_fake_pass.isolate. | |
71 # Warning: we need to copy the files around, since the original .isolate | |
72 # file is modified. | |
73 gtest_fake_base_py = os.path.join( | |
74 'tests', 'gtest_fake', 'gtest_fake_base.py') | |
75 gtest_fake_pass_py = os.path.join( | |
76 'tests', 'gtest_fake', 'gtest_fake_pass.py') | |
77 gtest_fake_pass_isolate = os.path.join( | |
78 'tests', 'isolate_test_cases', 'gtest_fake_pass.isolate') | |
79 | |
80 self.tempdir = tempfile.mkdtemp(prefix='isolate_test_cases_test') | |
81 os.mkdir(os.path.join(self.tempdir, 'isolated')) | |
82 os.mkdir(os.path.join(self.tempdir, 'tests')) | |
83 os.mkdir(os.path.join(self.tempdir, 'tests', 'gtest_fake')) | |
84 os.mkdir(os.path.join(self.tempdir, 'tests', 'isolate_test_cases')) | |
85 self._copy(ROOT_DIR, 'isolate.py') | |
86 self._copy(GOOGLETEST_DIR, gtest_fake_base_py) | |
87 self._copy(GOOGLETEST_DIR, gtest_fake_pass_isolate) | |
88 self._copy(GOOGLETEST_DIR, gtest_fake_pass_py) | |
89 | |
90 basename = os.path.join(self.tempdir, 'isolated', 'gtest_fake_pass') | |
91 isolated = basename + '.isolated' | |
92 | |
93 # Create a proper .isolated file. | |
94 cmd = [ | |
95 sys.executable, 'isolate.py', | |
96 'check', | |
97 '--variable', 'FLAG', 'run', | |
98 '--isolate', os.path.join(self.tempdir, gtest_fake_pass_isolate), | |
99 '--isolated', isolated, | |
100 ] | |
101 if VERBOSE: | |
102 cmd.extend(['-v'] * 3) | |
103 subprocess.check_call(cmd, cwd=ROOT_DIR) | |
104 | |
105 # Assert the content of the .isolated file. | |
106 with open(isolated) as f: | |
107 actual_isolated = json.load(f) | |
108 root_dir_gtest_fake_pass_py = os.path.join( | |
109 GOOGLETEST_DIR, gtest_fake_pass_py) | |
110 rel_gtest_fake_pass_py = os.path.join(u'gtest_fake', 'gtest_fake_pass.py') | |
111 expected_isolated = { | |
112 u'algo': u'sha-1', | |
113 u'command': [u'../gtest_fake/gtest_fake_pass.py'], | |
114 u'files': { | |
115 rel_gtest_fake_pass_py: { | |
116 u'm': 488, | |
117 u'h': unicode(hashlib.sha1( | |
118 open(root_dir_gtest_fake_pass_py, 'rb').read()).hexdigest()), | |
119 u's': os.stat(root_dir_gtest_fake_pass_py).st_size, | |
120 }, | |
121 }, | |
122 u'os': unicode(isolate.get_flavor()), | |
123 u'relative_cwd': u'isolate_test_cases', | |
124 u'version': u'1.0', | |
125 } | |
126 if sys.platform == 'win32': | |
127 expected_isolated['files'][rel_gtest_fake_pass_py].pop('m') | |
128 self.assertEqual(expected_isolated, actual_isolated) | |
129 | |
130 cmd = [ | |
131 sys.executable, | |
132 os.path.join(GOOGLETEST_DIR, 'isolate_test_cases.py'), | |
133 # Forces 4 parallel jobs. | |
134 '--jobs', '4', | |
135 '--isolated', isolated, | |
136 ] | |
137 if VERBOSE: | |
138 cmd.extend(['-v'] * 3) | |
139 logging.debug(' '.join(cmd)) | |
140 proc = subprocess.Popen( | |
141 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
142 out, err = proc.communicate() or ('', '') # pylint is confused. | |
143 logging.info(err) | |
144 self.assertEqual(0, proc.returncode, (out, err)) | |
145 lines = out.splitlines() | |
146 expected_out_re = [ | |
147 r'\[0/3\] \d\.\d\ds ', | |
148 r'\[1/3\] \d+\.\d\ds .+', | |
149 r'\[2/3\] \d+\.\d\ds .+', | |
150 r'\[3/3\] \d+\.\d\ds .+', | |
151 ] | |
152 self.assertEqual(len(expected_out_re), len(lines), (out, err)) | |
153 for index in range(len(expected_out_re)): | |
154 self.assertTrue( | |
155 re.match('^%s$' % expected_out_re[index], lines[index]), | |
156 '%d\n%r\n%r\n%r' % ( | |
157 index, expected_out_re[index], lines[index], out)) | |
158 # Junk is printed on win32. | |
159 if sys.platform != 'win32' and not VERBOSE: | |
160 self.assertEqual('', err) | |
161 | |
162 test_cases = ( | |
163 'Foo.Bar1', | |
164 'Foo.Bar2', | |
165 'Foo.Bar/3', | |
166 ) | |
167 expected = { | |
168 'conditions': [ | |
169 ['OS=="%s"' % isolate.get_flavor(), { | |
170 'variables': { | |
171 'isolate_dependency_untracked': [ | |
172 '../gtest_fake/', | |
173 ], | |
174 }, | |
175 }], | |
176 ], | |
177 } | |
178 for test_case in test_cases: | |
179 tracename = trace_test_cases.sanitize_test_case_name(test_case) | |
180 with open(basename + '.' + tracename + '.isolate', 'r') as f: | |
181 result = eval(f.read(), {'__builtins__': None}, None) | |
182 self.assertEqual(expected, result) | |
183 | |
184 # Now verify the .isolate file was updated! (That's the magical part where | |
185 # you say wow!) | |
186 with open(os.path.join(self.tempdir, gtest_fake_pass_isolate)) as f: | |
187 actual = eval(f.read(), {'__builtins__': None}, None) | |
188 all_oses = set(['linux', 'mac', 'win']) | |
189 host_os = isolate.get_flavor() | |
190 other_oses = all_oses - set([host_os]) | |
191 expected = { | |
192 'conditions': sorted([ | |
193 ['OS=="%s"' % isolate.get_flavor(), { | |
194 'variables': { | |
195 'isolate_dependency_untracked': [ | |
196 '../gtest_fake/', | |
197 ], | |
198 }, | |
199 }], | |
200 [' or '.join('OS=="%s"' % os for os in sorted(other_oses)), { | |
201 'variables': { | |
202 'isolate_dependency_tracked': [ | |
203 '../gtest_fake/gtest_fake_pass.py', | |
204 ], | |
205 }, | |
206 }], | |
207 [' or '.join('OS=="%s"' % os for os in sorted(all_oses)), { | |
208 'variables': { | |
209 'command': ['../gtest_fake/gtest_fake_pass.py'], | |
210 }, | |
211 }], | |
212 ]), | |
213 } | |
214 self.assertEqual(expected, actual) | |
215 | |
216 | |
217 if __name__ == '__main__': | |
218 VERBOSE = '-v' in sys.argv | |
219 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | |
220 unittest.main() | |
OLD | NEW |