Skip to content

Commit a623576

Browse files
test(spanner): clear context-aware mTLS env var in unit tests (#17838)
Unset CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE in mTLS unit tests to prevent failures in Google and Kokoro CI environments. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-cloud-python/issues) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕 --------- Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 744e826 commit a623576

22 files changed

Lines changed: 115 additions & 18 deletions

File tree

.librarian/generator-input/client-post-processing/spanner-integration.yaml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,42 @@ replacements:
9292
after: |
9393
\g<1> metrics_interceptor=mock.ANY,
9494
count: 1
95-
95+
# Temporary patch until gapic-generator 1.38 or newer is released.
96+
# From https://github.com/googleapis/google-cloud-python/pull/17838
97+
- paths:
98+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py
99+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py
100+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py
101+
before: |
102+
for config_data, expected_cert_source in test_cases:
103+
\ env = os.environ.copy\(\)
104+
\ env.pop\("GOOGLE_API_USE_CLIENT_CERTIFICATE", None\)
105+
\ with mock.patch.dict\(os.environ, env, clear=True\):
106+
after: |
107+
for config_data, expected_cert_source in test_cases:
108+
env = os.environ.copy()
109+
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
110+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
111+
with mock.patch.dict(os.environ, env, clear=True):
112+
count: 3
113+
# Temporary patch until gapic-generator 1.38 or newer is released.
114+
# From https://github.com/googleapis/google-cloud-python/pull/17838
115+
- paths:
116+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py
117+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py
118+
- packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py
119+
before: |
120+
for config_data, expected_cert_source in test_cases:
121+
\ env = os.environ.copy\(\)
122+
\ env.pop\("GOOGLE_API_USE_CLIENT_CERTIFICATE", ""\)
123+
\ with mock.patch.dict\(os.environ, env, clear=True\):
124+
after: |
125+
for config_data, expected_cert_source in test_cases:
126+
env = os.environ.copy()
127+
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
128+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
129+
with mock.patch.dict(os.environ, env, clear=True):
130+
count: 3
96131
- paths:
97132
- packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py
98133
# Group 1: Captures ONLY the spaces/tabs before the text

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

Lines changed: 2 additions & 0 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)

packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
696696
for config_data, expected_cert_source in test_cases:
697697
env = os.environ.copy()
698698
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
699+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
699700
with mock.patch.dict(os.environ, env, clear=True):
700701
config_filename = "mock_certificate_config.json"
701702
config_file_content = json.dumps(config_data)
@@ -743,6 +744,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
743744
for config_data, expected_cert_source in test_cases:
744745
env = os.environ.copy()
745746
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
747+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
746748
with mock.patch.dict(os.environ, env, clear=True):
747749
config_filename = "mock_certificate_config.json"
748750
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
686686
for config_data, expected_cert_source in test_cases:
687687
env = os.environ.copy()
688688
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
689+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
689690
with mock.patch.dict(os.environ, env, clear=True):
690691
config_filename = "mock_certificate_config.json"
691692
config_file_content = json.dumps(config_data)
@@ -733,6 +734,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
733734
for config_data, expected_cert_source in test_cases:
734735
env = os.environ.copy()
735736
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
737+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
736738
with mock.patch.dict(os.environ, env, clear=True):
737739
config_filename = "mock_certificate_config.json"
738740
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
717717
for config_data, expected_cert_source in test_cases:
718718
env = os.environ.copy()
719719
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
720+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
720721
with mock.patch.dict(os.environ, env, clear=True):
721722
config_filename = "mock_certificate_config.json"
722723
config_file_content = json.dumps(config_data)
@@ -764,6 +765,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
764765
for config_data, expected_cert_source in test_cases:
765766
env = os.environ.copy()
766767
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
768+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
767769
with mock.patch.dict(os.environ, env, clear=True):
768770
config_filename = "mock_certificate_config.json"
769771
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def test_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_class
674674
for config_data, expected_cert_source in test_cases:
675675
env = os.environ.copy()
676676
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
677+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
677678
with mock.patch.dict(os.environ, env, clear=True):
678679
config_filename = "mock_certificate_config.json"
679680
config_file_content = json.dumps(config_data)
@@ -721,6 +722,7 @@ def test_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_class
721722
for config_data, expected_cert_source in test_cases:
722723
env = os.environ.copy()
723724
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
725+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
724726
with mock.patch.dict(os.environ, env, clear=True):
725727
config_filename = "mock_certificate_config.json"
726728
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
675675
for config_data, expected_cert_source in test_cases:
676676
env = os.environ.copy()
677677
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
678+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
678679
with mock.patch.dict(os.environ, env, clear=True):
679680
config_filename = "mock_certificate_config.json"
680681
config_file_content = json.dumps(config_data)
@@ -722,6 +723,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
722723
for config_data, expected_cert_source in test_cases:
723724
env = os.environ.copy()
724725
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
726+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
725727
with mock.patch.dict(os.environ, env, clear=True):
726728
config_filename = "mock_certificate_config.json"
727729
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ def test_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
673673
for config_data, expected_cert_source in test_cases:
674674
env = os.environ.copy()
675675
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
676+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
676677
with mock.patch.dict(os.environ, env, clear=True):
677678
config_filename = "mock_certificate_config.json"
678679
config_file_content = json.dumps(config_data)
@@ -720,6 +721,7 @@ def test_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
720721
for config_data, expected_cert_source in test_cases:
721722
env = os.environ.copy()
722723
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
724+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
723725
with mock.patch.dict(os.environ, env, clear=True):
724726
config_filename = "mock_certificate_config.json"
725727
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def test_base_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_
674674
for config_data, expected_cert_source in test_cases:
675675
env = os.environ.copy()
676676
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
677+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
677678
with mock.patch.dict(os.environ, env, clear=True):
678679
config_filename = "mock_certificate_config.json"
679680
config_file_content = json.dumps(config_data)
@@ -721,6 +722,7 @@ def test_base_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_
721722
for config_data, expected_cert_source in test_cases:
722723
env = os.environ.copy()
723724
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
725+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
724726
with mock.patch.dict(os.environ, env, clear=True):
725727
config_filename = "mock_certificate_config.json"
726728
config_file_content = json.dumps(config_data)

packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
675675
for config_data, expected_cert_source in test_cases:
676676
env = os.environ.copy()
677677
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
678+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
678679
with mock.patch.dict(os.environ, env, clear=True):
679680
config_filename = "mock_certificate_config.json"
680681
config_file_content = json.dumps(config_data)
@@ -722,6 +723,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
722723
for config_data, expected_cert_source in test_cases:
723724
env = os.environ.copy()
724725
env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
726+
env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
725727
with mock.patch.dict(os.environ, env, clear=True):
726728
config_filename = "mock_certificate_config.json"
727729
config_file_content = json.dumps(config_data)

0 commit comments

Comments
 (0)