|
| 1 | +from unittest.mock import patch, Mock |
| 2 | + |
| 3 | +from cloudshell.api.cloudshell_api import NameValuePair |
| 4 | +from dotenv import load_dotenv |
| 5 | +from tests.integration_tests.constants import SHELL_NAME, ATTRIBUTE_NAMES |
| 6 | +from typing import Callable |
| 7 | + |
| 8 | +from tests.integration_tests.helper_objects.integration_context import IntegrationData |
| 9 | + |
| 10 | +import os |
| 11 | +from unittest import TestCase, mock |
| 12 | + |
| 13 | +from tests.integration_tests.helper_services.service_attributes_factory import ServiceAttributesFactory |
| 14 | + |
| 15 | + |
| 16 | +class TestMockTerraformExecuteDestroy(TestCase): |
| 17 | + @patch('cloudshell.iac.terraform.services.object_factory.CloudShellSessionContext') |
| 18 | + def setUp(self, patched_api) -> None: |
| 19 | + load_dotenv() |
| 20 | + |
| 21 | + self._prepare_mock_api() |
| 22 | + patched_api.return_value.get_api.return_value = self.mock_api |
| 23 | + |
| 24 | + self._mocked_tf_working_dir = '' |
| 25 | + self._prepare_mock_services() |
| 26 | + self._prepare_integration_data() |
| 27 | + |
| 28 | + self.mock_api.GetReservationDetails.return_value.ReservationDescription.Services = [self._service1, |
| 29 | + self._service2] |
| 30 | + |
| 31 | + def _prepare_integration_data(self): |
| 32 | + self.integration_data1 = IntegrationData(self._service1.Alias, False, self.mock_api) |
| 33 | + self.integration_data2 = IntegrationData(self._service2.Alias, False, self.mock_api) |
| 34 | + |
| 35 | + def _prepare_mock_services(self): |
| 36 | + self._service1 = Mock() |
| 37 | + self._service1.Alias = os.environ.get("SB_SERVICE_ALIAS1") |
| 38 | + self._service1.Attributes = ServiceAttributesFactory.create_empty_attributes() |
| 39 | + self._service2 = Mock() |
| 40 | + self._service2.Alias = os.environ.get("SB_SERVICE_ALIAS2") |
| 41 | + self._service2.Attributes = ServiceAttributesFactory.create_empty_attributes() |
| 42 | + |
| 43 | + def _prepare_mock_api(self): |
| 44 | + self.mock_api = Mock() |
| 45 | + self.mock_api.DecryptPassword = _decrypt_password |
| 46 | + self.mock_api.GetResourceDetails.return_value.ResourceFamilyName = 'Cloud Provider' |
| 47 | + self.mock_api.GetResourceDetails.return_value.ResourceModelName = 'Microsoft Azure' |
| 48 | + self.mock_api.GetResourceDetails.return_value.ResourceAttributes = [ |
| 49 | + NameValuePair(Name="Azure Subscription ID", Value=os.environ.get("AZURE_SUBSCRIPTION_ID")), |
| 50 | + NameValuePair(Name="Azure Tenant ID", Value=os.environ.get("AZURE_TENANT_ID")), |
| 51 | + NameValuePair(Name="Azure Application ID", Value=os.environ.get("AZURE_APPLICATION_ID")), |
| 52 | + NameValuePair(Name="Azure Application Key", Value=os.environ.get("AZURE_APPLICATION_KEY_DEC")) |
| 53 | + ] |
| 54 | + |
| 55 | + '''------------------------------ Generic Execute/Destroy functions ---------------------------------''' |
| 56 | + |
| 57 | + def run_execute(self, pre_exec_function: Callable, integration_data: IntegrationData): |
| 58 | + self.pre_exec_prep(pre_exec_function, integration_data) |
| 59 | + integration_data.tf_shell.execute_terraform() |
| 60 | + |
| 61 | + def run_destroy(self, pre_destroy_function: Callable, integration_data: IntegrationData): |
| 62 | + self.pre_destroy_prep(pre_destroy_function, integration_data) |
| 63 | + integration_data.tf_shell.destroy_terraform() |
| 64 | + |
| 65 | + '''------------------------------ Test Cases ---------------------------------''' |
| 66 | + |
| 67 | + @patch('cloudshell.iac.terraform.services.tf_proc_exec.TfProcExec.can_destroy_run') |
| 68 | + @patch('cloudshell.iac.terraform.terraform_shell.SandboxDataHandler') |
| 69 | + @patch('cloudshell.iac.terraform.services.object_factory.CloudShellSessionContext') |
| 70 | + def test_execute_and_destroy_azure_vault(self, patch_api, patched_sbdata_handler, can_destroy_run): |
| 71 | + can_destroy_run.return_value = True |
| 72 | + patch_api.return_value.get_api.return_value = self.mock_api |
| 73 | + mock_sbdata_handler = Mock() |
| 74 | + mock_sbdata_handler.get_tf_working_dir = self._get_mocked_tf_working_dir |
| 75 | + mock_sbdata_handler.set_tf_working_dir = self._set_mocked_tf_working_dir |
| 76 | + patched_sbdata_handler.return_value = mock_sbdata_handler |
| 77 | + self.run_execute_and_destroy( |
| 78 | + pre_exec_function=self.pre_exec_azure_vault, |
| 79 | + pre_destroy_function=self.pre_destroy, |
| 80 | + integration_data=self.integration_data1 |
| 81 | + ) |
| 82 | + |
| 83 | + '''------------------------------ Functions : general _pre prep functions ---------------------------------''' |
| 84 | + |
| 85 | + def pre_exec_prep(self, pre_exec_function: Callable, integration_data: IntegrationData): |
| 86 | + pre_exec_function(integration_data) |
| 87 | + |
| 88 | + def pre_destroy_prep(self, pre_destroy_function: Callable, integration_data: IntegrationData): |
| 89 | + pre_destroy_function(integration_data) |
| 90 | + |
| 91 | + def clear_sb_data(self): |
| 92 | + self.integration_data1.api.ClearSandboxData(self.integration_data1.context.reservation.reservation_id) |
| 93 | + |
| 94 | + '''------------------------------ Functions : prep before exec -------------------------------------------''' |
| 95 | + |
| 96 | + def pre_exec(self, integration_data: IntegrationData): |
| 97 | + pass |
| 98 | + |
| 99 | + def pre_exec_azure_vault(self, integration_data: IntegrationData): |
| 100 | + self._set_attribute_on_mock_service( |
| 101 | + f"{SHELL_NAME}.Terraform Inputs", |
| 102 | + os.environ.get("AZUREAPP_TF_INPUTS"), |
| 103 | + integration_data |
| 104 | + ) |
| 105 | + self._set_attribute_on_mock_service( |
| 106 | + f"{SHELL_NAME}.Github Terraform Module URL", |
| 107 | + os.environ.get("GITHUB_TF_PRIVATE_AZUREAPP_URL"), |
| 108 | + integration_data |
| 109 | + ) |
| 110 | + self._set_attribute_on_mock_service( |
| 111 | + f"{SHELL_NAME}.{ATTRIBUTE_NAMES.GITHUB_TOKEN}", |
| 112 | + os.environ.get("GITHUB_TOKEN_DEC"), |
| 113 | + integration_data |
| 114 | + ) |
| 115 | + self._set_attribute_on_mock_service( |
| 116 | + f"{SHELL_NAME}.{ATTRIBUTE_NAMES.TERRAFORM_VERSION}", |
| 117 | + os.environ.get("0.15.1"), |
| 118 | + integration_data |
| 119 | + ) |
| 120 | + self._set_attribute_on_mock_service( |
| 121 | + f"{SHELL_NAME}.{ATTRIBUTE_NAMES.CLOUD_PROVIDER}", |
| 122 | + os.environ.get("CLP_RESOURSE"), |
| 123 | + integration_data |
| 124 | + ) |
| 125 | + service1 = Mock() |
| 126 | + service1.Alias = integration_data.context.resource.name |
| 127 | + service1.Attributes = integration_data.context.resource.attributes |
| 128 | + self.mock_api.GetReservationDetails.return_value.ReservationDescription.Services = [service1] |
| 129 | + integration_data.create_tf_shell() |
| 130 | + |
| 131 | + def pre_exec_azure_mssql(self, integration_data: IntegrationData): |
| 132 | + self._set_attribute_on_mock_service( |
| 133 | + f"{SHELL_NAME}.Terraform Inputs", |
| 134 | + os.environ.get("AZUREMSSQL_TF_INPUTS"), |
| 135 | + integration_data |
| 136 | + ) |
| 137 | + self._set_attribute_on_mock_service( |
| 138 | + f"{SHELL_NAME}.Github Terraform Module URL", |
| 139 | + os.environ.get("GITHUB_TF_PRIVATE_AZUREMSSQL_URL"), |
| 140 | + integration_data |
| 141 | + ) |
| 142 | + self._set_attribute_on_mock_service( |
| 143 | + f"{SHELL_NAME}.UUID", |
| 144 | + os.environ.get(""), |
| 145 | + integration_data |
| 146 | + ) |
| 147 | + |
| 148 | + def pre_exec_azure_vault_with_remote_access_key_based(self, integration_data: IntegrationData): |
| 149 | + self.pre_exec_azure_vault(integration_data) |
| 150 | + self._set_attribute_on_mock_service( |
| 151 | + f"{SHELL_NAME}.Remote State Provider", |
| 152 | + os.environ.get("REMOTE_STATE_PROVIDER_ACCESS_KEY"), |
| 153 | + integration_data |
| 154 | + ) |
| 155 | + |
| 156 | + def pre_exec_azure_vault_with_remote_cloud_cred_based(self, integration_data: IntegrationData): |
| 157 | + self.pre_exec_azure_vault(integration_data) |
| 158 | + self._set_attribute_on_mock_service( |
| 159 | + f"{SHELL_NAME}.Remote State Provider", |
| 160 | + os.environ.get("REMOTE_STATE_PROVIDER_CLOUD_CRED"), |
| 161 | + integration_data |
| 162 | + ) |
| 163 | + |
| 164 | + def pre_exec_azure_vault_with_remote_invalid_nonexistent(self, integration_data: IntegrationData): |
| 165 | + self.pre_exec_azure_vault(integration_data) |
| 166 | + self._set_attribute_on_mock_service( |
| 167 | + f"{SHELL_NAME}.Remote State Provider", |
| 168 | + os.environ.get("REMOTE_STATE_PROVIDER_INVALID_NO_RESOURCE"), |
| 169 | + integration_data |
| 170 | + ) |
| 171 | + |
| 172 | + def pre_exec_azure_vault_with_remote_invalid_wrong(self, integration_data: IntegrationData): |
| 173 | + self.pre_exec_azure_vault(integration_data) |
| 174 | + self._set_attribute_on_mock_service( |
| 175 | + f"{SHELL_NAME}.Remote State Provider", |
| 176 | + os.environ.get("REMOTE_STATE_PROVIDER_INVALID_WRONG_RESOURCE"), |
| 177 | + integration_data |
| 178 | + ) |
| 179 | + |
| 180 | + def pre_exec_azure_vault_without_remote(self, integration_data: IntegrationData): |
| 181 | + self.pre_exec_azure_vault(integration_data) |
| 182 | + self._set_attribute_on_mock_service( |
| 183 | + f"{SHELL_NAME}.Remote State Provider", |
| 184 | + os.environ.get(""), |
| 185 | + integration_data |
| 186 | + ) |
| 187 | + |
| 188 | + '''------------------------------ Functions : prep before destroy -----------------------------------------''' |
| 189 | + |
| 190 | + def pre_destroy(self, integration_data: IntegrationData): |
| 191 | + # As UUID has been created and SB data now contains UUID and Status we must update context so destroy can run |
| 192 | + for attribute in integration_data.context.resource.attributes: |
| 193 | + if attribute.Name == f"{SHELL_NAME}.UUID": |
| 194 | + attribute.Value = integration_data.tf_shell._tf_service.attributes[f"{SHELL_NAME}.UUID"] |
| 195 | + |
| 196 | + '''------------------------------ Helper Functions ---------------------------------------------------------''' |
| 197 | + @staticmethod |
| 198 | + def _set_attribute_on_mock_service(attr_name: str, attr_value: str, integration_data: IntegrationData): |
| 199 | + for attribute in integration_data.context.resource.attributes: |
| 200 | + if attribute.Name == attr_name: |
| 201 | + attribute.Value = attr_value |
| 202 | + return |
| 203 | + |
| 204 | + def _get_mocked_tf_working_dir(self): |
| 205 | + return self._mocked_tf_working_dir |
| 206 | + |
| 207 | + def _set_mocked_tf_working_dir(self, tf_working_dir: str): |
| 208 | + self._mocked_tf_working_dir = tf_working_dir |
| 209 | + |
| 210 | + |
| 211 | +def _decrypt_password(x): |
| 212 | + result = mock.MagicMock() |
| 213 | + result.Value = x |
| 214 | + return result |
0 commit comments