Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(661)

Side by Side Diff: appengine/swarming/handlers_endpoints_test.py

Issue 2832243002: swarming: mechanical changes to unit tests (Closed)
Patch Set: Small tweaks Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 protorpc.remote import protojson 19 from protorpc.remote import protojson
20 import webapp2 20 import webapp2
21 import webtest 21 import webtest
22 22
23 from components import ereporter2 23 from components import ereporter2
24 from components import utils 24 from components import utils
25 25
26 import handlers_backend
26 import handlers_bot 27 import handlers_bot
27 import handlers_endpoints 28 import handlers_endpoints
28 import swarming_rpcs 29 import swarming_rpcs
29 30
30 from server import acl 31 from server import acl
31 from server import bot_code 32 from server import bot_code
32 from server import bot_management 33 from server import bot_management
33 from server import config 34 from server import config
34 from server import large 35 from server import large
35 from server import task_pack 36 from server import task_pack
(...skipping 18 matching lines...) Expand all
54 webapp2.WSGIApplication(handlers_bot.get_routes(), debug=True), 55 webapp2.WSGIApplication(handlers_bot.get_routes(), debug=True),
55 extra_environ={ 56 extra_environ={
56 'REMOTE_ADDR': self.source_ip, 57 'REMOTE_ADDR': self.source_ip,
57 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'], 58 'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'],
58 }) 59 })
59 self.mock( 60 self.mock(
60 ereporter2, 'log_request', 61 ereporter2, 'log_request',
61 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs))) 62 lambda *args, **kwargs: self.fail('%s, %s' % (args, kwargs)))
62 # Client API test cases run by default as user. 63 # Client API test cases run by default as user.
63 self.set_as_user() 64 self.set_as_user()
65 self.mock(utils, 'enqueue_task', self._enqueue_task)
66
67 def _enqueue_task(self, url, queue_name, **kwargs):
68 if queue_name == 'task-dimensions':
69 # Call directly into it.
70 handlers_backend.TaskDimensionsHandler.tidy_stale(kwargs['payload'])
71 return True
72 if queue_name == 'pubsub':
73 return True
74 self.fail(url)
64 75
65 76
66 class ServerApiTest(BaseTest): 77 class ServerApiTest(BaseTest):
67 api_service_cls = handlers_endpoints.SwarmingServerService 78 api_service_cls = handlers_endpoints.SwarmingServerService
68 79
69 def test_details(self): 80 def test_details(self):
70 """Asserts that server_details returns the correct version.""" 81 """Asserts that server_details returns the correct version."""
71 self.mock(config.config, 'config_service_hostname', lambda: 'a.server') 82 self.mock(config.config, 'config_service_hostname', lambda: 'a.server')
72 response = self.call_api('details') 83 response = self.call_api('details')
73 expected = { 84 expected = {
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 api_service_cls = handlers_endpoints.SwarmingTaskService 1221 api_service_cls = handlers_endpoints.SwarmingTaskService
1211 1222
1212 def setUp(self): 1223 def setUp(self):
1213 super(TaskApiTest, self).setUp() 1224 super(TaskApiTest, self).setUp()
1214 self.tasks_api = test_case.Endpoints( 1225 self.tasks_api = test_case.Endpoints(
1215 handlers_endpoints.SwarmingTasksService) 1226 handlers_endpoints.SwarmingTasksService)
1216 1227
1217 def test_cancel_ok(self): 1228 def test_cancel_ok(self):
1218 """Asserts that task cancellation goes smoothly.""" 1229 """Asserts that task cancellation goes smoothly."""
1219 # catch PubSub notification 1230 # 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. 1231 # Create and cancel a task as a non-privileged user.
1227 self.mock(random, 'getrandbits', lambda _: 0x88) 1232 self.mock(random, 'getrandbits', lambda _: 0x88)
1228 now = datetime.datetime(2010, 1, 2, 3, 4, 5) 1233 now = datetime.datetime(2010, 1, 2, 3, 4, 5)
1229 self.mock_now(now) 1234 self.mock_now(now)
1230 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) 1235 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO))
1231 _, task_id = self.client_create_task_raw( 1236 _, task_id = self.client_create_task_raw(
1232 pubsub_topic='projects/abc/topics/def', 1237 pubsub_topic='projects/abc/topics/def',
1233 pubsub_userdata='blah') 1238 pubsub_userdata='blah')
1234 expected = {u'ok': True, u'was_running': False} 1239 expected = {u'ok': True, u'was_running': False}
1235 response = self.call_api('cancel', body={'task_id': task_id}) 1240 response = self.call_api('cancel', body={'task_id': task_id})
(...skipping 24 matching lines...) Expand all
1260 # notification has been sent. 1265 # notification has been sent.
1261 expected = [ 1266 expected = [
1262 { 1267 {
1263 'payload': '{"auth_token":null,"task_id":"5cee488008810",' 1268 'payload': '{"auth_token":null,"task_id":"5cee488008810",'
1264 '"topic":"projects/abc/topics/def","userdata":"blah"}', 1269 '"topic":"projects/abc/topics/def","userdata":"blah"}',
1265 'queue_name': 'pubsub', 1270 'queue_name': 'pubsub',
1266 'transactional': True, 1271 'transactional': True,
1267 'url': '/internal/taskqueue/pubsub/5cee488008810', 1272 'url': '/internal/taskqueue/pubsub/5cee488008810',
1268 }, 1273 },
1269 ] 1274 ]
1270 self.assertEqual(expected, notifies)
1271 1275
1272 def test_cancel_forbidden(self): 1276 def test_cancel_forbidden(self):
1273 """Asserts that non-privileged non-owner can't cancel tasks.""" 1277 """Asserts that non-privileged non-owner can't cancel tasks."""
1274 # catch PubSub notification 1278 # catch PubSub notification
1275 notifies = [] 1279 notifies = []
1276 def enqueue_task_mock(**kwargs): 1280 def enqueue_task_mock(**kwargs):
1277 notifies.append(kwargs) 1281 notifies.append(kwargs)
1278 return True 1282 return True
1279 self.mock(utils, 'enqueue_task', enqueue_task_mock) 1283 self.mock(utils, 'enqueue_task', enqueue_task_mock)
1280 1284
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) 2188 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403)
2185 2189
2186 2190
2187 if __name__ == '__main__': 2191 if __name__ == '__main__':
2188 if '-v' in sys.argv: 2192 if '-v' in sys.argv:
2189 unittest.TestCase.maxDiff = None 2193 unittest.TestCase.maxDiff = None
2190 logging.basicConfig(level=logging.DEBUG) 2194 logging.basicConfig(level=logging.DEBUG)
2191 else: 2195 else:
2192 logging.basicConfig(level=logging.CRITICAL) 2196 logging.basicConfig(level=logging.CRITICAL)
2193 unittest.main() 2197 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698