forked from dcos/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
burrow_test.go
285 lines (237 loc) · 6.56 KB
/
burrow_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
274
275
276
277
278
279
280
281
282
283
284
285
package burrow
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
// remap uri to json file, eg: /v3/kafka -> ./testdata/v3_kafka.json
func getResponseJSON(requestURI string) ([]byte, int) {
uri := strings.TrimLeft(requestURI, "/")
mappedFile := strings.Replace(uri, "/", "_", -1)
jsonFile := fmt.Sprintf("./testdata/%s.json", mappedFile)
code := 200
_, err := os.Stat(jsonFile)
if err != nil {
code = 404
jsonFile = "./testdata/error.json"
}
// respond with file
b, _ := ioutil.ReadFile(jsonFile)
return b, code
}
// return mocked HTTP server
func getHTTPServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}))
}
// return mocked HTTP server with basic auth
func getHTTPServerBasicAuth() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
username, password, authOK := r.BasicAuth()
if authOK == false {
http.Error(w, "Not authorized", 401)
return
}
if username != "test" && password != "test" {
http.Error(w, "Not authorized", 401)
return
}
// ok, continue
body, code := getResponseJSON(r.RequestURI)
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}))
}
// test burrow_topic measurement
func TestBurrowTopic(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{Servers: []string{s.URL}}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
fields := []map[string]interface{}{
// topicA
{"offset": int64(459178195)},
{"offset": int64(459178022)},
{"offset": int64(456491598)},
}
tags := []map[string]string{
// topicA
{"cluster": "clustername1", "topic": "topicA", "partition": "0"},
{"cluster": "clustername1", "topic": "topicA", "partition": "1"},
{"cluster": "clustername1", "topic": "topicA", "partition": "2"},
}
require.Empty(t, acc.Errors)
require.Equal(t, true, acc.HasMeasurement("burrow_topic"))
for i := 0; i < len(fields); i++ {
acc.AssertContainsTaggedFields(t, "burrow_topic", fields[i], tags[i])
}
}
// test burrow_partition measurement
func TestBurrowPartition(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
fields := []map[string]interface{}{
{
"status": "OK",
"status_code": 1,
"lag": int64(0),
"offset": int64(431323195),
"timestamp": int64(1515609490008),
},
{
"status": "OK",
"status_code": 1,
"lag": int64(0),
"offset": int64(431322962),
"timestamp": int64(1515609490008),
},
{
"status": "OK",
"status_code": 1,
"lag": int64(0),
"offset": int64(428636563),
"timestamp": int64(1515609490008),
},
}
tags := []map[string]string{
{"cluster": "clustername1", "group": "group1", "topic": "topicA", "partition": "0", "owner": "kafka1"},
{"cluster": "clustername1", "group": "group1", "topic": "topicA", "partition": "1", "owner": "kafka2"},
{"cluster": "clustername1", "group": "group1", "topic": "topicA", "partition": "2", "owner": "kafka3"},
}
require.Empty(t, acc.Errors)
require.Equal(t, true, acc.HasMeasurement("burrow_partition"))
for i := 0; i < len(fields); i++ {
acc.AssertContainsTaggedFields(t, "burrow_partition", fields[i], tags[i])
}
}
// burrow_group
func TestBurrowGroup(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
fields := []map[string]interface{}{
{
"status": "OK",
"status_code": 1,
"partition_count": 3,
"total_lag": int64(0),
"lag": int64(0),
"offset": int64(431323195 + 431322962 + 428636563),
"timestamp": int64(1515609490008),
},
}
tags := []map[string]string{
{"cluster": "clustername1", "group": "group1"},
}
require.Empty(t, acc.Errors)
require.Equal(t, true, acc.HasMeasurement("burrow_group"))
for i := 0; i < len(fields); i++ {
acc.AssertContainsTaggedFields(t, "burrow_group", fields[i], tags[i])
}
}
// collect from multiple servers
func TestMultipleServers(t *testing.T) {
s1 := getHTTPServer()
defer s1.Close()
s2 := getHTTPServer()
defer s2.Close()
plugin := &burrow{
Servers: []string{s1.URL, s2.URL},
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.Exactly(t, 14, len(acc.Metrics))
require.Empty(t, acc.Errors)
}
// collect multiple times
func TestMultipleRuns(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
}
for i := 0; i < 4; i++ {
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.Exactly(t, 7, len(acc.Metrics))
require.Empty(t, acc.Errors)
}
}
// collect from http basic auth server
func TestBasicAuthConfig(t *testing.T) {
s := getHTTPServerBasicAuth()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
Username: "test",
Password: "test",
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.Exactly(t, 7, len(acc.Metrics))
require.Empty(t, acc.Errors)
}
// collect from whitelisted clusters
func TestFilterClusters(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
ClustersInclude: []string{"wrongname*"}, // clustername1 -> no match
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
// no match by cluster
require.Exactly(t, 0, len(acc.Metrics))
require.Empty(t, acc.Errors)
}
// collect from whitelisted groups
func TestFilterGroups(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
GroupsInclude: []string{"group?"}, // group1 -> match
TopicsExclude: []string{"*"}, // exclude all
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.Exactly(t, 4, len(acc.Metrics))
require.Empty(t, acc.Errors)
}
// collect from whitelisted topics
func TestFilterTopics(t *testing.T) {
s := getHTTPServer()
defer s.Close()
plugin := &burrow{
Servers: []string{s.URL},
TopicsInclude: []string{"topic?"}, // topicA -> match
GroupsExclude: []string{"*"}, // exclude all
}
acc := &testutil.Accumulator{}
plugin.Gather(acc)
require.Exactly(t, 3, len(acc.Metrics))
require.Empty(t, acc.Errors)
}