...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/zap_raft_test.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver

     1  // Copyright 2018 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package etcdserver
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	"go.etcd.io/etcd/client/pkg/v3/logutil"
    28  	"go.uber.org/zap"
    29  	"go.uber.org/zap/zapcore"
    30  )
    31  
    32  func TestNewRaftLogger(t *testing.T) {
    33  	logPath := filepath.Join(os.TempDir(), fmt.Sprintf("test-log-%d", time.Now().UnixNano()))
    34  	defer os.RemoveAll(logPath)
    35  
    36  	lcfg := &zap.Config{
    37  		Level:       zap.NewAtomicLevelAt(zap.DebugLevel),
    38  		Development: false,
    39  		Sampling: &zap.SamplingConfig{
    40  			Initial:    100,
    41  			Thereafter: 100,
    42  		},
    43  		Encoding:         "json",
    44  		EncoderConfig:    logutil.DefaultZapLoggerConfig.EncoderConfig,
    45  		OutputPaths:      []string{logPath},
    46  		ErrorOutputPaths: []string{logPath},
    47  	}
    48  	gl, err := NewRaftLogger(lcfg)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	gl.Info("etcd-logutil-1")
    54  	data, err := ioutil.ReadFile(logPath)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if !bytes.Contains(data, []byte("etcd-logutil-1")) {
    59  		t.Fatalf("can't find data in log %q", string(data))
    60  	}
    61  
    62  	gl.Warning("etcd-logutil-2")
    63  	data, err = ioutil.ReadFile(logPath)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if !bytes.Contains(data, []byte("etcd-logutil-2")) {
    68  		t.Fatalf("can't find data in log %q", string(data))
    69  	}
    70  	if !bytes.Contains(data, []byte("zap_raft_test.go:")) {
    71  		t.Fatalf("unexpected caller; %q", string(data))
    72  	}
    73  }
    74  
    75  func TestNewRaftLoggerFromZapCore(t *testing.T) {
    76  	buf := bytes.NewBuffer(nil)
    77  	syncer := zapcore.AddSync(buf)
    78  	cr := zapcore.NewCore(
    79  		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
    80  		syncer,
    81  		zap.NewAtomicLevelAt(zap.InfoLevel),
    82  	)
    83  
    84  	lg := NewRaftLoggerFromZapCore(cr, syncer)
    85  	lg.Info("TestNewRaftLoggerFromZapCore")
    86  	txt := buf.String()
    87  	if !strings.Contains(txt, "TestNewRaftLoggerFromZapCore") {
    88  		t.Fatalf("unexpected log %q", txt)
    89  	}
    90  }
    91  

View as plain text