From 2e691eb3eeb3799494cbdc0b5f1d96b53af906fc Mon Sep 17 00:00:00 2001 From: Mitchell de Bruyn Date: Mon, 15 Jun 2020 19:20:57 +0200 Subject: [PATCH] Fix missing fields in verbose InfoS Add Verbose InfoS test --- klog.go | 2 +- klog_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/klog.go b/klog.go index b0091f43..ae2b8613 100644 --- a/klog.go +++ b/klog.go @@ -1320,7 +1320,7 @@ func (v Verbose) Infof(format string, args ...interface{}) { // See the documentation of V for usage. func (v Verbose) InfoS(msg string, keysAndValues ...interface{}) { if v.enabled { - logging.infoS(v.logr, msg, keysAndValues) + logging.infoS(v.logr, msg, keysAndValues...) } } diff --git a/klog_test.go b/klog_test.go index f46ba915..ff958147 100644 --- a/klog_test.go +++ b/klog_test.go @@ -820,6 +820,64 @@ func TestInfoS(t *testing.T) { } } +// Test that Verbose.InfoS works as advertised. +func TestVInfoS(t *testing.T) { + setFlags() + defer logging.swap(logging.newBuffers()) + timeNow = func() time.Time { + return time.Date(2006, 1, 2, 15, 4, 5, .067890e9, time.Local) + } + pid = 1234 + var testDataInfo = []struct { + msg string + format string + keysValues []interface{} + }{ + { + msg: "test", + format: "I0102 15:04:05.067890 1234 klog_test.go:%d] \"test\" pod=\"kubedns\"\n", + keysValues: []interface{}{"pod", "kubedns"}, + }, + { + msg: "test", + format: "I0102 15:04:05.067890 1234 klog_test.go:%d] \"test\" replicaNum=20\n", + keysValues: []interface{}{"replicaNum", 20}, + }, + { + msg: "test", + format: "I0102 15:04:05.067890 1234 klog_test.go:%d] \"test\" err=\"test error\"\n", + keysValues: []interface{}{"err", errors.New("test error")}, + }, + } + + logging.verbosity.Set("2") + defer logging.verbosity.Set("0") + + for l := Level(0); l < Level(4); l++ { + for _, data := range testDataInfo { + logging.file[infoLog] = &flushBuffer{} + + V(l).InfoS(data.msg, data.keysValues...) + + var want string + var line int + if l <= 2 { + n, err := fmt.Sscanf(contents(infoLog), data.format, &line) + if n != 1 || err != nil { + t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog)) + } + + want = fmt.Sprintf(data.format, line) + } else { + want = "" + } + if contents(infoLog) != want { + t.Errorf("V(%d).InfoS has unexpected output: \n got:\t%s\nwant:\t%s", l, contents(infoLog), want) + } + } + } +} + // Test that ErrorS works as advertised. func TestErrorS(t *testing.T) { setFlags()