diff --git a/.librarian/generator-input/client-post-processing/spanner-integration.yaml b/.librarian/generator-input/client-post-processing/spanner-integration.yaml index e8116fddcd91..77061bbc94a6 100644 --- a/.librarian/generator-input/client-post-processing/spanner-integration.yaml +++ b/.librarian/generator-input/client-post-processing/spanner-integration.yaml @@ -92,7 +92,42 @@ replacements: after: | \g<1> metrics_interceptor=mock.ANY, count: 1 - + # Temporary patch until gapic-generator 1.38 or newer is released. + # From https://github.com/googleapis/google-cloud-python/pull/17838 + - paths: + - packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py + - packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py + - packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py + before: | + for config_data, expected_cert_source in test_cases: + \ env = os.environ.copy\(\) + \ env.pop\("GOOGLE_API_USE_CLIENT_CERTIFICATE", None\) + \ with mock.patch.dict\(os.environ, env, clear=True\): + after: | + for config_data, expected_cert_source in test_cases: + env = os.environ.copy() + env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) + with mock.patch.dict(os.environ, env, clear=True): + count: 3 + # Temporary patch until gapic-generator 1.38 or newer is released. + # From https://github.com/googleapis/google-cloud-python/pull/17838 + - paths: + - packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py + - packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py + - packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py + before: | + for config_data, expected_cert_source in test_cases: + \ env = os.environ.copy\(\) + \ env.pop\("GOOGLE_API_USE_CLIENT_CERTIFICATE", ""\) + \ with mock.patch.dict\(os.environ, env, clear=True\): + after: | + for config_data, expected_cert_source in test_cases: + env = os.environ.copy() + env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") + with mock.patch.dict(os.environ, env, clear=True): + count: 3 - paths: - packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py # Group 1: Captures ONLY the spaces/tabs before the text diff --git a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index c432b75621ea..1ac3d0eb46d4 100644 --- a/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -896,6 +896,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source( for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -943,6 +944,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source( for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 1a367689d14e..624dae1d6279 100755 --- a/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -696,6 +696,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -743,6 +744,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 508e17a7e889..fb1efaef9fb7 100755 --- a/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -686,6 +686,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -733,6 +734,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py index b9495ef6e42d..31e992ed4261 100755 --- a/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py +++ b/packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py @@ -717,6 +717,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -764,6 +765,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 8cc17d810664..4e6d6a028dcc 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -674,6 +674,7 @@ def test_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_class for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" 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 for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index fd56f0210d78..981fe7c5c569 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -675,6 +675,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" 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 for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index 762b4b3ab94d..b75b76971760 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -673,6 +673,7 @@ def test_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" 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 for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py index 93731051d1bc..056a60ca77f0 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -674,6 +674,7 @@ def test_base_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_ for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" 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_ for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py index fd56f0210d78..981fe7c5c569 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -675,6 +675,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" 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 for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index 090ea4a91bec..3cbb28eec983 100755 --- a/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -673,6 +673,7 @@ def test_base_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -720,6 +721,7 @@ def test_base_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index d58d99d1fdb8..f9f5e27adf58 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -704,6 +704,7 @@ def test_cloud_redis_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -751,6 +752,7 @@ def test_cloud_redis_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py index 780964608350..2328c8c618c1 100755 --- a/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/packages/gapic-generator/tests/integration/goldens/redis_selective/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -704,6 +704,7 @@ def test_cloud_redis_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -751,6 +752,7 @@ def test_cloud_redis_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py index 892375775385..2db6f81e5e3f 100755 --- a/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py +++ b/packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_storage_batch_operations.py @@ -772,6 +772,7 @@ def test_storage_batch_operations_client_get_mtls_endpoint_and_cert_source(clien for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -819,6 +820,7 @@ def test_storage_batch_operations_client_get_mtls_endpoint_and_cert_source(clien for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py b/packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py index a9b1c448860c..6026e2db81f6 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py @@ -48,20 +48,24 @@ try: from opentelemetry.propagate import inject from opentelemetry.propagators.textmap import Setter + from opentelemetry.semconv.resource import ResourceAttributes + + HAS_OPENTELEMETRY_INSTALLED = True +except ImportError: + HAS_OPENTELEMETRY_INSTALLED = False + +try: from opentelemetry.resourcedetector import gcp_resource_detector from opentelemetry.resourcedetector.gcp_resource_detector import ( GoogleCloudResourceDetector, ) - from opentelemetry.semconv.resource import ResourceAttributes # Overwrite the requests timeout for the detector. # This is necessary as the client will wait the full timeout if the # code is not run in a GCP environment, with the location endpoints available. gcp_resource_detector._TIMEOUT_SEC = 0.2 - - HAS_OPENTELEMETRY_INSTALLED = True except ImportError: - HAS_OPENTELEMETRY_INSTALLED = False + GoogleCloudResourceDetector = None import random from typing import List, Tuple diff --git a/packages/google-cloud-spanner/tests/unit/_async/test_database.py b/packages/google-cloud-spanner/tests/unit/_async/test_database.py index 93a68fa65eba..bfb96e0aa091 100644 --- a/packages/google-cloud-spanner/tests/unit/_async/test_database.py +++ b/packages/google-cloud-spanner/tests/unit/_async/test_database.py @@ -4211,14 +4211,16 @@ def __init__(self, name, instance=None): self._sessions_manager = mock.Mock() # Configure get_session to return sessions from the pool self._sessions_manager.get_session = mock.AsyncMock( - side_effect=lambda tx_type: self._pool.get() - if hasattr(self, "_pool") and self._pool - else None + side_effect=lambda tx_type: ( + self._pool.get() if hasattr(self, "_pool") and self._pool else None + ) ) self._sessions_manager.put_session = mock.AsyncMock( - side_effect=lambda session: self._pool.put(session) - if hasattr(self, "_pool") and self._pool - else None + side_effect=lambda session: ( + self._pool.put(session) + if hasattr(self, "_pool") and self._pool + else None + ) ) @property diff --git a/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py b/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py index 12cd33a4c98a..274a2adb6d14 100644 --- a/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py +++ b/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py @@ -974,6 +974,7 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -1028,6 +1029,7 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py b/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py index cedbd78610c9..e62d408163e5 100644 --- a/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py +++ b/packages/google-cloud-spanner/tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py @@ -960,6 +960,7 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -1014,6 +1015,7 @@ def test_instance_admin_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py b/packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py index 8990906cd1b5..e0c51416ceac 100644 --- a/packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py +++ b/packages/google-cloud-spanner/tests/unit/gapic/spanner_v1/test_spanner.py @@ -921,6 +921,7 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None) + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None) with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) @@ -975,6 +976,7 @@ def test_spanner_client_get_mtls_endpoint_and_cert_source(client_class): for config_data, expected_cert_source in test_cases: env = os.environ.copy() env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "") + env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "") with mock.patch.dict(os.environ, env, clear=True): config_filename = "mock_certificate_config.json" config_file_content = json.dumps(config_data) diff --git a/packages/google-cloud-spanner/tests/unit/test__helpers.py b/packages/google-cloud-spanner/tests/unit/test__helpers.py index 53f38b7aa76e..b98d4a36e85b 100644 --- a/packages/google-cloud-spanner/tests/unit/test__helpers.py +++ b/packages/google-cloud-spanner/tests/unit/test__helpers.py @@ -104,6 +104,11 @@ def _callFUT(self, *args, **kw): return _get_cloud_region(*args, **kw) + @unittest.skipUnless( + hasattr(_helpers, "GoogleCloudResourceDetector") + and _helpers.GoogleCloudResourceDetector is not None, + "opentelemetry-resourcedetector-gcp not installed", + ) @mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect") def test_get_location_with_region(self, mock_detect): """Test that _get_cloud_region returns the region when detected.""" @@ -115,6 +120,11 @@ def test_get_location_with_region(self, mock_detect): location = self._callFUT() self.assertEqual(location, "us-central1") + @unittest.skipUnless( + hasattr(_helpers, "GoogleCloudResourceDetector") + and _helpers.GoogleCloudResourceDetector is not None, + "opentelemetry-resourcedetector-gcp not installed", + ) @mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect") def test_get_location_without_region(self, mock_detect): """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): location = self._callFUT() self.assertEqual(location, "global") + @unittest.skipUnless( + hasattr(_helpers, "GoogleCloudResourceDetector") + and _helpers.GoogleCloudResourceDetector is not None, + "opentelemetry-resourcedetector-gcp not installed", + ) @mock.patch("google.cloud.spanner_v1._helpers.GoogleCloudResourceDetector.detect") def test_get_location_with_exception(self, mock_detect): """Test that _get_cloud_region returns 'global' and logs a warning on exception.""" diff --git a/packages/google-cloud-spanner/tests/unit/test_batch.py b/packages/google-cloud-spanner/tests/unit/test_batch.py index 228c2025c14c..384a1855bb14 100644 --- a/packages/google-cloud-spanner/tests/unit/test_batch.py +++ b/packages/google-cloud-spanner/tests/unit/test_batch.py @@ -759,7 +759,14 @@ def test_batch_write_no_request_options(self, mock_region): return_value="global", ) def test_batch_write_end_to_end_tracing_enabled(self, mock_region): - self._test_batch_write_with_request_options(enable_end_to_end_tracing=True) + if ot_helpers.HAS_OPENTELEMETRY_INSTALLED: + tracer = _opentelemetry_tracing.get_tracer() + with tracer.start_as_current_span("test"): + self._test_batch_write_with_request_options( + enable_end_to_end_tracing=True + ) + else: + self._test_batch_write_with_request_options(enable_end_to_end_tracing=True) @mock.patch( "google.cloud.spanner_v1._opentelemetry_tracing._get_cloud_region", diff --git a/packages/google-cloud-spanner/tests/unit/test_database.py b/packages/google-cloud-spanner/tests/unit/test_database.py index d69c33a08fb0..106321cc68e6 100644 --- a/packages/google-cloud-spanner/tests/unit/test_database.py +++ b/packages/google-cloud-spanner/tests/unit/test_database.py @@ -3658,14 +3658,16 @@ def _resource_info(self): self._sessions_manager = mock.Mock() # Configure get_session to return sessions from the pool self._sessions_manager.get_session = mock.Mock( - side_effect=lambda tx_type: self._pool.get() - if hasattr(self, "_pool") and self._pool - else None + side_effect=lambda tx_type: ( + self._pool.get() if hasattr(self, "_pool") and self._pool else None + ) ) self._sessions_manager.put_session = mock.Mock( - side_effect=lambda session: self._pool.put(session) - if hasattr(self, "_pool") and self._pool - else None + side_effect=lambda session: ( + self._pool.put(session) + if hasattr(self, "_pool") and self._pool + else None + ) ) @property