-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
273 lines (247 loc) · 7.18 KB
/
client_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
package hec
import (
"compress/gzip"
"crypto/tls"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
testSplunkURL = "http://localhost:8088"
testSplunkToken = "00000000-0000-0000-0000-000000000000"
)
var testHttpClient *http.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 100 * time.Millisecond,
}
func jsonEndpoint(t *testing.T, compression string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
failed := false
input := make(map[string]interface{})
content := r.Body
if compression == "gzip" {
var err error
content, err = gzip.NewReader(r.Body)
if err != nil {
t.Errorf("Unexpected error in gzip: %v", err)
}
header := r.Header.Get("Content-Encoding")
if header != "gzip" {
t.Errorf("Content-Encoding header wasn't sent for gzip")
}
}
j := json.NewDecoder(content)
err := j.Decode(&input)
if err != nil {
t.Errorf("Decoding JSON: %v", err)
failed = true
}
requiredFields := []string{"event"}
allowedFields := map[string]struct{}{
"channel": struct{}{},
"event": struct{}{},
"fields": struct{}{},
"host": struct{}{},
"index": struct{}{},
"source": struct{}{},
"sourcetype": struct{}{},
"time": struct{}{},
}
for _, f := range requiredFields {
if _, ok := input[f]; !ok {
t.Errorf("Required field %q missing in %v", f, input)
}
}
for f := range input {
if _, ok := allowedFields[f]; !ok {
t.Errorf("Unexpected field %q in %v", f, input)
}
}
if failed {
w.WriteHeader(400)
w.Write([]byte(`{"text": "Error processing event", "code": 90}`))
} else {
w.Write([]byte(`{"text":"Success","code":0}`))
}
})
}
func TestHEC_WriteEvent(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
event := &Event{
Index: String("main"),
Source: String("test-hec-raw"),
SourceType: String("manual"),
Host: String("localhost"),
Time: String("1485237827.123"),
Event: "hello, world",
}
ts := httptest.NewServer(jsonEndpoint(t, compression))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetCompression(compression)
err := c.WriteEvent(event)
assert.NoError(t, err)
}
}
func TestHEC_WriteEventServerFailure(t *testing.T) {
event := &Event{
Index: String("main"),
Source: String("test-hec-raw"),
SourceType: String("manual"),
Host: String("localhost"),
Time: String("1485237827.123"),
Event: "hello, world",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Write([]byte(`{"text":"Data channel is missing","code":10}`))
}))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
err := c.WriteEvent(event)
assert.Error(t, err)
}
func TestHEC_WriteObjectEvent(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
event := &Event{
Index: String("main"),
Source: String("test-hec-raw"),
SourceType: String("manual"),
Host: String("localhost"),
Time: String("1485237827.123"),
Event: map[string]interface{}{
"str": "hello",
"time": time.Now(),
},
}
ts := httptest.NewServer(jsonEndpoint(t, compression))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetCompression(compression)
err := c.WriteEvent(event)
assert.NoError(t, err)
}
}
func TestHEC_WriteLongEvent(t *testing.T) {
event := &Event{
Index: String("main"),
Source: String("test-hec-raw"),
SourceType: String("manual"),
Host: String("localhost"),
Time: String("1485237827.123"),
Event: "hello, world",
}
ts := httptest.NewServer(jsonEndpoint(t, ""))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetMaxContentLength(20) // less than full event
err := c.WriteEvent(event)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "too long")
}
func TestHEC_WriteEventBatch(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
events := []*Event{
{Event: "event one"},
{Event: "event two"},
}
ts := httptest.NewServer(jsonEndpoint(t, compression))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetCompression(compression)
err := c.WriteBatch(events)
assert.NoError(t, err)
}
}
func TestHEC_WriteLongEventBatch(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
events := []*Event{
{Event: "event one"},
{Event: "event two"},
}
ts := httptest.NewServer(jsonEndpoint(t, compression))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetMaxContentLength(25)
c.SetCompression(compression)
err := c.WriteBatch(events)
assert.NoError(t, err)
}
}
func TestHEC_WriteEventRaw(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
events := `2017-01-24T06:07:10.488Z Raw event one
2017-01-24T06:07:12.434Z Raw event two`
metadata := EventMetadata{
Source: String("test-hec-raw"),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"text":"Success","code":0}`))
}))
c := NewClient(ts.URL, testSplunkToken)
c.SetHTTPClient(testHttpClient)
c.SetCompression(compression)
err := c.WriteRaw(strings.NewReader(events), &metadata)
assert.NoError(t, err)
}
}
func TestHEC_WriteLongEventRaw(t *testing.T) {
for _, compression := range []string{"", "gzip"} {
events := `2017-01-24T06:07:10.488Z Raw event one
2017-01-24T06:07:12.434Z Raw event two`
metadata := EventMetadata{
Source: String("test-hec-raw"),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"text":"Success","code":0}`))
}))
c := NewClient(ts.URL, testSplunkToken)
c.SetMaxContentLength(40)
c.SetHTTPClient(testHttpClient)
c.SetCompression(compression)
err := c.WriteRaw(strings.NewReader(events), &metadata)
assert.NoError(t, err)
}
}
func TestHEC_WriteRawFailure(t *testing.T) {
events := `2017-01-24T06:07:10.488Z Raw event one
2017-01-24T06:07:12.434Z Raw event two`
metadata := EventMetadata{
Source: String("test-hec-raw"),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Write([]byte(`{"text":"Oh no","code":90}`))
}))
c := NewClient(ts.URL, testSplunkToken)
c.SetMaxContentLength(40)
c.SetHTTPClient(testHttpClient)
err := c.WriteRaw(strings.NewReader(events), &metadata)
assert.Error(t, err)
}
func TestBreakStream(t *testing.T) {
text := "This is line A\nThis is line B" // length of every line is 14
getCountFunc := func(counter *int) func(chunk []byte) error {
// returned function adds count of all character except "\n"
return func(chunk []byte) error {
for _, b := range chunk {
if b != '\n' {
*counter++
}
}
return nil
}
}
for _, max := range []int{13, 14, 15, 28, 5, 30} {
var counter int = 0
err := breakStream(strings.NewReader(text), max, getCountFunc(&counter))
assert.NoError(t, err)
assert.Equal(t, 28, counter)
}
}