forked from kubernetes/klog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testinglogger_test.go
174 lines (162 loc) · 4.72 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
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2020 Intel Coporation.
SPDX-License-Identifier: Apache-2.0
*/
package ktesting_test
import (
"bytes"
"errors"
"fmt"
"sync"
"testing"
"k8s.io/klog/v2/ktesting"
)
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: `INFO test akey="avalue"
`,
},
"should support single name": {
names: []string{"hello"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `INFO hello: test akey="avalue"
`,
},
"should support multiple names": {
names: []string{"hello", "world"},
text: "test",
keysAndValues: []interface{}{"akey", "avalue"},
expectedOutput: `INFO hello/world: test akey="avalue"
`,
},
"should not print duplicate keys with the same value": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey", "avalue"},
expectedOutput: `INFO 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: `INFO 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: `INFO 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: `INFO test akey="avalue2"
`,
},
"should correctly handle odd-numbers of KVs": {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
expectedOutput: `INFO test akey="avalue" akey2="(MISSING)"
`,
},
"should correctly html characters": {
text: "test",
keysAndValues: []interface{}{"akey", "<&>"},
expectedOutput: `INFO 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: `INFO test basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`,
},
"should correctly print regular error types": {
text: "test",
keysAndValues: []interface{}{"err", errors.New("whoops")},
expectedOutput: `INFO test err="whoops"
`,
},
"should correctly print regular error types when using logr.Error": {
text: "test",
err: errors.New("whoops"),
expectedOutput: `ERROR 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()
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 {
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 stopped
// before returning from the test.
var wg sync.WaitGroup
wg.Add(1)
t.Run("Sub", func(t *testing.T) {
logger, _ := ktesting.NewTestContext(t)
defer ktesting.Stop(logger)
go func() {
// Wait for test to return.
wg.Wait()
logger.Info("hello world")
}()
})
// Allow goroutine above to proceed.
wg.Done()
}