Skip to content

Commit 2ba4dfd

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 2ba4dfd

6 files changed

Lines changed: 80 additions & 15 deletions

File tree

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

Lines changed: 4 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,13 @@ 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(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never", "GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
967969
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
968970
assert api_endpoint == client_class.DEFAULT_ENDPOINT
969971
assert cert_source is None
970972

971973
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
972-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
974+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
973975
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
974976
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
975977
assert cert_source is None

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,12 @@ 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("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
982983
with mock.patch.dict(
983984
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
984985
):
@@ -1021,11 +1022,12 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10211022
for config_data, expected_cert_source in test_cases:
10221023
env = os.environ.copy()
10231024
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
1025+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
10241026
with mock.patch.dict(os.environ, env, clear=True):
10251027
config_filename = "mock_certificate_config.json"
10261028
config_file_content = json.dumps(config_data)
10271029
m = mock.mock_open(read_data=config_file_content)
1028-
with mock.patch("builtins.open", m):
1030+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
10291031
with mock.patch.dict(
10301032
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
10311033
):
@@ -1041,13 +1043,25 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10411043
assert cert_source is expected_cert_source
10421044

10431045
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
1044-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
1046+
with mock.patch.dict(
1047+
os.environ,
1048+
{
1049+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
1050+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1051+
},
1052+
):
10451053
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10461054
assert api_endpoint == client_class.DEFAULT_ENDPOINT
10471055
assert cert_source is None
10481056

10491057
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
1050-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1058+
with mock.patch.dict(
1059+
os.environ,
1060+
{
1061+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1062+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1063+
},
1064+
):
10511065
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10521066
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10531067
assert cert_source is None

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,12 @@ 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("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
968969
with mock.patch.dict(
969970
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
970971
):
@@ -1007,11 +1008,12 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10071008
for config_data, expected_cert_source in test_cases:
10081009
env = os.environ.copy()
10091010
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
1011+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
10101012
with mock.patch.dict(os.environ, env, clear=True):
10111013
config_filename = "mock_certificate_config.json"
10121014
config_file_content = json.dumps(config_data)
10131015
m = mock.mock_open(read_data=config_file_content)
1014-
with mock.patch("builtins.open", m):
1016+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
10151017
with mock.patch.dict(
10161018
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
10171019
):
@@ -1027,13 +1029,25 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class):
10271029
assert cert_source is expected_cert_source
10281030

10291031
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
1030-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
1032+
with mock.patch.dict(
1033+
os.environ,
1034+
{
1035+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
1036+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1037+
},
1038+
):
10311039
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10321040
assert api_endpoint == client_class.DEFAULT_ENDPOINT
10331041
assert cert_source is None
10341042

10351043
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
1036-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1044+
with mock.patch.dict(
1045+
os.environ,
1046+
{
1047+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1048+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1049+
},
1050+
):
10371051
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
10381052
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10391053
assert cert_source is None

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,11 +921,12 @@ 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("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
929930
with mock.patch.dict(
930931
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
931932
):
@@ -968,11 +969,12 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class):
968969
for config_data, expected_cert_source in test_cases:
969970
env = os.environ.copy()
970971
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
972+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
971973
with mock.patch.dict(os.environ, env, clear=True):
972974
config_filename = "mock_certificate_config.json"
973975
config_file_content = json.dumps(config_data)
974976
m = mock.mock_open(read_data=config_file_content)
975-
with mock.patch("builtins.open", m):
977+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
976978
with mock.patch.dict(
977979
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
978980
):
@@ -988,13 +990,25 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class):
988990
assert cert_source is expected_cert_source
989991

990992
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
991-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
993+
with mock.patch.dict(
994+
os.environ,
995+
{
996+
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
997+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
998+
},
999+
):
9921000
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
9931001
assert api_endpoint == client_class.DEFAULT_ENDPOINT
9941002
assert cert_source is None
9951003

9961004
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
997-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
1005+
with mock.patch.dict(
1006+
os.environ,
1007+
{
1008+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
1009+
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
1010+
},
1011+
):
9981012
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
9991013
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
10001014
assert cert_source is None

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

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

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

107+
@unittest.skipUnless(
108+
hasattr(_helpers, "GoogleCloudResourceDetector"),
109+
"opentelemetry-resourcedetector-gcp not installed",
110+
)
107111
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
108112
def test_get_location_with_region(self, mock_detect):
109113
"""Test that _get_cloud_region returns the region when detected."""
@@ -115,6 +119,10 @@ def test_get_location_with_region(self, mock_detect):
115119
location = self._callFUT()
116120
self.assertEqual(location, "us-central1")
117121

122+
@unittest.skipUnless(
123+
hasattr(_helpers, "GoogleCloudResourceDetector"),
124+
"opentelemetry-resourcedetector-gcp not installed",
125+
)
118126
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
119127
def test_get_location_without_region(self, mock_detect):
120128
"""Test that _get_cloud_region returns 'global' when no region is detected."""
@@ -124,6 +132,10 @@ def test_get_location_without_region(self, mock_detect):
124132
location = self._callFUT()
125133
self.assertEqual(location, "global")
126134

135+
@unittest.skipUnless(
136+
hasattr(_helpers, "GoogleCloudResourceDetector"),
137+
"opentelemetry-resourcedetector-gcp not installed",
138+
)
127139
@mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect")
128140
def test_get_location_with_exception(self, mock_detect):
129141
"""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)