Skip to content

Commit 9673382

Browse files
committed
address PR comments on client_utils.py and test_client_utils.py
1 parent c1b8d0c commit 9673382

2 files changed

Lines changed: 111 additions & 103 deletions

File tree

packages/google-api-core/google/api_core/gapic_v1/client_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
"""Helpers for client setup and configuration."""
1818

19-
from typing import Any, Optional
19+
from typing import Callable, Optional, Tuple
2020

2121
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2222

@@ -50,7 +50,7 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
5050

5151
def get_api_endpoint(
5252
api_override: Optional[str],
53-
client_cert_source: Optional[Any],
53+
client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]],
5454
universe_domain: str,
5555
use_mtls_endpoint: str,
5656
default_universe: str,
@@ -62,7 +62,8 @@ def get_api_endpoint(
6262
Args:
6363
api_override (Optional[str]): The API endpoint override. If specified,
6464
this is always returned.
65-
client_cert_source (Optional[Any]): The client certificate source used by the client.
65+
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): The client
66+
certificate source used by the client.
6667
universe_domain (str): The universe domain used by the client.
6768
use_mtls_endpoint (str): How to use the mTLS endpoint. Possible values
6869
are "always", "auto", or "never".

packages/google-api-core/tests/unit/gapic/test_client_utils.py

Lines changed: 107 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -57,129 +57,136 @@ def test_get_default_mtls_endpoint():
5757
assert get_default_mtls_endpoint(None) is None
5858

5959

60-
def test__get_api_endpoint():
61-
api_override = "foo.com"
62-
mock_client_cert_source = mock.Mock()
63-
default_universe = MockClient._DEFAULT_UNIVERSE
64-
default_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format(
65-
UNIVERSE_DOMAIN=default_universe
66-
)
67-
mock_universe = "bar.com"
68-
mock_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format(
69-
UNIVERSE_DOMAIN=mock_universe
70-
)
71-
72-
assert (
73-
get_api_endpoint(
74-
api_override,
75-
mock_client_cert_source,
76-
default_universe,
60+
@pytest.mark.parametrize(
61+
"api_override,client_cert_source,universe_domain,use_mtls_endpoint,default_universe,default_mtls_endpoint,default_endpoint_template,expected",
62+
[
63+
(
64+
"foo.com",
65+
mock.Mock(),
66+
"googleapis.com",
7767
"always",
78-
MockClient._DEFAULT_UNIVERSE,
79-
MockClient.DEFAULT_MTLS_ENDPOINT,
80-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
81-
)
82-
== api_override
83-
)
84-
assert (
85-
get_api_endpoint(
68+
"googleapis.com",
69+
"foo.mtls.googleapis.com",
70+
"foo.{UNIVERSE_DOMAIN}",
71+
"foo.com",
72+
),
73+
(
8674
None,
87-
mock_client_cert_source,
88-
default_universe,
75+
mock.Mock(),
76+
"googleapis.com",
8977
"auto",
90-
MockClient._DEFAULT_UNIVERSE,
91-
MockClient.DEFAULT_MTLS_ENDPOINT,
92-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
93-
)
94-
== MockClient.DEFAULT_MTLS_ENDPOINT
95-
)
96-
assert (
97-
get_api_endpoint(
78+
"googleapis.com",
79+
"foo.mtls.googleapis.com",
80+
"foo.{UNIVERSE_DOMAIN}",
81+
"foo.mtls.googleapis.com",
82+
),
83+
(
9884
None,
9985
None,
100-
default_universe,
86+
"googleapis.com",
10187
"auto",
102-
MockClient._DEFAULT_UNIVERSE,
103-
MockClient.DEFAULT_MTLS_ENDPOINT,
104-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
105-
)
106-
== default_endpoint
107-
)
108-
assert (
109-
get_api_endpoint(
88+
"googleapis.com",
89+
"foo.mtls.googleapis.com",
90+
"foo.{UNIVERSE_DOMAIN}",
91+
"foo.googleapis.com",
92+
),
93+
(
11094
None,
11195
None,
112-
default_universe,
96+
"googleapis.com",
11397
"always",
114-
MockClient._DEFAULT_UNIVERSE,
115-
MockClient.DEFAULT_MTLS_ENDPOINT,
116-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
117-
)
118-
== MockClient.DEFAULT_MTLS_ENDPOINT
119-
)
120-
assert (
121-
get_api_endpoint(
98+
"googleapis.com",
99+
"foo.mtls.googleapis.com",
100+
"foo.{UNIVERSE_DOMAIN}",
101+
"foo.mtls.googleapis.com",
102+
),
103+
(
122104
None,
123-
mock_client_cert_source,
124-
default_universe,
105+
mock.Mock(),
106+
"googleapis.com",
125107
"always",
126-
MockClient._DEFAULT_UNIVERSE,
127-
MockClient.DEFAULT_MTLS_ENDPOINT,
128-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
129-
)
130-
== MockClient.DEFAULT_MTLS_ENDPOINT
131-
)
132-
assert (
133-
get_api_endpoint(
108+
"googleapis.com",
109+
"foo.mtls.googleapis.com",
110+
"foo.{UNIVERSE_DOMAIN}",
111+
"foo.mtls.googleapis.com",
112+
),
113+
(
134114
None,
135115
None,
136-
mock_universe,
116+
"bar.com",
137117
"never",
138-
MockClient._DEFAULT_UNIVERSE,
139-
MockClient.DEFAULT_MTLS_ENDPOINT,
140-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
141-
)
142-
== mock_endpoint
143-
)
144-
assert (
145-
get_api_endpoint(
118+
"googleapis.com",
119+
"foo.mtls.googleapis.com",
120+
"foo.{UNIVERSE_DOMAIN}",
121+
"foo.bar.com",
122+
),
123+
(
146124
None,
147125
None,
148-
default_universe,
126+
"googleapis.com",
149127
"never",
150-
MockClient._DEFAULT_UNIVERSE,
151-
MockClient.DEFAULT_MTLS_ENDPOINT,
152-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
153-
)
154-
== default_endpoint
155-
)
156-
157-
with pytest.raises(MutualTLSChannelError) as excinfo:
158-
get_api_endpoint(
128+
"googleapis.com",
129+
"foo.mtls.googleapis.com",
130+
"foo.{UNIVERSE_DOMAIN}",
131+
"foo.googleapis.com",
132+
),
133+
(
159134
None,
160-
mock_client_cert_source,
161-
mock_universe,
135+
mock.Mock(),
136+
"bar.com",
162137
"auto",
163-
MockClient._DEFAULT_UNIVERSE,
164-
MockClient.DEFAULT_MTLS_ENDPOINT,
165-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
166-
)
167-
assert (
168-
str(excinfo.value)
169-
== "mTLS is not supported in any universe other than googleapis.com."
170-
)
171-
172-
with pytest.raises(ValueError) as excinfo:
173-
get_api_endpoint(
138+
"googleapis.com",
139+
"foo.mtls.googleapis.com",
140+
"foo.{UNIVERSE_DOMAIN}",
141+
MutualTLSChannelError,
142+
),
143+
(
174144
None,
175-
mock_client_cert_source,
176-
default_universe,
145+
mock.Mock(),
146+
"googleapis.com",
177147
"always",
178-
MockClient._DEFAULT_UNIVERSE,
148+
"googleapis.com",
179149
None,
180-
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
150+
"foo.{UNIVERSE_DOMAIN}",
151+
ValueError,
152+
),
153+
],
154+
)
155+
def test_get_api_endpoint(
156+
api_override,
157+
client_cert_source,
158+
universe_domain,
159+
use_mtls_endpoint,
160+
default_universe,
161+
default_mtls_endpoint,
162+
default_endpoint_template,
163+
expected,
164+
):
165+
if isinstance(expected, type) and issubclass(expected, Exception):
166+
with pytest.raises(expected):
167+
get_api_endpoint(
168+
api_override,
169+
client_cert_source,
170+
universe_domain,
171+
use_mtls_endpoint,
172+
default_universe,
173+
default_mtls_endpoint,
174+
default_endpoint_template,
175+
)
176+
else:
177+
assert (
178+
get_api_endpoint(
179+
api_override,
180+
client_cert_source,
181+
universe_domain,
182+
use_mtls_endpoint,
183+
default_universe,
184+
default_mtls_endpoint,
185+
default_endpoint_template,
186+
)
187+
== expected
181188
)
182-
assert str(excinfo.value) == "mTLS endpoint is not available."
189+
183190

184191

185192
def test__get_universe_domain():

0 commit comments

Comments
 (0)