-
Notifications
You must be signed in to change notification settings - Fork 9
/
event_test.go
358 lines (335 loc) · 8.92 KB
/
event_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package slog
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEventfFormatsParams(t *testing.T) {
e := Eventf(CriticalSeverity, nil, "foo: %s", "bar")
assert.Equal(t, "foo: bar", e.Message)
}
func TestEventfNilContext(t *testing.T) {
e := Eventf(CriticalSeverity, nil, "foo: %s", "bar")
if e.Context == nil {
t.Error("background context should have been used automatically")
}
}
func TestOriginalMessagePreserved(t *testing.T) {
testCases := []struct {
desc string
message string
params []interface{}
expectedMessage string
expectedOriginal string
}{
{
desc: "no formatting",
message: "foo",
params: []interface{}{},
expectedMessage: "foo",
expectedOriginal: "foo",
},
{
desc: "no formatting with error",
message: "foo",
params: []interface{}{assert.AnError},
expectedMessage: "foo",
expectedOriginal: "foo",
},
{
desc: "simple format string",
message: "foo: %s",
params: []interface{}{"bar"},
expectedMessage: "foo: bar",
expectedOriginal: "foo: %s",
},
{
desc: "formatting with error",
message: "foo: %v",
params: []interface{}{assert.AnError},
expectedMessage: "foo: assert.AnError general error for testing",
expectedOriginal: "foo: %v",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
e := Eventf(ErrorSeverity, nil, tC.message, tC.params...)
assert.Equal(t, tC.expectedMessage, e.Message)
assert.Equal(t, tC.expectedOriginal, e.OriginalMessage)
})
}
}
func TestEventMetadata(t *testing.T) {
testCases := []struct {
desc string
message string
params []interface{}
expected map[string]interface{}
expectedMessage string
expectedError error
}{
{
desc: "Message with no params",
message: "test",
params: nil,
expected: nil,
expectedMessage: "test",
expectedError: nil,
},
{
desc: "Message with no metadata",
message: "test %d",
params: []interface{}{43},
expected: nil,
expectedMessage: "test 43",
expectedError: nil,
},
{
desc: "Message with string metadata",
message: "test",
params: []interface{}{
map[string]string{
"foo": "bar",
},
},
expected: map[string]interface{}{
"foo": "bar",
},
expectedMessage: "test",
expectedError: nil,
},
{
desc: "Message with interface metadata",
message: "test",
params: []interface{}{
map[string]interface{}{
"foo": 42,
},
},
expected: map[string]interface{}{
"foo": 42,
},
expectedMessage: "test",
expectedError: nil,
},
{
desc: "map as format arg with metadata",
message: "foo: %v",
params: []interface{}{
map[string]string{
"bar": "bar",
},
map[string]string{
"foo": "foo",
},
},
expected: map[string]interface{}{
"bar": "bar",
"foo": "foo",
},
expectedMessage: "foo: map[bar:bar]",
expectedError: nil,
},
{
desc: "Message with special error case",
message: "test",
params: []interface{}{assert.AnError},
expected: map[string]interface{}(nil),
expectedMessage: "test",
expectedError: assert.AnError,
},
{
desc: "Message with special error case and metadata",
message: "test",
params: []interface{}{assert.AnError, map[string]interface{}{
"foo": "bar",
}},
expected: map[string]interface{}{
"foo": "bar",
},
expectedMessage: "test",
expectedError: assert.AnError,
},
{
desc: "Message with interpolated error",
message: "eaten by a grue: %v",
params: []interface{}{assert.AnError},
expected: map[string]interface{}(nil),
expectedMessage: "eaten by a grue: assert.AnError general error for testing",
expectedError: assert.AnError,
},
{
desc: "Message with error param and metadata",
message: "eaten by a grue: %v",
params: []interface{}{assert.AnError, map[string]interface{}{
"foo": "bar",
}},
expected: map[string]interface{}{
"foo": "bar",
},
expectedMessage: "eaten by a grue: assert.AnError general error for testing",
expectedError: assert.AnError,
},
{
desc: "Message with metadata nil explicitly",
message: "Foo %s",
params: []interface{}{"bar", nil, nil},
expected: nil,
expectedMessage: "Foo bar",
expectedError: nil,
},
{
desc: "Invalid: too many format params",
message: "Foo %s %s",
params: []interface{}{"bar"},
expected: nil,
expectedMessage: "Foo bar %!s(MISSING)",
expectedError: nil,
},
{
desc: "Invalid: too many format params with metadata",
message: "Foo %s %s %s",
params: []interface{}{"bar", map[string]interface{}{
"meta": "data",
}},
expected: map[string]interface{}{
"meta": "data",
},
expectedMessage: "Foo bar map[meta:data] %!s(MISSING)",
expectedError: nil,
},
{
desc: "Invalid: too many format params with error",
message: "Foo %s %s %s",
params: []interface{}{"bar", assert.AnError},
expected: map[string]interface{}(nil),
expectedMessage: "Foo bar assert.AnError general error for testing %!s(MISSING)",
expectedError: assert.AnError,
},
{
desc: "Invalid: too many format params with error and metadata",
message: "Foo %s %s %s %s",
params: []interface{}{"bar", assert.AnError, map[string]interface{}{
"meta": "data",
}},
expected: map[string]interface{}{
"meta": "data",
},
expectedMessage: "Foo bar assert.AnError general error for testing map[meta:data] %!s(MISSING)",
expectedError: assert.AnError,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
e := Eventf(ErrorSeverity, nil, tC.message, tC.params...)
assert.EqualValues(t, tC.expected, e.Metadata)
assert.Equal(t, tC.expectedMessage, e.Message)
assert.Equal(t, tC.expectedError, e.Error)
})
}
}
type testLogMetadataProvider map[string]string
func (p testLogMetadataProvider) LogMetadata() map[string]string {
return p
}
func TestEventfLogMetadataProvider(t *testing.T) {
param := testLogMetadataProvider{
"foo": "bar",
}
e := Eventf(CriticalSeverity, nil, "foo: %v", param)
expected := map[string]interface{}{
"foo": "bar",
}
assert.EqualValues(t, expected, e.Metadata)
}
func TestSerializeDeserialize(t *testing.T) {
event := Event{
Context: context.Background(),
Id: "test",
Timestamp: time.Now(),
Severity: ErrorSeverity,
Message: "foo",
OriginalMessage: "foo",
Metadata: map[string]interface{}{
"string": "value",
"number": float64(42),
},
Labels: map[string]string{
"label": "foo",
},
Error: errors.New("an error"),
}
out, err := json.Marshal(&event)
assert.NoError(t, err)
var undo Event
err = json.Unmarshal(out, &undo)
assert.NoError(t, err)
assert.Equal(t, event.Id, undo.Id)
assert.Equal(t, event.Severity, undo.Severity)
assert.Equal(t, event.Message, undo.Message)
assert.Equal(t, event.Metadata, undo.Metadata)
assert.Equal(t, event.Labels, undo.Labels)
// Note: go error types will not serialize by default, so we do not expect
// any data here.
assert.Equal(t, map[string]interface{}{}, undo.Error)
}
func TestSerializeDeserializeError(t *testing.T) {
type serializableError struct {
Message string `json:"message"`
}
event := Event{
Context: context.Background(),
Id: "test",
Timestamp: time.Now(),
Severity: ErrorSeverity,
Message: "foo",
OriginalMessage: "foo",
Metadata: map[string]interface{}{
"string": "value",
"number": float64(42),
},
Labels: map[string]string{
"label": "foo",
},
Error: serializableError{
Message: "test",
},
}
out, err := json.Marshal(&event)
assert.NoError(t, err)
var undo Event
err = json.Unmarshal(out, &undo)
assert.NoError(t, err)
assert.Equal(t, event.Id, undo.Id)
assert.Equal(t, event.Severity, undo.Severity)
assert.Equal(t, event.Message, undo.Message)
assert.Equal(t, event.Metadata, undo.Metadata)
assert.Equal(t, event.Labels, undo.Labels)
assert.Equal(t, map[string]interface{}{
"message": "test",
}, undo.Error)
}
func BenchmarkLogMetadataInterface(b *testing.B) {
for i := 0; i < b.N; i++ {
Eventf(ErrorSeverity, nil, "foo", map[string]interface{}{
"string": "foo",
"number": 42,
})
}
}
func BenchmarkLogMetadataStrings(b *testing.B) {
for i := 0; i < b.N; i++ {
Eventf(ErrorSeverity, nil, "foo", map[string]string{
"string": "foo",
"number": "42",
})
}
}
func BenchmarkLogMetadataInterpolated(b *testing.B) {
for i := 0; i < b.N; i++ {
Eventf(ErrorSeverity, nil, "foo %s %d", "foo", 42)
}
}