Skip to content

Commit f833abb

Browse files
pohlydims
authored andcommitted
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.
1 parent 70aa795 commit f833abb

File tree

4 files changed

+128
-37
lines changed

4 files changed

+128
-37
lines changed

examples/output_test/output_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,6 @@ import (
3434
"k8s.io/klog/v2/textlogger"
3535
)
3636

37-
// TestKlogOutput tests klog output without a logger.
38-
func TestKlogOutput(t *testing.T) {
39-
test.InitKlog(t)
40-
test.Output(t, test.OutputConfig{})
41-
}
42-
43-
// TestTextloggerOutput tests the textlogger, directly and as backend.
44-
func TestTextloggerOutput(t *testing.T) {
45-
test.InitKlog(t)
46-
newLogger := func(out io.Writer, v int, vmodule string) logr.Logger {
47-
config := textlogger.NewConfig(
48-
textlogger.Verbosity(v),
49-
textlogger.Output(out),
50-
)
51-
if err := config.VModule().Set(vmodule); err != nil {
52-
panic(err)
53-
}
54-
return textlogger.NewLogger(config)
55-
}
56-
t.Run("direct", func(t *testing.T) {
57-
test.Output(t, test.OutputConfig{NewLogger: newLogger, SupportsVModule: true})
58-
})
59-
t.Run("klog-backend", func(t *testing.T) {
60-
test.Output(t, test.OutputConfig{NewLogger: newLogger, AsBackend: true})
61-
})
62-
}
63-
6437
// TestZaprOutput tests the zapr, directly and as backend.
6538
func TestZaprOutput(t *testing.T) {
6639
test.InitKlog(t)
@@ -75,16 +48,6 @@ func TestZaprOutput(t *testing.T) {
7548
})
7649
}
7750

78-
// TestKlogrOutput tests klogr output via klog.
79-
func TestKlogrOutput(t *testing.T) {
80-
test.InitKlog(t)
81-
test.Output(t, test.OutputConfig{
82-
NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger {
83-
return klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))
84-
},
85-
})
86-
}
87-
8851
// TestKlogrStackText tests klogr.klogr -> klog -> text logger.
8952
func TestKlogrStackText(t *testing.T) {
9053
test.InitKlog(t)

klogr/output_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package klogr_test
18+
19+
import (
20+
"io"
21+
"testing"
22+
23+
"github.com/go-logr/logr"
24+
25+
"k8s.io/klog/v2/klogr"
26+
"k8s.io/klog/v2/test"
27+
)
28+
29+
// TestKlogrOutput tests klogr output via klog.
30+
func TestKlogrOutput(t *testing.T) {
31+
test.InitKlog(t)
32+
test.Output(t, test.OutputConfig{
33+
NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger {
34+
return klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))
35+
},
36+
})
37+
}

output_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package klog_test
18+
19+
import (
20+
"io"
21+
"testing"
22+
23+
"github.com/go-logr/logr"
24+
25+
"k8s.io/klog/v2"
26+
"k8s.io/klog/v2/test"
27+
)
28+
29+
// TestKlogOutput tests klog output without a logger.
30+
func TestKlogOutput(t *testing.T) {
31+
test.InitKlog(t)
32+
test.Output(t, test.OutputConfig{})
33+
}
34+
35+
// TestKlogKlogrOutput tests klogr output via klog, using the klog/v2 klogr.
36+
func TestKlogrOutput(t *testing.T) {
37+
test.InitKlog(t)
38+
test.Output(t, test.OutputConfig{
39+
NewLogger: func(out io.Writer, v int, vmodule string) logr.Logger {
40+
return klog.NewKlogr()
41+
},
42+
})
43+
}

textlogger/output_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package textlogger_test
18+
19+
import (
20+
"io"
21+
"testing"
22+
23+
"github.com/go-logr/logr"
24+
25+
"k8s.io/klog/v2/test"
26+
"k8s.io/klog/v2/textlogger"
27+
)
28+
29+
// TestTextloggerOutput tests the textlogger, directly and as backend.
30+
func TestTextloggerOutput(t *testing.T) {
31+
test.InitKlog(t)
32+
newLogger := func(out io.Writer, v int, vmodule string) logr.Logger {
33+
config := textlogger.NewConfig(
34+
textlogger.Verbosity(v),
35+
textlogger.Output(out),
36+
)
37+
if err := config.VModule().Set(vmodule); err != nil {
38+
panic(err)
39+
}
40+
return textlogger.NewLogger(config)
41+
}
42+
t.Run("direct", func(t *testing.T) {
43+
test.Output(t, test.OutputConfig{NewLogger: newLogger, SupportsVModule: true})
44+
})
45+
t.Run("klog-backend", func(t *testing.T) {
46+
test.Output(t, test.OutputConfig{NewLogger: newLogger, AsBackend: true})
47+
})
48+
}

0 commit comments

Comments
 (0)