From ff4f80fcbad7920bd92d07b63d23267f2ddd061a Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 16 Jan 2023 11:25:47 +0100 Subject: [PATCH] test: unit tests for vmodule support Text logger and the two klogr implementations both got this wrong. --- test/output.go | 24 ++++++++++++++++++++---- test/output_helper.go | 4 ++-- test/zapr.go | 5 +++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/test/output.go b/test/output.go index 16553f2b4..bfd026c2f 100644 --- a/test/output.go +++ b/test/output.go @@ -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:] "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:] "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", @@ -410,7 +426,7 @@ func printWithLogger(logger logr.Logger, test testcase) { } for _, logger := range loggers { if test.withHelper { - loggerHelper(logger, test.text, test.values) // + loggerHelper(logger.V(test.v), test.text, test.values) // } else if test.err != nil { logger.Error(test.err, test.text, test.values...) // } else { @@ -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 { @@ -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 @@ -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 diff --git a/test/output_helper.go b/test/output_helper.go index 499395e38..58b7348f9 100644 --- a/test/output_helper.go +++ b/test/output_helper.go @@ -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...) } diff --git a/test/zapr.go b/test/zapr.go index 9f6cd8b4c..48cf2c3da 100644 --- a/test/zapr.go +++ b/test/zapr.go @@ -319,6 +319,11 @@ I output.go:] "test" firstKey=1 secondKey=3 `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}} {"caller":"test/output.go:","msg":"map keys","v":0} `, + + // zapr does not support vmodule checks and thus always + // discards these messages. + `I output.go:] "v=11: you see me because of -vmodule output=11" +`: ``, } { mapping[key] = value }