forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kibana_test.go
66 lines (55 loc) · 1.38 KB
/
kibana_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
package kibana
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/influxdata/telegraf/testutil"
)
func defaultTags() map[string]string {
return map[string]string{
"name": "my-kibana",
"source": "example.com:5601",
"version": "6.3.2",
"status": "green",
}
}
type transportMock struct {
statusCode int
body string
}
func newTransportMock(statusCode int, body string) http.RoundTripper {
return &transportMock{
statusCode: statusCode,
body: body,
}
}
func (t *transportMock) RoundTrip(r *http.Request) (*http.Response, error) {
res := &http.Response{
Header: make(http.Header),
Request: r,
StatusCode: t.statusCode,
}
res.Header.Set("Content-Type", "application/json")
res.Body = ioutil.NopCloser(strings.NewReader(t.body))
return res, nil
}
func checkKibanaStatusResult(t *testing.T, acc *testutil.Accumulator) {
tags := defaultTags()
acc.AssertContainsTaggedFields(t, "kibana", kibanaStatusExpected, tags)
}
func TestGather(t *testing.T) {
ks := newKibanahWithClient()
ks.Servers = []string{"http://example.com:5601"}
ks.client.Transport = newTransportMock(http.StatusOK, kibanaStatusResponse)
var acc testutil.Accumulator
if err := acc.GatherError(ks.Gather); err != nil {
t.Fatal(err)
}
checkKibanaStatusResult(t, &acc)
}
func newKibanahWithClient() *Kibana {
ks := NewKibana()
ks.client = &http.Client{}
return ks
}