test_adx.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. import json
  20. import unittest
  21. from unittest import mock
  22. import pytest
  23. from azure.kusto.data.request import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder
  24. from airflow.exceptions import AirflowException
  25. from airflow.models import Connection
  26. from airflow.providers.microsoft.azure.hooks.adx import AzureDataExplorerHook
  27. from airflow.utils import db
  28. from airflow.utils.session import create_session
  29. ADX_TEST_CONN_ID = 'adx_test_connection_id'
  30. class TestAzureDataExplorerHook(unittest.TestCase):
  31. def tearDown(self):
  32. super().tearDown()
  33. with create_session() as session:
  34. session.query(Connection).filter(Connection.conn_id == ADX_TEST_CONN_ID).delete()
  35. def test_conn_missing_method(self):
  36. db.merge_conn(
  37. Connection(
  38. conn_id=ADX_TEST_CONN_ID,
  39. conn_type='azure_data_explorer',
  40. login='client_id',
  41. password='client secret',
  42. host='https://help.kusto.windows.net',
  43. extra=json.dumps({}),
  44. )
  45. )
  46. with pytest.raises(AirflowException) as ctx:
  47. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  48. assert 'is missing: `extra__azure_data_explorer__auth_method`' in str(ctx.value)
  49. def test_conn_unknown_method(self):
  50. db.merge_conn(
  51. Connection(
  52. conn_id=ADX_TEST_CONN_ID,
  53. conn_type='azure_data_explorer',
  54. login='client_id',
  55. password='client secret',
  56. host='https://help.kusto.windows.net',
  57. extra=json.dumps({'extra__azure_data_explorer__auth_method': 'AAD_OTHER'}),
  58. )
  59. )
  60. with pytest.raises(AirflowException) as ctx:
  61. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  62. assert 'Unknown authentication method: AAD_OTHER' in str(ctx.value)
  63. def test_conn_missing_cluster(self):
  64. db.merge_conn(
  65. Connection(
  66. conn_id=ADX_TEST_CONN_ID,
  67. conn_type='azure_data_explorer',
  68. login='client_id',
  69. password='client secret',
  70. extra=json.dumps({}),
  71. )
  72. )
  73. with pytest.raises(AirflowException) as ctx:
  74. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  75. assert 'Host connection option is required' in str(ctx.value)
  76. @mock.patch.object(KustoClient, '__init__')
  77. def test_conn_method_aad_creds(self, mock_init):
  78. mock_init.return_value = None
  79. db.merge_conn(
  80. Connection(
  81. conn_id=ADX_TEST_CONN_ID,
  82. conn_type='azure_data_explorer',
  83. login='client_id',
  84. password='client secret',
  85. host='https://help.kusto.windows.net',
  86. extra=json.dumps(
  87. {
  88. 'extra__azure_data_explorer__tenant': 'tenant',
  89. 'extra__azure_data_explorer__auth_method': 'AAD_CREDS',
  90. }
  91. ),
  92. )
  93. )
  94. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  95. assert mock_init.called_with(
  96. KustoConnectionStringBuilder.with_aad_user_password_authentication(
  97. 'https://help.kusto.windows.net', 'client_id', 'client secret', 'tenant'
  98. )
  99. )
  100. @mock.patch.object(KustoClient, '__init__')
  101. def test_conn_method_aad_app(self, mock_init):
  102. mock_init.return_value = None
  103. db.merge_conn(
  104. Connection(
  105. conn_id=ADX_TEST_CONN_ID,
  106. conn_type='azure_data_explorer',
  107. login='app_id',
  108. password='app key',
  109. host='https://help.kusto.windows.net',
  110. extra=json.dumps(
  111. {
  112. 'extra__azure_data_explorer__tenant': 'tenant',
  113. 'extra__azure_data_explorer__auth_method': 'AAD_APP',
  114. }
  115. ),
  116. )
  117. )
  118. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  119. assert mock_init.called_with(
  120. KustoConnectionStringBuilder.with_aad_application_key_authentication(
  121. 'https://help.kusto.windows.net', 'app_id', 'app key', 'tenant'
  122. )
  123. )
  124. @mock.patch.object(KustoClient, '__init__')
  125. def test_conn_method_aad_app_cert(self, mock_init):
  126. mock_init.return_value = None
  127. db.merge_conn(
  128. Connection(
  129. conn_id=ADX_TEST_CONN_ID,
  130. conn_type='azure_data_explorer',
  131. login='client_id',
  132. host='https://help.kusto.windows.net',
  133. extra=json.dumps(
  134. {
  135. 'extra__azure_data_explorer__tenant': 'tenant',
  136. 'extra__azure_data_explorer__auth_method': 'AAD_APP_CERT',
  137. 'extra__azure_data_explorer__certificate': 'PEM',
  138. 'extra__azure_data_explorer__thumbprint': 'thumbprint',
  139. }
  140. ),
  141. )
  142. )
  143. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  144. assert mock_init.called_with(
  145. KustoConnectionStringBuilder.with_aad_application_certificate_authentication(
  146. 'https://help.kusto.windows.net', 'client_id', 'PEM', 'thumbprint', 'tenant'
  147. )
  148. )
  149. @mock.patch.object(KustoClient, '__init__')
  150. def test_conn_method_aad_device(self, mock_init):
  151. mock_init.return_value = None
  152. db.merge_conn(
  153. Connection(
  154. conn_id=ADX_TEST_CONN_ID,
  155. conn_type='azure_data_explorer',
  156. host='https://help.kusto.windows.net',
  157. extra=json.dumps({'extra__azure_data_explorer__auth_method': 'AAD_DEVICE'}),
  158. )
  159. )
  160. AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  161. assert mock_init.called_with(
  162. KustoConnectionStringBuilder.with_aad_device_authentication('https://help.kusto.windows.net')
  163. )
  164. @mock.patch.object(KustoClient, 'execute')
  165. def test_run_query(self, mock_execute):
  166. mock_execute.return_value = None
  167. db.merge_conn(
  168. Connection(
  169. conn_id=ADX_TEST_CONN_ID,
  170. conn_type='azure_data_explorer',
  171. host='https://help.kusto.windows.net',
  172. extra=json.dumps({'extra__azure_data_explorer__auth_method': 'AAD_DEVICE'}),
  173. )
  174. )
  175. hook = AzureDataExplorerHook(azure_data_explorer_conn_id=ADX_TEST_CONN_ID)
  176. hook.run_query('Database', 'Logs | schema', options={'option1': 'option_value'})
  177. properties = ClientRequestProperties()
  178. properties.set_option('option1', 'option_value')
  179. assert mock_execute.called_with('Database', 'Logs | schema', properties=properties)