From f833abb683b1ea07d877cb888344508cb41ebb63 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 25 Nov 2022 10:15:11 +0100 Subject: [PATCH] move output tests into packages Previously it was necessary to enter the "examples" module to run output tests for code in the main module. Now "go test ./..." at the root or in individual directories also runs these tests. --- examples/output_test/output_test.go | 37 ---------------------- klogr/output_test.go | 37 ++++++++++++++++++++++ output_test.go | 43 ++++++++++++++++++++++++++ textlogger/output_test.go | 48 +++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 37 deletions(-) create mode 100644 klogr/output_test.go create mode 100644 output_test.go create mode 100644 textlogger/output_test.go diff --git a/examples/output_test/output_test.go b/examples/output_test/output_test.go index bd4e4787..697dc459 100644 --- a/examples/output_test/output_test.go +++ b/examples/output_test/output_test.go @@ -34,33 +34,6 @@ import ( "k8s.io/klog/v2/textlogger" ) -// TestKlogOutput tests klog output without a logger. -func TestKlogOutput(t *testing.T) { - test.InitKlog(t) - test.Output(t, test.OutputConfig{}) -} - -// TestTextloggerOutput tests the textlogger, directly and as backend. -func TestTextloggerOutput(t *testing.T) { - test.InitKlog(t) - newLogger := func(out io.Writer, v int, vmodule string) logr.Logger { - config := textlogger.NewConfig( - textlogger.Verbosity(v), - textlogger.Output(out), - ) - if err := config.VModule().Set(vmodule); err != nil { - panic(err) - } - return textlogger.NewLogger(config) - } - t.Run("direct", func(t *testing.T) { - test.Output(t, test.OutputConfig{NewLogger: newLogger, SupportsVModule: true}) - }) - t.Run("klog-backend", func(t *testing.T) { - test.Output(t, test.OutputConfig{NewLogger: newLogger, AsBackend: true}) - }) -} - // TestZaprOutput tests the zapr, directly and as backend. func TestZaprOutput(t *testing.T) { test.InitKlog(t) @@ -75,16 +48,6 @@ func TestZaprOutput(t *testing.T) { }) } -// TestKlogrOutput tests klogr output via klog. -func TestKlogrOutput(t *testing.T) { - test.InitKlog(t) - test.Output(t, test.OutputConfig{ - NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger { - return klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)) - }, - }) -} - // TestKlogrStackText tests klogr.klogr -> klog -> text logger. func TestKlogrStackText(t *testing.T) { test.InitKlog(t) diff --git a/klogr/output_test.go b/klogr/output_test.go new file mode 100644 index 00000000..aa87e953 --- /dev/null +++ b/klogr/output_test.go @@ -0,0 +1,37 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package klogr_test + +import ( + "io" + "testing" + + "github.com/go-logr/logr" + + "k8s.io/klog/v2/klogr" + "k8s.io/klog/v2/test" +) + +// TestKlogrOutput tests klogr output via klog. +func TestKlogrOutput(t *testing.T) { + test.InitKlog(t) + test.Output(t, test.OutputConfig{ + NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger { + return klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)) + }, + }) +} diff --git a/output_test.go b/output_test.go new file mode 100644 index 00000000..7e38e12c --- /dev/null +++ b/output_test.go @@ -0,0 +1,43 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package klog_test + +import ( + "io" + "testing" + + "github.com/go-logr/logr" + + "k8s.io/klog/v2" + "k8s.io/klog/v2/test" +) + +// TestKlogOutput tests klog output without a logger. +func TestKlogOutput(t *testing.T) { + test.InitKlog(t) + test.Output(t, test.OutputConfig{}) +} + +// TestKlogKlogrOutput tests klogr output via klog, using the klog/v2 klogr. +func TestKlogrOutput(t *testing.T) { + test.InitKlog(t) + test.Output(t, test.OutputConfig{ + NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger { + return klog.NewKlogr() + }, + }) +} diff --git a/textlogger/output_test.go b/textlogger/output_test.go new file mode 100644 index 00000000..9a3bc95e --- /dev/null +++ b/textlogger/output_test.go @@ -0,0 +1,48 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package textlogger_test + +import ( + "io" + "testing" + + "github.com/go-logr/logr" + + "k8s.io/klog/v2/test" + "k8s.io/klog/v2/textlogger" +) + +// TestTextloggerOutput tests the textlogger, directly and as backend. +func TestTextloggerOutput(t *testing.T) { + test.InitKlog(t) + newLogger := func(out io.Writer, v int, vmodule string) logr.Logger { + config := textlogger.NewConfig( + textlogger.Verbosity(v), + textlogger.Output(out), + ) + if err := config.VModule().Set(vmodule); err != nil { + panic(err) + } + return textlogger.NewLogger(config) + } + t.Run("direct", func(t *testing.T) { + test.Output(t, test.OutputConfig{NewLogger: newLogger, SupportsVModule: true}) + }) + t.Run("klog-backend", func(t *testing.T) { + test.Output(t, test.OutputConfig{NewLogger: newLogger, AsBackend: true}) + }) +}