...

Source file src/go.uber.org/zap/zapgrpc/zapgrpc_test.go

Documentation: go.uber.org/zap/zapgrpc

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package zapgrpc
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	"go.uber.org/zap"
    28  	"go.uber.org/zap/zapcore"
    29  	"go.uber.org/zap/zaptest/observer"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestLoggerInfoExpected(t *testing.T) {
    35  	checkMessages(t, zapcore.DebugLevel, nil, zapcore.InfoLevel, []string{
    36  		"hello",
    37  		"s1s21 2 3s34s56",
    38  		"hello world",
    39  		"",
    40  		"foo",
    41  		"foo bar",
    42  		"s1 s2 1 2 3 s3 4 s5 6",
    43  		"hello",
    44  		"s1s21 2 3s34s56",
    45  		"hello world",
    46  		"",
    47  		"foo",
    48  		"foo bar",
    49  		"s1 s2 1 2 3 s3 4 s5 6",
    50  	}, func(logger *Logger) {
    51  		logger.Info("hello")
    52  		logger.Info("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    53  		logger.Infof("%s world", "hello")
    54  		logger.Infoln()
    55  		logger.Infoln("foo")
    56  		logger.Infoln("foo", "bar")
    57  		logger.Infoln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    58  		logger.Print("hello")
    59  		logger.Print("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    60  		logger.Printf("%s world", "hello")
    61  		logger.Println()
    62  		logger.Println("foo")
    63  		logger.Println("foo", "bar")
    64  		logger.Println("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    65  	})
    66  }
    67  
    68  func TestLoggerDebugExpected(t *testing.T) {
    69  	checkMessages(t, zapcore.DebugLevel, []Option{WithDebug()}, zapcore.DebugLevel, []string{
    70  		"hello",
    71  		"s1s21 2 3s34s56",
    72  		"hello world",
    73  		"",
    74  		"foo",
    75  		"foo bar",
    76  		"s1 s2 1 2 3 s3 4 s5 6",
    77  	}, func(logger *Logger) {
    78  		logger.Print("hello")
    79  		logger.Print("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    80  		logger.Printf("%s world", "hello")
    81  		logger.Println()
    82  		logger.Println("foo")
    83  		logger.Println("foo", "bar")
    84  		logger.Println("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
    85  	})
    86  }
    87  
    88  func TestLoggerDebugSuppressed(t *testing.T) {
    89  	checkMessages(t, zapcore.InfoLevel, []Option{WithDebug()}, zapcore.DebugLevel, nil, func(logger *Logger) {
    90  		logger.Print("hello")
    91  		logger.Printf("%s world", "hello")
    92  		logger.Println()
    93  		logger.Println("foo")
    94  		logger.Println("foo", "bar")
    95  	})
    96  }
    97  
    98  func TestLoggerWarningExpected(t *testing.T) {
    99  	checkMessages(t, zapcore.DebugLevel, nil, zapcore.WarnLevel, []string{
   100  		"hello",
   101  		"s1s21 2 3s34s56",
   102  		"hello world",
   103  		"",
   104  		"foo",
   105  		"foo bar",
   106  		"s1 s2 1 2 3 s3 4 s5 6",
   107  	}, func(logger *Logger) {
   108  		logger.Warning("hello")
   109  		logger.Warning("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   110  		logger.Warningf("%s world", "hello")
   111  		logger.Warningln()
   112  		logger.Warningln("foo")
   113  		logger.Warningln("foo", "bar")
   114  		logger.Warningln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   115  	})
   116  }
   117  
   118  func TestLoggerErrorExpected(t *testing.T) {
   119  	checkMessages(t, zapcore.DebugLevel, nil, zapcore.ErrorLevel, []string{
   120  		"hello",
   121  		"s1s21 2 3s34s56",
   122  		"hello world",
   123  		"",
   124  		"foo",
   125  		"foo bar",
   126  		"s1 s2 1 2 3 s3 4 s5 6",
   127  	}, func(logger *Logger) {
   128  		logger.Error("hello")
   129  		logger.Error("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   130  		logger.Errorf("%s world", "hello")
   131  		logger.Errorln()
   132  		logger.Errorln("foo")
   133  		logger.Errorln("foo", "bar")
   134  		logger.Errorln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   135  	})
   136  }
   137  
   138  func TestLoggerFatalExpected(t *testing.T) {
   139  	checkMessages(t, zapcore.DebugLevel, nil, zapcore.FatalLevel, []string{
   140  		"hello",
   141  		"s1s21 2 3s34s56",
   142  		"hello world",
   143  		"",
   144  		"foo",
   145  		"foo bar",
   146  		"s1 s2 1 2 3 s3 4 s5 6",
   147  	}, func(logger *Logger) {
   148  		logger.Fatal("hello")
   149  		logger.Fatal("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   150  		logger.Fatalf("%s world", "hello")
   151  		logger.Fatalln()
   152  		logger.Fatalln("foo")
   153  		logger.Fatalln("foo", "bar")
   154  		logger.Fatalln("s1", "s2", 1, 2, 3, "s3", 4, "s5", 6)
   155  	})
   156  }
   157  
   158  func TestLoggerV(t *testing.T) {
   159  	tests := []struct {
   160  		zapLevel     zapcore.Level
   161  		grpcEnabled  []int
   162  		grpcDisabled []int
   163  	}{
   164  		{
   165  			zapLevel:     zapcore.DebugLevel,
   166  			grpcEnabled:  []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError, grpcLvlFatal},
   167  			grpcDisabled: []int{}, // everything is enabled, nothing is disabled
   168  		},
   169  		{
   170  			zapLevel:     zapcore.InfoLevel,
   171  			grpcEnabled:  []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError, grpcLvlFatal},
   172  			grpcDisabled: []int{}, // everything is enabled, nothing is disabled
   173  		},
   174  		{
   175  			zapLevel:     zapcore.WarnLevel,
   176  			grpcEnabled:  []int{grpcLvlWarn, grpcLvlError, grpcLvlFatal},
   177  			grpcDisabled: []int{grpcLvlInfo},
   178  		},
   179  		{
   180  			zapLevel:     zapcore.ErrorLevel,
   181  			grpcEnabled:  []int{grpcLvlError, grpcLvlFatal},
   182  			grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn},
   183  		},
   184  		{
   185  			zapLevel:     zapcore.DPanicLevel,
   186  			grpcEnabled:  []int{grpcLvlFatal},
   187  			grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
   188  		},
   189  		{
   190  			zapLevel:     zapcore.PanicLevel,
   191  			grpcEnabled:  []int{grpcLvlFatal},
   192  			grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
   193  		},
   194  		{
   195  			zapLevel:     zapcore.FatalLevel,
   196  			grpcEnabled:  []int{grpcLvlFatal},
   197  			grpcDisabled: []int{grpcLvlInfo, grpcLvlWarn, grpcLvlError},
   198  		},
   199  	}
   200  	for _, tst := range tests {
   201  		for _, grpcLvl := range tst.grpcEnabled {
   202  			t.Run(fmt.Sprintf("enabled %s %d", tst.zapLevel, grpcLvl), func(t *testing.T) {
   203  				checkLevel(t, tst.zapLevel, true, func(logger *Logger) bool {
   204  					return logger.V(grpcLvl)
   205  				})
   206  			})
   207  		}
   208  		for _, grpcLvl := range tst.grpcDisabled {
   209  			t.Run(fmt.Sprintf("disabled %s %d", tst.zapLevel, grpcLvl), func(t *testing.T) {
   210  				checkLevel(t, tst.zapLevel, false, func(logger *Logger) bool {
   211  					return logger.V(grpcLvl)
   212  				})
   213  			})
   214  		}
   215  	}
   216  }
   217  
   218  func checkLevel(
   219  	t testing.TB,
   220  	enab zapcore.LevelEnabler,
   221  	expectedBool bool,
   222  	f func(*Logger) bool,
   223  ) {
   224  	withLogger(enab, nil, func(logger *Logger, observedLogs *observer.ObservedLogs) {
   225  		actualBool := f(logger)
   226  		if expectedBool {
   227  			require.True(t, actualBool)
   228  		} else {
   229  			require.False(t, actualBool)
   230  		}
   231  	})
   232  }
   233  
   234  func checkMessages(
   235  	t testing.TB,
   236  	enab zapcore.LevelEnabler,
   237  	opts []Option,
   238  	expectedLevel zapcore.Level,
   239  	expectedMessages []string,
   240  	f func(*Logger),
   241  ) {
   242  	if expectedLevel == zapcore.FatalLevel {
   243  		expectedLevel = zapcore.WarnLevel
   244  	}
   245  	withLogger(enab, opts, func(logger *Logger, observedLogs *observer.ObservedLogs) {
   246  		f(logger)
   247  		logEntries := observedLogs.All()
   248  		require.Equal(t, len(expectedMessages), len(logEntries))
   249  		for i, logEntry := range logEntries {
   250  			require.Equal(t, expectedLevel, logEntry.Level)
   251  			require.Equal(t, expectedMessages[i], logEntry.Message)
   252  		}
   253  	})
   254  }
   255  
   256  func withLogger(
   257  	enab zapcore.LevelEnabler,
   258  	opts []Option,
   259  	f func(*Logger, *observer.ObservedLogs),
   260  ) {
   261  	core, observedLogs := observer.New(enab)
   262  	f(NewLogger(zap.New(core), append(opts, withWarn())...), observedLogs)
   263  }
   264  

View as plain text