Skip to content

Commit d19e180

Browse files
authored
fix: use context manager for mtls env var (#548)
1 parent 166c140 commit d19e180

File tree

2 files changed

+109
-111
lines changed

2 files changed

+109
-111
lines changed

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,21 @@ def test_{{ service.client_name|snake_case }}_client_options():
105105

106106
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
107107
# "never".
108-
os.environ["GOOGLE_API_USE_MTLS"] = "never"
109-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
110-
grpc_transport.return_value = None
111-
client = {{ service.client_name }}()
112-
grpc_transport.assert_called_once_with(
113-
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
114-
client_cert_source=None,
115-
credentials=None,
116-
host=client.DEFAULT_ENDPOINT,
117-
)
108+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}):
109+
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
110+
grpc_transport.return_value = None
111+
client = {{ service.client_name }}()
112+
grpc_transport.assert_called_once_with(
113+
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
114+
client_cert_source=None,
115+
credentials=None,
116+
host=client.DEFAULT_ENDPOINT,
117+
)
118118

119119
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
120120
# "always".
121-
os.environ["GOOGLE_API_USE_MTLS"] = "always"
122-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
123-
grpc_transport.return_value = None
124-
client = {{ service.client_name }}()
125-
grpc_transport.assert_called_once_with(
126-
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
127-
client_cert_source=None,
128-
credentials=None,
129-
host=client.DEFAULT_MTLS_ENDPOINT,
130-
)
131-
132-
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
133-
# "auto", and client_cert_source is provided.
134-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
135-
options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
136-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
137-
grpc_transport.return_value = None
138-
client = {{ service.client_name }}(client_options=options)
139-
grpc_transport.assert_called_once_with(
140-
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
141-
client_cert_source=client_cert_source_callback,
142-
credentials=None,
143-
host=client.DEFAULT_MTLS_ENDPOINT,
144-
)
145-
146-
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
147-
# "auto", and default_client_cert_source is provided.
148-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
149-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
150-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
121+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}):
122+
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
151123
grpc_transport.return_value = None
152124
client = {{ service.client_name }}()
153125
grpc_transport.assert_called_once_with(
@@ -158,26 +130,52 @@ def test_{{ service.client_name|snake_case }}_client_options():
158130
)
159131

160132
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
161-
# "auto", but client_cert_source and default_client_cert_source are None.
162-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
163-
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
164-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
133+
# "auto", and client_cert_source is provided.
134+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
135+
options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
136+
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
165137
grpc_transport.return_value = None
166-
client = {{ service.client_name }}()
138+
client = {{ service.client_name }}(client_options=options)
167139
grpc_transport.assert_called_once_with(
168-
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
169-
client_cert_source=None,
140+
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
141+
client_cert_source=client_cert_source_callback,
170142
credentials=None,
171-
host=client.DEFAULT_ENDPOINT,
143+
host=client.DEFAULT_MTLS_ENDPOINT,
172144
)
173145

146+
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
147+
# "auto", and default_client_cert_source is provided.
148+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
149+
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
150+
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
151+
grpc_transport.return_value = None
152+
client = {{ service.client_name }}()
153+
grpc_transport.assert_called_once_with(
154+
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
155+
client_cert_source=None,
156+
credentials=None,
157+
host=client.DEFAULT_MTLS_ENDPOINT,
158+
)
159+
160+
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
161+
# "auto", but client_cert_source and default_client_cert_source are None.
162+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
163+
with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
164+
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
165+
grpc_transport.return_value = None
166+
client = {{ service.client_name }}()
167+
grpc_transport.assert_called_once_with(
168+
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
169+
client_cert_source=None,
170+
credentials=None,
171+
host=client.DEFAULT_ENDPOINT,
172+
)
173+
174174
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has
175175
# unsupported value.
176-
os.environ["GOOGLE_API_USE_MTLS"] = "Unsupported"
177-
with pytest.raises(MutualTLSChannelError):
178-
client = {{ service.client_name }}()
179-
180-
del os.environ["GOOGLE_API_USE_MTLS"]
176+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}):
177+
with pytest.raises(MutualTLSChannelError):
178+
client = {{ service.client_name }}()
181179

182180

183181
def test_{{ service.client_name|snake_case }}_client_options_from_dict():

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -122,59 +122,24 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans
122122

123123
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
124124
# "never".
125-
os.environ["GOOGLE_API_USE_MTLS"] = "never"
126-
with mock.patch.object(transport_class, '__init__') as patched:
127-
patched.return_value = None
128-
client = client_class()
129-
patched.assert_called_once_with(
130-
credentials=None,
131-
credentials_file=None,
132-
host=client.DEFAULT_ENDPOINT,
133-
scopes=None,
134-
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
135-
client_cert_source=None,
136-
quota_project_id=None,
137-
)
125+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}):
126+
with mock.patch.object(transport_class, '__init__') as patched:
127+
patched.return_value = None
128+
client = client_class()
129+
patched.assert_called_once_with(
130+
credentials=None,
131+
credentials_file=None,
132+
host=client.DEFAULT_ENDPOINT,
133+
scopes=None,
134+
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
135+
client_cert_source=None,
136+
quota_project_id=None,
137+
)
138138

139139
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
140140
# "always".
141-
os.environ["GOOGLE_API_USE_MTLS"] = "always"
142-
with mock.patch.object(transport_class, '__init__') as patched:
143-
patched.return_value = None
144-
client = client_class()
145-
patched.assert_called_once_with(
146-
credentials=None,
147-
credentials_file=None,
148-
host=client.DEFAULT_MTLS_ENDPOINT,
149-
scopes=None,
150-
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
151-
client_cert_source=None,
152-
quota_project_id=None,
153-
)
154-
155-
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
156-
# "auto", and client_cert_source is provided.
157-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
158-
options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
159-
with mock.patch.object(transport_class, '__init__') as patched:
160-
patched.return_value = None
161-
client = client_class(client_options=options)
162-
patched.assert_called_once_with(
163-
credentials=None,
164-
credentials_file=None,
165-
host=client.DEFAULT_MTLS_ENDPOINT,
166-
scopes=None,
167-
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
168-
client_cert_source=client_cert_source_callback,
169-
quota_project_id=None,
170-
171-
)
172-
173-
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
174-
# "auto", and default_client_cert_source is provided.
175-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
176-
with mock.patch.object(transport_class, '__init__') as patched:
177-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
141+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}):
142+
with mock.patch.object(transport_class, '__init__') as patched:
178143
patched.return_value = None
179144
client = client_class()
180145
patched.assert_called_once_with(
@@ -188,22 +153,57 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans
188153
)
189154

190155
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
191-
# "auto", but client_cert_source and default_client_cert_source are None.
192-
os.environ["GOOGLE_API_USE_MTLS"] = "auto"
193-
with mock.patch.object(transport_class, '__init__') as patched:
194-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
156+
# "auto", and client_cert_source is provided.
157+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
158+
options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
159+
with mock.patch.object(transport_class, '__init__') as patched:
195160
patched.return_value = None
196-
client = client_class()
161+
client = client_class(client_options=options)
197162
patched.assert_called_once_with(
198163
credentials=None,
199164
credentials_file=None,
200-
host=client.DEFAULT_ENDPOINT,
165+
host=client.DEFAULT_MTLS_ENDPOINT,
201166
scopes=None,
202-
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
203-
client_cert_source=None,
167+
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
168+
client_cert_source=client_cert_source_callback,
204169
quota_project_id=None,
170+
205171
)
206172

173+
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
174+
# "auto", and default_client_cert_source is provided.
175+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
176+
with mock.patch.object(transport_class, '__init__') as patched:
177+
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
178+
patched.return_value = None
179+
client = client_class()
180+
patched.assert_called_once_with(
181+
credentials=None,
182+
credentials_file=None,
183+
host=client.DEFAULT_MTLS_ENDPOINT,
184+
scopes=None,
185+
api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
186+
client_cert_source=None,
187+
quota_project_id=None,
188+
)
189+
190+
# Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
191+
# "auto", but client_cert_source and default_client_cert_source are None.
192+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
193+
with mock.patch.object(transport_class, '__init__') as patched:
194+
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
195+
patched.return_value = None
196+
client = client_class()
197+
patched.assert_called_once_with(
198+
credentials=None,
199+
credentials_file=None,
200+
host=client.DEFAULT_ENDPOINT,
201+
scopes=None,
202+
api_mtls_endpoint=client.DEFAULT_ENDPOINT,
203+
client_cert_source=None,
204+
quota_project_id=None,
205+
)
206+
207207
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has
208208
# unsupported value.
209209
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}):

0 commit comments

Comments
 (0)