test_base_azure.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import unittest
  18. from unittest.mock import Mock, patch
  19. from airflow.models import Connection
  20. from airflow.providers.microsoft.azure.hooks.base_azure import AzureBaseHook
  21. class TestBaseAzureHook(unittest.TestCase):
  22. @patch('airflow.providers.microsoft.azure.hooks.base_azure.get_client_from_auth_file')
  23. @patch(
  24. 'airflow.providers.microsoft.azure.hooks.base_azure.AzureBaseHook.get_connection',
  25. return_value=Connection(conn_id='azure_default', extra='{ "key_path": "key_file.json" }'),
  26. )
  27. def test_get_conn_with_key_path(self, mock_connection, mock_get_client_from_auth_file):
  28. mock_sdk_client = Mock()
  29. auth_sdk_client = AzureBaseHook(mock_sdk_client).get_conn()
  30. mock_get_client_from_auth_file.assert_called_once_with(
  31. client_class=mock_sdk_client, auth_path=mock_connection.return_value.extra_dejson['key_path']
  32. )
  33. assert auth_sdk_client == mock_get_client_from_auth_file.return_value
  34. @patch('airflow.providers.microsoft.azure.hooks.base_azure.get_client_from_json_dict')
  35. @patch(
  36. 'airflow.providers.microsoft.azure.hooks.base_azure.AzureBaseHook.get_connection',
  37. return_value=Connection(conn_id='azure_default', extra='{ "key_json": { "test": "test" } }'),
  38. )
  39. def test_get_conn_with_key_json(self, mock_connection, mock_get_client_from_json_dict):
  40. mock_sdk_client = Mock()
  41. auth_sdk_client = AzureBaseHook(mock_sdk_client).get_conn()
  42. mock_get_client_from_json_dict.assert_called_once_with(
  43. client_class=mock_sdk_client, config_dict=mock_connection.return_value.extra_dejson['key_json']
  44. )
  45. assert auth_sdk_client == mock_get_client_from_json_dict.return_value
  46. @patch('airflow.providers.microsoft.azure.hooks.base_azure.ServicePrincipalCredentials')
  47. @patch(
  48. 'airflow.providers.microsoft.azure.hooks.base_azure.AzureBaseHook.get_connection',
  49. return_value=Connection(
  50. conn_id='azure_default',
  51. login='my_login',
  52. password='my_password',
  53. extra='{ "tenantId": "my_tenant", "subscriptionId": "my_subscription" }',
  54. ),
  55. )
  56. def test_get_conn_with_credentials(self, mock_connection, mock_spc):
  57. mock_sdk_client = Mock()
  58. auth_sdk_client = AzureBaseHook(mock_sdk_client).get_conn()
  59. mock_spc.assert_called_once_with(
  60. client_id=mock_connection.return_value.login,
  61. secret=mock_connection.return_value.password,
  62. tenant=mock_connection.return_value.extra_dejson['tenantId'],
  63. )
  64. mock_sdk_client.assert_called_once_with(
  65. credentials=mock_spc.return_value,
  66. subscription_id=mock_connection.return_value.extra_dejson['subscriptionId'],
  67. )
  68. assert auth_sdk_client == mock_sdk_client.return_value