forked from looplab/logspout-logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstash_test.go
162 lines (122 loc) · 3.29 KB
/
logstash_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
package logstash
import (
"encoding/json"
"net"
"testing"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/gliderlabs/logspout/router"
"github.com/stretchr/testify/assert"
)
var res string
type MockConn struct {
}
func (m MockConn) Close() error {
return nil
}
func (m MockConn) Read(b []byte) (n int, err error) {
return 0, nil
}
func (m MockConn) Write(b []byte) (n int, err error) {
res = string(b)
return 0, nil
}
func (m MockConn) LocalAddr() net.Addr {
return nil
}
func (m MockConn) RemoteAddr() net.Addr {
return nil
}
func (m MockConn) SetDeadline(t time.Time) error {
return nil
}
func (m MockConn) SetReadDeadline(t time.Time) error {
return nil
}
func (m MockConn) SetWriteDeadline(t time.Time) error {
return nil
}
func TestStreamNotJson(t *testing.T) {
assert := assert.New(t)
conn := MockConn{}
adapter := LogstashAdapter{
route: new(router.Route),
conn: conn,
}
assert.NotNil(adapter)
logstream := make(chan *router.Message)
containerConfig := docker.Config{}
containerConfig.Image = "image"
containerConfig.Hostname = "hostname"
container := docker.Container{}
container.Name = "name"
container.ID = "ID"
container.Config = &containerConfig
str := `foo bananas`
message := router.Message{
Container: &container,
Source: "FOOOOO",
Data: str,
Time: time.Now(),
}
go func() {
logstream <- &message
close(logstream)
}()
adapter.Stream(logstream)
var data map[string]interface{}
err := json.Unmarshal([]byte(res), &data)
assert.Nil(err)
assert.Equal("foo bananas", data["message"])
var dockerInfo map[string]interface{}
dockerInfo = data["docker"].(map[string]interface{})
assert.Equal("name", dockerInfo["name"])
assert.Equal("ID", dockerInfo["id"])
assert.Equal("image", dockerInfo["image"])
assert.Equal("hostname", dockerInfo["hostname"])
}
func TestStreamJson(t *testing.T) {
assert := assert.New(t)
conn := MockConn{}
adapter := LogstashAdapter{
route: new(router.Route),
conn: conn,
}
assert.NotNil(adapter)
logstream := make(chan *router.Message)
containerConfig := docker.Config{}
containerConfig.Image = "image"
containerConfig.Hostname = "hostname"
container := docker.Container{}
container.Name = "name"
container.ID = "ID"
container.Config = &containerConfig
str := `{ "remote_user": "-", "body_bytes_sent": "25", "request_time": "0.821", "status": "200", "request_method": "POST", "http_referrer": "-", "http_user_agent": "-" }`
message := router.Message{
Container: &container,
Source: "FOOOOO",
Data: str,
Time: time.Now(),
}
go func() {
logstream <- &message
close(logstream)
}()
adapter.Stream(logstream)
var data map[string]interface{}
err := json.Unmarshal([]byte(res), &data)
assert.Nil(err)
assert.Equal("-", data["remote_user"])
assert.Equal("25", data["body_bytes_sent"])
assert.Equal("0.821", data["request_time"])
assert.Equal("200", data["status"])
assert.Equal("POST", data["request_method"])
assert.Equal("-", data["http_referrer"])
assert.Equal("-", data["http_user_agent"])
var dockerInfo map[string]interface{}
dockerInfo = data["docker"].(map[string]interface{})
assert.Equal("name", dockerInfo["name"])
assert.Equal("ID", dockerInfo["id"])
assert.Equal("image", dockerInfo["image"])
assert.Equal("hostname", dockerInfo["hostname"])
}