forked from golang/glog
/
testinglogger_test.go
255 lines (232 loc) · 8.23 KB
/
testinglogger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2020 Intel Corporation.
SPDX-License-Identifier: Apache-2.0
*/
package ktesting_test
import (
"bytes"
"errors"
"flag"
"fmt"
"regexp"
"runtime"
"sync"
"testing"
"k8s.io/klog/v2"
"k8s.io/klog/v2/internal/test/require"
"k8s.io/klog/v2/ktesting"
)
var headerRe = regexp.MustCompile(`([IE])[[:digit:]]{4} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\.[[:digit:]]{6}\] `)
func TestInfo(t *testing.T) {
tests := map[string]struct {
text string
withValues []interface{}
keysAndValues []interface{}
names []string
err error
expectedOutput string
}{
"should log with values passed to keysAndValues": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `Ixxx test akey="avalue"
`,
},
"should support single name": {
names: []string{"hello"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `Ixxx hello: test akey="avalue"
`,
},
"should support multiple names": {
names: []string{"hello", "world"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `Ixxx hello/world: test akey="avalue"
`,
},
"should not print duplicate keys with the same value": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey", "avalue"},
expectedOutput: `Ixxx test akey="avalue" akey="avalue"
`,
},
"should only print the last duplicate key when the values are passed to Info": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey", "avalue2"},
expectedOutput: `Ixxx test akey="avalue" akey="avalue2"
`,
},
"should only print the duplicate key that is passed to Info if one was passed to the logger": {
withValues: []interface{}{"akey", "avalue"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `Ixxx test akey="avalue"
`,
},
"should only print the key passed to Info when one is already set on the logger": {
withValues: []interface{}{"akey", "avalue"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue2"},
expectedOutput: `Ixxx test akey="avalue2"
`,
},
"should correctly handle odd-numbers of KVs": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
expectedOutput: `Ixxx test akey="avalue" akey2="(MISSING)"
`,
},
"should correctly html characters": {
text: "test",
keysAndValues: []interface{}{"akey", "<&>"},
expectedOutput: `Ixxx test akey="<&>"
`,
},
"should correctly handle odd-numbers of KVs in both log values and Info args": {
withValues: []interface{}{"basekey1", "basevar1", "basekey2"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
expectedOutput: `Ixxx test basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`,
},
"should correctly print regular error types": {
text: "test",
keysAndValues: []interface{}{"err", errors.New("whoops")},
expectedOutput: `Ixxx test err="whoops"
`,
},
"should correctly print regular error types when using logr.Error": {
text: "test",
err: errors.New("whoops"),
expectedOutput: `Exxx test err="whoops"
`,
},
}
for n, test := range tests {
t.Run(n, func(t *testing.T) {
var buffer logToBuf
klogr := ktesting.NewLogger(&buffer, ktesting.NewConfig())
for _, name := range test.names {
klogr = klogr.WithName(name)
}
klogr = klogr.WithValues(test.withValues...)
if test.err != nil {
klogr.Error(test.err, test.text, test.keysAndValues...)
} else {
klogr.Info(test.text, test.keysAndValues...)
}
actual := buffer.String()
actual = headerRe.ReplaceAllString(actual, `${1}xxx `)
if actual != test.expectedOutput {
t.Errorf("Expected:\n%sActual:\n%s\n", test.expectedOutput, actual)
}
})
}
}
func TestCallDepth(t *testing.T) {
logger := ktesting.NewLogger(t, ktesting.NewConfig())
logger.Info("hello world")
}
type logToBuf struct {
ktesting.NopTL
bytes.Buffer
}
func (l *logToBuf) Helper() {
}
func (l *logToBuf) Log(args ...interface{}) {
for i, arg := range args {
if i > 0 {
l.Write([]byte(" "))
}
l.Write([]byte(fmt.Sprintf("%s", arg)))
}
l.Write([]byte("\n"))
}
func TestStop(t *testing.T) {
// This test is set up so that a subtest spawns a goroutine and that
// goroutine logs through ktesting *after* the subtest has
// completed. This is not supported by testing.T.Log and normally
// leads to:
// panic: Log in goroutine after TestGoroutines/Sub has completed: INFO hello world
//
// It works with ktesting if (and only if) logging gets redirected to klog
// before returning from the test.
// Capture output for testing.
state := klog.CaptureState()
defer state.Restore()
var output bytes.Buffer
var fs flag.FlagSet
klog.InitFlags(&fs)
require.NoError(t, fs.Set("alsologtostderr", "false"))
require.NoError(t, fs.Set("logtostderr", "false"))
require.NoError(t, fs.Set("stderrthreshold", "FATAL"))
require.NoError(t, fs.Set("one_output", "true"))
klog.SetOutput(&output)
var logger klog.Logger
var line int
var wg1, wg2 sync.WaitGroup
wg1.Add(1)
wg2.Add(1)
t.Run("Sub", func(t *testing.T) {
logger, _ = ktesting.NewTestContext(t)
go func() {
defer wg2.Done()
// Wait for test to have returned.
wg1.Wait()
// This output must go to klog because the test has
// completed.
_, _, line, _ = runtime.Caller(0)
logger.Info("simple info message")
logger.Error(nil, "error message")
logger.WithName("me").WithValues("completed", true).Info("complex info message", "anotherValue", 1)
}()
})
// Allow goroutine above to proceed.
wg1.Done()
// Ensure that goroutine has completed.
wg2.Wait()
actual := output.String()
// Strip time and pid prefix.
actual = regexp.MustCompile(`(?m)^.* testinglogger_test.go:`).ReplaceAllString(actual, `testinglogger_test.go:`)
// All lines from the callstack get stripped. We can be sure that it was non-empty because otherwise we wouldn't
// have the < > markers.
//
// Full output:
// testinglogger_test.go:194] "WARNING: test kept at least one goroutine running after test completion" logger="TestStop/Sub leaked goroutine" callstack=<
// goroutine 23 [running]:
// k8s.io/klog/v2/internal/dbg.Stacks(0x0)
// /nvme/gopath/src/k8s.io/klog/internal/dbg/dbg.go:34 +0x8a
// k8s.io/klog/v2/ktesting.tlogger.fallbackLogger({0xc0000f2780, {0x0, 0x0}, {0x0, 0x0, 0x0}})
// /nvme/gopath/src/k8s.io/klog/ktesting/testinglogger.go:292 +0x232
// k8s.io/klog/v2/ktesting.tlogger.Info({0xc0000f2780, {0x0, 0x0}, {0x0, 0x0, 0x0}}, 0x0, {0x5444a5, 0x13}, {0x0, ...})
// /nvme/gopath/src/k8s.io/klog/ktesting/testinglogger.go:316 +0x28a
// github.com/go-logr/logr.Logger.Info({{0x572438?, 0xc0000c0ff0?}, 0x0?}, {0x5444a5, 0x13}, {0x0, 0x0, 0x0})
// /nvme/gopath/pkg/mod/github.com/go-logr/logr@v1.2.0/logr.go:249 +0xd0
// k8s.io/klog/v2/ktesting_test.TestStop.func1.1()
// /nvme/gopath/src/k8s.io/klog/ktesting/testinglogger_test.go:194 +0xe5
// created by k8s.io/klog/v2/ktesting_test.TestStop.func1
// /nvme/gopath/src/k8s.io/klog/ktesting/testinglogger_test.go:185 +0x105
// >
actual = regexp.MustCompile(`(?m)^\t.*?\n`).ReplaceAllString(actual, ``)
expected := fmt.Sprintf(`testinglogger_test.go:%d] "WARNING: test kept at least one goroutine running after test completion" logger="TestStop/Sub leaked goroutine" callstack=<
>
testinglogger_test.go:%d] "simple info message" logger="TestStop/Sub leaked goroutine"
testinglogger_test.go:%d] "error message" logger="TestStop/Sub leaked goroutine"
testinglogger_test.go:%d] "complex info message" logger="TestStop/Sub leaked goroutine.me" completed=true anotherValue=1
`,
line+1, line+1, line+2, line+3)
if actual != expected {
t.Errorf("Output does not match. Expected:\n%s\nActual:\n%s\n", expected, actual)
}
testingLogger, ok := logger.GetSink().(ktesting.Underlier)
if !ok {
t.Fatal("should have had a ktesting logger")
}
captured := testingLogger.GetBuffer().String()
if captured != "" {
t.Errorf("testing logger should not have captured any output, got instead:\n%s", captured)
}
}