Skip to content

Commit ff4f80f

Browse files
pohlydims
authored andcommitted
test: unit tests for vmodule support
Text logger and the two klogr implementations both got this wrong.
1 parent 757e6bb commit ff4f80f

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

test/output.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,28 @@ var tests = map[string]testcase{
153153
text: "v=11: you see me because of -vmodule output=11",
154154
v: 11,
155155
vmodule: "output=11",
156+
expectedOutput: `I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
157+
`,
156158
},
157159
"other vmodule": {
158160
text: "v=11: you still don't see me because of -vmodule output_helper=11",
159161
v: 11,
160162
vmodule: "output_helper=11",
161163
},
164+
"vmodule with helper": {
165+
text: "v=11: you see me because of -vmodule output=11",
166+
withHelper: true,
167+
v: 11,
168+
vmodule: "output=11",
169+
expectedOutput: `I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
170+
`,
171+
},
172+
"other vmodule with helper": {
173+
text: "v=11: you still don't see me because of -vmodule output_helper=11",
174+
withHelper: true,
175+
v: 11,
176+
vmodule: "output_helper=11",
177+
},
162178
"log with name and values": {
163179
withNames: []string{"me"},
164180
text: "test",
@@ -410,7 +426,7 @@ func printWithLogger(logger logr.Logger, test testcase) {
410426
}
411427
for _, logger := range loggers {
412428
if test.withHelper {
413-
loggerHelper(logger, test.text, test.values) // <LINE>
429+
loggerHelper(logger.V(test.v), test.text, test.values) // <LINE>
414430
} else if test.err != nil {
415431
logger.Error(test.err, test.text, test.values...) // <LINE>
416432
} else {
@@ -480,7 +496,7 @@ func printWithKlog(test testcase) {
480496
text = strings.Join(test.withNames, "/") + ": " + text
481497
}
482498
if test.withHelper {
483-
klogHelper(text, kv)
499+
klogHelper(klog.Level(test.v), text, kv)
484500
} else if test.err != nil {
485501
klog.ErrorS(test.err, text, kv...)
486502
} else {
@@ -510,7 +526,7 @@ var _, _, printWithKlogLine, _ = runtime.Caller(0) // anchor for finding the lin
510526
func Output(t *testing.T, config OutputConfig) {
511527
for n, test := range tests {
512528
t.Run(n, func(t *testing.T) {
513-
defer klog.ClearLogger()
529+
initPrintWithKlog(t, test)
514530

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

562578
if config.AsBackend {
563579
testOutput(t, printWithKlogLine-1, func(buffer *bytes.Buffer) {
564-
klog.SetLogger(config.NewLogger(buffer, 10, ""))
580+
klog.SetLogger(config.NewLogger(buffer, 10, test.vmodule))
565581
printWithKlog(test)
566582
})
567583
return

test/output_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ func loggerHelper(logger logr.Logger, msg string, kv []interface{}) {
2727
logger.Info(msg, kv...)
2828
}
2929

30-
func klogHelper(msg string, kv []interface{}) {
31-
klog.InfoSDepth(1, msg, kv...)
30+
func klogHelper(level klog.Level, msg string, kv []interface{}) {
31+
klog.V(level).InfoSDepth(1, msg, kv...)
3232
}

test/zapr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
319319
`: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
320320
{"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
321321
`,
322+
323+
// zapr does not support vmodule checks and thus always
324+
// discards these messages.
325+
`I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
326+
`: ``,
322327
} {
323328
mapping[key] = value
324329
}

0 commit comments

Comments
 (0)