-
Notifications
You must be signed in to change notification settings - Fork 4
/
logrus_logger_test.go
293 lines (243 loc) · 9.26 KB
/
logrus_logger_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
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package bark_test
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"testing"
"errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-common/bark"
)
// Create a logrus logger that writes its out output to a buffer for inspection
func getLogrusLogger() (*logrus.Logger, *bytes.Buffer) {
logrusLogger := logrus.New()
buffer := &bytes.Buffer{}
logrusLogger.Out = buffer
logrusLogger.Formatter = new(logrus.JSONFormatter)
logrusLogger.Level = logrus.DebugLevel
return logrusLogger, buffer
}
// Create a bark wrapper for a logrus logger backed by a buffer
func getBarkLogger() (bark.Logger, *bytes.Buffer) {
logrusLogger, buffer := getLogrusLogger()
return bark.NewLoggerFromLogrus(logrusLogger), buffer
}
// Extract map of keys and values from raw json data in buffer
func parseLogBytes(logBytes []byte) map[string]interface{} {
var unmarshalledData interface{}
json.Unmarshal(logBytes, &unmarshalledData)
if unmarshalledData == nil {
return nil
}
return unmarshalledData.(map[string]interface{})
}
// Validate bark output against logrus output
func validateOutput(t *testing.T, barkBytes []byte, logrusBytes []byte) {
barkMap := parseLogBytes(barkBytes)
require.NotNil(t, barkMap, "Should be able to parse bark output as JSON")
logrusMap := parseLogBytes(logrusBytes)
require.NotNil(t, logrusMap, "Should be able to parse logrus output as JSON")
// Make sure we're checking at least the fields we expect
minFields := []string{"time", "level", "msg"}
for _, key := range minFields {
_, ok := logrusMap[key]
require.True(t, ok, "Logrus missing required field: %s", key)
}
// Make sure bark output has everything logrus does
for key, logrusValue := range logrusMap {
barkValue, ok := barkMap[key]
require.True(t, ok)
// Can't mock time to logrus, so have to skip it
if key != "time" {
require.Equal(t, logrusValue, barkValue, "Field of outputs of output should match logrus")
}
}
}
func TestInfo(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Info("info1", "info2")
logrusLogger.Info("info1", "info2")
})
}
func TestInfof(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Infof("infof1%s", "infof2")
logrusLogger.Infof("infof1%s", "infof2")
})
}
func TestError(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Error("error1", "error2")
logrusLogger.Error("error1", "error2")
})
}
func TestErrorf(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Errorf("errorf1%s", "errorf2")
logrusLogger.Errorf("errorf1%s", "errorf2")
})
}
func TestWarn(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Warn("warn1", "warn2")
logrusLogger.Warn("warn1", "warn2")
})
}
func TestWarnf(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.Warnf("warnf1%s", "warnf2")
logrusLogger.Warnf("warnf1%s", "warnf2")
})
}
func TestWithField(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.WithField("key", "value").Info("withfield")
logrusLogger.WithField("key", "value").Info("withfield")
})
}
func TestWithFields(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.WithFields(bark.Fields{"k1": "v1", "k2": "v2"}).Info("withfields")
logrusLogger.WithFields(logrus.Fields{"k1": "v1", "k2": "v2"}).Info("withfields")
})
}
func TestWithError(t *testing.T) {
err := errors.New("test error")
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
barkLogger.WithError(err).Info("witherror")
logrusLogger.WithError(err).Info("witherror")
})
}
func TestGetFields(t *testing.T) {
// Plain logger
logger := bark.NewLoggerFromLogrus(logrus.New())
require.Equal(t, logger.Fields(), bark.Fields(nil))
// Add nil, don't crash
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithFields(nil)
require.Equal(t, logger.Fields(), bark.Fields(nil))
// One field added
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithField("foo", "bar")
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar"})
// Two fields added at once
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump"})
// One then one
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithField("foo", "bar")
logger = logger.WithField("baz", "bump")
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump"})
// Two then one
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
logger = logger.WithField("x", "y")
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "x": "y"})
// One then two
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithField("x", "y")
logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "x": "y"})
// Two then two
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithFields(bark.Fields{"foo": "bar", "baz": "bump"})
logger = logger.WithFields(bark.Fields{"a": "b", "c": "d"})
require.Equal(t, logger.Fields(), bark.Fields{"foo": "bar", "baz": "bump", "a": "b", "c": "d"})
// Add empty map
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithFields(bark.Fields{})
require.Equal(t, logger.Fields(), bark.Fields{})
// Add error
err := errors.New("test error")
logger = bark.NewLoggerFromLogrus(logrus.New())
logger = logger.WithError(err)
require.Equal(t, logger.Fields(), bark.Fields{logrus.ErrorKey: err})
}
func doPanic(t *testing.T, panicker func(...interface{})) {
defer func() {
if r := recover(); r != nil {
return
}
}()
panicker("panic", "panic")
t.Error("Expected to panic but execution did not stop")
}
func TestPanic(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
doPanic(t, barkLogger.Panic)
doPanic(t, logrusLogger.Panic)
})
}
func doPanicf(t *testing.T, panicf func(string, ...interface{})) {
defer func() {
if r := recover(); r != nil {
return
}
}()
panicf("panicf%s", "panicf")
t.Error("Expected to panic but execution did not stop")
}
func TestPanicf(t *testing.T) {
logAndValidate(t, func(barkLogger bark.Logger, logrusLogger *logrus.Logger) {
doPanicf(t, barkLogger.Panicf)
doPanicf(t, logrusLogger.Panicf)
})
}
// Main test driver: create loggers backed by buffers, drive operations on both, compare results
func logAndValidate(t *testing.T, driver func(barkLogger bark.Logger, logrusLogger *logrus.Logger)) {
barkLogger, barkBuffer := getBarkLogger()
logrusLogger, logrusBuffer := getLogrusLogger()
driver(barkLogger, logrusLogger)
validateOutput(t, barkBuffer.Bytes(), logrusBuffer.Bytes())
}
// Fatal tests are special: have to execute a command and confirm that it exits
func execFatalTool(t *testing.T, how string) []byte {
helperBinary := "./testhelp/fatal"
_, err := os.Stat(helperBinary)
require.NoError(t, err, "Helper binary should exist: run 'make test'!")
cmd := exec.Command(helperBinary, how)
output, err := cmd.CombinedOutput()
if assert.Error(t, err, "Process should exit with an error") {
_, ok := err.(*exec.ExitError)
assert.True(t, ok, "Process should exit with an ExitError")
}
require.Contains(t, string(output), "fatal error")
return output
}
func TestFatal(t *testing.T) {
logrusStderr := execFatalTool(t, "logrus.Fatal")
barkStderr := execFatalTool(t, "bark.Fatal")
validateOutput(t, barkStderr, logrusStderr)
}
func TestFatalf(t *testing.T) {
logrusStderr := execFatalTool(t, "logrus.Fatalf")
barkStderr := execFatalTool(t, "bark.Fatalf")
validateOutput(t, barkStderr, logrusStderr)
}
func TestNopLogger(t *testing.T) {
assert.NotPanics(t, func() {
logger := bark.NewNopLogger()
logger.Info("hello")
})
}