2015-07-08 19:07:39 +00:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-01-06 01:06:30 +00:00
|
|
|
|
2015-10-28 05:31:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-07-08 19:07:39 +00:00
|
|
|
)
|
|
|
|
|
2015-07-10 07:00:28 +00:00
|
|
|
type transportMock struct {
|
2015-07-08 19:07:39 +00:00
|
|
|
statusCode int
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTransportMock(statusCode int, body string) http.RoundTripper {
|
2015-07-10 07:00:28 +00:00
|
|
|
return &transportMock{
|
2015-07-08 19:07:39 +00:00
|
|
|
statusCode: statusCode,
|
|
|
|
body: body,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 07:00:28 +00:00
|
|
|
func (t *transportMock) RoundTrip(r *http.Request) (*http.Response, error) {
|
2015-07-08 19:07:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-06 01:06:30 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_indices", indicesExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_os", osExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_process", processExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_jvm", jvmExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_thread_pool", threadPoolExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_fs", fsExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_transport", transportExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_http", httpExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_breakers", breakersExpected, tags)
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|
2015-10-28 05:31:25 +00:00
|
|
|
|
|
|
|
func TestGatherClusterStats(t *testing.T) {
|
|
|
|
es := NewElasticsearch()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.ClusterHealth = true
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, clusterResponse)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
require.NoError(t, es.Gather(&acc))
|
|
|
|
|
2016-01-06 01:06:30 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health",
|
|
|
|
clusterHealthExpected,
|
|
|
|
map[string]string{"name": "elasticsearch_telegraf"})
|
2015-10-28 05:31:25 +00:00
|
|
|
|
2016-01-06 01:06:30 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v1IndexExpected,
|
|
|
|
map[string]string{"index": "v1"})
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v2IndexExpected,
|
|
|
|
map[string]string{"index": "v2"})
|
2015-10-28 05:31:25 +00:00
|
|
|
}
|