github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/flogging/fabenc/encoder_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package fabenc_test
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"runtime"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/hyperledger/fabric/common/flogging/fabenc"
    17  	"github.com/stretchr/testify/assert"
    18  	"go.uber.org/zap"
    19  	"go.uber.org/zap/buffer"
    20  	"go.uber.org/zap/zapcore"
    21  )
    22  
    23  func TestEncodeEntry(t *testing.T) {
    24  	startTime := time.Now()
    25  	var tests = []struct {
    26  		name     string
    27  		spec     string
    28  		fields   []zapcore.Field
    29  		expected string
    30  	}{
    31  		{name: "empty spec and nil fields", spec: "", fields: nil, expected: "\n"},
    32  		{name: "empty spec with fields", spec: "", fields: []zapcore.Field{zap.String("key", "value")}, expected: "key=value\n"},
    33  		{name: "simple spec and nil fields", spec: "simple-string", expected: "simple-string\n"},
    34  		{name: "simple spec and empty fields", spec: "simple-string", fields: []zapcore.Field{}, expected: "simple-string\n"},
    35  		{name: "simple spec with fields", spec: "simple-string", fields: []zapcore.Field{zap.String("key", "value")}, expected: "simple-string key=value\n"},
    36  		{name: "duration", spec: "", fields: []zapcore.Field{zap.Duration("duration", time.Second)}, expected: "duration=1s\n"},
    37  		{name: "time", spec: "", fields: []zapcore.Field{zap.Time("time", startTime)}, expected: fmt.Sprintf("time=%s\n", startTime.Format("2006-01-02T15:04:05.999Z07:00"))},
    38  	}
    39  
    40  	for _, tc := range tests {
    41  		t.Run(tc.name, func(t *testing.T) {
    42  			formatters, err := fabenc.ParseFormat(tc.spec)
    43  			assert.NoError(t, err)
    44  
    45  			enc := fabenc.NewFormatEncoder(formatters...)
    46  
    47  			pc, file, l, ok := runtime.Caller(0)
    48  			line, err := enc.EncodeEntry(
    49  				zapcore.Entry{
    50  					// The entry information should be completely omitted
    51  					Level:      zapcore.InfoLevel,
    52  					Time:       startTime,
    53  					LoggerName: "logger-name",
    54  					Message:    "message",
    55  					Caller:     zapcore.NewEntryCaller(pc, file, l, ok),
    56  					Stack:      "stack",
    57  				},
    58  				tc.fields,
    59  			)
    60  			assert.NoError(t, err)
    61  			assert.Equal(t, tc.expected, line.String())
    62  		})
    63  	}
    64  }
    65  
    66  type brokenEncoder struct{ zapcore.Encoder }
    67  
    68  func (b *brokenEncoder) EncodeEntry(zapcore.Entry, []zapcore.Field) (*buffer.Buffer, error) {
    69  	return nil, errors.New("broken encoder")
    70  }
    71  
    72  func TestEncodeFieldsFailed(t *testing.T) {
    73  	enc := fabenc.NewFormatEncoder()
    74  	enc.Encoder = &brokenEncoder{}
    75  
    76  	_, err := enc.EncodeEntry(zapcore.Entry{}, nil)
    77  	assert.EqualError(t, err, "broken encoder")
    78  }
    79  
    80  func TestFormatEncoderClone(t *testing.T) {
    81  	enc := fabenc.NewFormatEncoder()
    82  	cloned := enc.Clone()
    83  	assert.Equal(t, enc, cloned)
    84  }