Skip to content

Commit 8c25c1b

Browse files
authored
fix(zb-experimental): pass creds to grpc channel (#1623)
fix(zb-experimental): pass creds to grpc channel
1 parent 46a5728 commit 8c25c1b

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

google/cloud/storage/_experimental/asyncio/async_grpc_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ def _create_async_grpc_client(
6565
transport_cls = storage_v2.StorageAsyncClient.get_transport_class(
6666
"grpc_asyncio"
6767
)
68-
channel = transport_cls.create_channel(attempt_direct_path=attempt_direct_path)
69-
transport = transport_cls(credentials=credentials, channel=channel)
68+
channel = transport_cls.create_channel(
69+
attempt_direct_path=attempt_direct_path, credentials=credentials
70+
)
71+
transport = transport_cls(channel=channel)
7072

7173
return storage_v2.StorageAsyncClient(
7274
transport=transport,

tests/unit/asyncio/test_async_grpc_client.py

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
from unittest import mock
1717
from google.auth import credentials as auth_credentials
18+
from google.auth.credentials import AnonymousCredentials
1819

1920

2021
def _make_credentials(spec=None):
@@ -38,12 +39,10 @@ def test_constructor_default_options(self, mock_async_storage_client):
3839
"grpc_asyncio"
3940
)
4041
mock_transport_cls.create_channel.assert_called_once_with(
41-
attempt_direct_path=True
42+
attempt_direct_path=True, credentials=mock_creds
4243
)
4344
mock_channel = mock_transport_cls.create_channel.return_value
44-
mock_transport_cls.assert_called_once_with(
45-
credentials=mock_creds, channel=mock_channel
46-
)
45+
mock_transport_cls.assert_called_once_with(channel=mock_channel)
4746
mock_transport = mock_transport_cls.return_value
4847
mock_async_storage_client.assert_called_once_with(
4948
transport=mock_transport,
@@ -64,21 +63,77 @@ def test_constructor_disables_directpath(self, mock_async_storage_client):
6463
)
6564

6665
mock_transport_cls.create_channel.assert_called_once_with(
67-
attempt_direct_path=False
66+
attempt_direct_path=False, credentials=mock_creds
6867
)
6968
mock_channel = mock_transport_cls.create_channel.return_value
70-
mock_transport_cls.assert_called_once_with(
71-
credentials=mock_creds, channel=mock_channel
72-
)
69+
mock_transport_cls.assert_called_once_with(channel=mock_channel)
7370

7471
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
75-
def test_grpc_client_property(self, mock_async_storage_client):
72+
def test_grpc_client_property(self, mock_grpc_gapic_client):
7673
from google.cloud.storage._experimental.asyncio import async_grpc_client
7774

75+
# Arrange
76+
mock_transport_cls = mock.MagicMock()
77+
mock_grpc_gapic_client.get_transport_class.return_value = mock_transport_cls
78+
channel_sentinel = mock.sentinel.channel
79+
80+
mock_transport_cls.create_channel.return_value = channel_sentinel
81+
mock_transport_cls.return_value = mock.sentinel.transport
82+
7883
mock_creds = _make_credentials()
84+
mock_client_info = mock.sentinel.client_info
85+
mock_client_options = mock.sentinel.client_options
86+
mock_attempt_direct_path = mock.sentinel.attempt_direct_path
87+
88+
# Act
89+
client = async_grpc_client.AsyncGrpcClient(
90+
credentials=mock_creds,
91+
client_info=mock_client_info,
92+
client_options=mock_client_options,
93+
attempt_direct_path=mock_attempt_direct_path,
94+
)
95+
96+
mock_grpc_gapic_client.get_transport_class.return_value = mock_transport_cls
97+
98+
mock_transport_cls.create_channel.return_value = channel_sentinel
99+
mock_transport_instance = mock.sentinel.transport
100+
mock_transport_cls.return_value = mock_transport_instance
101+
102+
retrieved_client = client.grpc_client
103+
104+
# Assert
105+
mock_transport_cls.create_channel.assert_called_once_with(
106+
attempt_direct_path=mock_attempt_direct_path, credentials=mock_creds
107+
)
108+
mock_transport_cls.assere_with(channel=channel_sentinel)
109+
mock_grpc_gapic_client.assert_called_once_with(
110+
transport=mock_transport_instance,
111+
client_info=mock_client_info,
112+
client_options=mock_client_options,
113+
)
114+
self.assertIs(retrieved_client, mock_grpc_gapic_client.return_value)
79115

80-
client = async_grpc_client.AsyncGrpcClient(credentials=mock_creds)
116+
@mock.patch("google.cloud._storage_v2.StorageAsyncClient")
117+
def test_grpc_client_with_anon_creds(self, mock_grpc_gapic_client):
118+
from google.cloud.storage._experimental.asyncio import async_grpc_client
81119

120+
# Arrange
121+
mock_transport_cls = mock.MagicMock()
122+
mock_grpc_gapic_client.get_transport_class.return_value = mock_transport_cls
123+
channel_sentinel = mock.sentinel.channel
124+
125+
mock_transport_cls.create_channel.return_value = channel_sentinel
126+
mock_transport_cls.return_value = mock.sentinel.transport
127+
128+
# Act
129+
anonymous_creds = AnonymousCredentials()
130+
client = async_grpc_client.AsyncGrpcClient(credentials=anonymous_creds)
82131
retrieved_client = client.grpc_client
83132

84-
self.assertIs(retrieved_client, mock_async_storage_client.return_value)
133+
# Assert
134+
self.assertIs(retrieved_client, mock_grpc_gapic_client.return_value)
135+
136+
mock_transport_cls.create_channel.assert_called_once_with(
137+
attempt_direct_path=True, credentials=anonymous_creds
138+
)
139+
mock_transport_cls.assert_called_once_with(channel=channel_sentinel)

0 commit comments

Comments
 (0)