diff --git a/proxy/handler_multi_test.go b/proxy/handler_multi_test.go index 0168222..36511aa 100644 --- a/proxy/handler_multi_test.go +++ b/proxy/handler_multi_test.go @@ -73,7 +73,7 @@ func (s *assertingMultiService) Ping(ctx context.Context, ping *pb.PingRequest) }, nil } -func (s *assertingMultiService) PingError(ctx context.Context, ping *pb.PingRequest) (*pb.Empty, error) { +func (s *assertingMultiService) PingError(ctx context.Context, ping *pb.PingRequest) (*pb.EmptyReply, error) { return nil, status.Errorf(codes.FailedPrecondition, "Userspace error.") } @@ -168,10 +168,14 @@ func (b *assertingBackend) AppendInfo(resp []byte) ([]byte, error) { } func (b *assertingBackend) BuildError(err error) ([]byte, error) { - return proto.Marshal(&pb.ResponseMetadataPrepender{ - Metadata: &pb.ResponseMetadata{ - Hostname: fmt.Sprintf("server%d", b.i), - UpstreamError: err.Error(), + return proto.Marshal(&pb.EmptyReply{ + Response: []*pb.EmptyResponse{ + { + Metadata: &pb.ResponseMetadata{ + Hostname: fmt.Sprintf("server%d", b.i), + UpstreamError: err.Error(), + }, + }, }, }) } @@ -216,6 +220,53 @@ func (s *MultiServiceSuite) TestPingEmptyCarriesClientMetadata() { s.Require().Empty(expectedUpstreams) } +func (s *MultiServiceSuite) TestPingEmpty_StressTest() { + for i := 0; i < 50; i++ { + s.TestPingEmptyCarriesClientMetadata() + } +} + +func (s *MultiServiceSuite) TestPingCarriesServerHeadersAndTrailers() { + headerMd := make(metadata.MD) + trailerMd := make(metadata.MD) + // This is an awkward calling convention... but meh. + out, err := s.testClient.Ping(s.ctx, &pb.PingRequest{Value: "foo"}, grpc.Header(&headerMd), grpc.Trailer(&trailerMd)) + require.NoError(s.T(), err, "Ping should succeed without errors") + + s.Require().Len(out.Response, numUpstreams) + for _, resp := range out.Response { + s.Require().Equal("foo", resp.Value) + s.Require().EqualValues(42, resp.Counter) + + // equal metadata set by proxy and server + s.Require().Equal(resp.Metadata.Hostname, resp.Server) + } + + assert.Contains(s.T(), headerMd, serverHeaderMdKey, "server response headers must contain server data") + assert.Len(s.T(), trailerMd, 1, "server response trailers must contain server data") +} + +func (s *MultiServiceSuite) TestPingErrorPropagatesAppError() { + out, err := s.testClient.PingError(s.ctx, &pb.PingRequest{Value: "foo"}) + s.Require().NoError(err, "error should be encapsulated in the response") + + s.Require().Len(out.Response, numUpstreams) + for _, resp := range out.Response { + s.Require().NotEmpty(resp.Metadata.UpstreamError) + s.Require().NotEmpty(resp.Metadata.Hostname) + s.Assert().Equal("rpc error: code = FailedPrecondition desc = Userspace error.", resp.Metadata.UpstreamError) + } +} + +func (s *MultiServiceSuite) TestDirectorErrorIsPropagated() { + // See SetupSuite where the StreamDirector has a special case. + ctx := metadata.NewOutgoingContext(s.ctx, metadata.Pairs(rejectingMdKey, "true")) + _, err := s.testClient.Ping(ctx, &pb.PingRequest{Value: "foo"}) + require.Error(s.T(), err, "Director should reject this RPC") + assert.Equal(s.T(), codes.PermissionDenied, status.Code(err)) + assert.Equal(s.T(), "testing rejection", status.Convert(err).Message()) +} + func (s *MultiServiceSuite) SetupTest() { s.ctx, s.ctxCancel = context.WithTimeout(context.TODO(), 120*time.Second) } diff --git a/testservice/test.pb.go b/testservice/test.pb.go index 34ffba3..e270439 100644 --- a/testservice/test.pb.go +++ b/testservice/test.pb.go @@ -329,6 +329,84 @@ func (m *MultiPingReply) GetResponse() []*MultiPingResponse { return nil } +type EmptyReply struct { + Response []*EmptyResponse `protobuf:"bytes,1,rep,name=response,proto3" json:"response,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *EmptyReply) Reset() { *m = EmptyReply{} } +func (m *EmptyReply) String() string { return proto.CompactTextString(m) } +func (*EmptyReply) ProtoMessage() {} +func (*EmptyReply) Descriptor() ([]byte, []int) { + return fileDescriptor_c161fcfdc0c3ff1e, []int{7} +} + +func (m *EmptyReply) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_EmptyReply.Unmarshal(m, b) +} +func (m *EmptyReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_EmptyReply.Marshal(b, m, deterministic) +} +func (m *EmptyReply) XXX_Merge(src proto.Message) { + xxx_messageInfo_EmptyReply.Merge(m, src) +} +func (m *EmptyReply) XXX_Size() int { + return xxx_messageInfo_EmptyReply.Size(m) +} +func (m *EmptyReply) XXX_DiscardUnknown() { + xxx_messageInfo_EmptyReply.DiscardUnknown(m) +} + +var xxx_messageInfo_EmptyReply proto.InternalMessageInfo + +func (m *EmptyReply) GetResponse() []*EmptyResponse { + if m != nil { + return m.Response + } + return nil +} + +type EmptyResponse struct { + Metadata *ResponseMetadata `protobuf:"bytes,99,opt,name=metadata,proto3" json:"metadata,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *EmptyResponse) Reset() { *m = EmptyResponse{} } +func (m *EmptyResponse) String() string { return proto.CompactTextString(m) } +func (*EmptyResponse) ProtoMessage() {} +func (*EmptyResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_c161fcfdc0c3ff1e, []int{8} +} + +func (m *EmptyResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_EmptyResponse.Unmarshal(m, b) +} +func (m *EmptyResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_EmptyResponse.Marshal(b, m, deterministic) +} +func (m *EmptyResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_EmptyResponse.Merge(m, src) +} +func (m *EmptyResponse) XXX_Size() int { + return xxx_messageInfo_EmptyResponse.Size(m) +} +func (m *EmptyResponse) XXX_DiscardUnknown() { + xxx_messageInfo_EmptyResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_EmptyResponse proto.InternalMessageInfo + +func (m *EmptyResponse) GetMetadata() *ResponseMetadata { + if m != nil { + return m.Metadata + } + return nil +} + func init() { proto.RegisterType((*Empty)(nil), "smira.testproto.Empty") proto.RegisterType((*PingRequest)(nil), "smira.testproto.PingRequest") @@ -337,38 +415,43 @@ func init() { proto.RegisterType((*ResponseMetadataPrepender)(nil), "smira.testproto.ResponseMetadataPrepender") proto.RegisterType((*MultiPingResponse)(nil), "smira.testproto.MultiPingResponse") proto.RegisterType((*MultiPingReply)(nil), "smira.testproto.MultiPingReply") + proto.RegisterType((*EmptyReply)(nil), "smira.testproto.EmptyReply") + proto.RegisterType((*EmptyResponse)(nil), "smira.testproto.EmptyResponse") } func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) } var fileDescriptor_c161fcfdc0c3ff1e = []byte{ - // 416 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x92, 0xcf, 0x4e, 0xea, 0x40, - 0x14, 0xc6, 0x6f, 0xe1, 0x02, 0xe5, 0xc0, 0xe5, 0xde, 0x3b, 0x31, 0xa4, 0x12, 0x8d, 0x3a, 0xc6, - 0x84, 0x55, 0x43, 0x70, 0x2d, 0x1b, 0x45, 0x17, 0x4a, 0xc4, 0xa2, 0x2e, 0xdc, 0x98, 0x0a, 0x13, - 0x6d, 0x42, 0xff, 0x38, 0x33, 0x25, 0xe1, 0x05, 0x7c, 0x08, 0xdf, 0xd5, 0xc4, 0x99, 0xa1, 0x25, - 0x48, 0x83, 0x2d, 0xc1, 0xe5, 0xf9, 0xe6, 0x3b, 0xbf, 0x39, 0x33, 0xdf, 0x01, 0xe0, 0x84, 0x71, - 0x33, 0xa0, 0x3e, 0xf7, 0xd1, 0x5f, 0xe6, 0x3a, 0xd4, 0x36, 0xa5, 0xa2, 0x04, 0x5c, 0x82, 0x42, - 0xd7, 0x0d, 0xf8, 0x14, 0x1f, 0x42, 0xa5, 0xef, 0x78, 0xcf, 0x16, 0x79, 0x0d, 0xc5, 0x21, 0xda, - 0x82, 0xc2, 0xc4, 0x1e, 0x87, 0xc4, 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04, 0x4a, 0xe9, 0x8d, 0xdd, - 0x84, 0xbe, 0x48, 0xc7, 0xbf, 0x50, 0x17, 0x7e, 0x4b, 0x05, 0xed, 0xac, 0x30, 0xaa, 0x9d, 0x4b, - 0xc7, 0x9c, 0x46, 0xc3, 0xc8, 0xd0, 0x53, 0x58, 0x2b, 0x46, 0x15, 0x90, 0x4b, 0xd0, 0xa5, 0xf1, - 0xca, 0x11, 0x5b, 0xbe, 0xd9, 0x3c, 0x2d, 0x0d, 0x5d, 0x03, 0x48, 0x6d, 0xa0, 0x96, 0x71, 0x43, - 0x5c, 0x53, 0x6b, 0x69, 0xed, 0xb7, 0x3c, 0x54, 0x55, 0x3e, 0x71, 0x00, 0xe7, 0x59, 0x02, 0xd8, - 0xfb, 0x2e, 0x63, 0xb1, 0x16, 0xe2, 0xd9, 0x17, 0x99, 0x22, 0xc8, 0x00, 0xfa, 0x91, 0x10, 0x7a, - 0x99, 0x43, 0x48, 0x9f, 0x48, 0xc4, 0x70, 0xb3, 0x46, 0x0c, 0xe9, 0x40, 0x19, 0xc4, 0x53, 0x51, - 0x9d, 0x1d, 0x7f, 0x06, 0x00, 0x00, 0xff, 0xff, 0x6a, 0xdd, 0xde, 0x4d, 0x3e, 0x05, 0x00, 0x00, + // 450 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x93, 0xcd, 0x6e, 0xda, 0x40, + 0x10, 0xc7, 0x6b, 0x28, 0x60, 0x86, 0x8f, 0xb6, 0xab, 0x0a, 0xb9, 0xf4, 0x7b, 0xab, 0x4a, 0x9c, + 0x2c, 0x44, 0x6f, 0x95, 0xca, 0xa5, 0xa5, 0x41, 0x4a, 0x48, 0x88, 0x49, 0x72, 0xc8, 0x25, 0x72, + 0x60, 0x95, 0x58, 0xc2, 0x1f, 0x59, 0xaf, 0x91, 0x78, 0x86, 0xbc, 0x41, 0xde, 0x35, 0x52, 0x76, + 0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xb8, 0x71, + 0x5d, 0x96, 0x67, 0xbf, 0x9e, 0x02, 0x00, 0x00, 0xff, 0xff, 0xbf, 0x8b, 0x3b, 0x5d, 0xdd, 0x05, + 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -661,7 +744,7 @@ var _TestService_serviceDesc = grpc.ServiceDesc{ type MultiServiceClient interface { PingEmpty(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*MultiPingReply, error) Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*MultiPingReply, error) - PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*Empty, error) + PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*EmptyReply, error) PingList(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (MultiService_PingListClient, error) PingStream(ctx context.Context, opts ...grpc.CallOption) (MultiService_PingStreamClient, error) } @@ -692,8 +775,8 @@ func (c *multiServiceClient) Ping(ctx context.Context, in *PingRequest, opts ... return out, nil } -func (c *multiServiceClient) PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*Empty, error) { - out := new(Empty) +func (c *multiServiceClient) PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*EmptyReply, error) { + out := new(EmptyReply) err := c.cc.Invoke(ctx, "/smira.testproto.MultiService/PingError", in, out, opts...) if err != nil { return nil, err @@ -768,7 +851,7 @@ func (x *multiServicePingStreamClient) Recv() (*MultiPingReply, error) { type MultiServiceServer interface { PingEmpty(context.Context, *Empty) (*MultiPingReply, error) Ping(context.Context, *PingRequest) (*MultiPingReply, error) - PingError(context.Context, *PingRequest) (*Empty, error) + PingError(context.Context, *PingRequest) (*EmptyReply, error) PingList(*PingRequest, MultiService_PingListServer) error PingStream(MultiService_PingStreamServer) error } @@ -783,7 +866,7 @@ func (*UnimplementedMultiServiceServer) PingEmpty(ctx context.Context, req *Empt func (*UnimplementedMultiServiceServer) Ping(ctx context.Context, req *PingRequest) (*MultiPingReply, error) { return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented") } -func (*UnimplementedMultiServiceServer) PingError(ctx context.Context, req *PingRequest) (*Empty, error) { +func (*UnimplementedMultiServiceServer) PingError(ctx context.Context, req *PingRequest) (*EmptyReply, error) { return nil, status.Errorf(codes.Unimplemented, "method PingError not implemented") } func (*UnimplementedMultiServiceServer) PingList(req *PingRequest, srv MultiService_PingListServer) error { diff --git a/testservice/test.proto b/testservice/test.proto index 89bf33c..633ebce 100644 --- a/testservice/test.proto +++ b/testservice/test.proto @@ -47,12 +47,20 @@ message MultiPingReply { repeated MultiPingResponse response = 1; } +message EmptyReply { + repeated EmptyResponse response = 1; +} + +message EmptyResponse { + ResponseMetadata metadata = 99; +} + service MultiService { rpc PingEmpty(Empty) returns (MultiPingReply) {} rpc Ping(PingRequest) returns (MultiPingReply) {} - rpc PingError(PingRequest) returns (Empty) {} + rpc PingError(PingRequest) returns (EmptyReply) {} rpc PingList(PingRequest) returns (stream MultiPingReply) {}