forked from fuyufjh/splunk-hec-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_test.go
91 lines (82 loc) · 2.39 KB
/
cluster_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
package hec
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testSplunkURLs = []string{"http://127.0.0.1:8088", "http://localhost:8088"}
)
func TestCluster_WriteEvent(t *testing.T) {
event := &Event{
Index: String("main"),
Source: String("test-hec-raw"),
SourceType: String("manual"),
Host: String("localhost"),
Time: String("1485237827.123"),
Event: String("hello, world"),
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"text":"Success","code":0}`))
}))
c := NewCluster([]string{ts.URL}, testSplunkToken)
c.SetHTTPClient(testHttpClient)
err := c.WriteEvent(event)
assert.NoError(t, err)
}
func TestCluster_WriteEventBatch(t *testing.T) {
eventBatches := [][]*Event{
{
{Event: "event one"},
{Event: "event two"},
},
{
{Event: "event foo"},
{Event: "event bar"},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"text":"Success","code":0}`))
}))
c := NewCluster([]string{ts.URL}, testSplunkToken)
c.SetHTTPClient(testHttpClient)
for _, batch := range eventBatches {
err := c.WriteBatch(batch)
assert.NoError(t, err)
}
}
func TestCluster_WriteEventRaw(t *testing.T) {
eventBlocks := []string{
`2017-01-24T06:07:10.488Z Raw event one
2017-01-24T06:07:12.434Z Raw event two`,
`2017-01-24T06:07:10.488Z Raw event foo
2017-01-24T06:07:12.434Z Raw event bar`,
}
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 := NewCluster([]string{ts.URL}, testSplunkToken)
c.SetHTTPClient(testHttpClient)
for _, block := range eventBlocks {
err := c.WriteRaw(strings.NewReader(block), &metadata)
assert.NoError(t, err)
}
}
func TestCluster_Retrying(t *testing.T) {
event := &Event{Event: "test retrying"}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"text":"Success","code":0}`))
}))
partlyBrokenUrls := []string{ts.URL, "http://example.com:8088", "http://example.com:88"}
c := NewCluster(partlyBrokenUrls, testSplunkToken)
c.SetHTTPClient(testHttpClient)
for i := 0; i < 5; i++ {
err := c.WriteEvent(event)
assert.NoError(t, err)
}
}