tavern icon indicating copy to clipboard operation
tavern copied to clipboard

Tavern GRPC support

Open kiril-me opened this issue 5 years ago • 1 comments

GRPC client support.

test_name: GRPC message echo
grpc: 
  connect:
    host: localhost
    port: 50051
    tls: false

stages:
  - name: Echo text
    grpc_request:
      service: helloworld.Greeter/SayHello
      body:
        name: "John"
    grpc_response:
      body:
        message: "Hello, John!"

GRPC client connection is described using host, port, tls.

On stage level, grpc_request create a request with the body. service is the full GRPC service name. Besides, there is a possibility to change the host inside request:

- name: Echo text
    grpc_request:
      host: myhost:50052
      service: helloworld.Greeter/SayHello
      body:
        name: "John"

Similar to http response GRPC plugin hasgrpc_response. To verify error code you can use status and detail:

stages:
  - name: Echo text
    grpc_request:
      service: helloworld.Greeter/SayHello
    grpc_response:
      status: INVALID_ARGUMENT
      details: "Name was not provided"

The main problem with GRPC what client should know the protobuf message to make request and read the response.

GRPC plugin supports several features to discover protobuf message structure:

  1. Provide protos file (https://developers.google.com/protocol-buffers/docs/proto3).
grpc:
  connect:
    ...
  proto:
    source: my-proto-folder

The tavern will automatically compile protos and start using them. 2. In case you already have Python generated classes (protoc output https://developers.google.com/protocol-buffers/docs/pythontutorial). Setup the proto module location.

grpc:
  connect:
    ...
  proto:
    module: proto-folder

Using proto source file actually generate GRPC python file inside the default module (proto) folder. But you can change it update module to custome name.

grpc:
  connect:
    ...
  proto:
    source: my-proto-folder
    module: py-generated-proto
  1. The plugin will try automatically to request protobuf structure from the server using reflections (https://github.com/grpc/grpc/blob/master/doc/server-reflection.md). Just skip proto section.

To send metadata with request GRPC has metadata map.

test_name: GRPC message echo
grpc: 
  connect:
    host: localhost
    port: 50051
    tls: false
  metadata:
    user: John

kiril-me avatar Sep 05 '19 16:09 kiril-me

Sorry for not leaving a comment on this earlier, but as mentioned in https://github.com/taverntesting/tavern/issues/419#issuecomment-524562314 I don't really want to merge something in that I won't be able to maintain. What we could do is put this as a 'tavern-grpc' plugin into a separate repository under the 'taverntesting' github organisation, and add the schema and a 'stub' implementation in Tavern that just raises an error and points to that repository?

michaelboulton avatar Apr 05 '20 15:04 michaelboulton

Just for the record, I've got this into a roughly working state with 2.0 but I don't want to force everyone who uses to Tavern to suddenly rely on all the random grpc libraries, and once I have a good idea of how to make it not rely on those libraries for an optional feature, it's ready to go

michaelboulton avatar May 27 '23 13:05 michaelboulton