Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame^] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for task_executor unittests.""" |
| 6 | # pylint: disable=g-bad-import-order |
| 7 | |
| 8 | import mock |
| 9 | import os |
| 10 | import sys |
| 11 | import unittest |
| 12 | |
| 13 | import task_executor |
| 14 | |
| 15 | from google.appengine.api import taskqueue |
| 16 | from google.appengine.ext import testbed |
| 17 | |
| 18 | |
| 19 | class FakeSwarmingLib(object): |
| 20 | |
| 21 | def __init__(self, success_num=sys.maxint): |
| 22 | self.success_num = success_num |
| 23 | |
| 24 | def run(self, **suite_kwargs): |
| 25 | if int(suite_kwargs.get('num', 0)) > self.success_num: |
| 26 | raise ValueError('test') |
| 27 | |
| 28 | |
| 29 | class TaskExecutorTestCase(unittest.TestCase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | self.testbed = testbed.Testbed() |
| 33 | self.testbed.activate() |
| 34 | self.addCleanup(self.testbed.deactivate) |
| 35 | |
| 36 | # root_path must be set the location of queue.yaml. |
| 37 | # Otherwise, only the 'default' queue will be available. |
| 38 | self.testbed.init_taskqueue_stub( |
| 39 | root_path=os.path.join(os.path.dirname(__file__))) |
| 40 | self.taskqueue_stub = self.testbed.get_stub( |
| 41 | testbed.TASKQUEUE_SERVICE_NAME) |
| 42 | |
| 43 | def testPushTask(self): |
| 44 | suite_kwargs = {'suite': 'fake_suite'} |
| 45 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 46 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 47 | self.assertEqual(len(tasks), 1) |
| 48 | self.assertEqual(suite_kwargs, tasks[0].extract_params()) |
| 49 | |
| 50 | def testBatchExecuteTaskSuccessfully(self): |
| 51 | """Test task_executor successfully execute tasks in batch.""" |
| 52 | suite_kwargs = {'suite': 'fake_suite'} |
| 53 | extra_num = 10 |
| 54 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 55 | suite_kwargs['num'] = i + 1 |
| 56 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 57 | |
| 58 | # Before batch_execute |
| 59 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 60 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 61 | with mock.patch('swarming_lib.SwarmingRunner', |
| 62 | return_value=FakeSwarmingLib()): |
| 63 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 64 | task_processor.batch_execute() |
| 65 | # After batch_execute succeeds, only extra_num tasks left. |
| 66 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 67 | self.assertEqual(len(tasks), extra_num) |
| 68 | |
| 69 | def testBatchExecuteTaskFailedSwarmingTotally(self): |
| 70 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
| 71 | suite_kwargs = {'suite': 'fake_suite'} |
| 72 | extra_num = 10 |
| 73 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 74 | suite_kwargs['num'] = i + 1 |
| 75 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 76 | |
| 77 | # Before batch_execute |
| 78 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 79 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 80 | with mock.patch('swarming_lib.SwarmingRunner', |
| 81 | return_value=FakeSwarmingLib(0)): |
| 82 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 83 | self.assertRaises(ValueError, task_processor.batch_execute) |
| 84 | # After batch_execute fails, no tasks are deleted from task queue. |
| 85 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 86 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 87 | |
| 88 | def testBatchExecuteTaskFailedSwarmingPartially(self): |
| 89 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
| 90 | suite_kwargs = {'suite': 'fake_suite'} |
| 91 | extra_num = 10 |
| 92 | success_num = 50 |
| 93 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 94 | suite_kwargs['num'] = i + 1 |
| 95 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 96 | |
| 97 | # Before batch_execute |
| 98 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 99 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 100 | with mock.patch('swarming_lib.SwarmingRunner', |
| 101 | return_value=FakeSwarmingLib(success_num)): |
| 102 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 103 | self.assertRaises(ValueError, task_processor.batch_execute) |
| 104 | # After batch_execute fails, no tasks are deleted from task queue. |
| 105 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 106 | self.assertEqual(len(tasks), |
| 107 | task_executor.BATCH_SIZE - success_num + extra_num) |
| 108 | |
| 109 | def testBatchExecuteTaskFailedLeasing(self): |
| 110 | """Test task_executor fails to lease task.""" |
| 111 | suite_kwargs = {'suite': 'fake_suite'} |
| 112 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 113 | |
| 114 | with mock.patch('swarming_lib.SwarmingRunner', |
| 115 | return_value=FakeSwarmingLib(False)): |
| 116 | task_processor = task_executor.TaskProcessor('nonExistentQueue') |
| 117 | self.assertRaises(taskqueue.UnknownQueueError, |
| 118 | task_processor.batch_execute) |
| 119 | # After batch_execute fails, no tasks are deleted from task queue. |
| 120 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 121 | self.assertEqual(len(tasks), 1) |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | unittest.main() |