github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/grpclogging/fields_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package grpclogging_test
     8  
     9  import (
    10  	"encoding/json"
    11  
    12  	"github.com/golang/protobuf/jsonpb"
    13  	"github.com/hyperledger/fabric/common/grpclogging"
    14  	"github.com/hyperledger/fabric/common/grpclogging/testpb"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	"github.com/pkg/errors"
    18  	"go.uber.org/zap"
    19  	"go.uber.org/zap/zapcore"
    20  )
    21  
    22  var _ = Describe("Fields", func() {
    23  	Describe("ProtoMessage", func() {
    24  		var message *testpb.Message
    25  
    26  		BeforeEach(func() {
    27  			message = &testpb.Message{
    28  				Message:  "Je suis une pizza avec du fromage.",
    29  				Sequence: 1337,
    30  			}
    31  		})
    32  
    33  		It("creates a reflect field for zap", func() {
    34  			field := grpclogging.ProtoMessage("field-key", message)
    35  			Expect(field.Key).To(Equal("field-key"))
    36  			_, ok := field.Interface.(json.Marshaler)
    37  			Expect(ok).To(BeTrue())
    38  		})
    39  
    40  		It("marshals messages compatible with jsonpb", func() {
    41  			field := grpclogging.ProtoMessage("field-key", message)
    42  			marshaler := field.Interface.(json.Marshaler)
    43  
    44  			marshaled, err := marshaler.MarshalJSON()
    45  			Expect(err).NotTo(HaveOccurred())
    46  
    47  			protoMarshaler := &jsonpb.Marshaler{}
    48  			protoJson, err := protoMarshaler.MarshalToString(message)
    49  			Expect(err).NotTo(HaveOccurred())
    50  
    51  			Expect(marshaled).To(MatchJSON(protoJson))
    52  		})
    53  
    54  		It("works with zap's json encoder", func() {
    55  			encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
    56  				MessageKey: "message",
    57  			})
    58  			buf, err := encoder.EncodeEntry(
    59  				zapcore.Entry{Message: "Oh là là"},
    60  				[]zapcore.Field{grpclogging.ProtoMessage("proto-message", message)},
    61  			)
    62  			Expect(err).NotTo(HaveOccurred())
    63  			Expect(buf.String()).To(MatchJSON(`{"message": "Oh là là", "proto-message": {"message": "Je suis une pizza avec du fromage.", "sequence": 1337}}`))
    64  		})
    65  
    66  		Context("when marshaling the message fails", func() {
    67  			It("it returns the error from marshaling", func() {
    68  				field := grpclogging.ProtoMessage("field-key", badProto{err: errors.New("Boom!")})
    69  				marshaler := field.Interface.(json.Marshaler)
    70  
    71  				_, err := marshaler.MarshalJSON()
    72  				Expect(err).To(MatchError("Boom!"))
    73  			})
    74  		})
    75  
    76  		Context("when something other than a proto.Message is provided", func() {
    77  			It("creates an any field", func() {
    78  				field := grpclogging.ProtoMessage("field-key", "Je ne suis pas une pizza.")
    79  				Expect(field).To(Equal(zap.Any("field-key", "Je ne suis pas une pizza.")))
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("Error", func() {
    85  		It("creates an error field for zap", func() {
    86  			err := errors.New("error")
    87  			field := grpclogging.Error(err)
    88  			Expect(field.Key).To(Equal("error"))
    89  			Expect(field.Type).To(Equal(zapcore.ErrorType))
    90  			Expect(field.Interface).To(Equal(struct{ error }{err}))
    91  		})
    92  
    93  		Context("when the error is nil", func() {
    94  			It("creates a skip field", func() {
    95  				field := grpclogging.Error(nil)
    96  				Expect(field.Type).To(Equal(zapcore.SkipType))
    97  			})
    98  		})
    99  
   100  		It("omits the verboseError field", func() {
   101  			encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
   102  				MessageKey: "message",
   103  			})
   104  			buf, err := encoder.EncodeEntry(
   105  				zapcore.Entry{Message: "the message"},
   106  				[]zapcore.Field{grpclogging.Error(errors.New("the error"))},
   107  			)
   108  			Expect(err).NotTo(HaveOccurred())
   109  			Expect(buf.String()).To(MatchJSON(`{"message": "the message", "error": "the error"}`))
   110  		})
   111  	})
   112  })
   113  
   114  type badProto struct{ err error }
   115  
   116  func (b badProto) Reset()         {}
   117  func (b badProto) String() string { return "" }
   118  func (b badProto) ProtoMessage()  {}
   119  func (b badProto) MarshalJSONPB(*jsonpb.Marshaler) ([]byte, error) {
   120  	return nil, b.err
   121  }