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 logging | 9 import logging |
10 import os | 10 import os |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 265 |
266 def test_poll_task_raw(self): | 266 def test_poll_task_raw(self): |
267 # Successfully poll a task. | 267 # Successfully poll a task. |
268 self.mock(random, 'getrandbits', lambda _: 0x88) | 268 self.mock(random, 'getrandbits', lambda _: 0x88) |
269 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 269 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
270 self.mock_now(now) | 270 self.mock_now(now) |
271 str_now = unicode(now.strftime(DATETIME_FORMAT)) | 271 str_now = unicode(now.strftime(DATETIME_FORMAT)) |
272 # A bot polls, gets a task, updates it, completes it. | 272 # A bot polls, gets a task, updates it, completes it. |
273 params = self.do_handshake() | 273 params = self.do_handshake() |
274 # Enqueue a task. | 274 # Enqueue a task. |
275 _, task_id = self.client_create_task_raw() | 275 _, _, task_id = self.client_create_task_raw() |
276 self.assertEqual('0', task_id[-1]) | 276 self.assertEqual('0', task_id[-1]) |
277 # Convert TaskResultSummary reference to TaskRunResult. | 277 # Convert TaskResultSummary reference to TaskRunResult. |
278 task_id = task_id[:-1] + '1' | 278 task_id = task_id[:-1] + '1' |
279 response = self.post_json('/swarming/api/v1/bot/poll', params) | 279 response = self.post_json('/swarming/api/v1/bot/poll', params) |
280 expected = { | 280 expected = { |
281 u'cmd': u'run', | 281 u'cmd': u'run', |
282 u'manifest': { | 282 u'manifest': { |
283 u'bot_id': u'bot1', | 283 u'bot_id': u'bot1', |
284 u'caches': [], | 284 u'caches': [], |
285 u'cipd_input': { | 285 u'cipd_input': { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 u'started_ts': str_now, | 334 u'started_ts': str_now, |
335 u'state': u'RUNNING', | 335 u'state': u'RUNNING', |
336 u'task_id': u'5cee488008811', | 336 u'task_id': u'5cee488008811', |
337 u'try_number': u'1', | 337 u'try_number': u'1', |
338 } | 338 } |
339 self.assertEqual(expected, response) | 339 self.assertEqual(expected, response) |
340 | 340 |
341 def test_poll_task_with_bot_service_account(self): | 341 def test_poll_task_with_bot_service_account(self): |
342 params = self.do_handshake() | 342 params = self.do_handshake() |
343 | 343 |
344 _, task_id = self.client_create_task_raw(service_account_token='bot') | 344 _, _, task_id = self.client_create_task_raw(service_account_token='bot') |
345 self.assertEqual('0', task_id[-1]) | 345 self.assertEqual('0', task_id[-1]) |
346 task_id = task_id[:-1] + '1' | 346 task_id = task_id[:-1] + '1' |
347 | 347 |
348 response = self.post_json('/swarming/api/v1/bot/poll', params) | 348 response = self.post_json('/swarming/api/v1/bot/poll', params) |
349 expected = { | 349 expected = { |
350 u'cmd': u'run', | 350 u'cmd': u'run', |
351 u'manifest': { | 351 u'manifest': { |
352 u'bot_id': u'bot1', | 352 u'bot_id': u'bot1', |
353 u'caches': [], | 353 u'caches': [], |
354 u'cipd_input': { | 354 u'cipd_input': { |
(...skipping 25 matching lines...) Expand all Loading... |
380 u'outputs': [u'foo', u'path/to/foobar'], | 380 u'outputs': [u'foo', u'path/to/foobar'], |
381 u'service_account': u'bot', | 381 u'service_account': u'bot', |
382 u'task_id': task_id, | 382 u'task_id': task_id, |
383 }, | 383 }, |
384 } | 384 } |
385 self.assertEqual(expected, response) | 385 self.assertEqual(expected, response) |
386 | 386 |
387 def test_poll_task_with_caches(self): | 387 def test_poll_task_with_caches(self): |
388 params = self.do_handshake() | 388 params = self.do_handshake() |
389 | 389 |
390 _, task_id = self.client_create_task_raw({ | 390 _, _, task_id = self.client_create_task_raw({ |
391 'caches': [{ | 391 'caches': [{ |
392 'name': 'git_infra', | 392 'name': 'git_infra', |
393 'path': 'git_cache', | 393 'path': 'git_cache', |
394 }], | 394 }], |
395 }) | 395 }) |
396 self.assertEqual('0', task_id[-1]) | 396 self.assertEqual('0', task_id[-1]) |
397 task_id = task_id[:-1] + '1' | 397 task_id = task_id[:-1] + '1' |
398 | 398 |
399 response = self.post_json('/swarming/api/v1/bot/poll', params) | 399 response = self.post_json('/swarming/api/v1/bot/poll', params) |
400 expected = { | 400 expected = { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 | 495 |
496 def test_complete_task_isolated(self): | 496 def test_complete_task_isolated(self): |
497 # Successfully poll a task. | 497 # Successfully poll a task. |
498 self.mock(random, 'getrandbits', lambda _: 0x88) | 498 self.mock(random, 'getrandbits', lambda _: 0x88) |
499 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 499 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
500 self.mock_now(now) | 500 self.mock_now(now) |
501 str_now = unicode(now.strftime(DATETIME_FORMAT)) | 501 str_now = unicode(now.strftime(DATETIME_FORMAT)) |
502 # A bot polls, gets a task, updates it, completes it. | 502 # A bot polls, gets a task, updates it, completes it. |
503 params = self.do_handshake() | 503 params = self.do_handshake() |
504 # Enqueue a task. | 504 # Enqueue a task. |
505 _, task_id = self.client_create_task_isolated() | 505 _, _, task_id = self.client_create_task_isolated() |
506 self.assertEqual('0', task_id[-1]) | 506 self.assertEqual('0', task_id[-1]) |
507 # Convert TaskResultSummary reference to TaskRunResult. | 507 # Convert TaskResultSummary reference to TaskRunResult. |
508 task_id = task_id[:-1] + '1' | 508 task_id = task_id[:-1] + '1' |
509 response = self.post_json('/swarming/api/v1/bot/poll', params) | 509 response = self.post_json('/swarming/api/v1/bot/poll', params) |
510 expected = { | 510 expected = { |
511 u'cmd': u'run', | 511 u'cmd': u'run', |
512 u'manifest': { | 512 u'manifest': { |
513 u'bot_id': u'bot1', | 513 u'bot_id': u'bot1', |
514 u'caches': [], | 514 u'caches': [], |
515 u'cipd_input': { | 515 u'cipd_input': { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 u'server_versions': [u'v1a'], | 631 u'server_versions': [u'v1a'], |
632 u'started_ts': str_now, | 632 u'started_ts': str_now, |
633 u'state': u'COMPLETED', | 633 u'state': u'COMPLETED', |
634 u'task_id': u'5cee488008811', | 634 u'task_id': u'5cee488008811', |
635 u'try_number': u'1', | 635 u'try_number': u'1', |
636 } | 636 } |
637 self.assertEqual(expected, response) | 637 self.assertEqual(expected, response) |
638 | 638 |
639 def test_poll_not_enough_time(self): | 639 def test_poll_not_enough_time(self): |
640 # Make sure there's a task that we don't get. | 640 # Make sure there's a task that we don't get. |
641 _, task_id = self.client_create_task_raw() | 641 _, _, task_id = self.client_create_task_raw() |
642 self.assertEqual('0', task_id[-1]) | 642 self.assertEqual('0', task_id[-1]) |
643 params = self.do_handshake() | 643 params = self.do_handshake() |
644 bot_info = bot_management.get_info_key('bot1').get() | 644 bot_info = bot_management.get_info_key('bot1').get() |
645 bot_info.lease_expiration_ts = datetime.datetime(1969, 1, 1) | 645 bot_info.lease_expiration_ts = datetime.datetime(1969, 1, 1) |
646 bot_info.put() | 646 bot_info.put() |
647 response = self.post_json('/swarming/api/v1/bot/poll', params) | 647 response = self.post_json('/swarming/api/v1/bot/poll', params) |
648 expected = { | 648 expected = { |
649 u'cmd': u'sleep', | 649 u'cmd': u'sleep', |
650 u'quarantined': False, | 650 u'quarantined': False, |
651 } | 651 } |
652 self.assertTrue(response.pop('duration')) | 652 self.assertTrue(response.pop('duration')) |
653 self.assertEqual(expected, response) | 653 self.assertEqual(expected, response) |
654 | 654 |
655 def test_poll_enough_time(self): | 655 def test_poll_enough_time(self): |
656 # Successfully poll a task. | 656 # Successfully poll a task. |
657 self.mock(random, 'getrandbits', lambda _: 0x88) | 657 self.mock(random, 'getrandbits', lambda _: 0x88) |
658 now = datetime.datetime(2010, 1, 2, 3, 4, 5) | 658 now = datetime.datetime(2010, 1, 2, 3, 4, 5) |
659 self.mock_now(now) | 659 self.mock_now(now) |
660 _, task_id = self.client_create_task_isolated() | 660 _, _, task_id = self.client_create_task_isolated() |
661 self.assertEqual('0', task_id[-1]) | 661 self.assertEqual('0', task_id[-1]) |
662 params = self.do_handshake() | 662 params = self.do_handshake() |
663 bot_info = bot_management.get_info_key('bot1').get() | 663 bot_info = bot_management.get_info_key('bot1').get() |
664 bot_info.lease_expiration_ts = datetime.datetime(3000, 1, 1) | 664 bot_info.lease_expiration_ts = datetime.datetime(3000, 1, 1) |
665 bot_info.put() | 665 bot_info.put() |
666 response = self.post_json('/swarming/api/v1/bot/poll', params) | 666 response = self.post_json('/swarming/api/v1/bot/poll', params) |
667 # Convert TaskResultSummary reference to TaskRunResult. | 667 # Convert TaskResultSummary reference to TaskRunResult. |
668 task_id = task_id[:-1] + '1' | 668 task_id = task_id[:-1] + '1' |
669 expected = { | 669 expected = { |
670 u'cmd': u'run', | 670 u'cmd': u'run', |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1150 self.app.get('/bot_code?tok=%s' % tok, status=200) | 1150 self.app.get('/bot_code?tok=%s' % tok, status=200) |
1151 | 1151 |
1152 | 1152 |
1153 if __name__ == '__main__': | 1153 if __name__ == '__main__': |
1154 if '-v' in sys.argv: | 1154 if '-v' in sys.argv: |
1155 unittest.TestCase.maxDiff = None | 1155 unittest.TestCase.maxDiff = None |
1156 logging.basicConfig( | 1156 logging.basicConfig( |
1157 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL, | 1157 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL, |
1158 format='%(levelname)-7s %(filename)s:%(lineno)3d %(message)s') | 1158 format='%(levelname)-7s %(filename)s:%(lineno)3d %(message)s') |
1159 unittest.main() | 1159 unittest.main() |
OLD | NEW |