Skip to content

Commit 15caba0

Browse files
test(spanner): clear context-aware mTLS env var in unit tests
Unset CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE in mTLS unit tests to prevent failures in Google and Kokoro CI environments.
1 parent 270c7fe commit 15caba0

7 files changed

Lines changed: 127 additions & 19 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
896896
for config_data, expected_cert_source in test_cases:
897897
env = os.environ.copy()
898898
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
899+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
899900
with mock.patch.dict(os.environ, env, clear=True):
900901
config_filename = "mock_certificate_config.json"
901902
config_file_content = json.dumps(config_data)
@@ -943,6 +944,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
943944
for config_data, expected_cert_source in test_cases:
944945
env = os.environ.copy()
945946
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
947+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
946948
with mock.patch.dict(os.environ, env, clear=True):
947949
config_filename = "mock_certificate_config.json"
948950
config_file_content = json.dumps(config_data)
@@ -963,13 +965,25 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
963965
assert cert_source is expected_cert_source
964966

965967
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
966-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
968+
with mock.patch.dict(
969+
os.environ,
970+
{
971+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
972+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
973+
},
974+
):
967975
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
968976
assert api_endpoint == client_class.DEFAULT_ENDPOINT
969977
assert cert_source is None
970978

971979
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
972-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
980+
with mock.patch.dict(
981+
os.environ,
982+
{
983+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
984+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
985+
},
986+
):
973987
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
974988
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
975989
assert cert_source is None

packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@
4848
try:
4949
from opentelemetry.propagate import inject
5050
from opentelemetry.propagators.textmap import Setter
51+
from opentelemetry.semconv.resource import ResourceAttributes
52+
53+
HAS_OPENTELEMETRY_INSTALLED = True
54+
except ImportError:
55+
HAS_OPENTELEMETRY_INSTALLED = False
56+
57+
try:
5158
from opentelemetry.resourcedetector import gcp_resource_detector
5259
from opentelemetry.resourcedetector.gcp_resource_detector import (
5360
GoogleCloudResourceDetector,
5461
)
55-
from opentelemetry.semconv.resource import ResourceAttributes
5662

5763
# Overwrite the requests timeout for the detector.
5864
# This is necessary as the client will wait the full timeout if the
5965
# code is not run in a GCP environment, with the location endpoints available.
6066
gcp_resource_detector._TIMEOUT_SEC = 0.2
61-
62-
HAS_OPENTELEMETRY_INSTALLED = True
6367
except ImportError:
64-
HAS_OPENTELEMETRY_INSTALLED = False
68+
GoogleCloudResourceDetector = None
6569
import random
6670
from typing import List, Tuple
6771

packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,16 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
974974
for config_data, expected_cert_source in test_cases:
975975
env = os.environ.copy()
976976
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
977+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
977978
with mock.patch.dict(os.environ, env, clear=True):
978979
config_filename = "mock_certificate_config.json"
979980
config_file_content = json.dumps(config_data)
980981
m = mock.mock_open(read_data=config_file_content)
981-
with mock.patch("builtins.open", m):
982+
with mock.patch("builtins.open", m), mock.patch(
983+
"os.path.exists",
984+
side_effect=lambda path: os.path.basename(path)
985+
== config_filename,
986+
):
982987
with mock.patch.dict(
983988
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
984989
):
@@ -1021,11 +1026,16 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10211026
for config_data, expected_cert_source in test_cases:
10221027
env = os.environ.copy()
10231028
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
1029+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
10241030
with mock.patch.dict(os.environ, env, clear=True):
10251031
config_filename = "mock_certificate_config.json"
10261032
config_file_content = json.dumps(config_data)
10271033
m = mock.mock_open(read_data=config_file_content)
1028-
with mock.patch("builtins.open", m):
1034+
with mock.patch("builtins.open", m), mock.patch(
1035+
"os.path.exists",
1036+
side_effect=lambda path: os.path.basename(path)
1037+
== config_filename,
1038+
):
10291039
with mock.patch.dict(
10301040
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
10311041
):
@@ -1041,13 +1051,25 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10411051
assert cert_source is expected_cert_source
10421052

10431053
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
1044-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
1054+
with mock.patch.dict(
1055+
os.environ,
1056+
{
1057+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
1058+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1059+
},
1060+
):
10451061
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10461062
assert api_endpoint == client_class.DEFAULT_ENDPOINT
10471063
assert cert_source is None
10481064

10491065
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
1050-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1066+
with mock.patch.dict(
1067+
os.environ,
1068+
{
1069+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1070+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1071+
},
1072+
):
10511073
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10521074
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10531075
assert cert_source is None

packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,16 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class):
960960
for config_data, expected_cert_source in test_cases:
961961
env = os.environ.copy()
962962
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
963+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
963964
with mock.patch.dict(os.environ, env, clear=True):
964965
config_filename = "mock_certificate_config.json"
965966
config_file_content = json.dumps(config_data)
966967
m = mock.mock_open(read_data=config_file_content)
967-
with mock.patch("builtins.open", m):
968+
with mock.patch("builtins.open", m), mock.patch(
969+
"os.path.exists",
970+
side_effect=lambda path: os.path.basename(path)
971+
== config_filename,
972+
):
968973
with mock.patch.dict(
969974
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
970975
):
@@ -1007,11 +1012,16 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10071012
for config_data, expected_cert_source in test_cases:
10081013
env = os.environ.copy()
10091014
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
1015+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
10101016
with mock.patch.dict(os.environ, env, clear=True):
10111017
config_filename = "mock_certificate_config.json"
10121018
config_file_content = json.dumps(config_data)
10131019
m = mock.mock_open(read_data=config_file_content)
1014-
with mock.patch("builtins.open", m):
1020+
with mock.patch("builtins.open", m), mock.patch(
1021+
"os.path.exists",
1022+
side_effect=lambda path: os.path.basename(path)
1023+
== config_filename,
1024+
):
10151025
with mock.patch.dict(
10161026
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
10171027
):
@@ -1027,13 +1037,25 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10271037
assert cert_source is expected_cert_source
10281038

10291039
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
1030-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
1040+
with mock.patch.dict(
1041+
os.environ,
1042+
{
1043+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
1044+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1045+
},
1046+
):
10311047
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10321048
assert api_endpoint == client_class.DEFAULT_ENDPOINT
10331049
assert cert_source is None
10341050

10351051
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
1036-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1052+
with mock.patch.dict(
1053+
os.environ,
1054+
{
1055+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1056+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1057+
},
1058+
):
10371059
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10381060
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10391061
assert cert_source is None

packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,11 +921,16 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class):
921921
for config_data, expected_cert_source in test_cases:
922922
env = os.environ.copy()
923923
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
924+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
924925
with mock.patch.dict(os.environ, env, clear=True):
925926
config_filename = "mock_certificate_config.json"
926927
config_file_content = json.dumps(config_data)
927928
m = mock.mock_open(read_data=config_file_content)
928-
with mock.patch("builtins.open", m):
929+
with mock.patch("builtins.open", m), mock.patch(
930+
"os.path.exists",
931+
side_effect=lambda path: os.path.basename(path)
932+
== config_filename,
933+
):
929934
with mock.patch.dict(
930935
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
931936
):
@@ -968,11 +973,16 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class):
968973
for config_data, expected_cert_source in test_cases:
969974
env = os.environ.copy()
970975
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
976+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
971977
with mock.patch.dict(os.environ, env, clear=True):
972978
config_filename = "mock_certificate_config.json"
973979
config_file_content = json.dumps(config_data)
974980
m = mock.mock_open(read_data=config_file_content)
975-
with mock.patch("builtins.open", m):
981+
with mock.patch("builtins.open", m), mock.patch(
982+
"os.path.exists",
983+
side_effect=lambda path: os.path.basename(path)
984+
== config_filename,
985+
):
976986
with mock.patch.dict(
977987
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
978988
):
@@ -988,13 +998,25 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class):
988998
assert cert_source is expected_cert_source
989999

9901000
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
991-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
1001+
with mock.patch.dict(
1002+
os.environ,
1003+
{
1004+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
1005+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1006+
},
1007+
):
9921008
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
9931009
assert api_endpoint == client_class.DEFAULT_ENDPOINT
9941010
assert cert_source is None
9951011

9961012
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
997-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1013+
with mock.patch.dict(
1014+
os.environ,
1015+
{
1016+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1017+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1018+
},
1019+
):
9981020
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
9991021
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10001022
assert cert_source is None

packages/google-cloud-spanner/tests/unit/test__helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def _callFUT(self, *args, **kw):
104104

105105
return _get_cloud_region(*args, **kw)
106106

107+
@unittest.skipUnless(
108+
hasattr(_helpers, "GoogleCloudResourceDetector")
109+
and _helpers.GoogleCloudResourceDetector is not None,
110+
"opentelemetry-resourcedetector-gcp not installed",
111+
)
107112
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
108113
def test_get_location_with_region(self, mock_detect):
109114
"""Test that _get_cloud_region returns the region when detected."""
@@ -115,6 +120,11 @@ def test_get_location_with_region(self, mock_detect):
115120
location = self._callFUT()
116121
self.assertEqual(location, "us-central1")
117122

123+
@unittest.skipUnless(
124+
hasattr(_helpers, "GoogleCloudResourceDetector")
125+
and _helpers.GoogleCloudResourceDetector is not None,
126+
"opentelemetry-resourcedetector-gcp not installed",
127+
)
118128
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
119129
def test_get_location_without_region(self, mock_detect):
120130
"""Test that _get_cloud_region returns 'global' when no region is detected."""
@@ -124,6 +134,11 @@ def test_get_location_without_region(self, mock_detect):
124134
location = self._callFUT()
125135
self.assertEqual(location, "global")
126136

137+
@unittest.skipUnless(
138+
hasattr(_helpers, "GoogleCloudResourceDetector")
139+
and _helpers.GoogleCloudResourceDetector is not None,
140+
"opentelemetry-resourcedetector-gcp not installed",
141+
)
127142
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
128143
def test_get_location_with_exception(self, mock_detect):
129144
"""Test that _get_cloud_region returns 'global' and logs a warning on exception."""

packages/google-cloud-spanner/tests/unit/test_batch.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,16 @@ def test_batch_write_no_request_options(self, mock_region):
759759
return_value="global",
760760
)
761761
def test_batch_write_end_to_end_tracing_enabled(self, mock_region):
762-
self._test_batch_write_with_request_options(enable_end_to_end_tracing=True)
762+
if ot_helpers.HAS_OPENTELEMETRY_INSTALLED:
763+
tracer = _opentelemetry_tracing.get_tracer()
764+
with tracer.start_as_current_span("test"):
765+
self._test_batch_write_with_request_options(
766+
enable_end_to_end_tracing=True
767+
)
768+
else:
769+
self._test_batch_write_with_request_options(
770+
enable_end_to_end_tracing=True
771+
)
763772

764773
@mock.patch(
765774
"google.cloud.spanner_v1._opentelemetry_tracing._get_cloud_region",

0 commit comments

Comments
 (0)