Skip to content

Commit

Permalink
Fix missing fields in verbose InfoS
Browse files Browse the repository at this point in the history
Add Verbose InfoS test
  • Loading branch information
physcat committed Jun 15, 2020
1 parent 0bedec3 commit 2e691eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion klog.go
Expand Up @@ -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...)
}
}

Expand Down
58 changes: 58 additions & 0 deletions klog_test.go
Expand Up @@ -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()
Expand Down

0 comments on commit 2e691eb

Please sign in to comment.