-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlog_test.go
338 lines (279 loc) · 9.46 KB
/
log_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright 2018 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 log
import (
"context"
"errors"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ logr.LogSink = &delegatingLogSink{}
// logInfo is the information for a particular fakeLogger message.
type logInfo struct {
name []string
tags []interface{}
msg string
}
// fakeLoggerRoot is the root object to which all fakeLoggers record their messages.
type fakeLoggerRoot struct {
messages []logInfo
}
// fakeLogger is a fake implementation of logr.Logger that records
// messages, tags, and names,
// just records the name.
type fakeLogger struct {
name []string
tags []interface{}
root *fakeLoggerRoot
}
func (f *fakeLogger) Init(info logr.RuntimeInfo) {
}
func (f *fakeLogger) WithName(name string) logr.LogSink {
names := append([]string(nil), f.name...)
names = append(names, name)
return &fakeLogger{
name: names,
tags: f.tags,
root: f.root,
}
}
func (f *fakeLogger) WithValues(vals ...interface{}) logr.LogSink {
tags := append([]interface{}(nil), f.tags...)
tags = append(tags, vals...)
return &fakeLogger{
name: f.name,
tags: tags,
root: f.root,
}
}
func (f *fakeLogger) Error(err error, msg string, vals ...interface{}) {
tags := append([]interface{}(nil), f.tags...)
tags = append(tags, "error", err)
tags = append(tags, vals...)
f.root.messages = append(f.root.messages, logInfo{
name: append([]string(nil), f.name...),
tags: tags,
msg: msg,
})
}
func (f *fakeLogger) Info(level int, msg string, vals ...interface{}) {
tags := append([]interface{}(nil), f.tags...)
tags = append(tags, vals...)
f.root.messages = append(f.root.messages, logInfo{
name: append([]string(nil), f.name...),
tags: tags,
msg: msg,
})
}
func (f *fakeLogger) Enabled(level int) bool { return true }
var _ = Describe("logging", func() {
Describe("top-level logger", func() {
It("hold newly created loggers until a logger is set", func() {
By("grabbing a new sub-logger and logging to it")
l1 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue1")
l1.Info("before msg")
By("actually setting the logger")
logger := &fakeLogger{root: &fakeLoggerRoot{}}
SetLogger(logr.New(logger))
By("grabbing another sub-logger and logging to both loggers")
l2 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue2")
l1.Info("after msg 1")
l2.Info("after msg 2")
By("ensuring that messages after the logger was set were logged")
Expect(logger.root.messages).To(ConsistOf(
logInfo{name: []string{"runtimeLog"}, tags: []interface{}{"newtag", "newvalue1"}, msg: "after msg 1"},
logInfo{name: []string{"runtimeLog"}, tags: []interface{}{"newtag", "newvalue2"}, msg: "after msg 2"},
))
})
})
Describe("lazy logger initialization", func() {
var (
root *fakeLoggerRoot
baseLog logr.LogSink
delegLog *delegatingLogSink
)
BeforeEach(func() {
root = &fakeLoggerRoot{}
baseLog = &fakeLogger{root: root}
delegLog = newDelegatingLogSink(NullLogSink{})
})
It("should delegate with name", func() {
By("asking for a logger with a name before fulfill, and logging")
befFulfill1 := logr.New(delegLog).WithName("before-fulfill")
befFulfill2 := befFulfill1.WithName("two")
befFulfill1.Info("before fulfill")
By("logging on the base logger before fulfill")
logr.New(delegLog).Info("before fulfill base")
By("ensuring that no messages were actually recorded")
Expect(root.messages).To(BeEmpty())
By("fulfilling the promise")
delegLog.Fulfill(baseLog)
By("logging with the existing loggers after fulfilling")
befFulfill1.Info("after 1")
befFulfill2.Info("after 2")
By("grabbing a new sub-logger of a previously constructed logger and logging to it")
befFulfill1.WithName("after-from-before").Info("after 3")
By("logging with new loggers")
logr.New(delegLog).WithName("after-fulfill").Info("after 4")
By("ensuring that the messages are appropriately named")
Expect(root.messages).To(ConsistOf(
logInfo{name: []string{"before-fulfill"}, msg: "after 1"},
logInfo{name: []string{"before-fulfill", "two"}, msg: "after 2"},
logInfo{name: []string{"before-fulfill", "after-from-before"}, msg: "after 3"},
logInfo{name: []string{"after-fulfill"}, msg: "after 4"},
))
})
// This test in itself will always succeed, a failure will be indicated by the
// race detector going off
It("should be threadsafe", func() {
fulfillDone := make(chan struct{})
withNameDone := make(chan struct{})
withValuesDone := make(chan struct{})
grandChildDone := make(chan struct{})
logEnabledDone := make(chan struct{})
logInfoDone := make(chan struct{})
logErrorDone := make(chan struct{})
logVDone := make(chan struct{})
// Constructing the child in the goroutine does not reliably
// trigger the race detector
child := logr.New(delegLog).WithName("child")
go func() {
defer GinkgoRecover()
delegLog.Fulfill(NullLogSink{})
close(fulfillDone)
}()
go func() {
defer GinkgoRecover()
delegLog.WithName("with-name")
close(withNameDone)
}()
go func() {
defer GinkgoRecover()
delegLog.WithValues("key", "with-value")
close(withValuesDone)
}()
go func() {
defer GinkgoRecover()
child.WithValues("key", "grandchild")
close(grandChildDone)
}()
go func() {
defer GinkgoRecover()
logr.New(delegLog).Enabled()
close(logEnabledDone)
}()
go func() {
defer GinkgoRecover()
logr.New(delegLog).Info("hello world")
close(logInfoDone)
}()
go func() {
defer GinkgoRecover()
delegLog.Error(errors.New("err"), "hello world")
close(logErrorDone)
}()
go func() {
defer GinkgoRecover()
logr.New(delegLog).V(1)
close(logVDone)
}()
<-fulfillDone
<-withNameDone
<-withValuesDone
<-grandChildDone
<-logEnabledDone
<-logInfoDone
<-logErrorDone
<-logVDone
})
It("should delegate with tags", func() {
By("asking for a logger with a name before fulfill, and logging")
befFulfill1 := logr.New(delegLog).WithValues("tag1", "val1")
befFulfill2 := befFulfill1.WithValues("tag2", "val2")
befFulfill1.Info("before fulfill")
By("logging on the base logger before fulfill")
logr.New(delegLog).Info("before fulfill base")
By("ensuring that no messages were actually recorded")
Expect(root.messages).To(BeEmpty())
By("fulfilling the promise")
delegLog.Fulfill(baseLog)
By("logging with the existing loggers after fulfilling")
befFulfill1.Info("after 1")
befFulfill2.Info("after 2")
By("grabbing a new sub-logger of a previously constructed logger and logging to it")
befFulfill1.WithValues("tag3", "val3").Info("after 3")
By("logging with new loggers")
logr.New(delegLog).WithValues("tag3", "val3").Info("after 4")
By("ensuring that the messages are appropriately named")
Expect(root.messages).To(ConsistOf(
logInfo{tags: []interface{}{"tag1", "val1"}, msg: "after 1"},
logInfo{tags: []interface{}{"tag1", "val1", "tag2", "val2"}, msg: "after 2"},
logInfo{tags: []interface{}{"tag1", "val1", "tag3", "val3"}, msg: "after 3"},
logInfo{tags: []interface{}{"tag3", "val3"}, msg: "after 4"},
))
})
It("shouldn't fulfill twice", func() {
By("fulfilling once")
delegLog.Fulfill(baseLog)
By("logging a bit")
logr.New(delegLog).Info("msg 1")
By("fulfilling with a new logger")
delegLog.Fulfill(&fakeLogger{})
By("logging some more")
logr.New(delegLog).Info("msg 2")
By("checking that all log messages are present")
Expect(root.messages).To(ConsistOf(
logInfo{msg: "msg 1"},
logInfo{msg: "msg 2"},
))
})
It("should handle nil sinks", func() {
By("fulfilling once")
delegLog.Fulfill(logr.Discard().GetSink())
By("grabbing a sub-logger and logging")
l1 := logr.New(delegLog).WithName("nilsink").WithValues("newtag", "newvalue2")
l1.Info("test")
})
})
Describe("logger from context", func() {
It("should return default logger when context is empty", func() {
gotLog := FromContext(context.Background())
Expect(gotLog).To(Not(BeNil()))
})
It("should return existing logger", func() {
root := &fakeLoggerRoot{}
baseLog := &fakeLogger{root: root}
wantLog := logr.New(baseLog).WithName("my-logger")
ctx := IntoContext(context.Background(), wantLog)
gotLog := FromContext(ctx)
Expect(gotLog).To(Not(BeNil()))
gotLog.Info("test message")
Expect(root.messages).To(ConsistOf(
logInfo{name: []string{"my-logger"}, msg: "test message"},
))
})
It("should have added key-values", func() {
root := &fakeLoggerRoot{}
baseLog := &fakeLogger{root: root}
wantLog := logr.New(baseLog).WithName("my-logger")
ctx := IntoContext(context.Background(), wantLog)
gotLog := FromContext(ctx, "tag1", "value1")
Expect(gotLog).To(Not(BeNil()))
gotLog.Info("test message")
Expect(root.messages).To(ConsistOf(
logInfo{name: []string{"my-logger"}, tags: []interface{}{"tag1", "value1"}, msg: "test message"},
))
})
})
})