Skip to content

Commit 01f1739

Browse files
committed
fixed mtls tests
1 parent d0c03d0 commit 01f1739

2 files changed

Lines changed: 47 additions & 32 deletions

File tree

packages/google-cloud-bigtable/tests/unit/data/_async/test_client.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _get_target_class():
9090
return CrossSync.DataClient
9191

9292
@classmethod
93-
def _make_client(cls, *args, use_emulator=True, **kwargs):
93+
def _make_client(cls, *args, use_emulator=True, use_mtls="auto", **kwargs):
9494
import os
9595

9696
env_mask = {}
@@ -105,6 +105,8 @@ def _make_client(cls, *args, use_emulator=True, **kwargs):
105105
# set some default values
106106
kwargs["credentials"] = kwargs.get("credentials", AnonymousCredentials())
107107
kwargs["project"] = kwargs.get("project", "project-id")
108+
if use_mtls is not None:
109+
env_mask["GOOGLE_API_USE_MTLS_ENDPOINT"] = use_mtls
108110
with mock.patch.dict(os.environ, env_mask):
109111
return cls._get_target_class()(*args, **kwargs)
110112

@@ -1046,13 +1048,17 @@ def test_client_ctor_sync(self):
10461048
assert client._channel_refresh_task is None
10471049

10481050
@CrossSync.pytest
1049-
async def test_default_universe_domain(self):
1051+
@pytest.mark.paramtrize(
1052+
"use_mtls, expected_domain",
1053+
[("never", "googleapis.com"), ("always", "mtls.googleapis.com")]
1054+
)
1055+
async def test_default_universe_domain(self, use_mtls, expected_domain):
10501056
"""
10511057
When not passed, universe_domain should default to googleapis.com
10521058
"""
1053-
async with self._make_client(project="project-id", credentials=None) as client:
1054-
assert client.universe_domain == "googleapis.com"
1055-
assert client.api_endpoint == "bigtable.googleapis.com"
1059+
async with self._make_client(project="project-id", credentials=None, use_mtls=expected_domain) as client:
1060+
assert client.universe_domain == expected_domain
1061+
assert client.api_endpoint == f"bigtable.{expected_domain}"
10561062

10571063
@CrossSync.pytest
10581064
async def test_custom_universe_domain(self):
@@ -1064,6 +1070,7 @@ async def test_custom_universe_domain(self):
10641070
client_options=options,
10651071
use_emulator=True,
10661072
credentials=None,
1073+
use_mtls="never",
10671074
) as client:
10681075
assert client.universe_domain == universe_domain
10691076
assert client.api_endpoint == f"bigtable.{universe_domain}"
@@ -1077,7 +1084,6 @@ async def test_configured_universe_domain_matches_GDU(self):
10771084
project="project_id", client_options=options, credentials=None
10781085
) as client:
10791086
assert client.universe_domain == "googleapis.com"
1080-
assert client.api_endpoint == "bigtable.googleapis.com"
10811087

10821088
@CrossSync.pytest
10831089
async def test_credential_universe_domain_matches_GDU(self):
@@ -1086,13 +1092,12 @@ async def test_credential_universe_domain_matches_GDU(self):
10861092
creds._universe_domain = "googleapis.com"
10871093
async with self._make_client(project="project_id", credentials=creds) as client:
10881094
assert client.universe_domain == "googleapis.com"
1089-
assert client.api_endpoint == "bigtable.googleapis.com"
10901095

10911096
@CrossSync.pytest
10921097
async def test_anomynous_credential_universe_domain(self):
10931098
"""Anomynopus credentials should use default universe domain"""
10941099
creds = AnonymousCredentials()
1095-
async with self._make_client(project="project_id", credentials=creds) as client:
1100+
async with self._make_client(project="project_id", credentials=creds, use_mtls="never") as client:
10961101
assert client.universe_domain == "googleapis.com"
10971102
assert client.api_endpoint == "bigtable.googleapis.com"
10981103

@@ -1111,6 +1116,7 @@ async def test_configured_universe_domain_mismatched_credentials(self):
11111116
client_options=options,
11121117
use_emulator=False,
11131118
credentials=creds,
1119+
use_mtls="never",
11141120
)
11151121
err_msg = (
11161122
f"The configured universe domain ({universe_domain}) does "
@@ -1131,7 +1137,7 @@ async def test_configured_universe_domain_matches_credentials(self):
11311137
creds = AnonymousCredentials()
11321138
creds._universe_domain = universe_domain
11331139
async with self._make_client(
1134-
project="project_id", credentials=creds, client_options=options
1140+
project="project_id", credentials=creds, client_options=options, use_mtls="never"
11351141
) as client:
11361142
assert client.universe_domain == universe_domain
11371143
assert client.api_endpoint == f"bigtable.{universe_domain}"

packages/google-cloud-bigtable/tests/unit/data/_sync_autogen/test_client.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _get_target_class():
6565
return CrossSync._Sync_Impl.DataClient
6666

6767
@classmethod
68-
def _make_client(cls, *args, use_emulator=True, **kwargs):
68+
def _make_client(cls, *args, use_emulator=True, use_mtls="auto", **kwargs):
6969
import os
7070

7171
env_mask = {}
@@ -77,6 +77,8 @@ def _make_client(cls, *args, use_emulator=True, **kwargs):
7777
else:
7878
kwargs["credentials"] = kwargs.get("credentials", AnonymousCredentials())
7979
kwargs["project"] = kwargs.get("project", "project-id")
80+
if use_mtls is not None:
81+
env_mask["GOOGLE_API_USE_MTLS_ENDPOINT"] = use_mtls
8082
with mock.patch.dict(os.environ, env_mask):
8183
return cls._get_target_class()(*args, **kwargs)
8284

@@ -206,10 +208,8 @@ def test__start_background_channel_refresh(self):
206208
def test__ping_and_warm_instances(self):
207209
"""test ping and warm with mocked asyncio.gather"""
208210
client_mock = mock.Mock()
209-
client_mock._execute_ping_and_warms = (
210-
lambda *args: self._get_target_class()._execute_ping_and_warms(
211-
client_mock, *args
212-
)
211+
client_mock._execute_ping_and_warms = lambda *args: (
212+
self._get_target_class()._execute_ping_and_warms(client_mock, *args)
213213
)
214214
with mock.patch.object(
215215
CrossSync._Sync_Impl, "gather_partials", CrossSync._Sync_Impl.Mock()
@@ -252,10 +252,8 @@ def test__ping_and_warm_instances(self):
252252
def test__ping_and_warm_single_instance(self):
253253
"""should be able to call ping and warm with single instance"""
254254
client_mock = mock.Mock()
255-
client_mock._execute_ping_and_warms = (
256-
lambda *args: self._get_target_class()._execute_ping_and_warms(
257-
client_mock, *args
258-
)
255+
client_mock._execute_ping_and_warms = lambda *args: (
256+
self._get_target_class()._execute_ping_and_warms(client_mock, *args)
259257
)
260258
with mock.patch.object(
261259
CrossSync._Sync_Impl, "gather_partials", CrossSync._Sync_Impl.Mock()
@@ -844,11 +842,17 @@ def test_context_manager(self):
844842
close_mock.assert_called_once()
845843
true_close()
846844

847-
def test_default_universe_domain(self):
845+
@pytest.mark.parametrize(
846+
"use_mtls, expected_domain",
847+
[("never", "googleapis.com"), ("always", "mtls.googleapis.com")],
848+
)
849+
def test_default_universe_domain(self, use_mtls, expected_domain):
848850
"""When not passed, universe_domain should default to googleapis.com"""
849-
with self._make_client(project="project-id", credentials=None) as client:
851+
with self._make_client(
852+
project="project-id", credentials=None, use_mtls=use_mtls
853+
) as client:
850854
assert client.universe_domain == "googleapis.com"
851-
assert client.api_endpoint == "bigtable.googleapis.com"
855+
assert client.api_endpoint == f"bigtable.{expected_domain}"
852856

853857
def test_custom_universe_domain(self):
854858
"""test with a customized universe domain value and emulator enabled"""
@@ -859,6 +863,7 @@ def test_custom_universe_domain(self):
859863
client_options=options,
860864
use_emulator=True,
861865
credentials=None,
866+
use_mtls="never",
862867
) as client:
863868
assert client.universe_domain == universe_domain
864869
assert client.api_endpoint == f"bigtable.{universe_domain}"
@@ -871,20 +876,20 @@ def test_configured_universe_domain_matches_GDU(self):
871876
project="project_id", client_options=options, credentials=None
872877
) as client:
873878
assert client.universe_domain == "googleapis.com"
874-
assert client.api_endpoint == "bigtable.googleapis.com"
875879

876880
def test_credential_universe_domain_matches_GDU(self):
877881
"""Test with credentials"""
878882
creds = AnonymousCredentials()
879883
creds._universe_domain = "googleapis.com"
880884
with self._make_client(project="project_id", credentials=creds) as client:
881885
assert client.universe_domain == "googleapis.com"
882-
assert client.api_endpoint == "bigtable.googleapis.com"
883886

884887
def test_anomynous_credential_universe_domain(self):
885888
"""Anomynopus credentials should use default universe domain"""
886889
creds = AnonymousCredentials()
887-
with self._make_client(project="project_id", credentials=creds) as client:
890+
with self._make_client(
891+
project="project_id", credentials=creds, use_mtls="never"
892+
) as client:
888893
assert client.universe_domain == "googleapis.com"
889894
assert client.api_endpoint == "bigtable.googleapis.com"
890895

@@ -901,6 +906,7 @@ def test_configured_universe_domain_mismatched_credentials(self):
901906
client_options=options,
902907
use_emulator=False,
903908
credentials=creds,
909+
use_mtls="never",
904910
)
905911
err_msg = f"The configured universe domain ({universe_domain}) does not match the universe domain found in the credentials ({creds.universe_domain}). If you haven't configured the universe domain explicitly, `googleapis.com` is the default."
906912
assert exc.value.args[0] == err_msg
@@ -913,7 +919,10 @@ def test_configured_universe_domain_matches_credentials(self):
913919
creds = AnonymousCredentials()
914920
creds._universe_domain = universe_domain
915921
with self._make_client(
916-
project="project_id", credentials=creds, client_options=options
922+
project="project_id",
923+
credentials=creds,
924+
client_options=options,
925+
use_mtls="never",
917926
) as client:
918927
assert client.universe_domain == universe_domain
919928
assert client.api_endpoint == f"bigtable.{universe_domain}"
@@ -1313,11 +1322,11 @@ def _make_client(self, *args, **kwargs):
13131322

13141323
def _make_table(self, *args, **kwargs):
13151324
client_mock = mock.Mock()
1316-
client_mock._register_instance.side_effect = (
1317-
lambda *args, **kwargs: CrossSync._Sync_Impl.yield_to_event_loop()
1325+
client_mock._register_instance.side_effect = lambda *args, **kwargs: (
1326+
CrossSync._Sync_Impl.yield_to_event_loop()
13181327
)
1319-
client_mock._remove_instance_registration.side_effect = (
1320-
lambda *args, **kwargs: CrossSync._Sync_Impl.yield_to_event_loop()
1328+
client_mock._remove_instance_registration.side_effect = lambda *args, **kwargs: (
1329+
CrossSync._Sync_Impl.yield_to_event_loop()
13211330
)
13221331
kwargs["instance_id"] = kwargs.get(
13231332
"instance_id", args[0] if args else "instance"
@@ -1779,9 +1788,8 @@ def test_read_rows_sharded_multiple_queries(self):
17791788
with mock.patch.object(
17801789
table.client._gapic_client, "read_rows"
17811790
) as read_rows:
1782-
read_rows.side_effect = (
1783-
lambda *args,
1784-
**kwargs: CrossSync._Sync_Impl.TestReadRows._make_gapic_stream(
1791+
read_rows.side_effect = lambda *args, **kwargs: (
1792+
CrossSync._Sync_Impl.TestReadRows._make_gapic_stream(
17851793
[
17861794
CrossSync._Sync_Impl.TestReadRows._make_chunk(row_key=k)
17871795
for k in args[0].rows.row_keys
@@ -2870,6 +2878,7 @@ def prepare_mock(self, client):
28702878
yield prepare_mock
28712879

28722880
def _make_gapic_stream(self, sample_list: list["ExecuteQueryResponse" | Exception]):
2881+
28732882
class MockStream:
28742883
def __init__(self, sample_list):
28752884
self.sample_list = sample_list

0 commit comments

Comments
 (0)