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 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 21 | def __init__(self, success_num=sys.maxint, error_num=0): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 22 | self.success_num = success_num |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 23 | self.error_num = error_num |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 24 | self.dummy_run_count = 0 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 25 | |
| 26 | def run(self, **suite_kwargs): |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 27 | num = int(suite_kwargs.get('num', 0)) |
| 28 | if num > self.success_num and num <= self.success_num + self.error_num: |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 29 | raise ValueError('test') |
| 30 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 31 | def dummy_run(self): |
| 32 | self.dummy_run_count += 1 |
| 33 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 34 | |
| 35 | class TaskExecutorTestCase(unittest.TestCase): |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.testbed = testbed.Testbed() |
| 39 | self.testbed.activate() |
| 40 | self.addCleanup(self.testbed.deactivate) |
| 41 | |
| 42 | # root_path must be set the location of queue.yaml. |
| 43 | # Otherwise, only the 'default' queue will be available. |
| 44 | self.testbed.init_taskqueue_stub( |
| 45 | root_path=os.path.join(os.path.dirname(__file__))) |
| 46 | self.taskqueue_stub = self.testbed.get_stub( |
| 47 | testbed.TASKQUEUE_SERVICE_NAME) |
| 48 | |
| 49 | def testPushTask(self): |
| 50 | suite_kwargs = {'suite': 'fake_suite'} |
| 51 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 52 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 53 | self.assertEqual(len(tasks), 1) |
| 54 | self.assertEqual(suite_kwargs, tasks[0].extract_params()) |
| 55 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 56 | def testBatchExecuteTaskWithGAETesting(self): |
| 57 | """Test task_executor execute tasks in batch in testing on GAE.""" |
| 58 | suite_kwargs = {'suite': 'fake_suite'} |
| 59 | for i in range(task_executor.BATCH_SIZE): |
| 60 | suite_kwargs['num'] = i + 1 |
| 61 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 62 | |
| 63 | with mock.patch('swarming_lib.SwarmingRunner', |
| 64 | return_value=FakeSwarmingLib()): |
| 65 | with mock.patch('global_config.GAE_TESTING', return_value=True): |
| 66 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 67 | task_processor.batch_execute() |
| 68 | self.assertEqual(task_executor.BATCH_SIZE, |
| 69 | task_processor.swarming.dummy_run_count) |
| 70 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 71 | def testBatchExecuteTaskSuccessfully(self): |
| 72 | """Test task_executor successfully execute tasks in batch.""" |
| 73 | suite_kwargs = {'suite': 'fake_suite'} |
| 74 | extra_num = 10 |
| 75 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 76 | suite_kwargs['num'] = i + 1 |
| 77 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 78 | |
| 79 | # Before batch_execute |
| 80 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 81 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 82 | with mock.patch('swarming_lib.SwarmingRunner', |
| 83 | return_value=FakeSwarmingLib()): |
| 84 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 85 | task_processor.batch_execute() |
| 86 | # After batch_execute succeeds, only extra_num tasks left. |
| 87 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 88 | self.assertEqual(len(tasks), extra_num) |
| 89 | |
| 90 | def testBatchExecuteTaskFailedSwarmingTotally(self): |
| 91 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
| 92 | suite_kwargs = {'suite': 'fake_suite'} |
| 93 | extra_num = 10 |
| 94 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 95 | suite_kwargs['num'] = i + 1 |
| 96 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 97 | |
| 98 | # Before batch_execute |
| 99 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 100 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 101 | with mock.patch('swarming_lib.SwarmingRunner', |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 102 | return_value=FakeSwarmingLib(0, error_num=len(tasks))): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 103 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 104 | task_processor.batch_execute() |
| 105 | # After batch_execute, no tasks are deleted from task queue, due |
| 106 | # to they're all failed to kick off. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 107 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 108 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 109 | |
| 110 | def testBatchExecuteTaskFailedSwarmingPartially(self): |
| 111 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
| 112 | suite_kwargs = {'suite': 'fake_suite'} |
| 113 | extra_num = 10 |
| 114 | success_num = 50 |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 115 | error_num = 5 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 116 | for i in range(task_executor.BATCH_SIZE + extra_num): |
| 117 | suite_kwargs['num'] = i + 1 |
| 118 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 119 | |
| 120 | # Before batch_execute |
| 121 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 122 | self.assertEqual(len(tasks), task_executor.BATCH_SIZE + extra_num) |
| 123 | with mock.patch('swarming_lib.SwarmingRunner', |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 124 | return_value=FakeSwarmingLib(success_num, error_num)): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 125 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 126 | task_processor.batch_execute() |
| 127 | # After batch_execute, only failed suites and extra suites are |
| 128 | # kept in task queue. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 129 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame^] | 130 | self.assertEqual(len(tasks), error_num + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 131 | |
| 132 | def testBatchExecuteTaskFailedLeasing(self): |
| 133 | """Test task_executor fails to lease task.""" |
| 134 | suite_kwargs = {'suite': 'fake_suite'} |
| 135 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 136 | |
| 137 | with mock.patch('swarming_lib.SwarmingRunner', |
| 138 | return_value=FakeSwarmingLib(False)): |
| 139 | task_processor = task_executor.TaskProcessor('nonExistentQueue') |
| 140 | self.assertRaises(taskqueue.UnknownQueueError, |
| 141 | task_processor.batch_execute) |
| 142 | # After batch_execute fails, no tasks are deleted from task queue. |
| 143 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 144 | self.assertEqual(len(tasks), 1) |
| 145 | |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | unittest.main() |