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): |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 64 | """Test task_executor execute tasks in batch in testing on GAE.""" |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 65 | extra_num = 10 |
| 66 | for i in range(constants.Buildbucket.MULTIREQUEST_SIZE): |
| 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) |
| 71 | for i in range(constants.Buildbucket.MULTIREQUEST_SIZE + extra_num): |
| 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() |
| 82 | client = task_processor.test_platform_client |
| 83 | self.assertEqual(1, len(client.frontdoor_run_count['foo_suite'])) |
| 84 | self.assertEqual(constants.Buildbucket.MULTIREQUEST_SIZE, |
| 85 | len(client.frontdoor_run_count['foo_suite'][0])) |
| 86 | self.assertEqual(1, len(client.frontdoor_run_count['hoo_suite'])) |
| 87 | self.assertEqual(constants.Buildbucket.MULTIREQUEST_SIZE, |
| 88 | len(client.frontdoor_run_count['hoo_suite'][0])) |
| 89 | # Now BATCH_SIZE = 2 * MULTIREQUEST_SIZE. After batch_execute run, |
| 90 | # the extra tasks should remain in the queue. |
| 91 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 92 | self.assertEqual(extra_num, len(tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 93 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 94 | def testBatchExecuteTaskFailedFrontdoorTotally(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 95 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
| 96 | suite_kwargs = {'suite': 'fake_suite'} |
| 97 | extra_num = 10 |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 98 | for i in range(constants.Buildbucket.MULTIREQUEST_SIZE + extra_num): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 99 | suite_kwargs['num'] = i + 1 |
| 100 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 101 | |
| 102 | # Before batch_execute |
| 103 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 104 | self.assertEqual(len(tasks), |
| 105 | constants.Buildbucket.MULTIREQUEST_SIZE + extra_num) |
| 106 | with mock.patch('buildbucket.TestPlatformClient', |
| 107 | return_value=FakeFrontdoorClient(0, error_num=len(tasks))): |
| 108 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 109 | task_processor.batch_execute() |
| 110 | # After batch_execute, no tasks are deleted from task queue, due |
| 111 | # to they're all failed to kick off. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 112 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 113 | self.assertEqual(len(tasks), |
| 114 | constants.Buildbucket.MULTIREQUEST_SIZE + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 115 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 116 | def testBatchExecuteTaskFailedFrontdoorPartially(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 117 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
| 118 | suite_kwargs = {'suite': 'fake_suite'} |
| 119 | extra_num = 10 |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 120 | success_num = (extra_num + constants.Buildbucket.MULTIREQUEST_SIZE) / 2 |
| 121 | error_num = extra_num + constants.Buildbucket.MULTIREQUEST_SIZE |
| 122 | for i in range(constants.Buildbucket.MULTIREQUEST_SIZE + extra_num): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 123 | suite_kwargs['num'] = i + 1 |
| 124 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 125 | |
| 126 | # Before batch_execute |
| 127 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 128 | self.assertEqual(len(tasks), |
| 129 | constants.Buildbucket.MULTIREQUEST_SIZE + extra_num) |
| 130 | with mock.patch('buildbucket.TestPlatformClient', |
| 131 | return_value=FakeFrontdoorClient(success_num, error_num)): |
| 132 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 133 | task_processor.batch_execute() |
| 134 | # After batch_execute, only failed suites and extra suites are |
| 135 | # kept in task queue. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 136 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 137 | self.assertEqual(len(tasks), error_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 138 | |
| 139 | def testBatchExecuteTaskFailedLeasing(self): |
| 140 | """Test task_executor fails to lease task.""" |
| 141 | suite_kwargs = {'suite': 'fake_suite'} |
| 142 | task_executor.push(task_executor.SUITES_QUEUE, **suite_kwargs) |
| 143 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame^] | 144 | with mock.patch('buildbucket.TestPlatformClient', |
| 145 | return_value=FakeFrontdoorClient(False)): |
| 146 | task_processor = task_executor.TaskProcessor('nonExistentQueue') |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 147 | self.assertRaises(taskqueue.UnknownQueueError, |
| 148 | task_processor.batch_execute) |
| 149 | # After batch_execute fails, no tasks are deleted from task queue. |
| 150 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 151 | self.assertEqual(len(tasks), 1) |
| 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | unittest.main() |