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

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

Issue 1946253003: swarming: refactor cipd input (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@default-isolate-server
Patch Set: fix import google.protobuf Created 4 years, 7 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 by the Apache v2.0 license that can be 4 # Use of this source code is governed by the Apache v2.0 license that can be
5 # found in the LICENSE file. 5 # 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
(...skipping 19 matching lines...) Expand all
30 30
31 from server import acl 31 from server import acl
32 from server import bot_management 32 from server import bot_management
33 from server import config 33 from server import config
34 from server import large 34 from server import large
35 from server import task_pack 35 from server import task_pack
36 from server import task_request 36 from server import task_request
37 from server import task_result 37 from server import task_result
38 38
39 39
40 CIPD_SETTINGS = test_env_handlers.CIPD_SETTINGS
41
42
40 def message_to_dict(rpc_message): 43 def message_to_dict(rpc_message):
41 return json.loads(protojson.encode_message(rpc_message)) 44 return json.loads(protojson.encode_message(rpc_message))
42 45
43 46
44 class BaseTest(test_env_handlers.AppTestBase, test_case.EndpointsTestCase): 47 class BaseTest(test_env_handlers.AppTestBase, test_case.EndpointsTestCase):
45 48
46 DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f' 49 DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
47 DATETIME_NO_MICRO = '%Y-%m-%dT%H:%M:%S' 50 DATETIME_NO_MICRO = '%Y-%m-%dT%H:%M:%S'
48 51
49 def setUp(self): 52 def setUp(self):
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 """Asserts that new generates appropriate metadata.""" 147 """Asserts that new generates appropriate metadata."""
145 self.mock(random, 'getrandbits', lambda _: 0x88) 148 self.mock(random, 'getrandbits', lambda _: 0x88)
146 now = datetime.datetime(2010, 1, 2, 3, 4, 5) 149 now = datetime.datetime(2010, 1, 2, 3, 4, 5)
147 self.mock_now(now) 150 self.mock_now(now)
148 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) 151 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO))
149 request = swarming_rpcs.NewTaskRequest( 152 request = swarming_rpcs.NewTaskRequest(
150 expiration_secs=30, 153 expiration_secs=30,
151 name='job1', 154 name='job1',
152 priority=200, 155 priority=200,
153 properties=swarming_rpcs.TaskProperties( 156 properties=swarming_rpcs.TaskProperties(
157 cipd_input=swarming_rpcs.CipdInput(
158 packages=[
159 swarming_rpcs.CipdPackage(
160 package_name='rm', version='latest'),
161 ],
162 settings=CIPD_SETTINGS.SerializeToString(),
163 ),
154 command=['rm', '-rf', '/'], 164 command=['rm', '-rf', '/'],
155 packages=[
156 swarming_rpcs.CipdPackage(package_name='rm', version='latest'),
157 ],
158 dimensions=[ 165 dimensions=[
159 swarming_rpcs.StringPair(key='pool', value='default'), 166 swarming_rpcs.StringPair(key='pool', value='default'),
160 ], 167 ],
161 env=[ 168 env=[
162 swarming_rpcs.StringPair(key='PATH', value='/'), 169 swarming_rpcs.StringPair(key='PATH', value='/'),
163 ], 170 ],
164 execution_timeout_secs=30, 171 execution_timeout_secs=30,
165 io_timeout_secs=30), 172 io_timeout_secs=30),
166 tags=['foo:bar'], 173 tags=['foo:bar'],
167 user='joe@localhost', 174 user='joe@localhost',
168 pubsub_topic='projects/abc/topics/def', 175 pubsub_topic='projects/abc/topics/def',
169 pubsub_auth_token='secret that must not be shown', 176 pubsub_auth_token='secret that must not be shown',
170 pubsub_userdata='userdata') 177 pubsub_userdata='userdata')
171 expected = { 178 expected = {
172 u'request': { 179 u'request': {
173 u'authenticated': u'user:user@example.com', 180 u'authenticated': u'user:user@example.com',
174 u'created_ts': str_now, 181 u'created_ts': str_now,
175 u'expiration_secs': u'30', 182 u'expiration_secs': u'30',
176 u'name': u'job1', 183 u'name': u'job1',
177 u'priority': u'200', 184 u'priority': u'200',
178 u'properties': { 185 u'properties': {
186 u'cipd_input': {
187 u'packages': [{
188 u'package_name': u'rm',
189 u'version': u'latest',
190 }],
191 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
192 },
179 u'command': [u'rm', u'-rf', u'/'], 193 u'command': [u'rm', u'-rf', u'/'],
180 u'packages': [{
181 u'package_name': u'rm',
182 u'version': u'latest',
183 }],
184 u'dimensions': [ 194 u'dimensions': [
185 {u'key': u'pool', u'value': u'default'}, 195 {u'key': u'pool', u'value': u'default'},
186 ], 196 ],
187 u'env': [ 197 u'env': [
188 {u'key': u'PATH', u'value': u'/'}, 198 {u'key': u'PATH', u'value': u'/'},
189 ], 199 ],
190 u'execution_timeout_secs': u'30', 200 u'execution_timeout_secs': u'30',
191 u'grace_period_secs': u'30', 201 u'grace_period_secs': u'30',
192 u'idempotent': False, 202 u'idempotent': False,
193 u'io_timeout_secs': u'30', 203 u'io_timeout_secs': u'30',
(...skipping 29 matching lines...) Expand all
223 now_30 = self.mock_now(now, 30) 233 now_30 = self.mock_now(now, 30)
224 str_now_30 = unicode(now_30.strftime(self.DATETIME_NO_MICRO)) 234 str_now_30 = unicode(now_30.strftime(self.DATETIME_NO_MICRO))
225 self.set_as_user() 235 self.set_as_user()
226 236
227 request = swarming_rpcs.NewTaskRequest( 237 request = swarming_rpcs.NewTaskRequest(
228 expiration_secs=30, 238 expiration_secs=30,
229 name='job1', 239 name='job1',
230 priority=200, 240 priority=200,
231 properties=swarming_rpcs.TaskProperties( 241 properties=swarming_rpcs.TaskProperties(
232 command=['python', 'run_test.py'], 242 command=['python', 'run_test.py'],
243 cipd_input=swarming_rpcs.CipdInput(
244 packages=[
245 swarming_rpcs.CipdPackage(
246 package_name='rm',
247 version=test_env_handlers.PINNED_PACKAGE_VERSION),
248 ],
249 settings=CIPD_SETTINGS.SerializeToString(),
250 ),
233 dimensions=[ 251 dimensions=[
234 swarming_rpcs.StringPair(key='os', value='Amiga'), 252 swarming_rpcs.StringPair(key='os', value='Amiga'),
235 swarming_rpcs.StringPair(key='pool', value='default'), 253 swarming_rpcs.StringPair(key='pool', value='default'),
236 ], 254 ],
237 execution_timeout_secs=3600, 255 execution_timeout_secs=3600,
238 idempotent=True, 256 idempotent=True,
239 io_timeout_secs=1200, 257 io_timeout_secs=1200),
240 packages=[
241 swarming_rpcs.CipdPackage(
242 package_name='rm',
243 version=test_env_handlers.PINNED_PACKAGE_VERSION),
244 ]),
245 tags=['foo:bar'], 258 tags=['foo:bar'],
246 user='joe@localhost') 259 user='joe@localhost')
247 expected = { 260 expected = {
248 u'request': { 261 u'request': {
249 u'authenticated': u'user:user@example.com', 262 u'authenticated': u'user:user@example.com',
250 u'created_ts': str_now_30, 263 u'created_ts': str_now_30,
251 u'expiration_secs': u'30', 264 u'expiration_secs': u'30',
252 u'name': u'job1', 265 u'name': u'job1',
253 u'priority': u'200', 266 u'priority': u'200',
254 u'properties': { 267 u'properties': {
268 u'cipd_input': {
269 u'packages': [{
270 u'package_name': u'rm',
271 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
272 }],
273 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
274 },
255 u'command': [u'python', u'run_test.py'], 275 u'command': [u'python', u'run_test.py'],
256 u'dimensions': [ 276 u'dimensions': [
257 {u'key': u'os', u'value': u'Amiga'}, 277 {u'key': u'os', u'value': u'Amiga'},
258 {u'key': u'pool', u'value': u'default'}, 278 {u'key': u'pool', u'value': u'default'},
259 ], 279 ],
260 u'execution_timeout_secs': u'3600', 280 u'execution_timeout_secs': u'3600',
261 u'grace_period_secs': u'30', 281 u'grace_period_secs': u'30',
262 u'idempotent': True, 282 u'idempotent': True,
263 u'io_timeout_secs': u'1200', 283 u'io_timeout_secs': u'1200',
264 u'packages': [{
265 u'package_name': u'rm',
266 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
267 }],
268 }, 284 },
269 u'tags': [ 285 u'tags': [
270 u'foo:bar', 286 u'foo:bar',
271 u'os:Amiga', 287 u'os:Amiga',
272 u'pool:default', 288 u'pool:default',
273 u'priority:200', 289 u'priority:200',
274 u'user:joe@localhost', 290 u'user:joe@localhost',
275 ], 291 ],
276 u'user': u'joe@localhost', 292 u'user': u'joe@localhost',
277 }, 293 },
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 request = swarming_rpcs.TasksRequest(start=start, end=end) 392 request = swarming_rpcs.TasksRequest(start=start, end=end)
377 expected = { 393 expected = {
378 u'items': [ 394 u'items': [
379 { 395 {
380 u'authenticated': u'user:user@example.com', 396 u'authenticated': u'user:user@example.com',
381 u'created_ts': str_now_30, 397 u'created_ts': str_now_30,
382 u'expiration_secs': u'30', 398 u'expiration_secs': u'30',
383 u'name': u'job1', 399 u'name': u'job1',
384 u'priority': u'200', 400 u'priority': u'200',
385 u'properties': { 401 u'properties': {
402 u'cipd_input': {
403 u'packages': [{
404 u'package_name': u'rm',
405 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
406 }],
407 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
408 },
386 u'command': [u'python', u'run_test.py'], 409 u'command': [u'python', u'run_test.py'],
387 u'dimensions': [ 410 u'dimensions': [
388 {u'key': u'os', u'value': u'Amiga'}, 411 {u'key': u'os', u'value': u'Amiga'},
389 {u'key': u'pool', u'value': u'default'}, 412 {u'key': u'pool', u'value': u'default'},
390 ], 413 ],
391 u'execution_timeout_secs': u'3600', 414 u'execution_timeout_secs': u'3600',
392 u'grace_period_secs': u'30', 415 u'grace_period_secs': u'30',
393 u'idempotent': True, 416 u'idempotent': True,
394 u'io_timeout_secs': u'1200', 417 u'io_timeout_secs': u'1200',
395 u'packages': [{
396 u'package_name': u'rm',
397 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
398 }],
399 }, 418 },
400 u'tags': [ 419 u'tags': [
401 u'foo:bar', 420 u'foo:bar',
402 u'os:Amiga', 421 u'os:Amiga',
403 u'pool:default', 422 u'pool:default',
404 u'priority:200', 423 u'priority:200',
405 u'user:joe@localhost', 424 u'user:joe@localhost',
406 ], 425 ],
407 u'user': u'joe@localhost', 426 u'user': u'joe@localhost',
408 }, 427 },
409 { 428 {
410 u'authenticated': u'user:user@example.com', 429 u'authenticated': u'user:user@example.com',
411 u'created_ts': str_now, 430 u'created_ts': str_now,
412 u'expiration_secs': u'86400', 431 u'expiration_secs': u'86400',
413 u'name': u'task', 432 u'name': u'task',
414 u'priority': u'10', 433 u'priority': u'10',
415 u'properties': { 434 u'properties': {
435 u'cipd_input': {
436 u'packages': [{
437 u'package_name': u'rm',
438 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
439 }],
440 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
441 },
416 u'command': [u'python', u'run_test.py'], 442 u'command': [u'python', u'run_test.py'],
417 u'dimensions': [ 443 u'dimensions': [
418 {u'key': u'os', u'value': u'Amiga'}, 444 {u'key': u'os', u'value': u'Amiga'},
419 {u'key': u'pool', u'value': u'default'}, 445 {u'key': u'pool', u'value': u'default'},
420 ], 446 ],
421 u'execution_timeout_secs': u'3600', 447 u'execution_timeout_secs': u'3600',
422 u'grace_period_secs': u'30', 448 u'grace_period_secs': u'30',
423 u'idempotent': True, 449 u'idempotent': True,
424 u'io_timeout_secs': u'1200', 450 u'io_timeout_secs': u'1200',
425 u'packages': [{
426 u'package_name': u'rm',
427 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
428 }],
429 }, 451 },
430 u'tags': [ 452 u'tags': [
431 u'commit:post', 453 u'commit:post',
432 u'os:Amiga', 454 u'os:Amiga',
433 u'os:Win', 455 u'os:Win',
434 u'pool:default', 456 u'pool:default',
435 u'priority:10', 457 u'priority:10',
436 u'project:yay', 458 u'project:yay',
437 u'user:joe@localhost', 459 u'user:joe@localhost',
438 ], 460 ],
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 u'priority:200', 590 u'priority:200',
569 u'user:joe@localhost', 591 u'user:joe@localhost',
570 ], 592 ],
571 u'user': u'joe@localhost', 593 u'user': u'joe@localhost',
572 }, 594 },
573 u'task_id': u'5cee488008810', 595 u'task_id': u'5cee488008810',
574 } 596 }
575 response = self.call_api('new', body=message_to_dict(request)) 597 response = self.call_api('new', body=message_to_dict(request))
576 self.assertEqual(expected, response.json) 598 self.assertEqual(expected, response.json)
577 599
600 def test_new_cipd_packages_with_defaults(self):
601 self.mock(random, 'getrandbits', lambda _: 0x88)
602 now = datetime.datetime(2010, 1, 2, 3, 4, 5)
603 self.mock_now(now)
604 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO))
605
606 # Define settings on the server.
607 cfg = config.settings()
608 cfg.cipd.CopyFrom(CIPD_SETTINGS)
609 self.mock(config, 'settings', lambda: cfg)
610
611 request = swarming_rpcs.NewTaskRequest(
612 expiration_secs=30,
613 name='job1',
614 priority=200,
615 properties=swarming_rpcs.TaskProperties(
616 cipd_input=swarming_rpcs.CipdInput(
617 packages=[
618 swarming_rpcs.CipdPackage(
619 package_name='rm',
620 version='latest'),
621 ],
622 ),
623 command=['rm', '-rf', '/'],
624 dimensions=[
625 swarming_rpcs.StringPair(key='pool', value='default'),
626 ],
627 env=[
628 swarming_rpcs.StringPair(key='PATH', value='/'),
629 ],
630 execution_timeout_secs=30,
631 io_timeout_secs=30),
632 tags=['foo:bar'],
633 user='joe@localhost',
634 pubsub_topic='projects/abc/topics/def',
635 pubsub_auth_token='secret that must not be shown',
636 pubsub_userdata='userdata')
637 expected = {
638 u'request': {
639 u'authenticated': u'user:user@example.com',
640 u'created_ts': str_now,
641 u'expiration_secs': u'30',
642 u'name': u'job1',
643 u'priority': u'200',
644 u'properties': {
645 u'cipd_input': {
646 u'packages': [{
647 u'package_name': u'rm',
648 u'version': u'latest',
649 }],
650 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
651 },
652 u'command': [u'rm', u'-rf', u'/'],
653 u'dimensions': [
654 {u'key': u'pool', u'value': u'default'},
655 ],
656 u'env': [
657 {u'key': u'PATH', u'value': u'/'},
658 ],
659 u'execution_timeout_secs': u'30',
660 u'grace_period_secs': u'30',
661 u'idempotent': False,
662 u'io_timeout_secs': u'30',
663 },
664 u'pubsub_topic': u'projects/abc/topics/def',
665 u'pubsub_userdata': u'userdata',
666 u'tags': [
667 u'foo:bar',
668 u'pool:default',
669 u'priority:200',
670 u'user:joe@localhost',
671 ],
672 u'user': u'joe@localhost',
673 },
674 u'task_id': u'5cee488008810',
675 }
676 response = self.call_api('new', body=message_to_dict(request))
677 self.assertEqual(expected, response.json)
678
578 def test_list_ok(self): 679 def test_list_ok(self):
579 """Asserts that list requests all TaskResultSummaries.""" 680 """Asserts that list requests all TaskResultSummaries."""
580 first, second, str_now_120, start, end = self._gen_two_tasks() 681 first, second, str_now_120, start, end = self._gen_two_tasks()
581 first_no_perf = first.copy() 682 first_no_perf = first.copy()
582 first_no_perf.pop('performance_stats') 683 first_no_perf.pop('performance_stats')
583 # Basic request. 684 # Basic request.
584 request = swarming_rpcs.TasksRequest( 685 request = swarming_rpcs.TasksRequest(
585 end=end, start=start, include_performance_stats=True) 686 end=end, start=start, include_performance_stats=True)
586 expected = {u'now': str_now_120, u'items': [second, first]} 687 expected = {u'now': str_now_120, u'items': [second, first]}
587 actual = self.call_api('list', body=message_to_dict(request)).json 688 actual = self.call_api('list', body=message_to_dict(request)).json
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 _, task_id = self.client_create_task_raw() 1206 _, task_id = self.client_create_task_raw()
1106 self.set_as_bot() 1207 self.set_as_bot()
1107 response = self.call_api('request', body={'task_id': task_id}) 1208 response = self.call_api('request', body={'task_id': task_id})
1108 expected = { 1209 expected = {
1109 u'authenticated': u'user:user@example.com', 1210 u'authenticated': u'user:user@example.com',
1110 u'created_ts': unicode(now.strftime(self.DATETIME_FORMAT)), 1211 u'created_ts': unicode(now.strftime(self.DATETIME_FORMAT)),
1111 u'expiration_secs': unicode(24 * 60 * 60), 1212 u'expiration_secs': unicode(24 * 60 * 60),
1112 u'name': u'hi', 1213 u'name': u'hi',
1113 u'priority': u'10', 1214 u'priority': u'10',
1114 u'properties': { 1215 u'properties': {
1216 u'cipd_input': {
1217 u'packages': [{
1218 u'package_name': u'rm',
1219 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
1220 }],
1221 u'settings': base64.b64encode(CIPD_SETTINGS.SerializeToString()),
1222 },
1115 u'command': [u'python', u'run_test.py'], 1223 u'command': [u'python', u'run_test.py'],
1116 u'dimensions': [ 1224 u'dimensions': [
1117 {u'key': u'os', u'value': u'Amiga'}, 1225 {u'key': u'os', u'value': u'Amiga'},
1118 {u'key': u'pool', u'value': u'default'}, 1226 {u'key': u'pool', u'value': u'default'},
1119 ], 1227 ],
1120 u'execution_timeout_secs': u'3600', 1228 u'execution_timeout_secs': u'3600',
1121 u'grace_period_secs': u'30', 1229 u'grace_period_secs': u'30',
1122 u'idempotent': False, 1230 u'idempotent': False,
1123 u'io_timeout_secs': u'1200', 1231 u'io_timeout_secs': u'1200',
1124 u'packages': [{
1125 u'package_name': u'rm',
1126 u'version': test_env_handlers.PINNED_PACKAGE_VERSION,
1127 }],
1128 }, 1232 },
1129 u'tags': [ 1233 u'tags': [
1130 u'os:Amiga', 1234 u'os:Amiga',
1131 u'pool:default', 1235 u'pool:default',
1132 u'priority:10', 1236 u'priority:10',
1133 u'user:joe@localhost', 1237 u'user:joe@localhost',
1134 ], 1238 ],
1135 u'user': u'joe@localhost', 1239 u'user': u'joe@localhost',
1136 } 1240 }
1137 self.assertEqual(expected, response.json) 1241 self.assertEqual(expected, response.json)
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
1433 self.assertEqual(expected, response.json) 1537 self.assertEqual(expected, response.json)
1434 1538
1435 1539
1436 if __name__ == '__main__': 1540 if __name__ == '__main__':
1437 if '-v' in sys.argv: 1541 if '-v' in sys.argv:
1438 unittest.TestCase.maxDiff = None 1542 unittest.TestCase.maxDiff = None
1439 logging.basicConfig(level=logging.DEBUG) 1543 logging.basicConfig(level=logging.DEBUG)
1440 else: 1544 else:
1441 logging.basicConfig(level=logging.CRITICAL) 1545 logging.basicConfig(level=logging.CRITICAL)
1442 unittest.main() 1546 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698