This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
331 lines (297 loc) · 8.93 KB
/
output_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
package xlog
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"os"
"testing"
"time"
"github.com/rs/xlog"
"github.com/stretchr/testify/assert"
)
type testOutput struct {
err error
w chan map[string]interface{}
}
func newTestOutput() *testOutput {
return &testOutput{w: make(chan map[string]interface{}, 10)}
}
func newTestOutputErr(err error) *testOutput {
return &testOutput{w: make(chan map[string]interface{}, 10), err: err}
}
func (o *testOutput) Write(fields map[string]interface{}) (err error) {
o.w <- fields
return o.err
}
func (o *testOutput) reset() {
o.w = make(chan map[string]interface{}, 10)
}
func (o *testOutput) empty() bool {
select {
case <-o.w:
return false
default:
return true
}
}
func (o *testOutput) get() map[string]interface{} {
select {
case last := <-o.w:
return last
case <-time.After(2 * time.Second):
return nil
}
}
func TestOutputChannel(t *testing.T) {
o := newTestOutput()
oc := NewOutputChannel(o)
defer oc.Close()
oc.input <- xlog.F{"foo": "bar"}
assert.Equal(t, xlog.F{"foo": "bar"}, xlog.F(o.get()))
}
func TestOutputChannelError(t *testing.T) {
// Trigger error path
r, w := io.Pipe()
go func() {
critialLoggerMux.Lock()
defer critialLoggerMux.Unlock()
oldCritialLogger := critialLogger
critialLogger = log.New(w, "", 0)
o := newTestOutputErr(errors.New("some error"))
oc := NewOutputChannel(o)
oc.input <- xlog.F{"foo": "bar"}
o.get()
oc.Close()
critialLogger = oldCritialLogger
w.Close()
}()
b, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.Contains(t, string(b), "cannot write log message: some error")
}
func TestOutputChannelClose(t *testing.T) {
oc := NewOutputChannel(newTestOutput())
defer oc.Close()
assert.NotNil(t, oc.stop)
oc.Close()
assert.Nil(t, oc.stop)
oc.Close()
}
func TestDiscard(t *testing.T) {
assert.NoError(t, Discard.Write(xlog.F{}))
}
func TestFilterOutput(t *testing.T) {
o := newTestOutput()
f := FilterOutput{
Cond: func(fields map[string]interface{}) bool {
return fields["foo"] == "bar"
},
Output: o,
}
err := f.Write(xlog.F{"foo": "bar"})
assert.NoError(t, err)
assert.Equal(t, xlog.F{"foo": "bar"}, xlog.F(o.get()))
o.reset()
err = f.Write(xlog.F{"foo": "baz"})
assert.NoError(t, err)
assert.True(t, o.empty())
f.Output = nil
err = f.Write(xlog.F{"foo": "baz"})
assert.NoError(t, err)
}
func TestLevelOutput(t *testing.T) {
oInfo := newTestOutput()
oError := newTestOutput()
oFatal := newTestOutput()
oWarn := &testOutput{err: errors.New("some error")}
reset := func() {
oInfo.reset()
oError.reset()
oFatal.reset()
oWarn.reset()
}
l := LevelOutput{
Info: oInfo,
Error: oError,
Fatal: oFatal,
Warn: oWarn,
}
err := l.Write(xlog.F{"level": "fatal", "foo": "bar"})
assert.NoError(t, err)
assert.True(t, oInfo.empty())
assert.True(t, oError.empty())
assert.Equal(t, xlog.F{"level": "fatal", "foo": "bar"}, xlog.F(<-oFatal.w))
assert.True(t, oWarn.empty())
reset()
err = l.Write(xlog.F{"level": "error", "foo": "bar"})
assert.NoError(t, err)
assert.True(t, oInfo.empty())
assert.Equal(t, xlog.F{"level": "error", "foo": "bar"}, xlog.F(<-oError.w))
assert.True(t, oFatal.empty())
assert.True(t, oWarn.empty())
reset()
err = l.Write(xlog.F{"level": "info", "foo": "bar"})
assert.NoError(t, err)
assert.Equal(t, xlog.F{"level": "info", "foo": "bar"}, xlog.F(<-oInfo.w))
assert.True(t, oFatal.empty())
assert.True(t, oError.empty())
assert.True(t, oWarn.empty())
reset()
err = l.Write(xlog.F{"level": "warn", "foo": "bar"})
assert.EqualError(t, err, "some error")
assert.True(t, oInfo.empty())
assert.True(t, oError.empty())
assert.True(t, oFatal.empty())
assert.Equal(t, xlog.F{"level": "warn", "foo": "bar"}, xlog.F(<-oWarn.w))
reset()
err = l.Write(xlog.F{"level": "debug", "foo": "bar"})
assert.NoError(t, err)
assert.True(t, oInfo.empty())
assert.True(t, oError.empty())
assert.True(t, oFatal.empty())
assert.True(t, oWarn.empty())
reset()
err = l.Write(xlog.F{"foo": "bar"})
assert.NoError(t, err)
assert.True(t, oInfo.empty())
assert.True(t, oError.empty())
assert.True(t, oFatal.empty())
assert.True(t, oWarn.empty())
}
func TestSyslogOutput(t *testing.T) {
buf := bytes.NewBuffer(nil)
critialLoggerMux.Lock()
oldCritialLogger := critialLogger
critialLogger = log.New(buf, "", 0)
defer func() {
critialLogger = oldCritialLogger
critialLoggerMux.Unlock()
}()
m := NewSyslogOutput("udp", "127.0.0.1:1234", "mytag")
assert.IsType(t, LevelOutput{}, m)
assert.Panics(t, func() {
NewSyslogOutput("tcp", "an invalid host name", "mytag")
})
assert.Regexp(t, "syslog dial error: dial tcp:.*missing port in address.*", buf.String())
}
func TestRecorderOutput(t *testing.T) {
o := RecorderOutput{}
o.Write(xlog.F{"foo": "bar"})
o.Write(xlog.F{"bar": "baz"})
assert.Equal(t, []xlog.F{{"foo": "bar"}, {"bar": "baz"}}, o.Messages)
o.Reset()
assert.Equal(t, []xlog.F{}, o.Messages)
}
func TestNewConsoleOutput(t *testing.T) {
old := isTerminal
defer func() { isTerminal = old }()
isTerminal = func(w io.Writer) bool { return true }
c := NewConsoleOutput()
if assert.IsType(t, consoleOutput{}, c) {
assert.Equal(t, os.Stderr, c.(consoleOutput).w)
}
isTerminal = func(w io.Writer) bool { return false }
c = NewConsoleOutput()
if assert.IsType(t, logfmtOutput{}, c) {
assert.Equal(t, os.Stderr, c.(logfmtOutput).w)
}
}
func TestNewConsoleOutputW(t *testing.T) {
b := bytes.NewBuffer([]byte{})
c := NewConsoleOutputW(b, NewLogfmtOutput(b))
assert.IsType(t, logfmtOutput{}, c)
old := isTerminal
defer func() { isTerminal = old }()
isTerminal = func(w io.Writer) bool { return true }
c = NewConsoleOutputW(b, NewLogfmtOutput(b))
if assert.IsType(t, consoleOutput{}, c) {
assert.Equal(t, b, c.(consoleOutput).w)
}
}
func TestConsoleOutput(t *testing.T) {
buf := &bytes.Buffer{}
c := consoleOutput{w: buf}
err := c.Write(xlog.F{"message": "some message", "level": "info", "time": time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC), "foo": "bar"})
assert.NoError(t, err)
assert.Equal(t, "2000/01/02 03:04:05 \x1b[34mINFO\x1b[0m some message \x1b[32mfoo\x1b[0m=bar\n", buf.String())
buf.Reset()
err = c.Write(xlog.F{"message": "some debug", "level": "debug"})
assert.NoError(t, err)
assert.Equal(t, "\x1b[37mDEBU\x1b[0m some debug\n", buf.String())
buf.Reset()
err = c.Write(xlog.F{"message": "some warning", "level": "warn"})
assert.NoError(t, err)
assert.Equal(t, "\x1b[33mWARN\x1b[0m some warning\n", buf.String())
buf.Reset()
err = c.Write(xlog.F{"message": "some error", "level": "error"})
assert.NoError(t, err)
assert.Equal(t, "\x1b[31mERRO\x1b[0m some error\n", buf.String())
}
func TestLogfmtOutput(t *testing.T) {
buf := &bytes.Buffer{}
c := NewLogfmtOutput(buf)
err := c.Write(xlog.F{
"time": time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC),
"message": "some message",
"level": "info",
"string": "foo",
"null": nil,
"quoted": "needs \" quotes",
"err": errors.New("error"),
"errq": errors.New("error with \" quote"),
})
assert.NoError(t, err)
assert.Equal(t, "level=info message=\"some message\" time=\"2000-01-02 03:04:05 +0000 UTC\" err=error errq=\"error with \\\" quote\" null=null quoted=\"needs \\\" quotes\" string=foo\n", buf.String())
}
func TestJSONOutput(t *testing.T) {
buf := &bytes.Buffer{}
j := NewJSONOutput(buf)
err := j.Write(xlog.F{"message": "some message", "level": "info", "foo": "bar"})
assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\",\"level\":\"info\",\"message\":\"some message\"}\n", buf.String())
}
func TestLogstashOutput(t *testing.T) {
buf := &bytes.Buffer{}
o := NewLogstashOutput(buf)
err := o.Write(xlog.F{
"message": "some message",
"level": "info",
"time": time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC),
"file": "test.go:234",
"foo": "bar",
})
assert.NoError(t, err)
assert.Equal(t, "{\"@timestamp\":\"2000-01-02T03:04:05Z\",\"@version\":1,\"file\":\"test.go:234\",\"foo\":\"bar\",\"level\":\"INFO\",\"message\":\"some message\"}", buf.String())
}
func TestUIDOutput(t *testing.T) {
o := newTestOutput()
i := NewUIDOutput("id", o)
err := i.Write(xlog.F{"message": "some message", "level": "info", "foo": "bar"})
last := o.get()
assert.NoError(t, err)
assert.NotNil(t, last["id"])
assert.Len(t, last["id"], 20)
}
func TestTrimOutput(t *testing.T) {
o := newTestOutput()
i := NewTrimOutput(10, o)
err := i.Write(xlog.F{"short": "short", "long": "too long message", "number": 20})
last := o.get()
assert.NoError(t, err)
assert.Equal(t, "short", last["short"])
assert.Equal(t, "too long m", last["long"])
assert.Equal(t, 20, last["number"])
}
func TestTrimFieldsOutput(t *testing.T) {
o := newTestOutput()
i := NewTrimFieldsOutput([]string{"short", "trim", "number"}, 10, o)
err := i.Write(xlog.F{"short": "short", "long": "too long message", "trim": "too long message", "number": 20})
last := o.get()
assert.NoError(t, err)
assert.Equal(t, "short", last["short"])
assert.Equal(t, "too long m", last["trim"])
assert.Equal(t, "too long message", last["long"])
assert.Equal(t, 20, last["number"])
}