Skip to content

Commit

Permalink
Factoring out the parts of make_secure_stub() that create a channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Nov 4, 2016
1 parent 75e4613 commit 2d42c97
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
41 changes: 31 additions & 10 deletions core/google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ def __call__(self, unused_context, callback):
callback(headers, None)


def make_secure_stub(credentials, user_agent, stub_class, host):
"""Makes a secure stub for an RPC service.
def make_secure_channel(credentials, user_agent, host):
"""Makes a secure channel for an RPC service.
Uses / depends on gRPC.
Expand All @@ -630,16 +630,13 @@ def make_secure_stub(credentials, user_agent, stub_class, host):
access tokens.
:type user_agent: str
:param user_agent: (Optional) The user agent to be used with API requests.
:type stub_class: type
:param stub_class: A gRPC stub type for a given service.
:param user_agent: The user agent to be used with API requests.
:type host: str
:param host: The host for the service.
:rtype: object, instance of ``stub_class``
:returns: The stub object used to make gRPC requests to a given API.
:rtype: :class:`grpc._channel.Channel`
:returns: gRPC secure channel with credentials attached.
"""
# ssl_channel_credentials() loads root certificates from
# `grpc/_adapter/credentials/roots.pem`.
Expand All @@ -653,8 +650,32 @@ def make_secure_stub(credentials, user_agent, stub_class, host):
channel_args = (
('grpc.primary_user_agent', user_agent),
)
channel = grpc.secure_channel(target, channel_creds,
options=channel_args)
return grpc.secure_channel(target, channel_creds,
options=channel_args)


def make_secure_stub(credentials, user_agent, stub_class, host):
"""Makes a secure stub for an RPC service.
Uses / depends on gRPC.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for creating
access tokens.
:type user_agent: str
:param user_agent: The user agent to be used with API requests.
:type stub_class: type
:param stub_class: A gRPC stub type for a given service.
:type host: str
:param host: The host for the service.
:rtype: object, instance of ``stub_class``
:returns: The stub object used to make gRPC requests to a given API.
"""
channel = make_secure_channel(credentials, user_agent, host)
return stub_class(channel)


Expand Down
56 changes: 41 additions & 15 deletions core/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,20 +917,17 @@ def callback(*args):
self.assertEqual(len(credentials._tokens), 1)


class Test_make_secure_stub(unittest.TestCase):
class Test_make_secure_channel(unittest.TestCase):

def _callFUT(self, *args, **kwargs):
from google.cloud._helpers import make_secure_stub
return make_secure_stub(*args, **kwargs)
from google.cloud._helpers import make_secure_channel
return make_secure_channel(*args, **kwargs)

def test_it(self):
from six.moves import http_client
from google.cloud._testing import _Monkey
from google.cloud import _helpers as MUT

mock_result = object()
stub_inputs = []

SSL_CREDS = object()
METADATA_CREDS = object()
COMPOSITE_CREDS = object()
Expand Down Expand Up @@ -961,11 +958,6 @@ def secure_channel(self, *args, **kwargs):
return CHANNEL

grpc_mod = _GRPCModule()

def mock_stub_class(channel):
stub_inputs.append(channel)
return mock_result

metadata_plugin = object()
plugin_args = []

Expand All @@ -978,11 +970,9 @@ def mock_plugin(*args):
user_agent = 'USER_AGENT'
with _Monkey(MUT, grpc=grpc_mod,
MetadataPlugin=mock_plugin):
result = self._callFUT(credentials, user_agent,
mock_stub_class, host)
result = self._callFUT(credentials, user_agent, host)

self.assertIs(result, mock_result)
self.assertEqual(stub_inputs, [CHANNEL])
self.assertIs(result, CHANNEL)
self.assertEqual(plugin_args, [(credentials,)])
self.assertEqual(grpc_mod.ssl_channel_credentials_args, ())
self.assertEqual(grpc_mod.metadata_call_credentials_args,
Expand All @@ -999,6 +989,42 @@ def mock_plugin(*args):
(secure_args, secure_kwargs))


class Test_make_secure_stub(unittest.TestCase):

def _callFUT(self, *args, **kwargs):
from google.cloud._helpers import make_secure_stub
return make_secure_stub(*args, **kwargs)

def test_it(self):
from google.cloud._testing import _Monkey
from google.cloud import _helpers as MUT

result = object()
channel_obj = object()
channels = []
channel_args = []

def stub_class(channel):
channels.append(channel)
return result

def mock_channel(*args):
channel_args.append(args)
return channel_obj

credentials = object()
user_agent = 'you-sir-age-int'
host = 'localhost'
with _Monkey(MUT, make_secure_channel=mock_channel):
stub = self._callFUT(credentials, user_agent,
stub_class, host)

self.assertIs(stub, result)
self.assertEqual(channels, [channel_obj])
self.assertEqual(channel_args,
[(credentials, user_agent, host)])


class Test_make_insecure_stub(unittest.TestCase):

def _callFUT(self, *args, **kwargs):
Expand Down

0 comments on commit 2d42c97

Please sign in to comment.