Example #1
0
def _stub(args):
    if args.oauth_scope:
        if args.test_case == "oauth2_auth_token":
            # TODO(jtattermusch): This testcase sets the auth metadata key-value
            # manually, which also means that the user would need to do the same
            # thing every time he/she would like to use and out of band oauth token.
            # The transformer function that produces the metadata key-value from
            # the access token should be provided by gRPC auth library.
            access_token = _oauth_access_token(args)
            metadata_transformer = lambda x: [("authorization", "Bearer %s" % access_token)]
        else:
            metadata_transformer = lambda x: [("authorization", "Bearer %s" % _oauth_access_token(args))]
    else:
        metadata_transformer = lambda x: []
    if args.use_tls:
        if args.use_test_ca:
            root_certificates = resources.test_root_certificates()
        else:
            root_certificates = None  # will load default roots.

        channel = test_utilities.not_really_secure_channel(
            args.server_host,
            args.server_port,
            implementations.ssl_channel_credentials(root_certificates),
            args.server_host_override,
        )
        stub = test_pb2.beta_create_TestService_stub(channel, metadata_transformer=metadata_transformer)
    else:
        channel = implementations.insecure_channel(args.server_host, args.server_port)
        stub = test_pb2.beta_create_TestService_stub(channel)
    return stub
Example #2
0
def _stub(args):
  if args.test_case == 'oauth2_auth_token':
    creds = oauth2client_client.GoogleCredentials.get_application_default()
    scoped_creds = creds.create_scoped([args.oauth_scope])
    access_token = scoped_creds.get_access_token().access_token
    call_creds = implementations.access_token_call_credentials(access_token)
  elif args.test_case == 'compute_engine_creds':
    creds = oauth2client_client.GoogleCredentials.get_application_default()
    scoped_creds = creds.create_scoped([args.oauth_scope])
    call_creds = implementations.google_call_credentials(scoped_creds)
  elif args.test_case == 'jwt_token_creds':
    creds = oauth2client_client.GoogleCredentials.get_application_default()
    call_creds = implementations.google_call_credentials(creds)
  else:
    call_creds = None
  if args.use_tls:
    if args.use_test_ca:
      root_certificates = resources.test_root_certificates()
    else:
      root_certificates = None  # will load default roots.

    channel_creds = implementations.ssl_channel_credentials(root_certificates)
    if call_creds is not None:
      channel_creds = implementations.composite_channel_credentials(
          channel_creds, call_creds)

    channel = test_utilities.not_really_secure_channel(
        args.server_host, args.server_port, channel_creds,
        args.server_host_override)
    stub = test_pb2.beta_create_TestService_stub(channel)
  else:
    channel = implementations.insecure_channel(
        args.server_host, args.server_port)
    stub = test_pb2.beta_create_TestService_stub(channel)
  return stub
Example #3
0
    def test_stub_context(self):
        server = implementations.server(
            self._method_implementations, options=self._server_options)
        port = server.add_secure_port('[::]:0', self._server_credentials)
        server.start()

        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE)
        dynamic_stub = implementations.dynamic_stub(
            channel, _GROUP, self._cardinalities, options=self._stub_options)
        for _ in range(100):
            with dynamic_stub:
                pass
        for _ in range(10):
            with dynamic_stub:
                call_options = interfaces.grpc_call_options(
                    disable_compression=True)
                response = getattr(dynamic_stub, _UNARY_UNARY)(
                    _REQUEST,
                    test_constants.LONG_TIMEOUT,
                    protocol_options=call_options)
                self.assertEqual(_RESPONSE, response)
                self.assertIsNotNone(self._servicer.peer())

        server.stop(test_constants.SHORT_TIMEOUT).wait()
Example #4
0
def _stub(args):
    if args.test_case == 'oauth2_auth_token':
        creds = oauth2client_client.GoogleCredentials.get_application_default()
        scoped_creds = creds.create_scoped([args.oauth_scope])
        access_token = scoped_creds.get_access_token().access_token
        call_creds = implementations.access_token_call_credentials(
            access_token)
    elif args.test_case == 'compute_engine_creds':
        creds = oauth2client_client.GoogleCredentials.get_application_default()
        scoped_creds = creds.create_scoped([args.oauth_scope])
        call_creds = implementations.google_call_credentials(scoped_creds)
    else:
        call_creds = None
    if args.use_tls:
        if args.use_test_ca:
            root_certificates = resources.test_root_certificates()
        else:
            root_certificates = None  # will load default roots.

        channel_creds = implementations.ssl_channel_credentials(
            root_certificates)
        if call_creds is not None:
            channel_creds = implementations.composite_channel_credentials(
                channel_creds, call_creds)

        channel = test_utilities.not_really_secure_channel(
            args.server_host, args.server_port, channel_creds,
            args.server_host_override)
        stub = test_pb2.beta_create_TestService_stub(channel)
    else:
        channel = implementations.insecure_channel(args.server_host,
                                                   args.server_port)
        stub = test_pb2.beta_create_TestService_stub(channel)
    return stub
Example #5
0
  def __init__(self, server, config, hist):
    # Create the stub
    host, port = server.split(':')
    port = int(port)
    if config.HasField('security_params'):
      creds = implementations.ssl_channel_credentials(
          resources.test_root_certificates())
      channel = test_utilities.not_really_secure_channel(
          host, port, creds, config.security_params.server_host_override)
    else:
      channel = implementations.insecure_channel(host, port)

    connected_event = threading.Event()
    def wait_for_ready(connectivity):
      if connectivity == grpc.ChannelConnectivity.READY:
        connected_event.set()
    channel.subscribe(wait_for_ready, try_to_connect=True)
    connected_event.wait()

    if config.payload_config.WhichOneof('payload') == 'simple_params':
      self._generic = False
      self._stub = services_pb2.beta_create_BenchmarkService_stub(channel)
      payload = messages_pb2.Payload(
          body='\0' * config.payload_config.simple_params.req_size)
      self._request = messages_pb2.SimpleRequest(
          payload=payload,
          response_size=config.payload_config.simple_params.resp_size)
    else:
      self._generic = True
      self._stub = implementations.generic_stub(channel)
      self._request = '\0' * config.payload_config.bytebuf_params.req_size

    self._hist = hist
    self._response_callbacks = []
  def instantiate(
      self, methods, method_implementations, multi_method_implementation):
    serialization_behaviors = _serialization_behaviors_from_test_methods(
        methods)
    # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest.
    service = next(iter(methods))[0]
    # TODO(nathaniel): Add a "cardinalities_by_group" attribute to
    # _digest.TestServiceDigest.
    cardinalities = {
        method: method_object.cardinality()
        for (group, method), method_object in six.iteritems(methods)}

    server_options = implementations.server_options(
        request_deserializers=serialization_behaviors.request_deserializers,
        response_serializers=serialization_behaviors.response_serializers,
        thread_pool_size=test_constants.POOL_SIZE)
    server = implementations.server(
        method_implementations, options=server_options)
    server_credentials = implementations.ssl_server_credentials(
        [(resources.private_key(), resources.certificate_chain(),),])
    port = server.add_secure_port('[::]:0', server_credentials)
    server.start()
    channel_credentials = implementations.ssl_channel_credentials(
        resources.test_root_certificates())
    channel = test_utilities.not_really_secure_channel(
        'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE)
    stub_options = implementations.stub_options(
        request_serializers=serialization_behaviors.request_serializers,
        response_deserializers=serialization_behaviors.response_deserializers,
        thread_pool_size=test_constants.POOL_SIZE)
    generic_stub = implementations.generic_stub(channel, options=stub_options)
    dynamic_stub = implementations.dynamic_stub(
        channel, service, cardinalities, options=stub_options)
    return generic_stub, {service: dynamic_stub}, server
Example #7
0
    def test_stub_context(self):
        server = implementations.server(self._method_implementations,
                                        options=self._server_options)
        port = server.add_secure_port('[::]:0', self._server_credentials)
        server.start()

        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials,
            _SERVER_HOST_OVERRIDE)
        dynamic_stub = implementations.dynamic_stub(channel,
                                                    _GROUP,
                                                    self._cardinalities,
                                                    options=self._stub_options)
        for _ in range(100):
            with dynamic_stub:
                pass
        for _ in range(10):
            with dynamic_stub:
                call_options = interfaces.grpc_call_options(
                    disable_compression=True)
                response = getattr(dynamic_stub,
                                   _UNARY_UNARY)(_REQUEST,
                                                 test_constants.LONG_TIMEOUT,
                                                 protocol_options=call_options)
                self.assertEqual(_RESPONSE, response)
                self.assertIsNotNone(self._servicer.peer())

        server.stop(test_constants.SHORT_TIMEOUT).wait()
Example #8
0
def _stub(args):
  if args.oauth_scope:
    if args.test_case == 'oauth2_auth_token':
      # TODO(jtattermusch): This testcase sets the auth metadata key-value
      # manually, which also means that the user would need to do the same
      # thing every time he/she would like to use and out of band oauth token.
      # The transformer function that produces the metadata key-value from
      # the access token should be provided by gRPC auth library.
      access_token = _oauth_access_token(args)
      metadata_transformer = lambda x: [
          ('authorization', 'Bearer %s' % access_token)]
    else:
      metadata_transformer = lambda x: [
          ('authorization', 'Bearer %s' % _oauth_access_token(args))]
  else:
    metadata_transformer = lambda x: []
  if args.use_tls:
    if args.use_test_ca:
      root_certificates = resources.test_root_certificates()
    else:
      root_certificates = None  # will load default roots.

    channel = test_utilities.not_really_secure_channel(
        args.server_host, args.server_port,
        implementations.ssl_channel_credentials(root_certificates, None, None),
        args.server_host_override)
    stub = test_pb2.beta_create_TestService_stub(
        channel, metadata_transformer=metadata_transformer)
  else:
    channel = implementations.insecure_channel(
        args.server_host, args.server_port)
    stub = test_pb2.beta_create_TestService_stub(channel)
  return stub
  def instantiate(
      self, methods, method_implementations, multi_method_implementation):
    serialization_behaviors = _serialization_behaviors_from_test_methods(
        methods)
    # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest.
    service = next(iter(methods))[0]
    # TODO(nathaniel): Add a "cardinalities_by_group" attribute to
    # _digest.TestServiceDigest.
    cardinalities = {
        method: method_object.cardinality()
        for (group, method), method_object in methods.iteritems()}

    server_options = implementations.server_options(
        request_deserializers=serialization_behaviors.request_deserializers,
        response_serializers=serialization_behaviors.response_serializers,
        thread_pool_size=test_constants.POOL_SIZE)
    server = implementations.server(
        method_implementations, options=server_options)
    server_credentials = implementations.ssl_server_credentials(
        [(resources.private_key(), resources.certificate_chain(),),])
    port = server.add_secure_port('[::]:0', server_credentials)
    server.start()
    channel_credentials = implementations.ssl_channel_credentials(
        resources.test_root_certificates(), None, None)
    channel = test_utilities.not_really_secure_channel(
        'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE)
    stub_options = implementations.stub_options(
        request_serializers=serialization_behaviors.request_serializers,
        response_deserializers=serialization_behaviors.response_deserializers,
        thread_pool_size=test_constants.POOL_SIZE)
    generic_stub = implementations.generic_stub(channel, options=stub_options)
    dynamic_stub = implementations.dynamic_stub(
        channel, service, cardinalities, options=stub_options)
    return generic_stub, {service: dynamic_stub}, server
Example #10
0
    def __init__(self, server, config, hist):
        # Create the stub
        host, port = server.split(':')
        port = int(port)
        if config.HasField('security_params'):
            creds = implementations.ssl_channel_credentials(
                resources.test_root_certificates())
            channel = test_utilities.not_really_secure_channel(
                host, port, creds, config.security_params.server_host_override)
        else:
            channel = implementations.insecure_channel(host, port)

        if config.payload_config.WhichOneof('payload') == 'simple_params':
            self._generic = False
            self._stub = services_pb2.beta_create_BenchmarkService_stub(
                channel)
            payload = messages_pb2.Payload(
                body='\0' * config.payload_config.simple_params.req_size)
            self._request = messages_pb2.SimpleRequest(
                payload=payload,
                response_size=config.payload_config.simple_params.resp_size)
        else:
            self._generic = True
            self._stub = implementations.generic_stub(channel)
            self._request = '\0' * config.payload_config.bytebuf_params.req_size

        self._hist = hist
        self._response_callbacks = []
Example #11
0
 def setUp(self):
   self.server = test_pb2.beta_create_TestService_server(methods.TestService())
   port = self.server.add_secure_port(
       '[::]:0', implementations.ssl_server_credentials(
           [(resources.private_key(), resources.certificate_chain())]))
   self.server.start()
   self.stub = test_pb2.beta_create_TestService_stub(
       test_utilities.not_really_secure_channel(
           '[::]', port, implementations.ssl_channel_credentials(
               resources.test_root_certificates()),
               _SERVER_HOST_OVERRIDE))
 def setUp(self):
   self.server = test_pb2.beta_create_TestService_server(methods.TestService())
   port = self.server.add_secure_port(
       '[::]:0', implementations.ssl_server_credentials(
           [(resources.private_key(), resources.certificate_chain())]))
   self.server.start()
   self.stub = test_pb2.beta_create_TestService_stub(
       test_utilities.not_really_secure_channel(
           '[::]', port, implementations.ssl_channel_credentials(
               resources.test_root_certificates()),
               _SERVER_HOST_OVERRIDE))
Example #13
0
    def setUp(self):
        self._servicer = _Servicer()
        method_implementations = {
            (_GROUP, _UNARY_UNARY):
            utilities.unary_unary_inline(self._servicer.unary_unary),
            (_GROUP, _UNARY_STREAM):
            utilities.unary_stream_inline(self._servicer.unary_stream),
            (_GROUP, _STREAM_UNARY):
            utilities.stream_unary_inline(self._servicer.stream_unary),
            (_GROUP, _STREAM_STREAM):
            utilities.stream_stream_inline(self._servicer.stream_stream),
        }

        cardinalities = {
            _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY,
            _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM,
            _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY,
            _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM,
        }

        server_options = implementations.server_options(
            thread_pool_size=test_constants.POOL_SIZE)
        self._server = implementations.server(method_implementations,
                                              options=server_options)
        server_credentials = implementations.ssl_server_credentials([
            (
                resources.private_key(),
                resources.certificate_chain(),
            ),
        ])
        port = self._server.add_secure_port('[::]:0', server_credentials)
        self._server.start()
        self._channel_credentials = implementations.ssl_channel_credentials(
            resources.test_root_certificates())
        self._call_credentials = implementations.metadata_call_credentials(
            _metadata_plugin)
        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials,
            _SERVER_HOST_OVERRIDE)
        stub_options = implementations.stub_options(
            thread_pool_size=test_constants.POOL_SIZE)
        self._dynamic_stub = implementations.dynamic_stub(channel,
                                                          _GROUP,
                                                          cardinalities,
                                                          options=stub_options)
Example #14
0
    def setUp(self):
        self._servicer = _Servicer()
        method_implementations = {
            (_GROUP, _UNARY_UNARY):
            utilities.unary_unary_inline(self._servicer.unary_unary),
            (_GROUP, _UNARY_STREAM):
            utilities.unary_stream_inline(self._servicer.unary_stream),
            (_GROUP, _STREAM_UNARY):
            utilities.stream_unary_inline(self._servicer.stream_unary),
            (_GROUP, _STREAM_STREAM):
            utilities.stream_stream_inline(self._servicer.stream_stream),
        }

        cardinalities = {
            _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY,
            _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM,
            _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY,
            _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM,
        }

        server_options = implementations.server_options(
            thread_pool_size=test_constants.POOL_SIZE)
        self._server = implementations.server(
            method_implementations, options=server_options)
        server_credentials = implementations.ssl_server_credentials([
            (
                resources.private_key(),
                resources.certificate_chain(),
            ),
        ])
        port = self._server.add_secure_port('[::]:0', server_credentials)
        self._server.start()
        self._channel_credentials = implementations.ssl_channel_credentials(
            resources.test_root_certificates())
        self._call_credentials = implementations.metadata_call_credentials(
            _metadata_plugin)
        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE)
        stub_options = implementations.stub_options(
            thread_pool_size=test_constants.POOL_SIZE)
        self._dynamic_stub = implementations.dynamic_stub(
            channel, _GROUP, cardinalities, options=stub_options)