OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2011 Google Inc. | 2 # Copyright 2011 Google Inc. |
3 # | 3 # |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
7 # | 7 # |
8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
9 # | 9 # |
10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
11 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
14 # limitations under the License. | 14 # limitations under the License. |
15 | 15 |
16 """Tests for buildbucket client.""" | 16 """Tests for buildbucket client.""" |
17 | 17 |
18 import json | 18 import json |
19 import logging | 19 import logging |
20 import unittest | 20 import unittest |
21 | 21 |
22 import setup | 22 import setup |
23 setup.process_args() | 23 setup.process_args() |
24 | 24 |
25 | 25 |
26 from google.appengine.api import app_identity | 26 from google.appengine.api import app_identity |
27 from google.appengine.api import users | |
27 from google.appengine.ext import ndb | 28 from google.appengine.ext import ndb |
28 | 29 |
29 from utils import TestCase | 30 from utils import TestCase |
30 | 31 |
31 from codereview import buildbucket | 32 from codereview import buildbucket |
32 from codereview import common | 33 from codereview import common |
33 from codereview import models | 34 from codereview import models |
34 from codereview import net | 35 from codereview import net |
35 | 36 |
36 | 37 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 self.requests = [] | 170 self.requests = [] |
170 def json_request_async(*args, **kwargs): | 171 def json_request_async(*args, **kwargs): |
171 self.requests.append((args, kwargs)) | 172 self.requests.append((args, kwargs)) |
172 future = ndb.Future() | 173 future = ndb.Future() |
173 future.set_result(self.fake_responses.pop(0)) | 174 future.set_result(self.fake_responses.pop(0)) |
174 return future | 175 return future |
175 | 176 |
176 self.mock(net, 'json_request_async', json_request_async) | 177 self.mock(net, 'json_request_async', json_request_async) |
177 self.mock(common, 'get_preferred_domain', | 178 self.mock(common, 'get_preferred_domain', |
178 lambda *_, **__: 'codereview.chromium.org') | 179 lambda *_, **__: 'codereview.chromium.org') |
179 | |
180 def test_get_try_job_results_for_patchset(self): | |
181 models.Account.current_user_account = models.Account( | 180 models.Account.current_user_account = models.Account( |
182 email='johndoe@chromium.org') | 181 email='johndoe@chromium.org') |
183 | 182 |
183 | |
184 def test_get_try_job_results_for_patchset(self): | |
184 put_builds_response = { | 185 put_builds_response = { |
185 'builds': [ | 186 'builds': [ |
186 {'id': '1', 'status': 'SCHEDULED'}, | 187 {'id': '1', 'status': 'SCHEDULED'}, |
187 {'id': '2', 'status': 'SCHEDULED'}, | 188 {'id': '2', 'status': 'SCHEDULED'}, |
188 ], | 189 ], |
189 } | 190 } |
190 self.fake_responses = [ | 191 self.fake_responses = [ |
191 {'delegation_token': 'deltok', 'validity_duration': 1000}, | 192 {'delegation_token': 'deltok', 'validity_duration': 1000}, |
192 put_builds_response, | 193 put_builds_response, |
193 ] | 194 ] |
194 actual_builds = buildbucket.get_builds_for_patchset_async( | 195 actual_builds = buildbucket.get_builds_for_patchset_async( |
195 'project', 1, 2).get_result() | 196 'project', 1, 2).get_result() |
196 self.assertEqual(actual_builds, put_builds_response['builds']) | 197 self.assertEqual(actual_builds, put_builds_response['builds']) |
197 | 198 |
198 mint_token_req_body = self.requests[0][1]['payload'] | 199 mint_token_req_body = self.requests[0][1]['payload'] |
199 self.assertEqual(mint_token_req_body, { | 200 self.assertEqual(mint_token_req_body, { |
200 'audience': ['user:test@localhost'], | 201 'audience': ['user:test@localhost'], |
201 'services': ['service:cr-buildbucket-test'], | 202 'services': ['service:cr-buildbucket-test'], |
202 'impersonate': 'user:johndoe@chromium.org', | 203 'impersonate': 'user:johndoe@chromium.org', |
203 }) | 204 }) |
204 put_builds_req_headers = self.requests[1][1]['headers'] | 205 put_builds_req_headers = self.requests[1][1]['headers'] |
205 self.assertEqual( | 206 self.assertEqual( |
206 put_builds_req_headers['X-Delegation-Token-V1'], | 207 put_builds_req_headers['X-Delegation-Token-V1'], |
207 'deltok') | 208 'deltok') |
208 | 209 |
210 def test_schedule(self): | |
211 models.Account.current_user_account = models.Account( | |
212 email='johndoe@chromium.org') | |
tandrii(chromium)
2015/09/16 11:02:32
this is not necessary, you set it up in setUp() no
nodir
2015/09/17 17:44:17
Done.
| |
213 | |
214 put_builds_response = { | |
215 'results': [ | |
216 { | |
217 'build': {'id': '1'}, | |
218 }, | |
219 { | |
220 'build': {'id': '1'}, | |
221 }, | |
222 ] | |
223 } | |
224 self.fake_responses = [ | |
225 {'delegation_token': 'deltok', 'validity_duration': 1000}, | |
226 put_builds_response, | |
227 ] | |
228 issue = models.Issue( | |
229 id='123', | |
230 project='chromium', | |
231 owner=users.User(email='owner@chromium.org'), | |
232 ) | |
233 builds = buildbucket.schedule(issue, '1', [ | |
234 ('tryserver.chromium.linux', 'linux_rel'), | |
235 ('tryserver.chromium.linux', 'win_rel'), | |
236 ]) | |
237 self.assertEqual( | |
238 builds, [r['build'] for r in put_builds_response['results']]) | |
239 | |
209 | 240 |
210 if __name__ == '__main__': | 241 if __name__ == '__main__': |
211 unittest.main() | 242 unittest.main() |
OLD | NEW |