Skip to content

Commit

Permalink
move output tests into packages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly authored and dims committed Jan 19, 2023
1 parent 70aa795 commit f833abb
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 37 deletions.
37 changes: 0 additions & 37 deletions examples/output_test/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions klogr/output_test.go
Original file line number Diff line number Diff line change
@@ -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))
},
})
}
43 changes: 43 additions & 0 deletions output_test.go
Original file line number Diff line number Diff line change
@@ -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()
},
})
}
48 changes: 48 additions & 0 deletions textlogger/output_test.go
Original file line number Diff line number Diff line change
@@ -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})
})
}

0 comments on commit f833abb

Please sign in to comment.