Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,20 @@ 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)
m = mock.mock_open(read_data=config_file_content)
with mock.patch("builtins.open", m):
with (
mock.patch("builtins.open", m),
mock.patch(
"os.path.exists",
side_effect=lambda path: (
os.path.basename(path) == config_filename
),
),
):
with mock.patch.dict(
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
):
Expand Down Expand Up @@ -1021,11 +1030,20 @@ 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)
m = mock.mock_open(read_data=config_file_content)
with mock.patch("builtins.open", m):
with (
mock.patch("builtins.open", m),
mock.patch(
"os.path.exists",
side_effect=lambda path: (
os.path.basename(path) == config_filename
),
),
):
with mock.patch.dict(
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
):
Expand All @@ -1041,13 +1059,25 @@ def test_database_admin_client_get_mtls_endpoint_and_cert_source(client_class):
assert cert_source is expected_cert_source

# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
with mock.patch.dict(
os.environ,
{
"GOOGLE_API_USE_MTLS_ENDPOINT": "never",
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
},
):
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
assert api_endpoint == client_class.DEFAULT_ENDPOINT
assert cert_source is None

# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
with mock.patch.dict(
os.environ,
{
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
},
):
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
assert cert_source is None
Expand Down
Loading
Loading