|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +from cm.models import MaintenanceMode, ServiceComponent |
| 14 | +from cm.services.job.run.repo import JobRepoImpl |
| 15 | + |
| 16 | +from ansible_plugin.errors import ( |
| 17 | + PluginValidationError, |
| 18 | +) |
| 19 | +from ansible_plugin.executors.change_maintenance_mode import ADCMChangeMMExecutor |
| 20 | +from ansible_plugin.tests.base import BaseTestEffectsOfADCMAnsiblePlugins |
| 21 | + |
| 22 | + |
| 23 | +class TestEffectsOfADCMAnsiblePlugins(BaseTestEffectsOfADCMAnsiblePlugins): |
| 24 | + def setUp(self) -> None: |
| 25 | + super().setUp() |
| 26 | + |
| 27 | + self.service_1 = self.add_services_to_cluster(["service_1"], cluster=self.cluster).first() |
| 28 | + self.component_1 = ServiceComponent.objects.filter(service=self.service_1).first() |
| 29 | + |
| 30 | + self.add_host_to_cluster(cluster=self.cluster, host=self.host_1) |
| 31 | + self.add_host_to_cluster(cluster=self.cluster, host=self.host_2) |
| 32 | + |
| 33 | + def test_simple_call_success(self) -> None: |
| 34 | + for object_, arguments, expected_mm in ( |
| 35 | + (self.service_1, {"type": "service", "value": False}, MaintenanceMode.OFF), |
| 36 | + (self.component_1, {"type": "component", "value": True}, MaintenanceMode.ON), |
| 37 | + (self.host_1, {"type": "host", "value": True}, MaintenanceMode.ON), |
| 38 | + ): |
| 39 | + object_.maintenance_mode = MaintenanceMode.CHANGING |
| 40 | + object_.save() |
| 41 | + |
| 42 | + with self.subTest(arguments["type"]): |
| 43 | + task = self.prepare_task(owner=object_, name="dummy") |
| 44 | + job, *_ = JobRepoImpl.get_task_jobs(task.id) |
| 45 | + |
| 46 | + executor = self.prepare_executor( |
| 47 | + executor_type=ADCMChangeMMExecutor, |
| 48 | + call_arguments=arguments, |
| 49 | + call_context=job, |
| 50 | + ) |
| 51 | + |
| 52 | + result = executor.execute() |
| 53 | + self.assertIsNone(result.error) |
| 54 | + |
| 55 | + object_.refresh_from_db() |
| 56 | + self.assertEqual(object_.maintenance_mode, expected_mm) |
| 57 | + |
| 58 | + def test_call_from_another_context_success(self) -> None: |
| 59 | + self.service_1.maintenance_mode = MaintenanceMode.CHANGING |
| 60 | + self.service_1.save() |
| 61 | + self.component_1.maintenance_mode = MaintenanceMode.CHANGING |
| 62 | + self.component_1.save() |
| 63 | + self.host_1.maintenance_mode = MaintenanceMode.CHANGING |
| 64 | + self.host_1.save() |
| 65 | + |
| 66 | + with self.subTest("component-from-host"): |
| 67 | + task = self.prepare_task(owner=self.component_1, name="on_host", host=self.host_1) |
| 68 | + job, *_ = JobRepoImpl.get_task_jobs(task.id) |
| 69 | + |
| 70 | + executor = self.prepare_executor( |
| 71 | + executor_type=ADCMChangeMMExecutor, |
| 72 | + call_arguments=""" |
| 73 | + type: component |
| 74 | + value: yes |
| 75 | + """, |
| 76 | + call_context=job, |
| 77 | + ) |
| 78 | + |
| 79 | + result = executor.execute() |
| 80 | + self.assertIsNone(result.error) |
| 81 | + |
| 82 | + self.component_1.refresh_from_db() |
| 83 | + self.assertEqual(self.component_1.maintenance_mode, MaintenanceMode.ON) |
| 84 | + self.service_1.refresh_from_db() |
| 85 | + self.assertEqual(self.service_1.maintenance_mode, MaintenanceMode.CHANGING) |
| 86 | + self.host_1.refresh_from_db() |
| 87 | + self.assertEqual(self.host_1.maintenance_mode, MaintenanceMode.CHANGING) |
| 88 | + |
| 89 | + with self.subTest("service-from-component"): |
| 90 | + task = self.prepare_task(owner=self.component_1, name="dummy") |
| 91 | + job, *_ = JobRepoImpl.get_task_jobs(task.id) |
| 92 | + |
| 93 | + executor = self.prepare_executor( |
| 94 | + executor_type=ADCMChangeMMExecutor, |
| 95 | + call_arguments=""" |
| 96 | + type: service |
| 97 | + value: no |
| 98 | + """, |
| 99 | + call_context=job, |
| 100 | + ) |
| 101 | + |
| 102 | + result = executor.execute() |
| 103 | + self.assertIsNone(result.error) |
| 104 | + |
| 105 | + self.service_1.refresh_from_db() |
| 106 | + self.assertEqual(self.service_1.maintenance_mode, MaintenanceMode.OFF) |
| 107 | + self.component_1.refresh_from_db() |
| 108 | + self.assertEqual(self.component_1.maintenance_mode, MaintenanceMode.ON) |
| 109 | + self.host_1.refresh_from_db() |
| 110 | + self.assertEqual(self.host_1.maintenance_mode, MaintenanceMode.CHANGING) |
| 111 | + |
| 112 | + def test_incorrect_type_fail(self) -> None: |
| 113 | + for type_ in ("cluster", "provider"): |
| 114 | + with self.subTest(type_): |
| 115 | + task = self.prepare_task(owner=self.component_1, name="dummy") |
| 116 | + job, *_ = JobRepoImpl.get_task_jobs(task.id) |
| 117 | + executor = self.prepare_executor( |
| 118 | + executor_type=ADCMChangeMMExecutor, |
| 119 | + call_arguments=f""" |
| 120 | + type: {type_} |
| 121 | + value: true |
| 122 | + """, |
| 123 | + call_context=job, |
| 124 | + ) |
| 125 | + |
| 126 | + result = executor.execute() |
| 127 | + |
| 128 | + self.assertIsInstance(result.error, PluginValidationError) |
| 129 | + self.assertIn(f"plugin can't be called to change {type_}'s MM", result.error.message) |
0 commit comments