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 shutil | |
11 import subprocess | |
12 import sys | |
13 import tempfile | |
14 import unittest | |
15 | |
16 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
17 GOOGLETEST_DIR = os.path.dirname(BASE_DIR) | |
18 ROOT_DIR = os.path.dirname(GOOGLETEST_DIR) | |
19 | |
20 sys.path.insert(0, ROOT_DIR) | |
21 | |
22 import isolateserver | |
23 import run_isolated | |
24 | |
25 | |
26 class FixTestCases(unittest.TestCase): | |
27 def setUp(self): | |
28 self.tempdir = tempfile.mkdtemp(prefix='fix_test_case') | |
29 self.srcdir = os.path.join(self.tempdir, 'srcdir') | |
30 os.mkdir(self.srcdir) | |
31 | |
32 def tearDown(self): | |
33 if self.tempdir: | |
34 if VERBOSE: | |
35 # If -v is used, this means the user wants to do further analysis on | |
36 # the data. | |
37 print('Leaking %s' % self.tempdir) | |
38 else: | |
39 shutil.rmtree(self.tempdir) | |
40 | |
41 def _run(self, cmd): | |
42 if VERBOSE: | |
43 cmd = cmd + ['--verbose'] * 3 | |
44 logging.info(cmd) | |
45 proc = subprocess.Popen( | |
46 [sys.executable] + cmd, | |
47 cwd=self.srcdir, | |
48 stdout=subprocess.PIPE, | |
49 stderr=subprocess.STDOUT) | |
50 out = proc.communicate()[0] | |
51 if VERBOSE: | |
52 print '\n-----' | |
53 print out.strip() | |
54 print '-----\n' | |
55 self.assertEqual(0, proc.returncode) | |
56 return out | |
57 | |
58 def test_simple(self): | |
59 # Create a directory with nothing in it and progressively add more stuff. | |
60 isolate = os.path.join(self.srcdir, 'gtest_fake_pass.isolate') | |
61 chromeos_value = int(run_isolated.get_flavor() == 'linux') | |
62 condition = 'OS=="%s" and chromeos==%d' % (run_isolated.get_flavor(), | |
63 chromeos_value) | |
64 with open(isolate, 'w') as f: | |
65 # Write a minimal .isolate file. | |
66 f.write(str({ | |
67 'conditions': [ | |
68 [condition, { | |
69 'variables': { | |
70 'command': [ | |
71 'run_test_cases.py', 'gtest_fake_pass.py', | |
72 ], | |
73 }, | |
74 }], | |
75 ], | |
76 })) | |
77 def _copy(filename): | |
78 shutil.copy( | |
79 os.path.join(BASE_DIR, 'gtest_fake', filename), | |
80 os.path.join(self.srcdir, filename)) | |
81 _copy('gtest_fake_base.py') | |
82 _copy('gtest_fake_pass.py') | |
83 shutil.copy( | |
84 os.path.join(GOOGLETEST_DIR, 'run_test_cases.py'), | |
85 os.path.join(self.srcdir, 'run_test_cases.py')) | |
86 # Deploy run_isolated with dependencies as zip into srcdir. | |
87 run_isolated.get_as_zip_package(executable=False).zip_into_file( | |
88 os.path.join(self.srcdir, 'run_isolated.zip')) | |
89 | |
90 logging.debug('1. Create a .isolated file out of the .isolate file.') | |
91 isolated = os.path.join(self.srcdir, 'gtest_fake_pass.isolated') | |
92 out = self._run( | |
93 [ | |
94 os.path.join(ROOT_DIR, 'isolate.py'), | |
95 'check', '-i', isolate, '-s', isolated, | |
96 '-V', 'chromeos', str(chromeos_value), | |
97 ]) | |
98 if not VERBOSE: | |
99 self.assertEqual('', out) | |
100 | |
101 logging.debug('2. Run fix_test_cases.py on it.') | |
102 # Give up on looking at stdout. | |
103 cmd = [ | |
104 os.path.join(GOOGLETEST_DIR, 'fix_test_cases.py'), | |
105 '-s', isolated, | |
106 '--trace-blacklist', '.*\\.run_test_cases', | |
107 ] | |
108 _ = self._run(cmd) | |
109 | |
110 logging.debug('3. Asserting the content of the .isolated file.') | |
111 with open(isolated) as f: | |
112 actual_isolated = json.load(f) | |
113 gtest_fake_base_py = os.path.join(self.srcdir, 'gtest_fake_base.py') | |
114 gtest_fake_pass_py = os.path.join(self.srcdir, 'gtest_fake_pass.py') | |
115 run_isolated_zip = os.path.join(self.srcdir, 'run_isolated.zip') | |
116 run_test_cases_py = os.path.join(self.srcdir, 'run_test_cases.py') | |
117 algo = hashlib.sha1 | |
118 expected_isolated = { | |
119 u'algo': u'sha-1', | |
120 u'command': [u'run_test_cases.py', u'gtest_fake_pass.py'], | |
121 u'files': { | |
122 u'gtest_fake_base.py': { | |
123 u'm': 416, | |
124 u'h': unicode(isolateserver.hash_file(gtest_fake_base_py, algo)), | |
125 u's': os.stat(gtest_fake_base_py).st_size, | |
126 }, | |
127 u'gtest_fake_pass.py': { | |
128 u'm': 488, | |
129 u'h': unicode(isolateserver.hash_file(gtest_fake_pass_py, algo)), | |
130 u's': os.stat(gtest_fake_pass_py).st_size, | |
131 }, | |
132 u'run_isolated.zip': { | |
133 u'm': 416, | |
134 u'h': unicode(isolateserver.hash_file(run_isolated_zip, algo)), | |
135 u's': os.stat(run_isolated_zip).st_size, | |
136 }, | |
137 u'run_test_cases.py': { | |
138 u'm': 488, | |
139 u'h': unicode(isolateserver.hash_file(run_test_cases_py, algo)), | |
140 u's': os.stat(run_test_cases_py).st_size, | |
141 }, | |
142 }, | |
143 u'os': unicode(run_isolated.get_flavor()), | |
144 u'relative_cwd': u'.', | |
145 u'version': u'1.0', | |
146 } | |
147 if sys.platform == 'win32': | |
148 for value in expected_isolated['files'].itervalues(): | |
149 self.assertTrue(value.pop('m')) | |
150 self.assertEqual(expected_isolated, actual_isolated) | |
151 | |
152 # Now verify the .isolate file was updated! (That's the magical part where | |
153 # you say wow!) | |
154 with open(isolate) as f: | |
155 actual = eval(f.read(), {'__builtins__': None}, None) | |
156 expected = { | |
157 'conditions': [ | |
158 [condition, { | |
159 'variables': { | |
160 'command': [ | |
161 'run_test_cases.py', 'gtest_fake_pass.py' | |
162 ], | |
163 'isolate_dependency_tracked': [ | |
164 'gtest_fake_base.py', | |
165 'gtest_fake_pass.py', | |
166 'run_isolated.zip', | |
167 'run_test_cases.py', | |
168 ], | |
169 }, | |
170 }], | |
171 ], | |
172 } | |
173 self.assertEqual(expected, actual) | |
174 | |
175 | |
176 if __name__ == '__main__': | |
177 VERBOSE = '-v' in sys.argv | |
178 if VERBOSE: | |
179 unittest.TestCase.maxDiff = None | |
180 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | |
181 unittest.main() | |
OLD | NEW |