RehanAhmad25 commented on code in PR #75815:
URL: https://github.com/apache/airflow/pull/75815#discussion_r4741088674
##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/data_lake.py:
##########
@@ -382,6 +390,51 @@ def get_conn(self) -> DataLakeServiceClient:
**extra,
)
+ async def get_async_conn(self) -> AsyncDataLakeServiceClient:
+ """
+ Return an async DataLakeServiceClient, built from the connection as
:meth:`get_conn` builds its client.
+
+ The connection is read with the async accessors, so the event loop is
not blocked. Unlike
+ :meth:`get_conn`, the client is not cached: an async client belongs to
the event loop it
+ was created on, so close it (``async with``) when done, and close its
credential if it has
+ a ``close`` method (an Active Directory or default credential).
+ """
+ conn = await get_async_connection(self.conn_id)
+ extra = dict(await get_async_extra_dejson(conn) or {})
+
+ connection_string = self._get_field(extra, "connection_string")
+ if connection_string:
+ # connection_string auth takes priority
+ return
AsyncDataLakeServiceClient.from_connection_string(connection_string, **extra)
+
+ credential: AsyncCredentials | str
+ tenant = self._get_field(extra, "tenant_id")
+ if tenant:
+ # use Active Directory auth
+ credential = AsyncClientSecretCredential(
+ tenant_id=tenant,
+ client_id=cast("str", conn.login),
+ client_secret=cast("str", conn.password),
+ proxies=extra.get("proxies", {}),
+ )
+ elif conn.password:
+ credential = conn.password
+ else:
+ credential = get_async_default_azure_credential(
+ managed_identity_client_id=self._get_field(extra,
"managed_identity_client_id"),
+ workload_identity_tenant_id=self._get_field(extra,
"workload_identity_tenant_id"),
+ )
Review Comment:
Could we add a test for the default-credential branch in `get_async_conn`,
including checking that `managed_identity_client_id` and
`workload_identity_tenant_id` are forwarded from the connection extras? The
current tests cover the service-principal, connection-string, and account-key
paths, but not this fallback.
##########
providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_data_lake.py:
##########
@@ -318,6 +325,145 @@ def test_proxies_passed_to_credentials(self, mock_conn):
assert self.find_policy(conn, ProxyPolicy) is not None
assert self.find_policy(conn, ProxyPolicy).proxies["https"] ==
"https://proxy:80"
+ @mock.patch(f"{MODULE}.AzureDataLakeStorageV2Hook.get_file_system")
+ def test_load_bytes(self, mock_get_file_system):
+ mock_get_file_system.return_value =
mock.create_autospec(FileSystemClient, instance=True)
+ hook = AzureDataLakeStorageV2Hook(adls_conn_id=self.conn_id)
+
+ hook.load_bytes(self.file_system_name, "dir/file.jsonl", b"data",
overwrite=True)
+
+ mock_get_file_system.assert_called_once_with(self.file_system_name)
+ file_system = mock_get_file_system.return_value
+ file_system.get_file_client.assert_called_once_with("dir/file.jsonl")
+
file_system.get_file_client.return_value.upload_data.assert_called_once_with(b"data",
overwrite=True)
+
+ @staticmethod
+ def async_service_client():
+ """An async DataLakeServiceClient mock whose file client records the
upload."""
+ client = mock.create_autospec(AsyncDataLakeServiceClient,
instance=True)
+ client.__aenter__.return_value = client
+ file_system = mock.create_autospec(AsyncFileSystemClient,
instance=True)
+ file_system.get_file_client.return_value = mock.create_autospec(
+ AsyncDataLakeFileClient, instance=True
+ )
+ client.get_file_system_client.return_value = file_system
+ return client
+
+ async def run_aload_bytes(self, connection: Connection):
+ """Run aload_bytes() against ``connection``; return the client class
and credential class mocks."""
+ client = self.async_service_client()
+ client_cls = mock.create_autospec(AsyncDataLakeServiceClient,
return_value=client)
+ client_cls.from_connection_string.return_value = client
+ credential_cls = mock.create_autospec(AsyncClientSecretCredential)
+ client.credential = credential_cls.return_value
Review Comment:
In `run_aload_bytes`, client.credential is always set to
`credential_cls.return_value`, including for the account-key and
connection-string cases. That means the tests never exercise the cleanup path
where the client credential has no close method, such as when it is the
account-key string. Could we set the mock client's credential to match the auth
mode so this branch is covered?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]