Skip to content

Commit

Permalink
test: unit tests for vmodule support
Browse files Browse the repository at this point in the history
Text logger and the two klogr implementations both got this wrong.
  • Loading branch information
pohly authored and dims committed Jan 19, 2023
1 parent 757e6bb commit ff4f80f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
24 changes: 20 additions & 4 deletions test/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,28 @@ var tests = map[string]testcase{
text: "v=11: you see me because of -vmodule output=11",
v: 11,
vmodule: "output=11",
expectedOutput: `I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
`,
},
"other vmodule": {
text: "v=11: you still don't see me because of -vmodule output_helper=11",
v: 11,
vmodule: "output_helper=11",
},
"vmodule with helper": {
text: "v=11: you see me because of -vmodule output=11",
withHelper: true,
v: 11,
vmodule: "output=11",
expectedOutput: `I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
`,
},
"other vmodule with helper": {
text: "v=11: you still don't see me because of -vmodule output_helper=11",
withHelper: true,
v: 11,
vmodule: "output_helper=11",
},
"log with name and values": {
withNames: []string{"me"},
text: "test",
Expand Down Expand Up @@ -410,7 +426,7 @@ func printWithLogger(logger logr.Logger, test testcase) {
}
for _, logger := range loggers {
if test.withHelper {
loggerHelper(logger, test.text, test.values) // <LINE>
loggerHelper(logger.V(test.v), test.text, test.values) // <LINE>
} else if test.err != nil {
logger.Error(test.err, test.text, test.values...) // <LINE>
} else {
Expand Down Expand Up @@ -480,7 +496,7 @@ func printWithKlog(test testcase) {
text = strings.Join(test.withNames, "/") + ": " + text
}
if test.withHelper {
klogHelper(text, kv)
klogHelper(klog.Level(test.v), text, kv)
} else if test.err != nil {
klog.ErrorS(test.err, text, kv...)
} else {
Expand Down Expand Up @@ -510,7 +526,7 @@ var _, _, printWithKlogLine, _ = runtime.Caller(0) // anchor for finding the lin
func Output(t *testing.T, config OutputConfig) {
for n, test := range tests {
t.Run(n, func(t *testing.T) {
defer klog.ClearLogger()
initPrintWithKlog(t, test)

testOutput := func(t *testing.T, expectedLine int, print func(buffer *bytes.Buffer)) {
var tmpWriteBuffer bytes.Buffer
Expand Down Expand Up @@ -561,7 +577,7 @@ func Output(t *testing.T, config OutputConfig) {

if config.AsBackend {
testOutput(t, printWithKlogLine-1, func(buffer *bytes.Buffer) {
klog.SetLogger(config.NewLogger(buffer, 10, ""))
klog.SetLogger(config.NewLogger(buffer, 10, test.vmodule))
printWithKlog(test)
})
return
Expand Down
4 changes: 2 additions & 2 deletions test/output_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func loggerHelper(logger logr.Logger, msg string, kv []interface{}) {
logger.Info(msg, kv...)
}

func klogHelper(msg string, kv []interface{}) {
klog.InfoSDepth(1, msg, kv...)
func klogHelper(level klog.Level, msg string, kv []interface{}) {
klog.V(level).InfoSDepth(1, msg, kv...)
}
5 changes: 5 additions & 0 deletions test/zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
`,

// zapr does not support vmodule checks and thus always
// discards these messages.
`I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
`: ``,
} {
mapping[key] = value
}
Expand Down

0 comments on commit ff4f80f

Please sign in to comment.