12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import json
16
15
from typing import Any , Optional
17
16
18
17
from aiohttp import ClientResponseError
19
- from aiohttp import web
20
18
from aioresponses import aioresponses
21
- from mocks import FakeCredentials
19
+ from mocks import FakeAlloyDBAdminAsyncClient , FakeCredentials
22
20
import pytest
23
21
22
+ from google .api_core .exceptions import RetryError
24
23
from google .cloud import alloydb_v1beta
25
24
from google .cloud .alloydb .connector .client import AlloyDBClient
26
25
from google .cloud .alloydb .connector .utils import generate_keys
27
26
from google .cloud .alloydb .connector .version import __version__ as version
28
27
29
28
30
- async def connectionInfo (request : Any ) -> alloydb_v1beta .types .resources .ConnectionInfo :
31
- ci = alloydb_v1beta .types .resources .ConnectionInfo ()
32
- ci .ip_address = "10.0.0.1"
33
- ci .instance_uid = "123456789"
34
- return ci
35
-
36
-
37
- async def connectionInfoPublicIP (request : Any ) -> alloydb_v1beta .types .resources .ConnectionInfo :
38
- ci = alloydb_v1beta .types .resources .ConnectionInfo ()
39
- ci .ip_address = "10.0.0.1"
40
- ci .public_ip_address = "127.0.0.1"
41
- ci .instance_uid = "123456789"
42
- return ci
43
-
44
-
45
- async def connectionInfoPsc (request : Any ) -> alloydb_v1beta .types .resources .ConnectionInfo :
46
- ci = alloydb_v1beta .types .resources .ConnectionInfo ()
47
- ci .psc_dns_name = "x.y.alloydb.goog"
48
- ci .instance_uid = "123456789"
49
- return ci
50
-
51
-
52
- async def generateClientCertificate (request : Any ) -> alloydb_v1beta .types .service .GenerateClientCertificateResponse :
53
- ccr = alloydb_v1beta .types .service .GenerateClientCertificateResponse ()
54
- ccr .ca_cert = "This is the CA cert"
55
- ccr .pem_certificate_chain .append ("This is the client cert" )
56
- ccr .pem_certificate_chain .append ("This is the intermediate cert" )
57
- ccr .pem_certificate_chain .append ("This is the root cert" )
58
- return ccr
59
-
60
-
61
- class MockAlloyDBAdminAsyncClient :
62
- async def get_connection_info (self , request : alloydb_v1beta .GetConnectionInfoRequest ) -> alloydb_v1beta .types .resources .ConnectionInfo :
63
- parent = request .parent
64
- instance = parent .split ("/" )[- 1 ]
65
- if instance == "test-instance" :
66
- return connectionInfo (request )
67
- elif instance == "public-instance" :
68
- return connectionInfoPublicIP (request )
69
- else :
70
- return connectionInfoPsc (request )
71
-
72
- async def generate_client_certificate (self , request : alloydb_v1beta .GenerateClientCertificateRequest ) -> web .Response :
73
- return generateClientCertificate (request )
74
-
75
-
76
29
@pytest .mark .asyncio
77
30
async def test__get_metadata (credentials : FakeCredentials ) -> None :
78
31
"""
79
32
Test _get_metadata returns successfully.
80
33
"""
81
- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
34
+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
82
35
ip_addrs = await test_client ._get_metadata (
83
36
"test-project" ,
84
37
"test-region" ,
@@ -99,7 +52,7 @@ async def test__get_metadata_with_public_ip(
99
52
"""
100
53
Test _get_metadata returns successfully with Public IP.
101
54
"""
102
- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
55
+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
103
56
ip_addrs = await test_client ._get_metadata (
104
57
"test-project" ,
105
58
"test-region" ,
@@ -120,7 +73,7 @@ async def test__get_metadata_with_psc(
120
73
"""
121
74
Test _get_metadata returns successfully with PSC DNS name.
122
75
"""
123
- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
76
+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
124
77
ip_addrs = await test_client ._get_metadata (
125
78
"test-project" ,
126
79
"test-region" ,
@@ -140,34 +93,14 @@ async def test__get_metadata_error(
140
93
"""
141
94
Test that AlloyDB API error messages are raised for _get_metadata.
142
95
"""
143
- # mock AlloyDB API calls with exceptions
144
96
client = AlloyDBClient (
145
- alloydb_api_endpoint = "https:// alloydb.googleapis.com" ,
97
+ alloydb_api_endpoint = "alloydb.googleapis.com" ,
146
98
quota_project = None ,
147
99
credentials = credentials ,
148
100
)
149
- get_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster/instances/my-instance/connectionInfo"
150
- resp_body = {
151
- "error" : {
152
- "code" : 403 ,
153
- "message" : "AlloyDB API has not been used in project 123456789 before or it is disabled" ,
154
- }
155
- }
156
- with aioresponses () as mocked :
157
- mocked .get (
158
- get_url ,
159
- status = 403 ,
160
- payload = resp_body ,
161
- repeat = True ,
162
- )
163
- with pytest .raises (ClientResponseError ) as exc_info :
164
- await client ._get_metadata (
165
- "my-project" , "my-region" , "my-cluster" , "my-instance"
166
- )
167
- assert exc_info .value .status == 403
168
- assert (
169
- exc_info .value .message
170
- == "AlloyDB API has not been used in project 123456789 before or it is disabled"
101
+ with pytest .raises (RetryError ) as exc_info :
102
+ await client ._get_metadata (
103
+ "my-project" , "my-region" , "my-cluster" , "my-instance"
171
104
)
172
105
await client .close ()
173
106
@@ -179,7 +112,7 @@ async def test__get_client_certificate(
179
112
"""
180
113
Test _get_client_certificate returns successfully.
181
114
"""
182
- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
115
+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
183
116
keys = await generate_keys ()
184
117
certs = await test_client ._get_client_certificate (
185
118
"test-project" , "test-region" , "test-cluster" , keys [1 ]
@@ -197,32 +130,16 @@ async def test__get_client_certificate_error(
197
130
"""
198
131
Test that AlloyDB API error messages are raised for _get_client_certificate.
199
132
"""
200
- # mock AlloyDB API calls with exceptions
201
133
client = AlloyDBClient (
202
- alloydb_api_endpoint = "https:// alloydb.googleapis.com" ,
134
+ alloydb_api_endpoint = "alloydb.googleapis.com" ,
203
135
quota_project = None ,
204
136
credentials = credentials ,
205
137
)
206
- post_url = "https://alloydb.googleapis.com/v1beta/projects/my-project/locations/my-region/clusters/my-cluster:generateClientCertificate"
207
- resp_body = {
208
- "error" : {
209
- "code" : 404 ,
210
- "message" : "The AlloyDB instance does not exist." ,
211
- }
212
- }
213
- with aioresponses () as mocked :
214
- mocked .post (
215
- post_url ,
216
- status = 404 ,
217
- payload = resp_body ,
218
- repeat = True ,
138
+ with pytest .raises (RetryError ) as exc_info :
139
+ await client ._get_client_certificate (
140
+ "my-project" , "my-region" , "my-cluster" , ""
219
141
)
220
- with pytest .raises (ClientResponseError ) as exc_info :
221
- await client ._get_client_certificate (
222
- "my-project" , "my-region" , "my-cluster" , ""
223
- )
224
- assert exc_info .value .status == 404
225
- assert exc_info .value .message == "The AlloyDB instance does not exist."
142
+ print (exc_info )
226
143
await client .close ()
227
144
228
145
@@ -234,10 +151,11 @@ async def test_AlloyDBClient_init_(credentials: FakeCredentials) -> None:
234
151
"""
235
152
client = AlloyDBClient ("www.test-endpoint.com" , "my-quota-project" , credentials )
236
153
# verify base endpoint is set
237
- assert client ._alloydb_api_endpoint == "www.test-endpoint.com"
154
+ assert client ._client . api_endpoint == "www.test-endpoint.com"
238
155
# verify proper headers are set
239
- assert client ._client .headers ["User-Agent" ] == f"alloydb-python-connector/{ version } "
240
- assert client ._client .headers ["x-goog-user-project" ] == "my-quota-project"
156
+ got_user_agent = client ._client .transport ._wrapped_methods [client ._client .transport .list_clusters ]._metadata [0 ][1 ]
157
+ assert got_user_agent .startswith (f"alloydb-python-connector/{ version } " )
158
+ assert client ._client ._client ._client_options .quota_project_id == "my-quota-project"
241
159
# close client
242
160
await client .close ()
243
161
@@ -255,10 +173,8 @@ async def test_AlloyDBClient_init_custom_user_agent(
255
173
credentials ,
256
174
user_agent = "custom-agent/v1.0.0 other-agent/v2.0.0" ,
257
175
)
258
- assert (
259
- client ._client .headers ["User-Agent" ]
260
- == f"alloydb-python-connector/{ version } custom-agent/v1.0.0 other-agent/v2.0.0"
261
- )
176
+ got_user_agent = client ._client .transport ._wrapped_methods [client ._client .transport .list_clusters ]._metadata [0 ][1 ]
177
+ assert got_user_agent .startswith (f"alloydb-python-connector/{ version } custom-agent/v1.0.0 other-agent/v2.0.0" )
262
178
await client .close ()
263
179
264
180
@@ -277,10 +193,11 @@ async def test_AlloyDBClient_user_agent(
277
193
client = AlloyDBClient (
278
194
"www.test-endpoint.com" , "my-quota-project" , credentials , driver = driver
279
195
)
196
+ got_user_agent = client ._client .transport ._wrapped_methods [client ._client .transport .list_clusters ]._metadata [0 ][1 ]
280
197
if driver is None :
281
- assert client . _user_agent == f"alloydb-python-connector/{ version } "
198
+ assert got_user_agent . startswith ( f"alloydb-python-connector/{ version } " )
282
199
else :
283
- assert client . _user_agent == f"alloydb-python-connector/{ version } +{ driver } "
200
+ assert got_user_agent . startswith ( f"alloydb-python-connector/{ version } +{ driver } " )
284
201
# close client
285
202
await client .close ()
286
203
0 commit comments