1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import json
1615from typing import Any , Optional
1716
1817from aiohttp import ClientResponseError
19- from aiohttp import web
2018from aioresponses import aioresponses
21- from mocks import FakeCredentials
19+ from mocks import FakeAlloyDBAdminAsyncClient , FakeCredentials
2220import pytest
2321
22+ from google .api_core .exceptions import RetryError
2423from google .cloud import alloydb_v1beta
2524from google .cloud .alloydb .connector .client import AlloyDBClient
2625from google .cloud .alloydb .connector .utils import generate_keys
2726from google .cloud .alloydb .connector .version import __version__ as version
2827
2928
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-
7629@pytest .mark .asyncio
7730async def test__get_metadata (credentials : FakeCredentials ) -> None :
7831 """
7932 Test _get_metadata returns successfully.
8033 """
81- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
34+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
8235 ip_addrs = await test_client ._get_metadata (
8336 "test-project" ,
8437 "test-region" ,
@@ -99,7 +52,7 @@ async def test__get_metadata_with_public_ip(
9952 """
10053 Test _get_metadata returns successfully with Public IP.
10154 """
102- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
55+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
10356 ip_addrs = await test_client ._get_metadata (
10457 "test-project" ,
10558 "test-region" ,
@@ -120,7 +73,7 @@ async def test__get_metadata_with_psc(
12073 """
12174 Test _get_metadata returns successfully with PSC DNS name.
12275 """
123- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
76+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
12477 ip_addrs = await test_client ._get_metadata (
12578 "test-project" ,
12679 "test-region" ,
@@ -140,34 +93,14 @@ async def test__get_metadata_error(
14093 """
14194 Test that AlloyDB API error messages are raised for _get_metadata.
14295 """
143- # mock AlloyDB API calls with exceptions
14496 client = AlloyDBClient (
145- alloydb_api_endpoint = "https:// alloydb.googleapis.com" ,
97+ alloydb_api_endpoint = "alloydb.googleapis.com" ,
14698 quota_project = None ,
14799 credentials = credentials ,
148100 )
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"
171104 )
172105 await client .close ()
173106
@@ -179,7 +112,7 @@ async def test__get_client_certificate(
179112 """
180113 Test _get_client_certificate returns successfully.
181114 """
182- test_client = AlloyDBClient ("" , "" , credentials , MockAlloyDBAdminAsyncClient ())
115+ test_client = AlloyDBClient ("" , "" , credentials , FakeAlloyDBAdminAsyncClient ())
183116 keys = await generate_keys ()
184117 certs = await test_client ._get_client_certificate (
185118 "test-project" , "test-region" , "test-cluster" , keys [1 ]
@@ -197,32 +130,16 @@ async def test__get_client_certificate_error(
197130 """
198131 Test that AlloyDB API error messages are raised for _get_client_certificate.
199132 """
200- # mock AlloyDB API calls with exceptions
201133 client = AlloyDBClient (
202- alloydb_api_endpoint = "https:// alloydb.googleapis.com" ,
134+ alloydb_api_endpoint = "alloydb.googleapis.com" ,
203135 quota_project = None ,
204136 credentials = credentials ,
205137 )
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" , ""
219141 )
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 )
226143 await client .close ()
227144
228145
@@ -234,10 +151,11 @@ async def test_AlloyDBClient_init_(credentials: FakeCredentials) -> None:
234151 """
235152 client = AlloyDBClient ("www.test-endpoint.com" , "my-quota-project" , credentials )
236153 # 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"
238155 # 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"
241159 # close client
242160 await client .close ()
243161
@@ -255,10 +173,8 @@ async def test_AlloyDBClient_init_custom_user_agent(
255173 credentials ,
256174 user_agent = "custom-agent/v1.0.0 other-agent/v2.0.0" ,
257175 )
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" )
262178 await client .close ()
263179
264180
@@ -277,10 +193,11 @@ async def test_AlloyDBClient_user_agent(
277193 client = AlloyDBClient (
278194 "www.test-endpoint.com" , "my-quota-project" , credentials , driver = driver
279195 )
196+ got_user_agent = client ._client .transport ._wrapped_methods [client ._client .transport .list_clusters ]._metadata [0 ][1 ]
280197 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 } " )
282199 else :
283- assert client . _user_agent == f"alloydb-python-connector/{ version } +{ driver } "
200+ assert got_user_agent . startswith ( f"alloydb-python-connector/{ version } +{ driver } " )
284201 # close client
285202 await client .close ()
286203
0 commit comments