Skip to content
Merged
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 @@ -310,6 +310,8 @@ def copy(self):
node_count=self._node_count,
processing_units=self._processing_units,
display_name=self.display_name,
emulator_host=self.emulator_host,
labels=dict(self.labels or {}),
Comment thread
sakthivelmanii marked this conversation as resolved.
Resolved
)

@CrossSync.convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ def copy(self):
node_count=self._node_count,
processing_units=self._processing_units,
display_name=self.display_name,
emulator_host=self.emulator_host,
labels=dict(self.labels or {}),
Comment thread
sakthivelmanii marked this conversation as resolved.
Resolved
)

def create(self):
Expand Down
30 changes: 29 additions & 1 deletion packages/google-cloud-spanner/tests/_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import inspect
import os
from datetime import datetime
from logging import Logger
from typing import Mapping

from google.auth.credentials import Credentials, Scoped
from google.cloud._helpers import _datetime_to_pb_timestamp
from mock import create_autospec
from mock import create_autospec, patch

from google.cloud.aio._cross_sync import CrossSync
from google.cloud.spanner_dbapi import Connection
Expand Down Expand Up @@ -275,3 +277,29 @@ def build_spanner_api() -> SpannerClient:
api.create_session.return_value = build_session_pb()

return api


_AMBIENT_ENDPOINT_ENV_VARS = (
"GOOGLE_CLOUD_UNIVERSE_DOMAIN",
"GOOGLE_API_USE_MTLS_ENDPOINT",
"SPANNER_EMULATOR_HOST",
)


@contextlib.contextmanager
def hermetic_endpoint_env(row_env=None):
"""Pin endpoint-affecting env vars so only ``row_env`` influences routing."""
row_env = row_env or {}
with patch.dict(
os.environ,
{
"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false",
"SPANNER_DISABLE_BUILTIN_METRICS": "true",
**row_env,
},
clear=False,
):
for name in _AMBIENT_ENDPOINT_ENV_VARS:
if name not in row_env:
os.environ.pop(name, None)
yield
69 changes: 68 additions & 1 deletion packages/google-cloud-spanner/tests/unit/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from google.cloud.aio._cross_sync import CrossSync
from google.cloud.spanner_v1 import DefaultTransactionOptions, DirectedReadOptions
from tests._builders import build_scoped_credentials
from tests._builders import build_scoped_credentials, hermetic_endpoint_env


class IsolatedAsyncioTestCase(IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -1300,3 +1300,70 @@ async def test_copy_preserves_anonymous_credentials_client(self):
copied_anonymous = anonymous_client.copy()
self.assertEqual(copied_anonymous._emulator_host, "localhost:9010")
self.assertEqual(copied_anonymous._host, "localhost:9010")

def _assert_gapic_universe(self, api, expected_universe):
self.assertEqual(api.api_endpoint, f"spanner.{expected_universe}")
self.assertEqual(api.universe_domain, expected_universe)

@CrossSync.pytest
async def test_copy_preserves_universe_domain_for_gapic_endpoints(self):
from google.api_core.client_options import ClientOptions

cases = [
(
"dict_universe_domain",
{"universe_domain": "custom.example"},
{},
"custom.example",
),
(
"client_options_object",
ClientOptions(universe_domain="custom.example"),
{},
"custom.example",
),
(
"default_unset",
None,
{},
"googleapis.com",
),
(
"env_only",
None,
{"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "custom.example"},
"custom.example",
),
(
"explicit_overrides_env",
{"universe_domain": "custom-explicit.example"},
{"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "env-universe.example"},
"custom-explicit.example",
),
]

for case_name, client_options, row_env, expected_universe in cases:
with self.subTest(case=case_name):
transports = []
with hermetic_endpoint_env(row_env):
try:
client = self._make_one(
project=self.PROJECT,
credentials=self._make_credentials(),
client_options=client_options,
)
copied = client.copy()
database = await copied.instance(self.INSTANCE_ID).database(
"db-id"
)
apis = [
copied.instance_admin_api,
copied.database_admin_api,
database.spanner_api,
]
transports.extend(api.transport for api in apis)
for api in apis:
self._assert_gapic_universe(api, expected_universe)
finally:
for transport in transports:
await transport.close()
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,113 @@ def test_node_count_setter(self):

def test_copy(self):
# coverage for lines 272-289
self.client.copy.return_value = self.client
copied_client = mock.MagicMock()
self.client.copy.return_value = copied_client
inst_copy = self.instance.copy()
self.assertEqual(inst_copy.instance_id, self.instance.instance_id)
self.client.copy.assert_called_once_with()
self.assertIs(inst_copy._client, copied_client)

async def test_copy_preserves_emulator_host_and_labels(self):
import grpc
from google.auth.credentials import AnonymousCredentials

from google.cloud.spanner_v1._async.client import Client
from tests._builders import hermetic_endpoint_env

emulator_cases = [
(
"emulator_env_with_labels",
{"SPANNER_EMULATOR_HOST": "localhost:9010"},
None,
None,
{"env": "test"},
),
(
"anonymous_credentials_api_endpoint",
{},
AnonymousCredentials(),
{"api_endpoint": "localhost:9010"},
{"env": "anon"},
),
]

for case_name, row_env, creds, client_options, labels in emulator_cases:
with self.subTest(case=case_name):
transports = []
with hermetic_endpoint_env(row_env):
try:
client = Client(
project="project-id",
credentials=creds,
client_options=client_options,
)
instance = client.instance("instance-id", labels=labels)
copied_instance = instance.copy()
self.assertEqual(
copied_instance.emulator_host, "localhost:9010"
)
self.assertEqual(copied_instance.labels, labels)
self.assertIsNot(copied_instance.labels, instance.labels)

created = []
real_aio_insecure_channel = grpc.aio.insecure_channel

def _record_channel(target, *args, **kwargs):
channel = real_aio_insecure_channel(target, *args, **kwargs)
created.append((target, channel))
return channel

database = await copied_instance.database("db-id")
with mock.patch.object(
grpc.aio, "insecure_channel", side_effect=_record_channel
):
api = database.spanner_api
transports.append(api.transport)
self.assertEqual(
[target for target, _ in created], ["localhost:9010"]
)
self.assertIs(api.transport.grpc_channel, created[0][1])
finally:
for transport in transports:
await transport.close()

def test_copy_copies_labels_without_aliasing(self):
cases = [
("dict_labels", {"env": "test"}, {"env": "test"}),
("empty_labels", {}, {}),
("none_labels", None, {}),
]

self.client.copy.return_value = self.client
for case_name, initial_labels, expected_labels in cases:
with self.subTest(case=case_name):
instance = Instance("instance-id", self.client)
instance.labels = initial_labels
copied_instance = instance.copy()
self.assertIsInstance(copied_instance.labels, dict)
self.assertEqual(copied_instance.labels, expected_labels)
self.assertIsNot(copied_instance.labels, initial_labels)
Comment thread
sakthivelmanii marked this conversation as resolved.
Resolved

def test_copy_copies_reloaded_proto_labels_without_aliasing(self):
from google.cloud.spanner_admin_instance_v1 import Instance as InstancePB

self.client.copy.return_value = self.client
instance = Instance.from_pb(
InstancePB(
name="projects/project-id/instances/instance-id",
display_name="display-name",
labels={"env": "prod"},
),
self.client,
)

copied_instance = instance.copy()

self.assertEqual(copied_instance.labels, {"env": "prod"})
self.assertIsInstance(copied_instance.labels, dict)
copied_instance.labels["env"] = "dev"
self.assertEqual(instance.labels["env"], "prod")

async def test_init_validation(self):
# coverage for lines 133-137
Expand Down
67 changes: 66 additions & 1 deletion packages/google-cloud-spanner/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from google.auth.credentials import AnonymousCredentials

from google.cloud.spanner_v1 import DefaultTransactionOptions, DirectedReadOptions
from tests._builders import build_scoped_credentials
from tests._builders import build_scoped_credentials, hermetic_endpoint_env


@mock.patch.dict(os.environ, {"SPANNER_DISABLE_BUILTIN_METRICS": "true"})
Expand Down Expand Up @@ -871,6 +871,71 @@ def test_copy_preserves_anonymous_credentials_client(self):
self.assertEqual(copied_anonymous._emulator_host, "localhost:9010")
self.assertEqual(copied_anonymous._host, "localhost:9010")

def _assert_gapic_universe(self, api, expected_universe):
self.assertEqual(api.api_endpoint, f"spanner.{expected_universe}")
self.assertEqual(api.universe_domain, expected_universe)

def test_copy_preserves_universe_domain_for_gapic_endpoints(self):
from google.api_core.client_options import ClientOptions

cases = [
(
"dict_universe_domain",
{"universe_domain": "custom.example"},
{},
"custom.example",
),
(
"client_options_object",
ClientOptions(universe_domain="custom.example"),
{},
"custom.example",
),
(
"default_unset",
None,
{},
"googleapis.com",
),
(
"env_only",
None,
{"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "custom.example"},
"custom.example",
),
(
"explicit_overrides_env",
{"universe_domain": "custom-explicit.example"},
{"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "env-universe.example"},
"custom-explicit.example",
),
]

for case_name, client_options, row_env, expected_universe in cases:
with self.subTest(case=case_name):
transports = []
with hermetic_endpoint_env(row_env):
try:
client = self._make_one(
project=self.PROJECT,
credentials=self._make_credentials(),
client_options=client_options,
)
copied = client.copy()
apis = [
copied.instance_admin_api,
copied.database_admin_api,
copied.instance(self.INSTANCE_ID)
.database("db-id")
.spanner_api,
]
transports.extend(api.transport for api in apis)
for api in apis:
self._assert_gapic_universe(api, expected_universe)
finally:
for transport in transports:
transport.close()

def test_credentials_property(self):
credentials = build_scoped_credentials()
client = self._make_one(project=self.PROJECT, credentials=credentials)
Expand Down
Loading
Loading