|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for RemoteTaskSwarmingService.""" |
| 15 | + |
| 16 | +import unittest |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from clusterfuzz._internal.remote_task import remote_task_types |
| 20 | +from clusterfuzz._internal.swarming import remote_task_service |
| 21 | +from clusterfuzz._internal.tests.test_libs import helpers |
| 22 | + |
| 23 | + |
| 24 | +class RemoteTaskSwarmingServiceTest(unittest.TestCase): |
| 25 | + """Tests for RemoteTaskSwarmingService.""" |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + helpers.patch(self, [ |
| 29 | + 'clusterfuzz._internal.swarming.is_swarming_task', |
| 30 | + 'clusterfuzz._internal.swarming.push_swarming_task', |
| 31 | + 'clusterfuzz._internal.base.tasks.task_utils.get_command_from_module', |
| 32 | + ]) |
| 33 | + self.service = remote_task_service.RemoteTaskSwarmingService() |
| 34 | + |
| 35 | + def test_create_utask_main_job_success(self): |
| 36 | + """Test creating a single task successfully.""" |
| 37 | + self.mock.get_command_from_module.return_value = 'fuzz' |
| 38 | + self.mock.is_swarming_task.return_value = True |
| 39 | + |
| 40 | + result = self.service.create_utask_main_job('fuzz_task', 'job_type', |
| 41 | + 'http://url') |
| 42 | + |
| 43 | + # Success returns None in this interface (consistent with GcpBatchService) |
| 44 | + self.assertIsNone(result) |
| 45 | + |
| 46 | + self.mock.push_swarming_task.assert_called_once_with( |
| 47 | + 'fuzz', 'http://url', 'job_type') |
| 48 | + |
| 49 | + def test_create_utask_main_job_failure(self): |
| 50 | + """Test creating a single task that is not a swarming task.""" |
| 51 | + self.mock.get_command_from_module.return_value = 'fuzz' |
| 52 | + self.mock.is_swarming_task.return_value = False |
| 53 | + |
| 54 | + result = self.service.create_utask_main_job('fuzz_task', 'job_type', |
| 55 | + 'http://url') |
| 56 | + |
| 57 | + # Failure returns the task itself |
| 58 | + self.assertIsInstance(result, remote_task_types.RemoteTask) |
| 59 | + self.assertEqual(result.command, 'fuzz') |
| 60 | + self.mock.push_swarming_task.assert_not_called() |
| 61 | + |
| 62 | + def test_create_utask_main_jobs_mixed_results(self): |
| 63 | + """Test creating multiple tasks with mixed success/failure.""" |
| 64 | + tasks = [ |
| 65 | + remote_task_types.RemoteTask('fuzz', 'job1', 'url1'), |
| 66 | + remote_task_types.RemoteTask('fuzz', 'job2', 'url2'), |
| 67 | + remote_task_types.RemoteTask('fuzz', 'job3', 'url3'), |
| 68 | + ] |
| 69 | + |
| 70 | + # job1 succeeds, job2 fails (not a swarming task), job3 succeeds |
| 71 | + self.mock.is_swarming_task.side_effect = [True, False, True] |
| 72 | + |
| 73 | + unscheduled = self.service.create_utask_main_jobs(tasks) |
| 74 | + |
| 75 | + self.assertEqual(len(unscheduled), 1) |
| 76 | + self.assertEqual(unscheduled[0].job_type, 'job2') |
| 77 | + |
| 78 | + self.assertEqual(self.mock.push_swarming_task.call_count, 2) |
| 79 | + self.mock.push_swarming_task.assert_has_calls([ |
| 80 | + mock.call('fuzz', 'url1', 'job1'), |
| 81 | + mock.call('fuzz', 'url3', 'job3'), |
| 82 | + ]) |
| 83 | + |
| 84 | + def test_create_utask_main_jobs_all_success(self): |
| 85 | + """Test creating multiple tasks where all succeed.""" |
| 86 | + tasks = [ |
| 87 | + remote_task_types.RemoteTask('fuzz', 'job1', 'url1'), |
| 88 | + remote_task_types.RemoteTask('fuzz', 'job2', 'url2'), |
| 89 | + ] |
| 90 | + self.mock.is_swarming_task.return_value = True |
| 91 | + |
| 92 | + unscheduled = self.service.create_utask_main_jobs(tasks) |
| 93 | + |
| 94 | + self.assertEqual(unscheduled, []) |
| 95 | + self.assertEqual(self.mock.push_swarming_task.call_count, 2) |
| 96 | + |
| 97 | + def test_create_utask_main_jobs_all_fail(self): |
| 98 | + """Test creating multiple tasks where all fail.""" |
| 99 | + tasks = [ |
| 100 | + remote_task_types.RemoteTask('fuzz', 'job1', 'url1'), |
| 101 | + remote_task_types.RemoteTask('fuzz', 'job2', 'url2'), |
| 102 | + ] |
| 103 | + self.mock.is_swarming_task.return_value = False |
| 104 | + |
| 105 | + unscheduled = self.service.create_utask_main_jobs(tasks) |
| 106 | + |
| 107 | + self.assertEqual(unscheduled, tasks) |
| 108 | + self.mock.push_swarming_task.assert_not_called() |
| 109 | + |
| 110 | + def test_create_utask_main_jobs_empty(self): |
| 111 | + """Test creating tasks with an empty list.""" |
| 112 | + unscheduled = self.service.create_utask_main_jobs([]) |
| 113 | + self.assertEqual(unscheduled, []) |
| 114 | + self.mock.push_swarming_task.assert_not_called() |
| 115 | + |
| 116 | + def test_create_utask_main_jobs_exception(self): |
| 117 | + """Test creating tasks when push_swarming_task raises an exception.""" |
| 118 | + tasks = [ |
| 119 | + remote_task_types.RemoteTask('fuzz', 'job1', 'url1'), |
| 120 | + ] |
| 121 | + |
| 122 | + self.mock.is_swarming_task.return_value = True |
| 123 | + self.mock.push_swarming_task.side_effect = Exception('error') |
| 124 | + |
| 125 | + unscheduled = self.service.create_utask_main_jobs(tasks) |
| 126 | + |
| 127 | + self.assertEqual(len(unscheduled), 1) |
| 128 | + self.assertEqual(unscheduled[0].job_type, 'job1') |
0 commit comments