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