2015-07-08 19:07:39 +00:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdb/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tranportMock struct {
|
|
|
|
statusCode int
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTransportMock(statusCode int, body string) http.RoundTripper {
|
|
|
|
return &tranportMock{
|
|
|
|
statusCode: statusCode,
|
|
|
|
body: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tranportMock) 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 TestElasticsearch(t *testing.T) {
|
|
|
|
es := NewElasticsearch()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
2015-07-09 17:51:12 +00:00
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, statsResponse)
|
2015-07-08 19:07:39 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
if err := es.Gather(&acc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
2015-07-09 16:41:16 +00:00
|
|
|
"cluster_name": "es-testcluster",
|
|
|
|
"node_attribute_master": "true",
|
|
|
|
"node_id": "SDFsfSDFsdfFSDSDfSFDSDF",
|
|
|
|
"node_name": "test.host.com",
|
|
|
|
"node_host": "test",
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range indicesExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:01:59 +00:00
|
|
|
|
|
|
|
for key, val := range osExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:06:30 +00:00
|
|
|
|
|
|
|
for key, val := range processExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:11:46 +00:00
|
|
|
|
|
|
|
for key, val := range jvmExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:18:24 +00:00
|
|
|
|
|
|
|
for key, val := range threadPoolExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:23:04 +00:00
|
|
|
|
|
|
|
for key, val := range networkExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:32:56 +00:00
|
|
|
|
|
|
|
for key, val := range fsExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:36:22 +00:00
|
|
|
|
|
|
|
for key, val := range transportExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-09 18:38:51 +00:00
|
|
|
for key, val := range httpExpected {
|
|
|
|
assert.NoError(t, acc.ValidateTaggedValue(key, val, tags))
|
|
|
|
}
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|