OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for gatekeeper_ng.py. | 6 """Unit tests for gatekeeper_ng.py. |
7 | 7 |
8 This is a basic check that gatekeeper_ng.py can properly interpret builds and | 8 This is a basic check that gatekeeper_ng.py can properly interpret builds and |
9 close the tree. | 9 close the tree. |
10 | 10 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 params = req.get_data() | 365 params = req.get_data() |
366 else: | 366 else: |
367 url = req | 367 url = req |
368 | 368 |
369 call = {'url': url} | 369 call = {'url': url} |
370 if params: | 370 if params: |
371 call['params'] = params | 371 call['params'] = params |
372 self.url_calls.append(call) | 372 self.url_calls.append(call) |
373 | 373 |
374 if url in self.urls: | 374 if url in self.urls: |
375 return copy.copy(self.urls[url]) | 375 return copy.copy(self.urls[url](params)) |
376 else: | 376 else: |
377 raise urllib2.HTTPError(url, 404, 'Not Found: %s' % url, | 377 raise urllib2.HTTPError(url, 404, 'Not Found: %s' % url, |
378 None, StringIO.StringIO('')) | 378 None, StringIO.StringIO('')) |
379 | 379 |
380 @staticmethod | 380 @staticmethod |
381 def decode_param_json(param): | 381 def decode_param_json(param): |
382 data = urlparse.parse_qs(param) | 382 data = urlparse.parse_qs(param) |
383 payload = json.loads(data['json'][0])['message'] | 383 payload = json.loads(data['json'][0])['message'] |
384 return json.loads(payload) | 384 return json.loads(payload) |
385 | 385 |
386 def handle_url_fp(self, url, fp): | 386 def handle_url_fp(self, url, fp): |
387 """Add a file object to handle a mocked URL.""" | 387 """Add a file object to handle a mocked URL.""" |
388 setattr(fp, 'getcode', lambda: 200) | 388 setattr(fp, 'getcode', lambda: 200) |
389 self.urls[url] = fp | 389 self.urls[url] = lambda _: fp |
| 390 |
| 391 def handle_url_custom(self, url, handler): |
| 392 """Handler func will be called with params as first argument.""" |
| 393 self.urls[url] = handler |
390 | 394 |
391 def handle_url_str(self, url, response): | 395 def handle_url_str(self, url, response): |
392 """Add a string to handle a mocked URL.""" | 396 """Add a string to handle a mocked URL.""" |
393 buf = StringIO.StringIO(response) | 397 buf = StringIO.StringIO(response) |
394 self.handle_url_fp(url, buf) | 398 self.handle_url_fp(url, buf) |
395 | 399 |
396 def handle_url_json(self, url, data): | 400 def handle_url_json(self, url, data): |
397 """Add a json object to handle a mocked URL.""" | 401 """Add a json object to handle a mocked URL.""" |
398 buf = StringIO.StringIO() | 402 buf = StringIO.StringIO() |
399 json.dump(data, buf) | 403 json.dump(data, buf) |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 self.handle_url_json(self.get_status_url, { | 778 self.handle_url_json(self.get_status_url, { |
775 'message': 'closed (automatic)', | 779 'message': 'closed (automatic)', |
776 'general_state': 'closed', | 780 'general_state': 'closed', |
777 }) | 781 }) |
778 self.call_gatekeeper(build_db=build_db) | 782 self.call_gatekeeper(build_db=build_db) |
779 self.assertEquals(self.url_calls[-1]['url'], self.set_status_url) | 783 self.assertEquals(self.url_calls[-1]['url'], self.set_status_url) |
780 status_data = urlparse.parse_qs(self.url_calls[-1]['params']) | 784 status_data = urlparse.parse_qs(self.url_calls[-1]['params']) |
781 self.assertTrue(status_data['message'][0].startswith( | 785 self.assertTrue(status_data['message'][0].startswith( |
782 "Tree is open (Automatic")) | 786 "Tree is open (Automatic")) |
783 | 787 |
| 788 # Same as above and get_status_url requires bot login. |
| 789 json_handler = self.urls.pop(self.get_status_url) |
| 790 def handler(params): |
| 791 if params == None: |
| 792 return StringIO.StringIO("<blabla>Login Required</blabla>") |
| 793 else: |
| 794 return json_handler(params) |
| 795 self.handle_url_custom(self.get_status_url, handler) |
| 796 self.call_gatekeeper(build_db=build_db) |
| 797 self.assertEquals(self.url_calls[-2]['url'], self.get_status_url) |
| 798 self.assertIsNotNone(self.url_calls[-2]['params']) |
| 799 status_data = urlparse.parse_qs(self.url_calls[-1]['params']) |
| 800 self.assertTrue(status_data['message'][0].startswith( |
| 801 "Tree is open (Automatic")) |
| 802 |
784 # However, don't touch the tree status if a human set it. | 803 # However, don't touch the tree status if a human set it. |
785 self.handle_url_json(self.get_status_url, { | 804 self.handle_url_json(self.get_status_url, { |
786 'message': 'closed, world is on fire', | 805 'message': 'closed, world is on fire', |
787 'general_state': 'closed', | 806 'general_state': 'closed', |
788 }) | 807 }) |
789 urls = self.call_gatekeeper(build_db=build_db) | 808 urls = self.call_gatekeeper(build_db=build_db) |
790 self.assertNotIn(self.set_status_url, urls) | 809 self.assertNotIn(self.set_status_url, urls) |
791 | 810 |
792 # Only change the tree status if it's currently 'closed' | 811 # Only change the tree status if it's currently 'closed' |
793 self.handle_url_json(self.get_status_url, { | 812 self.handle_url_json(self.get_status_url, { |
(...skipping 1960 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2754 json=os.path.join(SCRIPT_DIR, os.pardir, 'gatekeeper.json')) | 2773 json=os.path.join(SCRIPT_DIR, os.pardir, 'gatekeeper.json')) |
2755 | 2774 |
2756 def testCheckedInTreeConfigIsValid(self): # pylint: disable=R0201 | 2775 def testCheckedInTreeConfigIsValid(self): # pylint: disable=R0201 |
2757 tree_config = os.path.join(SCRIPT_DIR, os.pardir, 'gatekeeper_trees.json') | 2776 tree_config = os.path.join(SCRIPT_DIR, os.pardir, 'gatekeeper_trees.json') |
2758 gatekeeper_ng_config.load_gatekeeper_tree_config(tree_config) | 2777 gatekeeper_ng_config.load_gatekeeper_tree_config(tree_config) |
2759 | 2778 |
2760 | 2779 |
2761 if __name__ == '__main__': | 2780 if __name__ == '__main__': |
2762 with utils.print_coverage(include=[__file__]): | 2781 with utils.print_coverage(include=[__file__]): |
2763 unittest.main() | 2782 unittest.main() |
OLD | NEW |