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 |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 7 | # pylint: disable=unused-argument |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 8 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 9 | import collections |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 10 | import mock |
| 11 | import os |
| 12 | import sys |
| 13 | import unittest |
| 14 | |
| 15 | import task_executor |
| 16 | |
| 17 | from google.appengine.api import taskqueue |
| 18 | from google.appengine.ext import testbed |
| 19 | |
| 20 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 21 | class FakeFrontdoorClient(object): |
| 22 | |
| 23 | # pylint: disable=g-deprecated-member-used |
| 24 | def __init__(self, success_num=sys.maxint, error_num=0): |
| 25 | self.success_num = success_num |
| 26 | self.error_num = error_num |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 27 | self.frontdoor_run_count = collections.defaultdict(list) |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 28 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 29 | def multirequest_run(self, tasks, suite): |
| 30 | executed_tasks = [] |
| 31 | for task in tasks: |
| 32 | params = task.extract_params() |
| 33 | num = int(params.get('num', 0)) |
| 34 | if num > self.success_num and num <= self.success_num + self.error_num: |
| 35 | raise ValueError('test') |
| 36 | executed_tasks.append(task) |
| 37 | self.frontdoor_run_count[suite].append(executed_tasks) |
| 38 | return executed_tasks |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 39 | |
| 40 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 41 | class TaskExecutorTestCase(unittest.TestCase): |
| 42 | |
| 43 | def setUp(self): |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 44 | super(TaskExecutorTestCase, self).setUp() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 45 | self.testbed = testbed.Testbed() |
| 46 | self.testbed.activate() |
| 47 | self.addCleanup(self.testbed.deactivate) |
| 48 | |
| 49 | # root_path must be set the location of queue.yaml. |
| 50 | # Otherwise, only the 'default' queue will be available. |
| 51 | self.testbed.init_taskqueue_stub( |
| 52 | root_path=os.path.join(os.path.dirname(__file__))) |
| 53 | self.taskqueue_stub = self.testbed.get_stub( |
| 54 | testbed.TASKQUEUE_SERVICE_NAME) |
| 55 | |
Prathmesh Prabhu | 40f1e1e | 2020-03-07 00:04:41 -0800 | [diff] [blame^] | 56 | p = mock.patch('global_config.GAE_TESTING', return_value=True) |
| 57 | p.start() |
| 58 | self.addCleanup(p.stop) |
| 59 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 60 | def testPushTask(self): |
| 61 | suite_kwargs = {'suite': 'fake_suite'} |
| 62 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 63 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 64 | self.assertEqual(len(tasks), 1) |
| 65 | self.assertEqual(suite_kwargs, tasks[0].extract_params()) |
| 66 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 67 | def testBatchExecuteTask(self): |
Xinan Lin | a18d988 | 2019-12-18 11:16:33 -0800 | [diff] [blame] | 68 | """Test task_executor executes tasks not greater than the BATCH_SIZE.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 69 | batch_size = 100 |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 70 | extra_num = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 71 | for i in range(batch_size / 2): |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 72 | suite_kwargs = {'suite': 'foo_suite', |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 73 | 'num': i + 1} |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 74 | task_executor.push(task_executor.SUITES_QUEUE, |
| 75 | tag='foo_suite', **suite_kwargs) |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 76 | for i in range(batch_size / 2 + extra_num): |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 77 | suite_kwargs = {'suite': 'hoo_suite', |
| 78 | 'num': i + 1} |
| 79 | task_executor.push(task_executor.SUITES_QUEUE, |
| 80 | tag='hoo_suite', **suite_kwargs) |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 81 | with mock.patch('buildbucket.TestPlatformClient', |
| 82 | return_value=FakeFrontdoorClient()): |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 83 | with mock.patch('global_config.GAE_TESTING', return_value=True): |
| 84 | task_processor = task_executor.TaskProcessor( |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 85 | task_executor.SUITES_QUEUE, |
| 86 | options=task_executor.Options( |
| 87 | batch_size=batch_size, multirequest_size=1)) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 88 | task_processor.batch_execute() |
Xinan Lin | a18d988 | 2019-12-18 11:16:33 -0800 | [diff] [blame] | 89 | # After batch_execute runs, the extra tasks should remain in the queue. |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 90 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 91 | self.assertEqual(extra_num, len(tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 92 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 93 | def testBatchExecuteTaskFailedFrontdoorTotally(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 94 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 95 | batch_size = 100 |
| 96 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 97 | extra_num = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 98 | suite_kwargs = {'suite': 'fake_suite'} |
| 99 | for i in range(multirequest_size + extra_num): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 100 | suite_kwargs['num'] = i + 1 |
| 101 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 102 | |
| 103 | # Before batch_execute |
| 104 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 105 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 106 | with mock.patch('buildbucket.TestPlatformClient', |
| 107 | return_value=FakeFrontdoorClient(0, error_num=len(tasks))): |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 108 | task_processor = task_executor.TaskProcessor( |
| 109 | task_executor.SUITES_QUEUE, |
| 110 | options=task_executor.Options( |
| 111 | batch_size=batch_size, multirequest_size=multirequest_size)) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 112 | task_processor.batch_execute() |
| 113 | # After batch_execute, no tasks are deleted from task queue, due |
| 114 | # to they're all failed to kick off. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 115 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 116 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 117 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 118 | def testBatchExecuteTaskFailedFrontdoorPartially(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 119 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 120 | # Batch large enough to cover all added tasks. |
| 121 | batch_size = 1000 |
| 122 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 123 | extra_num = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 124 | suite_kwargs = {'suite': 'fake_suite'} |
| 125 | success_num = (multirequest_size + extra_num) / 2 |
| 126 | error_num = multirequest_size + extra_num |
| 127 | for i in range(multirequest_size + extra_num): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 128 | suite_kwargs['num'] = i + 1 |
| 129 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 130 | |
| 131 | # Before batch_execute |
| 132 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 133 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 134 | with mock.patch('buildbucket.TestPlatformClient', |
| 135 | return_value=FakeFrontdoorClient(success_num, error_num)): |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 136 | task_processor = task_executor.TaskProcessor( |
| 137 | task_executor.SUITES_QUEUE, |
| 138 | options=task_executor.Options( |
| 139 | batch_size=batch_size, multirequest_size=multirequest_size)) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 140 | task_processor.batch_execute() |
| 141 | # After batch_execute, only failed suites and extra suites are |
| 142 | # kept in task queue. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 143 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 144 | self.assertEqual(len(tasks), error_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 145 | |
| 146 | def testBatchExecuteTaskFailedLeasing(self): |
| 147 | """Test task_executor fails to lease task.""" |
| 148 | suite_kwargs = {'suite': 'fake_suite'} |
| 149 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 150 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 151 | with mock.patch('buildbucket.TestPlatformClient', |
| 152 | return_value=FakeFrontdoorClient(False)): |
| 153 | task_processor = task_executor.TaskProcessor('nonExistentQueue') |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 154 | self.assertRaises(taskqueue.UnknownQueueError, |
| 155 | task_processor.batch_execute) |
| 156 | # After batch_execute fails, no tasks are deleted from task queue. |
| 157 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 158 | self.assertEqual(len(tasks), 1) |
| 159 | |
| 160 | |
| 161 | if __name__ == '__main__': |
| 162 | unittest.main() |