-
Notifications
You must be signed in to change notification settings - Fork 82
/
functional_test.go
153 lines (131 loc) · 4.36 KB
/
functional_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
package logrustash_test
import (
"bytes"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/bshuster-repo/logrus-logstash-hook"
"github.com/sirupsen/logrus"
)
func TestEntryIsNotChangedByLogstashFormatter(t *testing.T) {
buffer := bytes.NewBufferString("")
bufferOut := bytes.NewBufferString("")
log := logrus.New()
log.Out = bufferOut
hook := logrustash.New(buffer, logrustash.DefaultFormatter(logrus.Fields{"NICKNAME": ""}))
log.Hooks.Add(hook)
log.Info("hello world")
if !strings.Contains(buffer.String(), "NICKNAME\":") {
t.Errorf("expected logstash message to have '%s': %#v", "NICKNAME\":", buffer.String())
}
if strings.Contains(bufferOut.String(), "NICKNAME\":") {
t.Errorf("expected main logrus message to not have '%s': %#v", "NICKNAME\":", buffer.String())
}
}
func TestTimestampFormatKitchen(t *testing.T) {
log := logrus.New()
buffer := bytes.NewBufferString("")
hook := logrustash.New(buffer, logrustash.LogstashFormatter{
Formatter: &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "@timestamp",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.Kitchen,
},
Fields: logrus.Fields{"HOSTNAME": "localhost", "USERNAME": "root"},
})
log.Hooks.Add(hook)
log.Error("this is an error message!")
mTime := time.Now()
expected := fmt.Sprintf(`{"@timestamp":"%s","HOSTNAME":"localhost","USERNAME":"root","level":"error","message":"this is an error message!"}
`, mTime.Format(time.Kitchen))
if buffer.String() != expected {
t.Errorf("expected JSON to be '%#v' but got '%#v'", expected, buffer.String())
}
}
func TestTextFormatLogstash(t *testing.T) {
log := logrus.New()
buffer := bytes.NewBufferString("")
hook := logrustash.New(buffer, logrustash.LogstashFormatter{
Formatter: &logrus.TextFormatter{
TimestampFormat: time.Kitchen,
},
Fields: logrus.Fields{"HOSTNAME": "localhost", "USERNAME": "root"},
})
log.Hooks.Add(hook)
log.Warning("this is a warning message!")
mTime := time.Now()
expected := fmt.Sprintf(`time="%s" level=warning msg="this is a warning message!" HOSTNAME=localhost USERNAME=root
`, mTime.Format(time.Kitchen))
if buffer.String() != expected {
t.Errorf("expected JSON to be '%#v' but got '%#v'", expected, buffer.String())
}
}
// Github issue #39
func TestLogWithFieldsDoesNotOverrideHookFields(t *testing.T) {
log := logrus.New()
buffer := bytes.NewBufferString("")
hook := logrustash.New(buffer, logrustash.LogstashFormatter{
Formatter: &logrus.JSONFormatter{},
Fields: logrus.Fields{},
})
log.Hooks.Add(hook)
log.WithField("animal", "walrus").Info("bla")
attr := "\"animal\":\"walrus\""
if !strings.Contains(buffer.String(), attr) {
t.Errorf("expected to have '%s' in '%s'", attr, buffer.String())
}
buffer.Reset()
log.Info("hahaha")
if strings.Contains(buffer.String(), attr) {
t.Errorf("expected not to have '%s' in '%s'", attr, buffer.String())
}
}
func TestDefaultFormatterNotOverrideMyLogstashFieldsValues(t *testing.T) {
formatter := logrustash.DefaultFormatter(logrus.Fields{"@version": "2", "type": "mylogs"})
dataBytes, err := formatter.Format(&logrus.Entry{Data: logrus.Fields{}})
if err != nil {
t.Errorf("expected Format to not return error: %s", err)
}
expected := []string{
`"@version":"2"`,
`"type":"mylogs"`,
}
for _, expField := range expected {
if !strings.Contains(string(dataBytes), expField) {
t.Errorf("expected '%s' to be in '%s'", expField, string(dataBytes))
}
}
}
func TestDefaultFormatterLogstashFields(t *testing.T) {
formatter := logrustash.DefaultFormatter(logrus.Fields{})
dataBytes, err := formatter.Format(&logrus.Entry{Data: logrus.Fields{}})
if err != nil {
t.Errorf("expected Format to not return error: %s", err)
}
expected := []string{
`"@version":"1"`,
`"type":"log"`,
}
for _, expField := range expected {
if !strings.Contains(string(dataBytes), expField) {
t.Errorf("expected '%s' to be in '%s'", expField, string(dataBytes))
}
}
}
// UDP will never fail because it's connectionless.
// That's why I am using it for this integration tests just to make sure
// it won't fail when a data is written.
func TestUDPWritter(t *testing.T) {
log := logrus.New()
conn, err := net.Dial("udp", ":8282")
if err != nil {
t.Errorf("expected Dial to not return error: %s", err)
}
hook := logrustash.New(conn, &logrus.JSONFormatter{})
log.Hooks.Add(hook)
log.Info("this is an information message")
}