-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgelf_test.go
272 lines (220 loc) · 6.06 KB
/
gelf_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
package gelf
import (
"bytes"
"encoding/binary"
"net"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/bmizerany/assert"
"github.com/lintianzhi/graylogd"
)
var validJson = `{
"version": "1.0",
"host": "localhost",
"timestamp": "123312312",
"facility": "Google Go",
"short_message": "Hello From Golang! :)"
}`
var inValidJson = `{
"_id": "23",
"version": "1.0",
"host": "localhost",
"timestamp": "123312312",
"facility": "Google Go",
"short_message": "Hello From Golang! :)"
}`
func Benchmark_LogWithShortMessage(b *testing.B) {
b.StopTimer()
g := New(Config{})
b.StartTimer()
for i := 0; i < b.N; i++ {
g.Log("Hello World")
}
}
func Benchmark_LogWithChunks(b *testing.B) {
b.StopTimer()
g := New(Config{
MaxChunkSizeWan: 10,
MaxChunkSizeLan: 10,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
g.Log("sdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddfsdfsdsdfdsfdsddddf")
}
}
func Test_New_itShouldUseDefaultConfigValuesIfNoOtherProvided(t *testing.T) {
g := New(Config{})
assert.Equal(t, g.Config.GraylogPort, defaultGraylogPort)
assert.Equal(t, g.Config.GraylogHostname, defaultGraylogHostname)
assert.Equal(t, g.Config.Connection, defaultConnection)
assert.Equal(t, g.Config.MaxChunkSizeWan, defaultMaxChunkSizeWan)
assert.Equal(t, g.Config.MaxChunkSizeLan, defaultMaxChunkSizeLan)
}
func Test_New_itShouldUseConfigValuesFromArguments(t *testing.T) {
g := New(Config{
GraylogPort: 80,
GraylogHostname: "foobarhost",
Connection: "wlan",
MaxChunkSizeWan: 42,
MaxChunkSizeLan: 1337,
})
assert.Equal(t, g.Config.GraylogPort, 80)
assert.Equal(t, g.Config.GraylogHostname, "foobarhost")
assert.Equal(t, g.Config.Connection, "wlan")
assert.Equal(t, g.Config.MaxChunkSizeWan, 42)
assert.Equal(t, g.Config.MaxChunkSizeLan, 1337)
}
func Test_ParseJson_itShouldReturnTypeMapStringInterface(t *testing.T) {
g := New(Config{})
res := g.ParseJson(validJson)
assert.Equal(t, reflect.TypeOf(res), reflect.TypeOf(make(map[string]interface{})))
}
func Test_ParseJson_itShouldParseTheStringToJson(t *testing.T) {
g := New(Config{})
res := g.ParseJson(validJson)
assert.Equal(t, res["version"], "1.0")
assert.Equal(t, res["host"], "localhost")
assert.Equal(t, res["timestamp"], "123312312")
assert.Equal(t, res["facility"], "Google Go")
assert.Equal(t, res["short_message"], "Hello From Golang! :)")
}
func Test_TestForForbiddenValues_itShouldReturnAnErrorIfForbiddenValuesAppear(t *testing.T) {
g := New(Config{})
res := g.ParseJson(inValidJson)
err := g.TestForForbiddenValues(res)
assert.NotEqual(t, nil, err)
}
func Test_TestSend_itShouldSendUdpPacketsToAServer(t *testing.T) {
g := New(Config{
GraylogPort: 55555,
})
done := make(chan int)
go Server(done, 55555, t)
g.Send([]byte("Hello Graylog"))
<-done
}
func Test_IntToBytes_itShouldCreateBytesFromInts(t *testing.T) {
g := New(Config{})
res := g.IntToBytes(20)
expected := make([]int32, 1)
expected[0] = 20
assert.Equal(t, bytes.Runes(res), expected)
}
func Test_GetChunksize_itShouldReturnTheValuesForWan(t *testing.T) {
g := New(Config{
Connection: "wan",
MaxChunkSizeWan: 42,
MaxChunkSizeLan: 1337,
})
res := g.GetChunksize()
assert.Equal(t, 42, res)
}
func Test_GetChunksize_itShouldReturnTheValuesForLan(t *testing.T) {
g := New(Config{
Connection: "lan",
MaxChunkSizeWan: 42,
MaxChunkSizeLan: 1337,
})
res := g.GetChunksize()
assert.Equal(t, 1337, res)
}
func Test_CreateChunkedMessages_itShouldStartWithTheMagicNumber(t *testing.T) {
g := New(Config{})
b := []byte("message")
buffer := bytes.NewBuffer(b)
packet := g.CreateChunkedMessage(1, 0, []byte("id"), buffer)
res := packet.String()
assert.Equal(t, strings.Contains(res, "\x1e\x0f"), true)
}
func Test_ChunkSize(t *testing.T) {
waitChan := make(chan bool, 1)
var realB []byte
daeCfg := graylogd.Config{
ListenAddr: "127.0.0.1:2211",
HandleRaw: func(b []byte) {
assert.Equal(t, realB, b)
waitChan <- true
},
HandleError: func(addr *net.UDPAddr, err error) {
t.Fatal("should be no error", err)
},
}
logd, err := graylogd.NewGraylogd(daeCfg)
assert.Equal(t, nil, err)
assert.Equal(t, nil, logd.Run())
defer logd.Close()
client := New(Config{
GraylogPort: 2211,
GraylogHostname: "127.0.0.1",
MaxChunkSizeWan: 1,
MaxChunkSizeLan: 1,
})
msgs := []string{
"11111",
"123jjdd",
}
for _, msg := range msgs {
realB = []byte(msg)
client.Log(msg)
select {
case <-waitChan:
case <-time.After(time.Second):
t.Fatal("message is not received")
}
}
}
func Test_CreateChunkedMessages_itShouldContainAnId(t *testing.T) {
g := New(Config{})
b := []byte("message")
buffer := bytes.NewBuffer(b)
packet := g.CreateChunkedMessage(1, 0, []byte("myId"), buffer)
res := packet.String()
assert.Equal(t, strings.Contains(res, "myId"), true)
}
func Test_CreateChunkedMessages_itShouldHaveTheIndex(t *testing.T) {
g := New(Config{})
b := []byte("message")
buffer := bytes.NewBuffer(b)
packet := g.CreateChunkedMessage(13, 42, []byte("id"), buffer)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, int8(13))
assert.Equal(t, bytes.Contains(packet.Bytes(), buf.Bytes()), true)
}
func Test_CreateChunkedMessages_itShouldHaveThePacketCount(t *testing.T) {
g := New(Config{})
b := []byte("message")
buffer := bytes.NewBuffer(b)
packet := g.CreateChunkedMessage(133, 42, []byte("id"), buffer)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, int8(42))
assert.Equal(t, bytes.Contains(packet.Bytes(), buf.Bytes()), true)
}
func Server(done chan<- int, port int, t *testing.T) {
laddr, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(port))
if err != nil {
panic(err)
}
buffer := make([]byte, 1024)
for {
conn, err := net.ListenUDP("udp", laddr)
if err != nil {
panic(err)
}
for {
n, err := conn.Read(buffer)
if err != nil {
panic(err)
}
conn.Close()
if string(buffer[:n]) != "Hello Graylog" {
t.Error("TestServer Error - String not Equal.")
}
done <- 0
return
}
conn.Close()
}
}