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