forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
papertrail_test.go
181 lines (158 loc) · 4.44 KB
/
papertrail_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
package papertrail
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
const (
contentType = "application/x-www-form-urlencoded"
)
func post(pt *PapertrailWebhook, contentType string, body string) *httptest.ResponseRecorder {
req, _ := http.NewRequest("POST", "/", strings.NewReader(body))
req.Header.Set("Content-Type", contentType)
w := httptest.NewRecorder()
pt.eventHandler(w, req)
return w
}
func TestWrongContentType(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
form := url.Values{}
form.Set("payload", sampleEventPayload)
data := form.Encode()
resp := post(pt, "", data)
require.Equal(t, http.StatusUnsupportedMediaType, resp.Code)
}
func TestMissingPayload(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
resp := post(pt, contentType, "")
require.Equal(t, http.StatusBadRequest, resp.Code)
}
func TestPayloadNotJSON(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
resp := post(pt, contentType, "payload={asdf]")
require.Equal(t, http.StatusBadRequest, resp.Code)
}
func TestPayloadInvalidJSON(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
resp := post(pt, contentType, `payload={"value": 42}`)
require.Equal(t, http.StatusBadRequest, resp.Code)
}
func TestEventPayload(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
form := url.Values{}
form.Set("payload", sampleEventPayload)
resp := post(pt, contentType, form.Encode())
require.Equal(t, http.StatusOK, resp.Code)
fields := map[string]interface{}{
"count": uint64(1),
}
tags1 := map[string]string{
"event": "Important stuff",
"host": "abc",
}
tags2 := map[string]string{
"event": "Important stuff",
"host": "def",
}
acc.AssertContainsTaggedFields(t, "papertrail", fields, tags1)
acc.AssertContainsTaggedFields(t, "papertrail", fields, tags2)
}
func TestCountPayload(t *testing.T) {
var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc}
form := url.Values{}
form.Set("payload", sampleCountPayload)
resp := post(pt, contentType, form.Encode())
require.Equal(t, http.StatusOK, resp.Code)
fields1 := map[string]interface{}{
"count": uint64(5),
}
fields2 := map[string]interface{}{
"count": uint64(3),
}
tags1 := map[string]string{
"event": "Important stuff",
"host": "arthur",
}
tags2 := map[string]string{
"event": "Important stuff",
"host": "ford",
}
acc.AssertContainsTaggedFields(t, "papertrail", fields1, tags1)
acc.AssertContainsTaggedFields(t, "papertrail", fields2, tags2)
}
const sampleEventPayload = `{
"events": [
{
"id": 7711561783320576,
"received_at": "2011-05-18T20:30:02-07:00",
"display_received_at": "May 18 20:30:02",
"source_ip": "208.75.57.121",
"source_name": "abc",
"source_id": 2,
"hostname": "abc",
"program": "CROND",
"severity": "Info",
"facility": "Cron",
"message": "message body"
},
{
"id": 7711562567655424,
"received_at": "2011-05-18T20:30:02-07:00",
"display_received_at": "May 18 20:30:02",
"source_ip": "208.75.57.120",
"source_name": "server1",
"source_id": 19,
"hostname": "def",
"program": "CROND",
"severity": "Info",
"facility": "Cron",
"message": "A short event"
}
],
"saved_search": {
"id": 42,
"name": "Important stuff",
"query": "cron OR server1",
"html_edit_url": "https://papertrailapp.com/searches/42/edit",
"html_search_url": "https://papertrailapp.com/searches/42"
},
"max_id": "7711582041804800",
"min_id": "7711561783320576"
}`
const sampleCountPayload = `{
"counts": [
{
"source_name": "arthur",
"source_id": 4,
"timeseries": {
"1453248895": 5
}
},
{
"source_name": "ford",
"source_id": 3,
"timeseries": {
"1453248927": 3
}
}
],
"saved_search": {
"id": 42,
"name": "Important stuff",
"query": "cron OR server1",
"html_edit_url": "https://papertrailapp.com/searches/42/edit",
"html_search_url": "https://papertrailapp.com/searches/42"
},
"max_id": "7711582041804800",
"min_id": "7711561783320576"
}`