Skip to content

Commit 4224f80

Browse files
committed
Adding make_insecure_stub().
1 parent 0f3ef0d commit 4224f80

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎google/cloud/_helpers.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,29 @@ def make_secure_stub(credentials, user_agent, stub_class, host, port):
649649
return stub_class(channel)
650650

651651

652+
def make_insecure_stub(stub_class, host, port):
653+
"""Makes an insecure stub for an RPC service.
654+
655+
Uses / depends on gRPC.
656+
657+
:type stub_class: type
658+
:param stub_class: A gRPC stub type for a given service.
659+
660+
:type host: str
661+
:param host: The host for the service.
662+
663+
:type port: int
664+
:param port: The port for the service.
665+
666+
:rtype: object, instance of ``stub_class``
667+
:returns: The stub object used to make gRPC requests to a given API.
668+
"""
669+
# NOTE: This assumes port != http_client.HTTPS_PORT:
670+
target = '%s:%d' % (host, port)
671+
channel = grpc.insecure_channel(target)
672+
return stub_class(channel)
673+
674+
652675
def exc_to_code(exc):
653676
"""Retrieves the status code from a gRPC exception.
654677

‎unit_tests/test__helpers.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,43 @@ def mock_plugin(*args):
969969
(target, COMPOSITE_CREDS))
970970

971971

972+
class Test_make_insecure_stub(unittest.TestCase):
973+
974+
def _callFUT(self, *args, **kwargs):
975+
from gcloud._helpers import make_insecure_stub
976+
return make_insecure_stub(*args, **kwargs)
977+
978+
def test_it(self):
979+
from unit_tests._testing import _Monkey
980+
from gcloud import _helpers as MUT
981+
982+
mock_result = object()
983+
stub_inputs = []
984+
CHANNEL = object()
985+
986+
class _GRPCModule(object):
987+
988+
def insecure_channel(self, *args):
989+
self.insecure_channel_args = args
990+
return CHANNEL
991+
992+
grpc_mod = _GRPCModule()
993+
994+
def mock_stub_class(channel):
995+
stub_inputs.append(channel)
996+
return mock_result
997+
998+
host = 'HOST'
999+
port = 1025
1000+
with _Monkey(MUT, grpc=grpc_mod):
1001+
result = self._callFUT(mock_stub_class, host, port)
1002+
1003+
self.assertTrue(result is mock_result)
1004+
self.assertEqual(stub_inputs, [CHANNEL])
1005+
target = '%s:%d' % (host, port)
1006+
self.assertEqual(grpc_mod.insecure_channel_args, (target,))
1007+
1008+
9721009
class Test_exc_to_code(unittest.TestCase):
9731010

9741011
def _callFUT(self, exc):

0 commit comments

Comments
 (0)