def test_echo_grpc_transport_channel_mtls_with_client_cert_source( grpc_create_channel, grpc_ssl_channel_cred): # Check that if channel is None, but api_mtls_endpoint and client_cert_source # are provided, then a mTLS channel will be created. mock_cred = mock.Mock() mock_ssl_cred = mock.Mock() grpc_ssl_channel_cred.return_value = mock_ssl_cred mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel transport = transports.EchoGrpcTransport( host="squid.clam.whelk", credentials=mock_cred, api_mtls_endpoint="mtls.squid.clam.whelk", client_cert_source=client_cert_source_callback, ) grpc_ssl_channel_cred.assert_called_once_with( certificate_chain=b"cert bytes", private_key=b"key bytes") grpc_create_channel.assert_called_once_with( "mtls.squid.clam.whelk:443", credentials=mock_cred, ssl_credentials=mock_ssl_cred, scopes=(), ) assert transport.grpc_channel == mock_grpc_channel
def test_echo_grpc_transport_channel_mtls_with_adc(grpc_create_channel, api_mtls_endpoint): # Check that if channel and client_cert_source are None, but api_mtls_endpoint # is provided, then a mTLS channel will be created with SSL ADC. mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel # Mock google.auth.transport.grpc.SslCredentials class. mock_ssl_cred = mock.Mock() with mock.patch.multiple( "google.auth.transport.grpc.SslCredentials", __init__=mock.Mock(return_value=None), ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), ): mock_cred = mock.Mock() transport = transports.EchoGrpcTransport( host="squid.clam.whelk", credentials=mock_cred, api_mtls_endpoint=api_mtls_endpoint, client_cert_source=None, ) grpc_create_channel.assert_called_once_with( "mtls.squid.clam.whelk:443", credentials=mock_cred, ssl_credentials=mock_ssl_cred, scopes=(), ) assert transport.grpc_channel == mock_grpc_channel
def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.EchoGrpcTransport( credentials=credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = EchoClient( credentials=credentials.AnonymousCredentials(), transport=transport, )
def test_echo_grpc_transport_channel(): channel = grpc.insecure_channel('http://localhost/') # Check that if channel is provided, mtls endpoint and client_cert_source # won't be used. callback = mock.MagicMock() transport = transports.EchoGrpcTransport( host="squid.clam.whelk", channel=channel, api_mtls_endpoint="mtls.squid.clam.whelk", client_cert_source=callback, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" assert not callback.called
def run_should_pass(): print("================= should pass ====================") header_adder_interceptor = client_interceptor.header_adder_interceptor( 'one-time-password', '42') channel = grpc.insecure_channel('localhost:50051') intercept_channel = grpc.intercept_channel(channel, header_adder_interceptor) transport = transports.EchoGrpcTransport(channel=intercept_channel) client = EchoClient(transport=transport) request = gs_echo.ExpandRequest(content='one two three four') responses = client.expand(request) for message in responses: print(message) print("trailing metadata...") print(responses.trailing_metadata())
def run_should_fail(): print( "================= should fail with 'Access denied!' ====================" ) channel = grpc.insecure_channel('localhost:50051') transport = transports.EchoGrpcTransport(channel=channel) client = EchoClient(transport=transport) try: request = gs_echo.ExpandRequest(content='one two three four') responses = client.expand(request) for message in responses: print(message) print("trailing metadata...") print(responses.trailing_metadata()) except: print(sys.exc_info())
def run_should_pass(): print("================= should pass ====================") header_adder_interceptor = client_interceptor.header_adder_interceptor( 'one-time-password', '42') channel = grpc.insecure_channel('localhost:50051') intercept_channel = grpc.intercept_channel(channel, header_adder_interceptor) transport = transports.EchoGrpcTransport(channel=intercept_channel) client = EchoClient(transport=transport) content = 'The rain in Spain stays mainly on the Plain!' requests = content.split(' ') requests = map(lambda s: gs_echo.EchoRequest(content=s), requests) responses = client.chat(iter(requests)) for res in responses: print(res) print("trailing metadata...") print(responses.trailing_metadata())
def run_should_fail(): print( "================= should fail with 'Access denied!' ====================" ) channel = grpc.insecure_channel('localhost:50051') transport = transports.EchoGrpcTransport(channel=channel) client = EchoClient(transport=transport) content = 'The rain in Spain stays mainly on the Plain!' requests = content.split(' ') requests = map(lambda s: gs_echo.EchoRequest(content=s), requests) try: responses = client.chat(iter(requests)) for res in responses: print(res) print("trailing metadata...") print(responses.trailing_metadata()) except: print(sys.exc_info())
def test_echo_client_client_options(): # Check that if channel is provided we won't create a new one. with mock.patch( 'google.showcase_v1beta1.services.echo.EchoClient.get_transport_class' ) as gtc: transport = transports.EchoGrpcTransport( credentials=credentials.AnonymousCredentials()) client = EchoClient(transport=transport) gtc.assert_not_called() # Check mTLS is not triggered with empty client options. options = client_options.ClientOptions() with mock.patch( 'google.showcase_v1beta1.services.echo.EchoClient.get_transport_class' ) as gtc: transport = gtc.return_value = mock.MagicMock() client = EchoClient(client_options=options) transport.assert_called_once_with( credentials=None, host=client.DEFAULT_ENDPOINT, ) # Check mTLS is not triggered if api_endpoint is provided but # client_cert_source is None. options = client_options.ClientOptions(api_endpoint="squid.clam.whelk") with mock.patch( 'google.showcase_v1beta1.services.echo.transports.EchoGrpcTransport.__init__' ) as grpc_transport: grpc_transport.return_value = None client = EchoClient(client_options=options) grpc_transport.assert_called_once_with( api_mtls_endpoint=None, client_cert_source=None, credentials=None, host="squid.clam.whelk", ) # Check mTLS is triggered if client_cert_source is provided. options = client_options.ClientOptions( client_cert_source=client_cert_source_callback) with mock.patch( 'google.showcase_v1beta1.services.echo.transports.EchoGrpcTransport.__init__' ) as grpc_transport: grpc_transport.return_value = None client = EchoClient(client_options=options) grpc_transport.assert_called_once_with( api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, client_cert_source=client_cert_source_callback, credentials=None, host=client.DEFAULT_ENDPOINT, ) # Check mTLS is triggered if api_endpoint and client_cert_source are provided. options = client_options.ClientOptions( api_endpoint="squid.clam.whelk", client_cert_source=client_cert_source_callback) with mock.patch( 'google.showcase_v1beta1.services.echo.transports.EchoGrpcTransport.__init__' ) as grpc_transport: grpc_transport.return_value = None client = EchoClient(client_options=options) grpc_transport.assert_called_once_with( api_mtls_endpoint="squid.clam.whelk", client_cert_source=client_cert_source_callback, credentials=None, host="squid.clam.whelk", )
def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.EchoGrpcTransport( credentials=credentials.AnonymousCredentials(), ) client = EchoClient(transport=transport) assert client._transport is transport
def test_echo_grpc_transport_channel(): channel = grpc.insecure_channel('http://localhost/') transport = transports.EchoGrpcTransport(channel=channel, ) assert transport.grpc_channel is channel
from google import auth from google.api_core import client_options from google.api_core import future from google.api_core import operations_v1 from google.auth import credentials from google.longrunning import operations_pb2 from google.oauth2 import service_account from google.rpc import status_pb2 as status # type: ignore from google.showcase_v1beta1.services.echo import EchoClient from google.showcase_v1beta1.services.echo import pagers from google.showcase_v1beta1.services.echo import transports from google.showcase_v1beta1.types import echo from google.showcase_v1beta1.types import echo as gs_echo channel = grpc.insecure_channel('localhost:50051') transport = transports.EchoGrpcTransport(channel=channel) def test_echo(): print("=================test echo ===================") client = EchoClient(transport=transport) request = gs_echo.EchoRequest(content='hello world') response = client.echo(request) print(response) def test_expand(): print("=================test expand ===================") client = EchoClient(transport=transport) request = gs_echo.ExpandRequest(content='one two three four') responses = client.expand(request) for message in responses: print(message)