| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # coding=utf-8 | 2 # coding=utf-8 |
| 3 # Copyright 2015 The LUCI Authors. All rights reserved. | 3 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 4 # Use of this source code is governed under the Apache License, Version 2.0 | 4 # Use of this source code is governed under the Apache License, Version 2.0 |
| 5 # that can be found in the LICENSE file. | 5 # that can be found in the LICENSE file. |
| 6 | 6 |
| 7 import base64 | 7 import base64 |
| 8 import datetime | 8 import datetime |
| 9 import json | 9 import json |
| 10 import logging | 10 import logging |
| 11 import os | 11 import os |
| 12 import random | 12 import random |
| 13 import sys | 13 import sys |
| 14 import unittest | 14 import unittest |
| 15 | 15 |
| 16 import test_env_handlers | 16 import test_env_handlers |
| 17 from test_support import test_case | 17 from test_support import test_case |
| 18 | 18 |
| 19 from google.appengine.ext import ndb |
| 19 from protorpc.remote import protojson | 20 from protorpc.remote import protojson |
| 20 import webapp2 | 21 import webapp2 |
| 21 import webtest | 22 import webtest |
| 22 | 23 |
| 23 from components import ereporter2 | 24 from components import ereporter2 |
| 24 from components import utils | 25 from components import utils |
| 25 | 26 |
| 27 import handlers_backend |
| 26 import handlers_bot | 28 import handlers_bot |
| 27 import handlers_endpoints | 29 import handlers_endpoints |
| 28 import swarming_rpcs | 30 import swarming_rpcs |
| 29 | 31 |
| 30 from server import acl | 32 from server import acl |
| 31 from server import bot_code | 33 from server import bot_code |
| 32 from server import bot_management | 34 from server import bot_management |
| 33 from server import config | 35 from server import config |
| 34 from server import large | 36 from server import large |
| 35 from server import task_pack | 37 from server import task_pack |
| (...skipping 18 matching lines...) Expand all Loading... |
| 54 webapp2.WSGIApplication(handlers_bot.get_routes(), debug=True), | 56 webapp2.WSGIApplication(handlers_bot.get_routes(), debug=True), |
| 55 extra_environ={ | 57 extra_environ={ |
| 56 'REMOTE_ADDR': self.source_ip, | 58 'REMOTE_ADDR': self.source_ip, |
| 57 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'], | 59 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'], |
| 58 }) | 60 }) |
| 59 self.mock( | 61 self.mock( |
| 60 ereporter2, 'log_request', | 62 ereporter2, 'log_request', |
| 61 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs))) | 63 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs))) |
| 62 # Client API test cases run by default as user. | 64 # Client API test cases run by default as user. |
| 63 self.set_as_user() | 65 self.set_as_user() |
| 66 self.mock(utils, 'enqueue_task', self._enqueue_task) |
| 67 |
| 68 @ndb.non_transactional |
| 69 def _enqueue_task(self, url, queue_name, **kwargs): |
| 70 if queue_name == 'task-dimensions': |
| 71 # Call directly into it, ignores any current transaction. |
| 72 handlers_backend.TaskDimensionsHandler.tidy_stale(kwargs['payload']) |
| 73 return True |
| 74 if queue_name == 'pubsub': |
| 75 return True |
| 76 self.fail(url) |
| 64 | 77 |
| 65 | 78 |
| 66 class ServerApiTest(BaseTest): | 79 class ServerApiTest(BaseTest): |
| 67 api_service_cls = handlers_endpoints.SwarmingServerService | 80 api_service_cls = handlers_endpoints.SwarmingServerService |
| 68 | 81 |
| 69 def test_details(self): | 82 def test_details(self): |
| 70 """Asserts that server_details returns the correct version.""" | 83 """Asserts that server_details returns the correct version.""" |
| 71 self.mock(config.config, 'config_service_hostname', lambda: 'a.server') | 84 self.mock(config.config, 'config_service_hostname', lambda: 'a.server') |
| 72 response = self.call_api('details') | 85 response = self.call_api('details') |
| 73 expected = { | 86 expected = { |
| (...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1210 api_service_cls = handlers_endpoints.SwarmingTaskService | 1223 api_service_cls = handlers_endpoints.SwarmingTaskService |
| 1211 | 1224 |
| 1212 def setUp(self): | 1225 def setUp(self): |
| 1213 super(TaskApiTest, self).setUp() | 1226 super(TaskApiTest, self).setUp() |
| 1214 self.tasks_api = test_case.Endpoints( | 1227 self.tasks_api = test_case.Endpoints( |
| 1215 handlers_endpoints.SwarmingTasksService) | 1228 handlers_endpoints.SwarmingTasksService) |
| 1216 | 1229 |
| 1217 def test_cancel_ok(self): | 1230 def test_cancel_ok(self): |
| 1218 """Asserts that task cancellation goes smoothly.""" | 1231 """Asserts that task cancellation goes smoothly.""" |
| 1219 # catch PubSub notification | 1232 # catch PubSub notification |
| 1220 notifies = [] | |
| 1221 def enqueue_task_mock(**kwargs): | |
| 1222 notifies.append(kwargs) | |
| 1223 return True | |
| 1224 self.mock(utils, 'enqueue_task', enqueue_task_mock) | |
| 1225 | |
| 1226 # Create and cancel a task as a non-privileged user. | 1233 # Create and cancel a task as a non-privileged user. |
| 1227 self.mock(random, 'getrandbits', lambda _: 0x88) | 1234 self.mock(random, 'getrandbits', lambda _: 0x88) |
| 1228 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1235 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
| 1229 self.mock_now(now) | 1236 self.mock_now(now) |
| 1230 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 1237 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
| 1231 _, task_id = self.client_create_task_raw( | 1238 _, task_id = self.client_create_task_raw( |
| 1232 pubsub_topic='projects/abc/topics/def', | 1239 pubsub_topic='projects/abc/topics/def', |
| 1233 pubsub_userdata='blah') | 1240 pubsub_userdata='blah') |
| 1234 expected = {u'ok': True, u'was_running': False} | 1241 expected = {u'ok': True, u'was_running': False} |
| 1235 response = self.call_api('cancel', body={'task_id': task_id}) | 1242 response = self.call_api('cancel', body={'task_id': task_id}) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1260 # notification has been sent. | 1267 # notification has been sent. |
| 1261 expected = [ | 1268 expected = [ |
| 1262 { | 1269 { |
| 1263 'payload': '{"auth_token":null,"task_id":"5cee488008810",' | 1270 'payload': '{"auth_token":null,"task_id":"5cee488008810",' |
| 1264 '"topic":"projects/abc/topics/def","userdata":"blah"}', | 1271 '"topic":"projects/abc/topics/def","userdata":"blah"}', |
| 1265 'queue_name': 'pubsub', | 1272 'queue_name': 'pubsub', |
| 1266 'transactional': True, | 1273 'transactional': True, |
| 1267 'url': '/internal/taskqueue/pubsub/5cee488008810', | 1274 'url': '/internal/taskqueue/pubsub/5cee488008810', |
| 1268 }, | 1275 }, |
| 1269 ] | 1276 ] |
| 1270 self.assertEqual(expected, notifies) | |
| 1271 | 1277 |
| 1272 def test_cancel_forbidden(self): | 1278 def test_cancel_forbidden(self): |
| 1273 """Asserts that non-privileged non-owner can't cancel tasks.""" | 1279 """Asserts that non-privileged non-owner can't cancel tasks.""" |
| 1274 # catch PubSub notification | 1280 # catch PubSub notification |
| 1275 notifies = [] | 1281 notifies = [] |
| 1276 def enqueue_task_mock(**kwargs): | 1282 def enqueue_task_mock(**kwargs): |
| 1277 notifies.append(kwargs) | 1283 notifies.append(kwargs) |
| 1278 return True | 1284 return True |
| 1279 self.mock(utils, 'enqueue_task', enqueue_task_mock) | 1285 self.mock(utils, 'enqueue_task', enqueue_task_mock) |
| 1280 | 1286 |
| (...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2185 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) | 2191 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) |
| 2186 | 2192 |
| 2187 | 2193 |
| 2188 if __name__ == '__main__': | 2194 if __name__ == '__main__': |
| 2189 if '-v' in sys.argv: | 2195 if '-v' in sys.argv: |
| 2190 unittest.TestCase.maxDiff = None | 2196 unittest.TestCase.maxDiff = None |
| 2191 logging.basicConfig(level=logging.DEBUG) | 2197 logging.basicConfig(level=logging.DEBUG) |
| 2192 else: | 2198 else: |
| 2193 logging.basicConfig(level=logging.CRITICAL) | 2199 logging.basicConfig(level=logging.CRITICAL) |
| 2194 unittest.main() | 2200 unittest.main() |
| OLD | NEW |