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 |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 ], | 618 ], |
619 u'user': u'joe@localhost', | 619 u'user': u'joe@localhost', |
620 }, | 620 }, |
621 ], | 621 ], |
622 u'now': str_now_30, | 622 u'now': str_now_30, |
623 } | 623 } |
624 self.assertEqual( | 624 self.assertEqual( |
625 expected, | 625 expected, |
626 self.call_api('requests', body=message_to_dict(request)).json) | 626 self.call_api('requests', body=message_to_dict(request)).json) |
627 | 627 |
628 def test_new_ok_deduped_by_transaction_id(self): | |
629 """Asserts that new returns task result for deduped.""" | |
630 # Run a task to completion. | |
631 self.mock(random, 'getrandbits', lambda _: 0x88) | |
632 now = self.mock_now(datetime.datetime(2010, 1, 2, 3, 4, 5)) | |
633 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | |
634 new_req, _, task_id = self.client_create_task_raw( | |
635 name='task', tags=['project:yay', 'commit:post'], | |
636 properties=dict(idempotent=True), | |
637 transaction_id='txn') | |
638 self.set_as_bot() | |
639 self.bot_run_task() | |
640 | |
641 self.mock(random, 'getrandbits', lambda _: 0x66) | |
642 self.set_as_user() | |
643 | |
644 expected = { | |
645 u'request': { | |
646 u'authenticated': u'user:user@example.com', | |
647 u'created_ts': str_now, | |
648 u'expiration_secs': u'86400', | |
649 u'name': u'task', | |
650 u'priority': u'100', | |
651 u'properties': { | |
652 u'cipd_input': { | |
653 u'client_package': { | |
654 u'package_name': u'infra/tools/cipd/${platform}', | |
655 u'version': u'git_revision:deadbeef', | |
656 }, | |
657 u'packages': [{ | |
658 u'package_name': u'rm', | |
659 u'path': u'bin', | |
660 u'version': u'git_revision:deadbeef', | |
661 }], | |
662 u'server': u'https://chrome-infra-packages.appspot.com', | |
663 }, | |
664 u'command': [u'python', u'run_test.py'], | |
665 u'dimensions': [ | |
666 {u'key': u'os', u'value': u'Amiga'}, | |
667 {u'key': u'pool', u'value': u'default'}, | |
668 ], | |
669 u'execution_timeout_secs': u'3600', | |
670 u'grace_period_secs': u'30', | |
671 u'idempotent': True, | |
672 u'io_timeout_secs': u'1200', | |
673 u'outputs': [u'foo', u'path/to/foobar'], | |
674 }, | |
675 u'service_account': u'none', | |
676 u'tags': [ | |
677 u'commit:post', | |
678 u'os:Amiga', | |
679 u'pool:default', | |
680 u'priority:100', | |
681 u'project:yay', | |
682 u'service_account:none', | |
683 u'user:joe@localhost', | |
684 ], | |
685 u'user': u'joe@localhost', | |
686 }, | |
687 u'task_id': unicode(task_id), | |
688 u'task_result': { | |
689 u'bot_dimensions': [ | |
690 {u'key': u'id', u'value': [u'bot1']}, | |
691 {u'key': u'os', u'value': [u'Amiga']}, | |
692 {u'key': u'pool', u'value': [u'default']}, | |
693 ], | |
694 u'bot_id': u'bot1', | |
695 u'bot_version': self.bot_version, | |
696 u'completed_ts': str_now, | |
697 u'costs_usd': [0.1], | |
698 u'created_ts': str_now, | |
699 u'duration': 0.1, | |
700 u'exit_code': u'0', | |
701 u'failure': False, | |
702 u'internal_failure': False, | |
703 u'modified_ts': str_now, | |
704 u'name': u'task', | |
705 u'properties_hash': ( | |
706 u'd35fe05074cbd9a2356c77c9983c71476f3a5f9415c5c5b1f3e2cdf7826b7261' | |
707 ), | |
708 u'run_id': u'5cee488008811', | |
709 u'server_versions': [u'v1a'], | |
710 u'started_ts': str_now, | |
711 u'state': u'COMPLETED', | |
712 u'tags': [ | |
713 u'commit:post', | |
714 u'os:Amiga', | |
715 u'pool:default', | |
716 u'priority:100', | |
717 u'project:yay', | |
718 u'service_account:none', | |
719 u'user:joe@localhost', | |
720 ], | |
721 u'task_id': unicode(task_id), | |
722 u'try_number': u'1', | |
723 u'user': u'joe@localhost', | |
724 }, | |
725 } | |
726 response = self.call_api('new', body=message_to_dict(new_req)) | |
727 self.assertEqual(expected, response.json) | |
728 | |
628 def test_new_ok_isolated(self): | 729 def test_new_ok_isolated(self): |
629 """Asserts that new generates appropriate metadata.""" | 730 """Asserts that new generates appropriate metadata.""" |
630 self.mock(random, 'getrandbits', lambda _: 0x88) | 731 self.mock(random, 'getrandbits', lambda _: 0x88) |
631 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 732 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
632 self.mock_now(now) | 733 self.mock_now(now) |
633 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 734 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
634 request = swarming_rpcs.NewTaskRequest( | 735 request = swarming_rpcs.NewTaskRequest( |
635 expiration_secs=30, | 736 expiration_secs=30, |
636 name='job1', | 737 name='job1', |
637 priority=200, | 738 priority=200, |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
873 # check URL | 974 # check URL |
874 self.assertEqual('/internal/taskqueue/cancel-tasks', args[0]) | 975 self.assertEqual('/internal/taskqueue/cancel-tasks', args[0]) |
875 # check task queue | 976 # check task queue |
876 self.assertEqual('cancel-tasks', args[1]) | 977 self.assertEqual('cancel-tasks', args[1]) |
877 return True | 978 return True |
878 self.mock(utils, 'enqueue_task', enqueue_task) | 979 self.mock(utils, 'enqueue_task', enqueue_task) |
879 | 980 |
880 response = self.call_api('cancel', body={u'tags': [u'os:Win']}) | 981 response = self.call_api('cancel', body={u'tags': [u'os:Win']}) |
881 self.assertEqual(expected, response.json) | 982 self.assertEqual(expected, response.json) |
882 | 983 |
883 | |
884 | |
885 def test_list_ok(self): | 984 def test_list_ok(self): |
886 """Asserts that list requests all TaskResultSummaries.""" | 985 """Asserts that list requests all TaskResultSummaries.""" |
887 first, second, str_now_120, start, end = self._gen_two_tasks() | 986 first, second, str_now_120, start, end = self._gen_two_tasks() |
888 first_no_perf = first.copy() | 987 first_no_perf = first.copy() |
889 first_no_perf.pop('performance_stats') | 988 first_no_perf.pop('performance_stats') |
890 # Basic request. | 989 # Basic request. |
891 request = swarming_rpcs.TasksRequest( | 990 request = swarming_rpcs.TasksRequest( |
892 end=end, start=start, include_performance_stats=True) | 991 end=end, start=start, include_performance_stats=True) |
893 expected = {u'now': str_now_120, u'items': [second, first]} | 992 expected = {u'now': str_now_120, u'items': [second, first]} |
894 actual = self.call_api('list', body=message_to_dict(request)).json | 993 actual = self.call_api('list', body=message_to_dict(request)).json |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1059 u'ts': unicode(now.strftime(self.DATETIME_FORMAT)), | 1158 u'ts': unicode(now.strftime(self.DATETIME_FORMAT)), |
1060 } | 1159 } |
1061 self.assertEqual(expected, self.call_api('tags', body={}).json) | 1160 self.assertEqual(expected, self.call_api('tags', body={}).json) |
1062 | 1161 |
1063 def _gen_two_tasks(self): | 1162 def _gen_two_tasks(self): |
1064 # first request | 1163 # first request |
1065 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1164 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1066 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 1165 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
1067 self.mock_now(now) | 1166 self.mock_now(now) |
1068 self.mock(random, 'getrandbits', lambda _: 0x66) | 1167 self.mock(random, 'getrandbits', lambda _: 0x66) |
1069 _, first_id = self.client_create_task_raw( | 1168 _, _, first_id = self.client_create_task_raw( |
1070 name='first', tags=['project:yay', 'commit:post', 'os:Win'], | 1169 name='first', tags=['project:yay', 'commit:post', 'os:Win'], |
1071 properties=dict(idempotent=True)) | 1170 properties=dict(idempotent=True)) |
1072 self.set_as_bot() | 1171 self.set_as_bot() |
1073 self.bot_run_task() | 1172 self.bot_run_task() |
1074 | 1173 |
1075 # second request | 1174 # second request |
1076 self.set_as_user() | 1175 self.set_as_user() |
1077 self.mock(random, 'getrandbits', lambda _: 0x88) | 1176 self.mock(random, 'getrandbits', lambda _: 0x88) |
1078 now_60 = self.mock_now(now, 60) | 1177 now_60 = self.mock_now(now, 60) |
1079 str_now_60 = unicode(now_60.strftime(self.DATETIME_NO_MICRO)) | 1178 str_now_60 = unicode(now_60.strftime(self.DATETIME_NO_MICRO)) |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1186 utils.datetime_to_timestamp(now + datetime.timedelta(days=1)) / | 1285 utils.datetime_to_timestamp(now + datetime.timedelta(days=1)) / |
1187 1000000.) | 1286 1000000.) |
1188 self.set_as_privileged_user() | 1287 self.set_as_privileged_user() |
1189 return first, second, str_now_120, start, end | 1288 return first, second, str_now_120, start, end |
1190 | 1289 |
1191 def _gen_three_pending_tasks(self): | 1290 def _gen_three_pending_tasks(self): |
1192 # Creates three pending tasks, spaced 1 minute apart | 1291 # Creates three pending tasks, spaced 1 minute apart |
1193 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1292 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1194 self.mock_now(now) | 1293 self.mock_now(now) |
1195 self.mock(random, 'getrandbits', lambda _: 0x66) | 1294 self.mock(random, 'getrandbits', lambda _: 0x66) |
1196 _, first_id = self.client_create_task_raw( | 1295 _, _, first_id = self.client_create_task_raw( |
1197 name='first', tags=['project:yay', 'commit:abcd', 'os:Win'], | 1296 name='first', tags=['project:yay', 'commit:abcd', 'os:Win'], |
1198 pubsub_topic='projects/abc/topics/def', | 1297 pubsub_topic='projects/abc/topics/def', |
1199 pubsub_userdata='1234', | 1298 pubsub_userdata='1234', |
1200 properties=dict(idempotent=True)) | 1299 properties=dict(idempotent=True)) |
1201 | 1300 |
1202 now_60 = self.mock_now(now, 60) | 1301 now_60 = self.mock_now(now, 60) |
1203 self.mock(random, 'getrandbits', lambda _: 0x88) | 1302 self.mock(random, 'getrandbits', lambda _: 0x88) |
1204 _, second_id = self.client_create_task_raw( | 1303 _, _, second_id = self.client_create_task_raw( |
1205 name='second', user='jack@localhost', | 1304 name='second', user='jack@localhost', |
1206 pubsub_topic='projects/abc/topics/def', | 1305 pubsub_topic='projects/abc/topics/def', |
1207 pubsub_userdata='5678', | 1306 pubsub_userdata='5678', |
1208 tags=['project:yay', 'commit:efgh', 'os:Win'], | 1307 tags=['project:yay', 'commit:efgh', 'os:Win'], |
1209 properties=dict(idempotent=True)) | 1308 properties=dict(idempotent=True)) |
1210 | 1309 |
1211 now_120 = self.mock_now(now, 120) | 1310 now_120 = self.mock_now(now, 120) |
1212 _, third_id = self.client_create_task_raw( | 1311 _, _, third_id = self.client_create_task_raw( |
1213 name='third', user='jack@localhost', | 1312 name='third', user='jack@localhost', |
1214 pubsub_topic='projects/abc/topics/def', | 1313 pubsub_topic='projects/abc/topics/def', |
1215 pubsub_userdata='9000', | 1314 pubsub_userdata='9000', |
1216 tags=['project:yay', 'commit:ijkhl', 'os:Linux'], | 1315 tags=['project:yay', 'commit:ijkhl', 'os:Linux'], |
1217 properties=dict(idempotent=True)) | 1316 properties=dict(idempotent=True)) |
1218 | 1317 |
1219 return first_id, second_id, third_id, now, now_60, now_120 | 1318 return first_id, second_id, third_id, now, now_60, now_120 |
1220 | 1319 |
1221 | 1320 |
1222 class TaskApiTest(BaseTest): | 1321 class TaskApiTest(BaseTest): |
1223 api_service_cls = handlers_endpoints.SwarmingTaskService | 1322 api_service_cls = handlers_endpoints.SwarmingTaskService |
1224 | 1323 |
1225 def setUp(self): | 1324 def setUp(self): |
1226 super(TaskApiTest, self).setUp() | 1325 super(TaskApiTest, self).setUp() |
1227 self.tasks_api = test_case.Endpoints( | 1326 self.tasks_api = test_case.Endpoints( |
1228 handlers_endpoints.SwarmingTasksService) | 1327 handlers_endpoints.SwarmingTasksService) |
1229 | 1328 |
1230 def test_cancel_ok(self): | 1329 def test_cancel_ok(self): |
1231 """Asserts that task cancellation goes smoothly.""" | 1330 """Asserts that task cancellation goes smoothly.""" |
1232 # catch PubSub notification | 1331 # catch PubSub notification |
1332 notifies = [] | |
1333 def enqueue_task_mock(**kwargs): | |
1334 notifies.append(kwargs) | |
1335 return True | |
1336 self.mock(utils, 'enqueue_task', enqueue_task_mock) | |
M-A Ruel
2017/05/03 01:33:20
enqueue_task is already mocked above, this is not
nodir
2017/05/05 18:11:38
Done
this code was copied from test_cancel_forbid
| |
1337 | |
1233 # Create and cancel a task as a non-privileged user. | 1338 # Create and cancel a task as a non-privileged user. |
1234 self.mock(random, 'getrandbits', lambda _: 0x88) | 1339 self.mock(random, 'getrandbits', lambda _: 0x88) |
1235 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1340 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1236 self.mock_now(now) | 1341 self.mock_now(now) |
1237 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 1342 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
1238 _, task_id = self.client_create_task_raw( | 1343 _, _, task_id = self.client_create_task_raw( |
1239 pubsub_topic='projects/abc/topics/def', | 1344 pubsub_topic='projects/abc/topics/def', |
1240 pubsub_userdata='blah') | 1345 pubsub_userdata='blah') |
1241 expected = {u'ok': True, u'was_running': False} | 1346 expected = {u'ok': True, u'was_running': False} |
1242 response = self.call_api('cancel', body={'task_id': task_id}) | 1347 response = self.call_api('cancel', body={'task_id': task_id}) |
1243 self.assertEqual(expected, response.json) | 1348 self.assertEqual(expected, response.json) |
1244 | 1349 |
1245 # determine that the task's state updates correctly | 1350 # determine that the task's state updates correctly |
1246 expected = { | 1351 expected = { |
1247 u'abandoned_ts': str_now, | 1352 u'abandoned_ts': str_now, |
1248 u'created_ts': str_now, | 1353 u'created_ts': str_now, |
(...skipping 18 matching lines...) Expand all Loading... | |
1267 # notification has been sent. | 1372 # notification has been sent. |
1268 expected = [ | 1373 expected = [ |
1269 { | 1374 { |
1270 'payload': '{"auth_token":null,"task_id":"5cee488008810",' | 1375 'payload': '{"auth_token":null,"task_id":"5cee488008810",' |
1271 '"topic":"projects/abc/topics/def","userdata":"blah"}', | 1376 '"topic":"projects/abc/topics/def","userdata":"blah"}', |
1272 'queue_name': 'pubsub', | 1377 'queue_name': 'pubsub', |
1273 'transactional': True, | 1378 'transactional': True, |
1274 'url': '/internal/taskqueue/pubsub/5cee488008810', | 1379 'url': '/internal/taskqueue/pubsub/5cee488008810', |
1275 }, | 1380 }, |
1276 ] | 1381 ] |
1382 self.assertEqual(expected, notifies) | |
1277 | 1383 |
1278 def test_cancel_forbidden(self): | 1384 def test_cancel_forbidden(self): |
1279 """Asserts that non-privileged non-owner can't cancel tasks.""" | 1385 """Asserts that non-privileged non-owner can't cancel tasks.""" |
1280 # catch PubSub notification | 1386 # catch PubSub notification |
1281 notifies = [] | 1387 notifies = [] |
1282 def enqueue_task_mock(**kwargs): | 1388 def enqueue_task_mock(**kwargs): |
1283 notifies.append(kwargs) | 1389 notifies.append(kwargs) |
1284 return True | 1390 return True |
1285 self.mock(utils, 'enqueue_task', enqueue_task_mock) | 1391 self.mock(utils, 'enqueue_task', enqueue_task_mock) |
1286 | 1392 |
1287 # Create a task as an admin. | 1393 # Create a task as an admin. |
1288 self.mock(random, 'getrandbits', lambda _: 0x88) | 1394 self.mock(random, 'getrandbits', lambda _: 0x88) |
1289 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1395 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1290 self.mock_now(now) | 1396 self.mock_now(now) |
1291 self.set_as_admin() | 1397 self.set_as_admin() |
1292 _, task_id = self.client_create_task_raw( | 1398 _, _, task_id = self.client_create_task_raw( |
1293 pubsub_topic='projects/abc/topics/def', | 1399 pubsub_topic='projects/abc/topics/def', |
1294 pubsub_userdata='blah') | 1400 pubsub_userdata='blah') |
1295 | 1401 |
1296 # Attempt to cancel as non-privileged user -> HTTP 403. | 1402 # Attempt to cancel as non-privileged user -> HTTP 403. |
1297 self.set_as_user() | 1403 self.set_as_user() |
1298 self.call_api('cancel', body={'task_id': task_id}, status=403) | 1404 self.call_api('cancel', body={'task_id': task_id}, status=403) |
1299 | 1405 |
1406 self.assertEqual([], notifies) | |
1407 | |
1408 def test_cancel_by_transaction_id_ok(self): | |
1409 """Asserts that task cancellation goes smoothly.""" | |
1410 # catch PubSub notification | |
1411 notifies = [] | |
1412 def enqueue_task_mock(**kwargs): | |
1413 notifies.append(kwargs) | |
1414 return True | |
1415 self.mock(utils, 'enqueue_task', enqueue_task_mock) | |
M-A Ruel
2017/05/03 01:33:20
same
nodir
2017/05/05 18:11:38
Done.
| |
1416 | |
1417 # Create and cancel a task as a non-privileged user. | |
1418 self.mock(random, 'getrandbits', lambda _: 0x88) | |
1419 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | |
1420 self.mock_now(now) | |
1421 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | |
1422 _, _, task_id = self.client_create_task_raw( | |
1423 pubsub_topic='projects/abc/topics/def', | |
1424 pubsub_userdata='blah', | |
1425 transaction_id='txn') | |
1426 expected = {u'ok': True, u'was_running': False} | |
1427 response = self.call_api('cancel', body={'transaction_id': 'txn'}) | |
1428 self.assertEqual(expected, response.json) | |
1429 | |
1430 # determine that the task's state updates correctly | |
1431 expected = { | |
1432 u'abandoned_ts': str_now, | |
1433 u'created_ts': str_now, | |
1434 u'failure': False, | |
1435 u'internal_failure': False, | |
1436 u'modified_ts': str_now, | |
1437 u'name': u'hi', | |
1438 u'state': u'CANCELED', | |
1439 u'tags': [ | |
1440 u'os:Amiga', | |
1441 u'pool:default', | |
1442 u'priority:100', | |
1443 u'service_account:none', | |
1444 u'user:joe@localhost', | |
1445 ], | |
1446 u'task_id': task_id, | |
1447 u'user': u'joe@localhost', | |
1448 } | |
1449 response = self.call_api('result', body={'task_id': task_id}) | |
1450 self.assertEqual(expected, response.json) | |
1451 | |
1452 expected = [ | |
1453 { | |
1454 'payload': '{"auth_token":null,"task_id":"5cee488008810",' | |
1455 '"topic":"projects/abc/topics/def","userdata":"blah"}', | |
1456 'queue_name': 'pubsub', | |
1457 'transactional': True, | |
1458 'url': '/internal/taskqueue/pubsub/5cee488008810', | |
1459 }, | |
1460 ] | |
1461 self.assertEqual(expected, notifies) | |
1462 | |
1463 def test_cancel_by_transaction_id_forbidden(self): | |
1464 """Asserts that non-privileged non-owner can't cancel tasks.""" | |
1465 # catch PubSub notification | |
1466 notifies = [] | |
1467 def enqueue_task_mock(**kwargs): | |
1468 notifies.append(kwargs) | |
1469 return True | |
1470 self.mock(utils, 'enqueue_task', enqueue_task_mock) | |
M-A Ruel
2017/05/03 01:33:20
same
nodir
2017/05/05 18:11:38
Done.
| |
1471 | |
1472 # Create a task as an admin. | |
1473 self.mock(random, 'getrandbits', lambda _: 0x88) | |
1474 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | |
1475 self.mock_now(now) | |
1476 self.set_as_admin() | |
1477 self.client_create_task_raw( | |
1478 pubsub_topic='projects/abc/topics/def', | |
1479 pubsub_userdata='blah', | |
1480 transaction_id='txn') | |
1481 | |
1482 # Attempt to cancel as non-privileged user -> HTTP 403. | |
1483 self.set_as_user() | |
1484 self.call_api('cancel', body={'transaction_id': 'txn'}, status=403) | |
1485 | |
1486 self.assertEqual([], notifies) | |
1487 | |
1300 def test_task_canceled(self): | 1488 def test_task_canceled(self): |
1301 self.mock(random, 'getrandbits', lambda _: 0x88) | 1489 self.mock(random, 'getrandbits', lambda _: 0x88) |
1302 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1490 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1303 self.mock_now(now) | 1491 self.mock_now(now) |
1304 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 1492 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
1305 _, task_id = self.client_create_task_raw( | 1493 _, _, task_id = self.client_create_task_raw( |
1306 properties=dict(command=['python', 'runtest.py'])) | 1494 properties=dict(command=['python', 'runtest.py'])) |
1307 | 1495 |
1308 self.set_as_bot() | 1496 self.set_as_bot() |
1309 params = self.do_handshake() | 1497 params = self.do_handshake() |
1310 data = self.post_json('/swarming/api/v1/bot/poll', params) | 1498 data = self.post_json('/swarming/api/v1/bot/poll', params) |
1311 run_id = data['manifest']['task_id'] | 1499 run_id = data['manifest']['task_id'] |
1312 def _params(**kwargs): | 1500 def _params(**kwargs): |
1313 out = { | 1501 out = { |
1314 'cost_usd': 0.1, | 1502 'cost_usd': 0.1, |
1315 'duration': None, | 1503 'duration': None, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1380 self.call_api('result', body={'task_id': '12310'}, status=404) | 1568 self.call_api('result', body={'task_id': '12310'}, status=404) |
1381 | 1569 |
1382 def test_result_ok(self): | 1570 def test_result_ok(self): |
1383 """Asserts that result produces a result entity.""" | 1571 """Asserts that result produces a result entity.""" |
1384 self.mock(random, 'getrandbits', lambda _: 0x88) | 1572 self.mock(random, 'getrandbits', lambda _: 0x88) |
1385 | 1573 |
1386 # pending task | 1574 # pending task |
1387 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 1575 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
1388 self.mock_now(now) | 1576 self.mock_now(now) |
1389 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) | 1577 str_now = unicode(now.strftime(self.DATETIME_NO_MICRO)) |
1390 _, task_id = self.client_create_task_raw() | 1578 _, _, task_id = self.client_create_task_raw() |
1391 response = self.call_api('result', body={'task_id': task_id}) | 1579 response = self.call_api('result', body={'task_id': task_id}) |
1392 expected = { | 1580 expected = { |
1393 u'created_ts': str_now, | 1581 u'created_ts': str_now, |
1394 u'failure': False, | 1582 u'failure': False, |
1395 u'internal_failure': False, | 1583 u'internal_failure': False, |
1396 u'modified_ts': str_now, | 1584 u'modified_ts': str_now, |
1397 u'name': u'hi', | 1585 u'name': u'hi', |
1398 u'state': u'PENDING', | 1586 u'state': u'PENDING', |
1399 u'tags': [ | 1587 u'tags': [ |
1400 u'os:Amiga', | 1588 u'os:Amiga', |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1512 | 1700 |
1513 self.set_as_privileged_user() | 1701 self.set_as_privileged_user() |
1514 run_id = task_id[:-1] + '1' | 1702 run_id = task_id[:-1] + '1' |
1515 expected = {u'output': u'rÉsult string'} | 1703 expected = {u'output': u'rÉsult string'} |
1516 for i in (task_id, run_id): | 1704 for i in (task_id, run_id): |
1517 response = self.call_api('stdout', body={'task_id': i}) | 1705 response = self.call_api('stdout', body={'task_id': i}) |
1518 self.assertEqual(expected, response.json) | 1706 self.assertEqual(expected, response.json) |
1519 | 1707 |
1520 def test_stdout_empty(self): | 1708 def test_stdout_empty(self): |
1521 """Asserts that incipient tasks produce no output.""" | 1709 """Asserts that incipient tasks produce no output.""" |
1522 _, task_id = self.client_create_task_raw() | 1710 _, _, task_id = self.client_create_task_raw() |
1523 response = self.call_api('stdout', body={'task_id': task_id}) | 1711 response = self.call_api('stdout', body={'task_id': task_id}) |
1524 self.assertEqual({}, response.json) | 1712 self.assertEqual({}, response.json) |
1525 | 1713 |
1526 run_id = task_id[:-1] + '1' | 1714 run_id = task_id[:-1] + '1' |
1527 self.call_api('stdout', body={'task_id': run_id}, status=404) | 1715 self.call_api('stdout', body={'task_id': run_id}, status=404) |
1528 | 1716 |
1529 def test_result_run_not_found(self): | 1717 def test_result_run_not_found(self): |
1530 """Asserts that getting results from incipient tasks raises 404.""" | 1718 """Asserts that getting results from incipient tasks raises 404.""" |
1531 _, task_id = self.client_create_task_raw() | 1719 _, _, task_id = self.client_create_task_raw() |
1532 run_id = task_id[:-1] + '1' | 1720 run_id = task_id[:-1] + '1' |
1533 self.call_api('stdout', body={'task_id': run_id}, status=404) | 1721 self.call_api('stdout', body={'task_id': run_id}, status=404) |
1534 | 1722 |
1535 def test_task_deduped(self): | 1723 def test_task_deduped(self): |
1536 """Asserts that task deduplication works as expected.""" | 1724 """Asserts that task deduplication works as expected.""" |
1537 _, task_id_1 = self.client_create_task_raw(properties=dict(idempotent=True)) | 1725 _, _, task_id_1 = self.client_create_task_raw(properties=dict(idempotent=Tru e)) |
1538 | 1726 |
1539 self.set_as_bot() | 1727 self.set_as_bot() |
1540 task_id_bot = self.bot_run_task() | 1728 task_id_bot = self.bot_run_task() |
1541 self.assertEqual(task_id_1, task_id_bot[:-1] + '0') | 1729 self.assertEqual(task_id_1, task_id_bot[:-1] + '0') |
1542 self.assertEqual('1', task_id_bot[-1:]) | 1730 self.assertEqual('1', task_id_bot[-1:]) |
1543 | 1731 |
1544 # second task; this one's results should be returned immediately | 1732 # second task; this one's results should be returned immediately |
1545 self.set_as_user() | 1733 self.set_as_user() |
1546 _, task_id_2 = self.client_create_task_raw( | 1734 _, _, task_id_2 = self.client_create_task_raw( |
1547 name='second', user='jack@localhost', properties=dict(idempotent=True)) | 1735 name='second', user='jack@localhost', properties=dict(idempotent=True)) |
1548 | 1736 |
1549 self.set_as_bot() | 1737 self.set_as_bot() |
1550 resp = self.bot_poll() | 1738 resp = self.bot_poll() |
1551 self.assertEqual('sleep', resp['cmd']) | 1739 self.assertEqual('sleep', resp['cmd']) |
1552 | 1740 |
1553 self.set_as_user() | 1741 self.set_as_user() |
1554 | 1742 |
1555 # results shouldn't change, even if the second task wasn't executed | 1743 # results shouldn't change, even if the second task wasn't executed |
1556 response = self.call_api('stdout', body={'task_id': task_id_2}) | 1744 response = self.call_api('stdout', body={'task_id': task_id_2}) |
1557 self.assertEqual({'output': u'rÉsult string'}, response.json) | 1745 self.assertEqual({'output': u'rÉsult string'}, response.json) |
1558 | 1746 |
1559 def test_request_unknown(self): | 1747 def test_request_unknown(self): |
1560 """Asserts that 404 is raised for unknown tasks.""" | 1748 """Asserts that 404 is raised for unknown tasks.""" |
1561 self.call_api('request', body={'task_id': '12310'}, status=404) | 1749 self.call_api('request', body={'task_id': '12310'}, status=404) |
1562 | 1750 |
1563 def test_request_ok(self): | 1751 def test_request_ok(self): |
1564 """Asserts that request produces a task request.""" | 1752 """Asserts that request produces a task request.""" |
1565 now = datetime.datetime(2010, 1, 2, 3, 4, 5, 6) | 1753 now = datetime.datetime(2010, 1, 2, 3, 4, 5, 6) |
1566 self.mock_now(now) | 1754 self.mock_now(now) |
1567 _, task_id = self.client_create_task_raw() | 1755 _, _, task_id = self.client_create_task_raw() |
1568 response = self.call_api('request', body={'task_id': task_id}) | 1756 response = self.call_api('request', body={'task_id': task_id}) |
1569 expected = { | 1757 expected = { |
1570 u'authenticated': u'user:user@example.com', | 1758 u'authenticated': u'user:user@example.com', |
1571 u'created_ts': unicode(now.strftime(self.DATETIME_FORMAT)), | 1759 u'created_ts': unicode(now.strftime(self.DATETIME_FORMAT)), |
1572 u'expiration_secs': unicode(24 * 60 * 60), | 1760 u'expiration_secs': unicode(24 * 60 * 60), |
1573 u'name': u'hi', | 1761 u'name': u'hi', |
1574 u'priority': u'100', | 1762 u'priority': u'100', |
1575 u'properties': { | 1763 u'properties': { |
1576 u'cipd_input': { | 1764 u'cipd_input': { |
1577 u'client_package': { | 1765 u'client_package': { |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2191 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) | 2379 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) |
2192 | 2380 |
2193 | 2381 |
2194 if __name__ == '__main__': | 2382 if __name__ == '__main__': |
2195 if '-v' in sys.argv: | 2383 if '-v' in sys.argv: |
2196 unittest.TestCase.maxDiff = None | 2384 unittest.TestCase.maxDiff = None |
2197 logging.basicConfig(level=logging.DEBUG) | 2385 logging.basicConfig(level=logging.DEBUG) |
2198 else: | 2386 else: |
2199 logging.basicConfig(level=logging.CRITICAL) | 2387 logging.basicConfig(level=logging.CRITICAL) |
2200 unittest.main() | 2388 unittest.main() |
OLD | NEW |