-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprotologbeat_test.go
81 lines (68 loc) · 2.4 KB
/
protologbeat_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
package main
import (
"testing"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/hartfordfive/protologbeat/config"
"github.com/hartfordfive/protologbeat/protolog"
"github.com/Graylog2/go-gelf/gelf"
"github.com/stretchr/testify/assert"
)
func TestGreylogReceive(t *testing.T) {
var logEntriesRecieved chan common.MapStr
var logEntriesErrors chan bool
logEntriesRecieved = make(chan common.MapStr, 1)
logEntriesErrors = make(chan bool, 1)
ll := protolog.NewLogListener(config.Config{EnableGelf: true, Port: 12000, DefaultEsLogType: "graylog"})
go func(logs chan common.MapStr, errs chan bool) {
ll.Start(logs, errs)
}(logEntriesRecieved, logEntriesErrors)
var event common.MapStr
gw, err := gelf.NewWriter("127.0.0.1:12000")
if err != nil {
t.Errorf("NewWriter: %s", err)
return
}
gw.CompressionType = gelf.CompressGzip
expectedVersion := "1.1"
expectedHost := "localhost"
expectedShort := "This is a test message for protologbeat"
expectedFull := "This is the full message expected for the test of gelf input."
expectedTs := float64(time.Now().Unix())
expectedLevel := int32(6)
expectedFacility := "local6"
exepectedType := "graylog"
if err := gw.WriteMessage(&gelf.Message{
Version: expectedVersion,
Host: expectedHost,
Short: expectedShort,
Full: expectedFull,
TimeUnix: expectedTs,
Level: expectedLevel,
Facility: expectedFacility,
Extra: map[string]interface{}{"type": exepectedType},
}); err != nil {
t.Errorf("Could not write message to GELF listener: %v", err)
return
}
for {
select {
case <-logEntriesErrors:
t.Errorf("Error receiving GELF format message")
return
case event = <-logEntriesRecieved:
if _, ok := event["@timestamp"]; !ok {
t.Errorf("Message missing timestamp field!: %v", event)
return
}
assert.Equal(t, event["gelf"].(map[string]interface{})["version"], expectedVersion, "Version should be the same")
assert.Equal(t, event["host"], expectedHost, "Host should be the same")
assert.Equal(t, event["short_message"], expectedShort, "Short message should be the same")
assert.Equal(t, event["full_message"], expectedFull, "Host should be the same")
assert.Equal(t, event["level"], expectedLevel, "Host should be the same")
assert.Equal(t, event["facility"], expectedFacility, "Host should be the same")
assert.Equal(t, event["type"], exepectedType, "Host should be the same")
return
}
}
}