Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use insecure grpc channel with emulator #946

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
fixed tests
  • Loading branch information
daniel-sanche committed Apr 11, 2024
commit d4e54fa390eda947666d355b94a93038e3665589
54 changes: 5 additions & 49 deletions tests/unit/v2_client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_client_constructor_w_emulator_host():

emulator_host = "localhost:8081"
with mock.patch("os.environ", {BIGTABLE_EMULATOR: emulator_host}):
with mock.patch("grpc.secure_channel") as factory:
with mock.patch("grpc.insecure_channel") as factory:
client = _make_client()
# don't test local_composite_credentials
# client._local_composite_credentials = lambda: credentials
Expand All @@ -188,7 +188,6 @@ def test_client_constructor_w_emulator_host():
assert client.project == _DEFAULT_BIGTABLE_EMULATOR_CLIENT
factory.assert_called_once_with(
emulator_host,
mock.ANY, # test of creds wrapping in '_emulator_host' below
options=_GRPC_CHANNEL_OPTIONS,
)

Expand All @@ -199,7 +198,7 @@ def test_client_constructor_w_emulator_host_w_project():

emulator_host = "localhost:8081"
with mock.patch("os.environ", {BIGTABLE_EMULATOR: emulator_host}):
with mock.patch("grpc.secure_channel") as factory:
with mock.patch("grpc.insecure_channel") as factory:
client = _make_client(project=PROJECT)
# channels are formed when needed, so access a client
# create a gapic channel
Expand All @@ -209,7 +208,6 @@ def test_client_constructor_w_emulator_host_w_project():
assert client.project == PROJECT
factory.assert_called_once_with(
emulator_host,
mock.ANY, # test of creds wrapping in '_emulator_host' below
options=_GRPC_CHANNEL_OPTIONS,
)

Expand All @@ -222,7 +220,7 @@ def test_client_constructor_w_emulator_host_w_credentials():
emulator_host = "localhost:8081"
credentials = _make_credentials()
with mock.patch("os.environ", {BIGTABLE_EMULATOR: emulator_host}):
with mock.patch("grpc.secure_channel") as factory:
with mock.patch("grpc.insecure_channel") as factory:
client = _make_client(credentials=credentials)
# channels are formed when needed, so access a client
# create a gapic channel
Expand All @@ -232,7 +230,6 @@ def test_client_constructor_w_emulator_host_w_credentials():
assert client.project == _DEFAULT_BIGTABLE_EMULATOR_CLIENT
factory.assert_called_once_with(
emulator_host,
mock.ANY, # test of creds wrapping in '_emulator_host' below
options=_GRPC_CHANNEL_OPTIONS,
)

Expand Down Expand Up @@ -271,15 +268,13 @@ def test_client__emulator_channel_w_sync():
project=PROJECT, credentials=_make_credentials(), read_only=True
)
client._emulator_host = emulator_host
lcc = client._local_composite_credentials = mock.Mock(spec=[])

with mock.patch("grpc.secure_channel") as patched:
with mock.patch("grpc.insecure_channel") as patched:
channel = client._emulator_channel(transport, options)

assert channel is patched.return_value
patched.assert_called_once_with(
emulator_host,
lcc.return_value,
options=options,
)

Expand All @@ -293,56 +288,17 @@ def test_client__emulator_channel_w_async():
project=PROJECT, credentials=_make_credentials(), read_only=True
)
client._emulator_host = emulator_host
lcc = client._local_composite_credentials = mock.Mock(spec=[])

with mock.patch("grpc.aio.secure_channel") as patched:
with mock.patch("grpc.aio.insecure_channel") as patched:
channel = client._emulator_channel(transport, options)

assert channel is patched.return_value
patched.assert_called_once_with(
emulator_host,
lcc.return_value,
options=options,
)


def test_client__local_composite_credentials():
client = _make_client(
project=PROJECT, credentials=_make_credentials(), read_only=True
)

wsir_patch = mock.patch("google.auth.credentials.with_scopes_if_required")
request_patch = mock.patch("google.auth.transport.requests.Request")
amp_patch = mock.patch("google.auth.transport.grpc.AuthMetadataPlugin")
grpc_patches = mock.patch.multiple(
"grpc",
metadata_call_credentials=mock.DEFAULT,
local_channel_credentials=mock.DEFAULT,
composite_channel_credentials=mock.DEFAULT,
)
with wsir_patch as wsir_patched:
with request_patch as request_patched:
with amp_patch as amp_patched:
with grpc_patches as grpc_patched:
credentials = client._local_composite_credentials()

grpc_mcc = grpc_patched["metadata_call_credentials"]
grpc_lcc = grpc_patched["local_channel_credentials"]
grpc_ccc = grpc_patched["composite_channel_credentials"]

assert credentials is grpc_ccc.return_value

wsir_patched.assert_called_once_with(client._credentials, None)
request_patched.assert_called_once_with()
amp_patched.assert_called_once_with(
wsir_patched.return_value,
request_patched.return_value,
)
grpc_mcc.assert_called_once_with(amp_patched.return_value)
grpc_lcc.assert_called_once_with()
grpc_ccc.assert_called_once_with(grpc_lcc.return_value, grpc_mcc.return_value)


def _create_gapic_client_channel_helper(endpoint=None, emulator_host=None):
from google.cloud.bigtable.client import _GRPC_CHANNEL_OPTIONS

Expand Down