OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2015 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 codecs | |
7 import copy | |
8 import json | |
9 import os | |
10 import sys | |
11 import unittest | |
12 | |
13 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
14 RESOURCE_DIR = os.path.join( | |
15 ROOT_DIR, 'recipe_modules', 'bot_update', 'resources') | |
16 | |
17 sys.path.insert(0, RESOURCE_DIR) | |
18 sys.platform = 'linux2' # For consistency, ya know? | |
19 import bot_update | |
20 | |
21 DEFAULT_PARAMS = { | |
22 'solutions': [{ | |
23 'name': 'somename', | |
24 'url': 'https://fake.com' | |
25 }], | |
26 'revisions': [], | |
27 'first_sln': 'somename', | |
28 'target_os': None, | |
29 'target_os_only': None, | |
30 'patch_root': None, | |
31 'issue': None, | |
32 'patchset': None, | |
33 'patch_url': None, | |
34 'rietveld_server': None, | |
35 'gerrit_ref': None, | |
36 'gerrit_repo': None, | |
37 'revision_mapping': {}, | |
38 'apply_issue_email_file': None, | |
39 'apply_issue_key_file': None, | |
40 'buildspec': False, | |
41 'gyp_env': None, | |
42 'shallow': False, | |
43 'runhooks': False, | |
44 'refs': [], | |
45 'git_cache_dir': 'fake_cache_dir' | |
46 } | |
47 | |
48 | |
49 class MockedPopen(object): | |
50 """A fake instance of a called subprocess. | |
51 | |
52 This is meant to be used in conjunction with MockedCall. | |
53 """ | |
54 def __init__(self, args=None, kwargs=None): | |
55 self.args = args or [] | |
56 self.kwargs = kwargs or {} | |
57 self.return_value = None | |
58 self.fails = False | |
59 | |
60 def returns(self, rv): | |
61 """Set the return value when this popen is called. | |
62 | |
63 rv can be a string, or a callable (eg function). | |
64 """ | |
65 self.return_value = rv | |
66 return self | |
67 | |
68 def check(self, args, kwargs): | |
69 """Check to see if the given args/kwargs call match this instance. | |
70 | |
71 This does a partial match, so that a call to "git clone foo" will match | |
72 this instance if this instance was recorded as "git clone" | |
73 """ | |
74 if any(input_arg != expected_arg | |
75 for (input_arg, expected_arg) in zip(args, self.args)): | |
76 return False | |
77 return self.return_value | |
78 | |
79 def __call__(self, args, kwargs): | |
80 """Actually call this popen instance.""" | |
81 if hasattr(self.return_value, '__call__'): | |
82 return self.return_value(*args, **kwargs) | |
83 return self.return_value | |
84 | |
85 | |
86 class MockedCall(object): | |
87 """A fake instance of bot_update.call(). | |
88 | |
89 This object is pre-seeded with "answers" in self.expectations. The type | |
90 is a MockedPopen object, or any object with a __call__() and check() method. | |
91 The check() method is used to check to see if the correct popen object is | |
92 chosen (can be a partial match, eg a "git clone" popen module would match | |
93 a "git clone foo" call). | |
94 By default, if no answers have been pre-seeded, the call() returns successful | |
95 with an empty string. | |
96 """ | |
97 def __init__(self, fake_filesystem): | |
98 self.expectations = [] | |
99 self.records = [] | |
100 | |
101 def expect(self, args=None, kwargs=None): | |
102 args = args or [] | |
103 kwargs = kwargs or {} | |
104 popen = MockedPopen(args, kwargs) | |
105 self.expectations.append(popen) | |
106 return popen | |
107 | |
108 def __call__(self, *args, **kwargs): | |
109 self.records.append((args, kwargs)) | |
110 for popen in self.expectations: | |
111 if popen.check(args, kwargs): | |
112 self.expectations.remove(popen) | |
113 return popen(args, kwargs) | |
114 return '' | |
115 | |
116 | |
117 class MockedGclientSync(): | |
118 """A class producing a callable instance of gclient sync. | |
119 | |
120 Because for bot_update, gclient sync also emits an output json file, we need | |
121 a callable object that can understand where the output json file is going, and | |
122 emit a (albite) fake file for bot_update to consume. | |
123 """ | |
124 def __init__(self, fake_filesystem): | |
125 self.output = {} | |
126 self.fake_filesystem = fake_filesystem | |
127 | |
128 def __call__(self, *args, **_): | |
129 output_json_index = args.index('--output-json') + 1 | |
130 with self.fake_filesystem.open(args[output_json_index], 'w') as f: | |
131 json.dump(self.output, f) | |
132 | |
133 | |
134 class FakeFile(): | |
135 def __init__(self): | |
136 self.contents = '' | |
137 | |
138 def write(self, buf): | |
139 self.contents += buf | |
140 | |
141 def read(self): | |
142 return self.contents | |
143 | |
144 def __enter__(self): | |
145 return self | |
146 | |
147 def __exit__(self, _, __, ___): | |
148 pass | |
149 | |
150 | |
151 class FakeFilesystem(): | |
152 def __init__(self): | |
153 self.files = {} | |
154 | |
155 def open(self, target, mode='r', encoding=None): | |
156 if 'w' in mode: | |
157 self.files[target] = FakeFile() | |
158 return self.files[target] | |
159 return self.files[target] | |
160 | |
161 | |
162 def fake_git(*args, **kwargs): | |
163 return bot_update.call('git', *args, **kwargs) | |
164 | |
165 | |
166 class EnsureCheckoutUnittests(unittest.TestCase): | |
167 def setUp(self): | |
168 self.filesystem = FakeFilesystem() | |
169 self.call = MockedCall(self.filesystem) | |
170 self.gclient = MockedGclientSync(self.filesystem) | |
171 self.call.expect(('gclient', 'sync')).returns(self.gclient) | |
172 self.old_call = getattr(bot_update, 'call') | |
173 self.params = copy.deepcopy(DEFAULT_PARAMS) | |
174 setattr(bot_update, 'call', self.call) | |
175 setattr(bot_update, 'git', fake_git) | |
176 | |
177 self.old_os_cwd = os.getcwd | |
178 setattr(os, 'getcwd', lambda: '/b/build/slave/foo/build') | |
179 | |
180 setattr(bot_update, 'open', self.filesystem.open) | |
181 self.old_codecs_open = codecs.open | |
182 setattr(codecs, 'open', self.filesystem.open) | |
183 | |
184 def tearDown(self): | |
185 setattr(bot_update, 'call', self.old_call) | |
186 setattr(os, 'getcwd', self.old_os_cwd) | |
187 delattr(bot_update, 'open') | |
188 setattr(codecs, 'open', self.old_codecs_open) | |
189 | |
190 def testBasic(self): | |
191 bot_update.ensure_checkout(**self.params) | |
192 return self.call.records | |
193 | |
194 def testBasicBuildspec(self): | |
195 self.params['buildspec'] = bot_update.BUILDSPEC_TYPE( | |
196 container='branches', | |
197 version='1.1.1.1' | |
198 ) | |
199 bot_update.ensure_checkout(**self.params) | |
200 return self.call.records | |
201 | |
202 def testBasicShallow(self): | |
203 self.params['shallow'] = True | |
204 bot_update.ensure_checkout(**self.params) | |
205 return self.call.records | |
206 | |
207 def testBasicSVNPatch(self): | |
208 self.params['patch_url'] = 'svn://server.com/patch.diff' | |
209 self.params['patch_root'] = 'somename' | |
210 bot_update.ensure_checkout(**self.params) | |
211 return self.call.records | |
212 | |
213 def testBasicRietveldPatch(self): | |
214 self.params['issue'] = '12345' | |
215 self.params['patchset'] = '1' | |
216 self.params['rietveld_server'] = 'https://rietveld.com' | |
217 self.params['patch_root'] = 'somename' | |
218 bot_update.ensure_checkout(**self.params) | |
219 return self.call.records | |
220 | |
221 | |
222 class SolutionsUnittests(unittest.TestCase): | |
223 def testBasicSVN(self): | |
224 sln = [{ | |
225 'name': 'src', | |
226 'url': 'svn://svn-mirror.golo.chromium.org/chrome/trunk/src' | |
227 }, { | |
228 'name': 'git', | |
229 'url': 'https://abc.googlesource.com/foo.git' | |
230 }] | |
231 git_slns, root, buildspec = bot_update.solutions_to_git(sln) | |
232 self.assertEquals(root, '/chrome/trunk/src') | |
233 self.assertEquals(git_slns, [{ | |
234 'deps_file': '.DEPS.git', | |
235 'managed': False, | |
236 'name': 'src', | |
237 'url': 'https://chromium.googlesource.com/chromium/src.git' | |
238 }, { | |
239 'url': 'https://abc.googlesource.com/foo.git', | |
240 'managed': False, | |
241 'name': 'git' | |
242 }]) | |
243 self.assertFalse(buildspec) | |
244 bot_update.solutions_printer(git_slns) | |
245 | |
246 def testBasicBuildspec(self): | |
247 sln = [{ | |
248 'name': 'buildspec', | |
249 'url': ('svn://svn.chromium.org/chrome-internal/' | |
250 'trunk/tools/buildspec/releases/1234'), | |
251 }] | |
252 git_slns, root, buildspec = bot_update.solutions_to_git(sln) | |
253 self.assertEquals( | |
254 buildspec, | |
255 bot_update.BUILDSPEC_TYPE(container='releases', version='1234')) | |
256 self.assertEquals( | |
257 root, '/chrome-internal/trunk/tools/buildspec/releases/1234') | |
258 self.assertEquals(git_slns[0]['deps_file'], 'releases/1234/DEPS') | |
259 | |
260 if __name__ == '__main__': | |
261 unittest.main() | |
OLD | NEW |