Skip to content

Commit adaae9d

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 adaae9d

4 files changed

Lines changed: 52 additions & 8 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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ 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)
@@ -1021,6 +1022,7 @@ 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)
@@ -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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ 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)
@@ -1007,6 +1008,7 @@ 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)
@@ -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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ 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)
@@ -968,6 +969,7 @@ 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)
@@ -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

0 commit comments

Comments
 (0)