test_azure.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. from subprocess import DEVNULL
  2. from typing import Generator
  3. from unittest.mock import Mock
  4. from azure.identity import DefaultAzureCredential
  5. from azure.storage.blob import (
  6. BlobClient,
  7. BlobProperties,
  8. BlobServiceClient,
  9. ContainerClient,
  10. )
  11. from pytest import fixture
  12. from pytest_mock import MockFixture
  13. from opta.core.azure import Azure
  14. from opta.layer import Layer, StructuredConfig
  15. @fixture()
  16. def azure_layer() -> Mock:
  17. layer = Mock(spec=Layer)
  18. layer.parent = None
  19. layer.cloud = "azurerm"
  20. layer.name = "blah"
  21. layer.providers = {
  22. "azurerm": {
  23. "location": "centralus",
  24. "tenant_id": "blahbc17-blah-blah-blah-blah291d395b",
  25. "subscription_id": "blah99ae-blah-blah-blah-blahd2a04788",
  26. }
  27. }
  28. layer.root.return_value = layer
  29. layer.gen_providers.return_value = {
  30. "terraform": {
  31. "backend": {
  32. "azurerm": {
  33. "resource_group_name": "dummy_resource_group",
  34. "storage_account_name": "dummy_storage_account",
  35. "container_name": "dummy_container_name",
  36. "key": "dummy_key",
  37. }
  38. }
  39. },
  40. "provider": {
  41. "azurerm": {
  42. "location": "centralus",
  43. "tenant_id": "blahbc17-blah-blah-blah-blah291d395b",
  44. "subscription_id": "blah99ae-blah-blah-blah-blahd2a04788",
  45. }
  46. },
  47. }
  48. layer.get_cluster_name.return_value = "mocked_cluster_name"
  49. return layer
  50. @fixture(autouse=True)
  51. def reset_azure_creds() -> Generator:
  52. Azure.credentials = None
  53. yield
  54. class TestAzure:
  55. def test_azure_set_kube_config(self, mocker: MockFixture, azure_layer: Mock) -> None:
  56. mocked_ensure_installed = mocker.patch("opta.core.azure.ensure_installed")
  57. mocker.patch(
  58. "opta.core.azure.Azure.cluster_exist", return_value=True,
  59. )
  60. mocked_nice_run = mocker.patch("opta.core.azure.nice_run",)
  61. Azure(azure_layer).set_kube_config()
  62. mocked_ensure_installed.assert_has_calls([mocker.call("az")])
  63. mocked_nice_run.assert_has_calls(
  64. [
  65. mocker.call(
  66. [
  67. "az",
  68. "aks",
  69. "get-credentials",
  70. "--resource-group",
  71. "dummy_resource_group",
  72. "--name",
  73. "mocked_cluster_name",
  74. "--admin",
  75. "--overwrite-existing",
  76. "--context",
  77. "dummy_resource_group-mocked_cluster_name",
  78. ],
  79. stdout=DEVNULL,
  80. check=True,
  81. ),
  82. ]
  83. )
  84. def test_get_credentials(self, mocker: MockFixture) -> None:
  85. mocked_default_creds = mocker.patch("opta.core.azure.DefaultAzureCredential")
  86. Azure.get_credentials()
  87. mocked_default_creds.assert_called_once_with()
  88. def test_get_remote_config(self, mocker: MockFixture, azure_layer: Mock) -> None:
  89. mocked_creds = mocker.Mock()
  90. mocked_default_creds = mocker.patch(
  91. "opta.core.azure.DefaultAzureCredential", return_value=mocked_creds
  92. )
  93. mocked_container_client_instance = mocker.Mock()
  94. mocked_container_client_instance.download_blob = mocker.Mock()
  95. download_stream_mock = mocker.Mock()
  96. download_stream_mock.readall = mocker.Mock(
  97. return_value='{"opta_version":"1", "date": "mock_date", "original_spec": "mock_spec", "defaults": {}}'
  98. )
  99. mocked_container_client_instance.download_blob.return_value = download_stream_mock
  100. mocked_container_client = mocker.patch(
  101. "opta.core.azure.ContainerClient",
  102. return_value=mocked_container_client_instance,
  103. )
  104. mocked_structured_config: StructuredConfig = {
  105. "opta_version": "1",
  106. "date": "mock_date",
  107. "original_spec": "mock_spec",
  108. "defaults": {},
  109. }
  110. assert Azure(azure_layer).get_remote_config() == mocked_structured_config
  111. azure_layer.gen_providers.assert_called_once_with(0)
  112. mocked_default_creds.assert_called_once_with()
  113. mocked_container_client.assert_called_once_with(
  114. account_url="https://dummy_storage_account.blob.core.windows.net",
  115. container_name="dummy_container_name",
  116. credential=mocked_creds,
  117. )
  118. mocked_container_client_instance.download_blob.assert_called_once_with(
  119. f"opta_config/{azure_layer.name}"
  120. )
  121. def test_upload_opta_config(self, mocker: MockFixture, azure_layer: Mock) -> None:
  122. Azure.credentials = None
  123. mocked_creds = mocker.Mock()
  124. mocked_default_creds = mocker.patch(
  125. "opta.core.azure.DefaultAzureCredential", return_value=mocked_creds
  126. )
  127. mocked_container_client_instance = mocker.Mock()
  128. mocked_container_client = mocker.patch(
  129. "opta.core.azure.ContainerClient",
  130. return_value=mocked_container_client_instance,
  131. )
  132. azure_layer.structured_config = mocker.Mock(return_value={"a": 1})
  133. Azure(azure_layer).upload_opta_config()
  134. azure_layer.gen_providers.assert_called_once_with(0)
  135. mocked_default_creds.assert_called_once_with()
  136. mocked_container_client.assert_called_once_with(
  137. account_url="https://dummy_storage_account.blob.core.windows.net",
  138. container_name="dummy_container_name",
  139. credential=mocked_creds,
  140. )
  141. mocked_container_client_instance.upload_blob.assert_called_once_with(
  142. name=f"opta_config/{azure_layer.name}", data='{"a": 1}', overwrite=True
  143. )
  144. def test_delete_opta_config(self, mocker: MockFixture, azure_layer: Mock) -> None:
  145. Azure.credentials = None
  146. mocked_creds = mocker.Mock()
  147. mocked_default_creds = mocker.patch(
  148. "opta.core.azure.DefaultAzureCredential", return_value=mocked_creds
  149. )
  150. mocked_container_client_instance = mocker.Mock()
  151. mocked_container_client = mocker.patch(
  152. "opta.core.azure.ContainerClient",
  153. return_value=mocked_container_client_instance,
  154. )
  155. Azure(azure_layer).delete_opta_config()
  156. azure_layer.gen_providers.assert_called_once_with(0)
  157. mocked_default_creds.assert_called_once_with()
  158. mocked_container_client.assert_called_once_with(
  159. account_url="https://dummy_storage_account.blob.core.windows.net",
  160. container_name="dummy_container_name",
  161. credential=mocked_creds,
  162. )
  163. mocked_container_client_instance.delete_blob.assert_called_once_with(
  164. f"opta_config/{azure_layer.name}", delete_snapshots="include"
  165. )
  166. def test_delete_remote_state(self, mocker: MockFixture, azure_layer: Mock) -> None:
  167. Azure.credentials = None
  168. mocked_creds = mocker.Mock()
  169. mocked_default_creds = mocker.patch(
  170. "opta.core.azure.DefaultAzureCredential", return_value=mocked_creds
  171. )
  172. mocked_container_client_instance = mocker.Mock()
  173. mocked_container_client = mocker.patch(
  174. "opta.core.azure.ContainerClient",
  175. return_value=mocked_container_client_instance,
  176. )
  177. Azure(azure_layer).delete_remote_state()
  178. azure_layer.gen_providers.assert_called_once_with(0)
  179. mocked_default_creds.assert_called_once_with()
  180. mocked_container_client.assert_called_once_with(
  181. account_url="https://dummy_storage_account.blob.core.windows.net",
  182. container_name="dummy_container_name",
  183. credential=mocked_creds,
  184. )
  185. mocked_container_client_instance.delete_blob.assert_called_once_with(
  186. azure_layer.name, delete_snapshots="include"
  187. )
  188. def test_get_terraform_lock_id(self, mocker: MockFixture, azure_layer: Mock) -> None:
  189. mocker.patch(
  190. "opta.core.azure.DefaultAzureCredential",
  191. return_value=mocker.Mock(spec=DefaultAzureCredential),
  192. )
  193. mock_blob_service_client = mocker.Mock(spec=BlobServiceClient)
  194. mock_container_client = mocker.Mock(spec=ContainerClient)
  195. mock_blob_client = mocker.Mock(spec=BlobClient)
  196. mock_blob_properties = mocker.Mock(spec=BlobProperties)
  197. mock_blob_properties.metadata = {
  198. "Terraformlockid": "J3siSUQiOiAibW9ja19sb2NrX2lkIn0n"
  199. }
  200. mocker.patch(
  201. "opta.core.azure.BlobServiceClient", return_value=mock_blob_service_client
  202. )
  203. mock_blob_service_client.get_container_client.return_value = mock_container_client
  204. mock_container_client.get_blob_client.return_value = mock_blob_client
  205. mock_blob_client.get_blob_properties.return_value = mock_blob_properties
  206. Azure(azure_layer).get_terraform_lock_id()