| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import datetime | 6 import datetime |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import random | 9 import random |
| 10 import sys | 10 import sys |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 'completed_ts': None, | 201 'completed_ts': None, |
| 202 'cost_usd': 0., | 202 'cost_usd': 0., |
| 203 'duration': None, | 203 'duration': None, |
| 204 'exit_code': None, | 204 'exit_code': None, |
| 205 'failure': False, | 205 'failure': False, |
| 206 'id': '1d69b9f088008811', | 206 'id': '1d69b9f088008811', |
| 207 'internal_failure': False, | 207 'internal_failure': False, |
| 208 'modified_ts': None, | 208 'modified_ts': None, |
| 209 'outputs_ref': None, | 209 'outputs_ref': None, |
| 210 'server_versions': ['v1a'], | 210 'server_versions': ['v1a'], |
| 211 'started_ts': self.now, | 211 'started_ts': None, |
| 212 'state': task_result.State.RUNNING, | 212 'state': task_result.State.RUNNING, |
| 213 'try_number': 1, | 213 'try_number': 1, |
| 214 } | 214 } |
| 215 self.assertEqual(expected, actual.to_dict()) | 215 self.assertEqual(expected, actual.to_dict()) |
| 216 self.assertEqual(50, actual.request.priority) | 216 self.assertEqual(50, actual.request.priority) |
| 217 self.assertEqual(False, actual.can_be_canceled) | 217 self.assertEqual(False, actual.can_be_canceled) |
| 218 | 218 |
| 219 def test_integration(self): | 219 def test_integration(self): |
| 220 # Creates a TaskRequest, along its TaskResultSummary and TaskToRun. Have a | 220 # Creates a TaskRequest, along its TaskResultSummary and TaskToRun. Have a |
| 221 # bot reap the task, and complete the task. Ensure the resulting | 221 # bot reap the task, and complete the task. Ensure the resulting |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 # Nothing changed 2 secs later except latency. | 264 # Nothing changed 2 secs later except latency. |
| 265 self.mock_now(self.now, 2) | 265 self.mock_now(self.now, 2) |
| 266 self.assertEqual(expected, result_summary.to_dict()) | 266 self.assertEqual(expected, result_summary.to_dict()) |
| 267 | 267 |
| 268 # Task is reaped after 2 seconds (4 secs total). | 268 # Task is reaped after 2 seconds (4 secs total). |
| 269 reap_ts = self.now + datetime.timedelta(seconds=4) | 269 reap_ts = self.now + datetime.timedelta(seconds=4) |
| 270 self.mock_now(reap_ts) | 270 self.mock_now(reap_ts) |
| 271 to_run.queue_number = None | 271 to_run.queue_number = None |
| 272 to_run.put() | 272 to_run.put() |
| 273 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) | 273 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) |
| 274 run_result.modified_ts = utils.utcnow() | 274 run_result.started_ts = utils.utcnow() |
| 275 run_result.modified_ts = run_result.started_ts |
| 275 result_summary.set_from_run_result(run_result, request) | 276 result_summary.set_from_run_result(run_result, request) |
| 276 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) | 277 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) |
| 277 expected = { | 278 expected = { |
| 278 'abandoned_ts': None, | 279 'abandoned_ts': None, |
| 279 'bot_dimensions': {}, | 280 'bot_dimensions': {}, |
| 280 'bot_id': u'localhost', | 281 'bot_id': u'localhost', |
| 281 'bot_version': u'abc', | 282 'bot_version': u'abc', |
| 282 'cipd_pins': None, | 283 'cipd_pins': None, |
| 283 'children_task_ids': [], | 284 'children_task_ids': [], |
| 284 'completed_ts': None, | 285 'completed_ts': None, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 task_pack.pack_run_result_key(run_result.key), | 412 task_pack.pack_run_result_key(run_result.key), |
| 412 run_result.task_id) | 413 run_result.task_id) |
| 413 self.assertEqual(complete_ts, run_result.ended_ts) | 414 self.assertEqual(complete_ts, run_result.ended_ts) |
| 414 | 415 |
| 415 def test_yield_run_result_keys_with_dead_bot(self): | 416 def test_yield_run_result_keys_with_dead_bot(self): |
| 416 request = mkreq(_gen_request()) | 417 request = mkreq(_gen_request()) |
| 417 result_summary = task_result.new_result_summary(request) | 418 result_summary = task_result.new_result_summary(request) |
| 418 result_summary.modified_ts = utils.utcnow() | 419 result_summary.modified_ts = utils.utcnow() |
| 419 ndb.transaction(result_summary.put) | 420 ndb.transaction(result_summary.put) |
| 420 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) | 421 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) |
| 421 run_result.completed_ts = utils.utcnow() | 422 run_result.started_ts = utils.utcnow() |
| 422 run_result.modified_ts = utils.utcnow() | 423 run_result.completed_ts = run_result.started_ts |
| 424 run_result.modified_ts = run_result.started_ts |
| 423 result_summary.set_from_run_result(run_result, request) | 425 result_summary.set_from_run_result(run_result, request) |
| 424 ndb.transaction(lambda: ndb.put_multi((run_result, result_summary))) | 426 ndb.transaction(lambda: ndb.put_multi((run_result, result_summary))) |
| 425 | 427 |
| 426 self.mock_now(self.now + task_result.BOT_PING_TOLERANCE) | 428 self.mock_now(self.now + task_result.BOT_PING_TOLERANCE) |
| 427 self.assertEqual( | 429 self.assertEqual( |
| 428 [], list(task_result.yield_run_result_keys_with_dead_bot())) | 430 [], list(task_result.yield_run_result_keys_with_dead_bot())) |
| 429 | 431 |
| 430 self.mock_now(self.now + task_result.BOT_PING_TOLERANCE, 1) | 432 self.mock_now(self.now + task_result.BOT_PING_TOLERANCE, 1) |
| 431 self.assertEqual( | 433 self.assertEqual( |
| 432 [run_result.key], | 434 [run_result.key], |
| 433 list(task_result.yield_run_result_keys_with_dead_bot())) | 435 list(task_result.yield_run_result_keys_with_dead_bot())) |
| 434 | 436 |
| 435 def test_set_from_run_result(self): | 437 def test_set_from_run_result(self): |
| 436 request = mkreq(_gen_request()) | 438 request = mkreq(_gen_request()) |
| 437 result_summary = task_result.new_result_summary(request) | 439 result_summary = task_result.new_result_summary(request) |
| 438 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) | 440 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) |
| 441 run_result.started_ts = utils.utcnow() |
| 439 self.assertTrue(result_summary.need_update_from_run_result(run_result)) | 442 self.assertTrue(result_summary.need_update_from_run_result(run_result)) |
| 440 result_summary.modified_ts = utils.utcnow() | 443 result_summary.modified_ts = utils.utcnow() |
| 441 run_result.modified_ts = utils.utcnow() | 444 run_result.modified_ts = utils.utcnow() |
| 442 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) | 445 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) |
| 443 | 446 |
| 444 self.assertTrue(result_summary.need_update_from_run_result(run_result)) | 447 self.assertTrue(result_summary.need_update_from_run_result(run_result)) |
| 445 result_summary.set_from_run_result(run_result, request) | 448 result_summary.set_from_run_result(run_result, request) |
| 446 ndb.transaction(lambda: ndb.put_multi([result_summary])) | 449 ndb.transaction(lambda: ndb.put_multi([result_summary])) |
| 447 | 450 |
| 448 self.assertFalse(result_summary.need_update_from_run_result(run_result)) | 451 self.assertFalse(result_summary.need_update_from_run_result(run_result)) |
| 449 | 452 |
| 450 def test_set_from_run_result_two_server_versions(self): | 453 def test_set_from_run_result_two_server_versions(self): |
| 451 request = mkreq(_gen_request()) | 454 request = mkreq(_gen_request()) |
| 452 result_summary = task_result.new_result_summary(request) | 455 result_summary = task_result.new_result_summary(request) |
| 453 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) | 456 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) |
| 457 run_result.started_ts = utils.utcnow() |
| 454 self.assertTrue(result_summary.need_update_from_run_result(run_result)) | 458 self.assertTrue(result_summary.need_update_from_run_result(run_result)) |
| 455 result_summary.modified_ts = utils.utcnow() | 459 result_summary.modified_ts = utils.utcnow() |
| 456 run_result.modified_ts = utils.utcnow() | 460 run_result.modified_ts = utils.utcnow() |
| 457 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) | 461 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) |
| 458 | 462 |
| 459 self.assertTrue(result_summary.need_update_from_run_result(run_result)) | 463 self.assertTrue(result_summary.need_update_from_run_result(run_result)) |
| 460 result_summary.set_from_run_result(run_result, request) | 464 result_summary.set_from_run_result(run_result, request) |
| 461 ndb.transaction(lambda: ndb.put_multi([result_summary])) | 465 ndb.transaction(lambda: ndb.put_multi([result_summary])) |
| 462 | 466 |
| 463 run_result.signal_server_version('new-version') | 467 run_result.signal_server_version('new-version') |
| 464 run_result.modified_ts = utils.utcnow() | 468 run_result.modified_ts = utils.utcnow() |
| 465 result_summary.set_from_run_result(run_result, request) | 469 result_summary.set_from_run_result(run_result, request) |
| 466 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) | 470 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) |
| 467 self.assertEqual( | 471 self.assertEqual( |
| 468 ['v1a', 'new-version'], run_result.key.get().server_versions) | 472 ['v1a', 'new-version'], run_result.key.get().server_versions) |
| 469 self.assertEqual( | 473 self.assertEqual( |
| 470 ['v1a', 'new-version'], result_summary.key.get().server_versions) | 474 ['v1a', 'new-version'], result_summary.key.get().server_versions) |
| 471 | 475 |
| 472 def test_set_from_run_result_two_tries(self): | 476 def test_set_from_run_result_two_tries(self): |
| 473 request = mkreq(_gen_request()) | 477 request = mkreq(_gen_request()) |
| 474 result_summary = task_result.new_result_summary(request) | 478 result_summary = task_result.new_result_summary(request) |
| 475 run_result_1 = task_result.new_run_result( | 479 run_result_1 = task_result.new_run_result( |
| 476 request, 1, 'localhost', 'abc', {}) | 480 request, 1, 'localhost', 'abc', {}) |
| 481 run_result_1.started_ts = utils.utcnow() |
| 477 run_result_2 = task_result.new_run_result( | 482 run_result_2 = task_result.new_run_result( |
| 478 request, 2, 'localhost', 'abc', {}) | 483 request, 2, 'localhost', 'abc', {}) |
| 484 run_result_2.started_ts = utils.utcnow() |
| 479 self.assertTrue(result_summary.need_update_from_run_result(run_result_1)) | 485 self.assertTrue(result_summary.need_update_from_run_result(run_result_1)) |
| 480 run_result_2.modified_ts = utils.utcnow() | 486 run_result_2.modified_ts = utils.utcnow() |
| 481 result_summary.modified_ts = utils.utcnow() | 487 result_summary.modified_ts = utils.utcnow() |
| 482 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result_2))) | 488 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result_2))) |
| 483 | 489 |
| 484 self.assertTrue(result_summary.need_update_from_run_result(run_result_1)) | 490 self.assertTrue(result_summary.need_update_from_run_result(run_result_1)) |
| 485 run_result_1.modified_ts = utils.utcnow() | 491 run_result_1.modified_ts = utils.utcnow() |
| 486 result_summary.set_from_run_result(run_result_1, request) | 492 result_summary.set_from_run_result(run_result_1, request) |
| 487 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result_1))) | 493 ndb.transaction(lambda: ndb.put_multi((result_summary, run_result_1))) |
| 488 | 494 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 516 | 522 |
| 517 def test_run_result_timeout(self): | 523 def test_run_result_timeout(self): |
| 518 request = mkreq(_gen_request()) | 524 request = mkreq(_gen_request()) |
| 519 result_summary = task_result.new_result_summary(request) | 525 result_summary = task_result.new_result_summary(request) |
| 520 result_summary.modified_ts = utils.utcnow() | 526 result_summary.modified_ts = utils.utcnow() |
| 521 ndb.transaction(result_summary.put) | 527 ndb.transaction(result_summary.put) |
| 522 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) | 528 run_result = task_result.new_run_result(request, 1, 'localhost', 'abc', {}) |
| 523 run_result.state = task_result.State.TIMED_OUT | 529 run_result.state = task_result.State.TIMED_OUT |
| 524 run_result.duration = 0.1 | 530 run_result.duration = 0.1 |
| 525 run_result.exit_code = -1 | 531 run_result.exit_code = -1 |
| 526 run_result.completed_ts = utils.utcnow() | 532 run_result.started_ts = utils.utcnow() |
| 527 run_result.modified_ts = utils.utcnow() | 533 run_result.completed_ts = run_result.started_ts |
| 534 run_result.modified_ts = run_result.started_ts |
| 528 result_summary.set_from_run_result(run_result, request) | 535 result_summary.set_from_run_result(run_result, request) |
| 529 ndb.transaction(lambda: ndb.put_multi((run_result, result_summary))) | 536 ndb.transaction(lambda: ndb.put_multi((run_result, result_summary))) |
| 530 run_result = run_result.key.get() | 537 run_result = run_result.key.get() |
| 531 result_summary = result_summary.key.get() | 538 result_summary = result_summary.key.get() |
| 532 self.assertEqual(True, run_result.failure) | 539 self.assertEqual(True, run_result.failure) |
| 533 self.assertEqual(True, result_summary.failure) | 540 self.assertEqual(True, result_summary.failure) |
| 534 | 541 |
| 535 def test_performance_stats_pre_put_hook(self): | 542 def test_performance_stats_pre_put_hook(self): |
| 536 with self.assertRaises(datastore_errors.BadValueError): | 543 with self.assertRaises(datastore_errors.BadValueError): |
| 537 task_result.PerformanceStats().put() | 544 task_result.PerformanceStats().put() |
| 538 | 545 |
| 539 def test_get_result_summaries_query(self): | 546 def test_get_result_summaries_query(self): |
| 540 # Indirectly tested by API. | 547 # Indirectly tested by API. |
| 541 pass | 548 pass |
| 542 | 549 |
| 543 def test_get_run_results_query(self): | 550 def test_get_run_results_query(self): |
| 544 # Indirectly tested by API. | 551 # Indirectly tested by API. |
| 545 pass | 552 pass |
| 546 | 553 |
| 547 | 554 |
| 548 class TestOutput(TestCase): | 555 class TestOutput(TestCase): |
| 549 def setUp(self): | 556 def setUp(self): |
| 550 super(TestOutput, self).setUp() | 557 super(TestOutput, self).setUp() |
| 551 request = mkreq(_gen_request()) | 558 request = mkreq(_gen_request()) |
| 552 result_summary = task_result.new_result_summary(request) | 559 result_summary = task_result.new_result_summary(request) |
| 553 result_summary.modified_ts = utils.utcnow() | 560 result_summary.modified_ts = utils.utcnow() |
| 554 ndb.transaction(result_summary.put) | 561 ndb.transaction(result_summary.put) |
| 555 self.run_result = task_result.new_run_result( | 562 self.run_result = task_result.new_run_result( |
| 556 request, 1, 'localhost', 'abc', {}) | 563 request, 1, 'localhost', 'abc', {}) |
| 564 self.run_result.started_ts = result_summary.modified_ts |
| 557 self.run_result.modified_ts = utils.utcnow() | 565 self.run_result.modified_ts = utils.utcnow() |
| 558 result_summary.set_from_run_result(self.run_result, request) | 566 result_summary.set_from_run_result(self.run_result, request) |
| 559 ndb.transaction(lambda: ndb.put_multi((result_summary, self.run_result))) | 567 ndb.transaction(lambda: ndb.put_multi((result_summary, self.run_result))) |
| 560 self.run_result = self.run_result.key.get() | 568 self.run_result = self.run_result.key.get() |
| 561 | 569 |
| 562 def assertTaskOutputChunk(self, expected): | 570 def assertTaskOutputChunk(self, expected): |
| 563 q = task_result.TaskOutputChunk.query().order( | 571 q = task_result.TaskOutputChunk.query().order( |
| 564 task_result.TaskOutputChunk.key) | 572 task_result.TaskOutputChunk.key) |
| 565 self.assertEqual(expected, [t.to_dict() for t in q.fetch()]) | 573 self.assertEqual(expected, [t.to_dict() for t in q.fetch()]) |
| 566 | 574 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 task_result.TaskOutput.CHUNK_SIZE * '\x00' + 'Baz\x00Bar\x00FooWow', | 701 task_result.TaskOutput.CHUNK_SIZE * '\x00' + 'Baz\x00Bar\x00FooWow', |
| 694 self.run_result.get_output()) | 702 self.run_result.get_output()) |
| 695 self.assertTaskOutputChunk( | 703 self.assertTaskOutputChunk( |
| 696 [{'chunk': 'Baz\x00Bar\x00FooWow', 'gaps': [3, 4, 7, 8]}]) | 704 [{'chunk': 'Baz\x00Bar\x00FooWow', 'gaps': [3, 4, 7, 8]}]) |
| 697 | 705 |
| 698 | 706 |
| 699 if __name__ == '__main__': | 707 if __name__ == '__main__': |
| 700 if '-v' in sys.argv: | 708 if '-v' in sys.argv: |
| 701 unittest.TestCase.maxDiff = None | 709 unittest.TestCase.maxDiff = None |
| 702 unittest.main() | 710 unittest.main() |
| OLD | NEW |