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
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:02:33 +00:00
|
|
|
func (t *transportMock) CancelRequest(_ *http.Request) {
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
func checkIsMaster(es *Elasticsearch, expected bool, t *testing.T) {
|
|
|
|
if es.isMaster != expected {
|
|
|
|
msg := fmt.Sprintf("IsMaster set incorrectly")
|
|
|
|
assert.Fail(t, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func checkNodeStatsResult(t *testing.T, acc *testutil.Accumulator) {
|
|
|
|
tags := map[string]string{
|
|
|
|
"cluster_name": "es-testcluster",
|
|
|
|
"node_attribute_master": "true",
|
|
|
|
"node_id": "SDFsfSDFsdfFSDSDfSFDSDF",
|
|
|
|
"node_name": "test.host.com",
|
|
|
|
"node_host": "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_indices", nodestatsIndicesExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_os", nodestatsOsExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_process", nodestatsProcessExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_jvm", nodestatsJvmExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_thread_pool", nodestatsThreadPoolExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_fs", nodestatsFsExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_transport", nodestatsTransportExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_http", nodestatsHttpExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_breakers", nodestatsBreakersExpected, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGather(t *testing.T) {
|
2016-06-22 15:23:49 +00:00
|
|
|
es := newElasticsearchWithClient()
|
2015-07-08 19:07:39 +00:00
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
2016-12-20 16:30:03 +00:00
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, nodeStatsResponse)
|
2015-07-08 19:07:39 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2017-04-24 18:13:26 +00:00
|
|
|
if err := acc.GatherError(es.Gather); err != nil {
|
2015-07-08 19:07:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
checkNodeStatsResult(t, &acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherNodeStats(t *testing.T) {
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, nodeStatsResponse)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
if err := es.gatherNodeStats("junk", &acc); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
checkNodeStatsResult(t, &acc)
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|
2015-10-28 05:31:25 +00:00
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
func TestGatherClusterHealth(t *testing.T) {
|
2016-06-22 15:23:49 +00:00
|
|
|
es := newElasticsearchWithClient()
|
2015-10-28 05:31:25 +00:00
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.ClusterHealth = true
|
2016-12-20 16:30:03 +00:00
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, clusterHealthResponse)
|
2015-10-28 05:31:25 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2016-12-20 16:30:03 +00:00
|
|
|
require.NoError(t, es.gatherClusterHealth("junk", &acc))
|
|
|
|
|
|
|
|
checkIsMaster(es, false, t)
|
2015-10-28 05:31:25 +00:00
|
|
|
|
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
|
|
|
}
|
2016-06-22 15:23:49 +00:00
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
func TestGatherClusterStatsMaster(t *testing.T) {
|
|
|
|
// This needs multiple steps to replicate the multiple calls internally.
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.ClusterStats = true
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
|
|
|
|
// first get catMaster
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, IsMasterResult)
|
|
|
|
require.NoError(t, es.setCatMaster("junk"))
|
|
|
|
|
|
|
|
IsMasterResultTokens := strings.Split(string(IsMasterResult), " ")
|
|
|
|
if es.catMasterResponseTokens[0] != IsMasterResultTokens[0] {
|
|
|
|
msg := fmt.Sprintf("catmaster is incorrect")
|
|
|
|
assert.Fail(t, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now get node status, which determines whether we're master
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
es.Local = true
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, nodeStatsResponse)
|
|
|
|
if err := es.gatherNodeStats("junk", &acc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIsMaster(es, true, t)
|
|
|
|
checkNodeStatsResult(t, &acc)
|
|
|
|
|
|
|
|
// now test the clusterstats method
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, clusterStatsResponse)
|
|
|
|
require.NoError(t, es.gatherClusterStats("junk", &acc))
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"cluster_name": "es-testcluster",
|
|
|
|
"node_name": "test.host.com",
|
|
|
|
"status": "red",
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_clusterstats_nodes", clusterstatsNodesExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_clusterstats_indices", clusterstatsIndicesExpected, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherClusterStatsNonMaster(t *testing.T) {
|
|
|
|
// This needs multiple steps to replicate the multiple calls internally.
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.ClusterStats = true
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
|
|
|
|
// first get catMaster
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, IsNotMasterResult)
|
|
|
|
require.NoError(t, es.setCatMaster("junk"))
|
|
|
|
|
|
|
|
IsNotMasterResultTokens := strings.Split(string(IsNotMasterResult), " ")
|
|
|
|
if es.catMasterResponseTokens[0] != IsNotMasterResultTokens[0] {
|
|
|
|
msg := fmt.Sprintf("catmaster is incorrect")
|
|
|
|
assert.Fail(t, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now get node status, which determines whether we're master
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
es.Local = true
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, nodeStatsResponse)
|
|
|
|
if err := es.gatherNodeStats("junk", &acc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure flag is clear so Cluster Stats would not be done
|
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
checkNodeStatsResult(t, &acc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:23:49 +00:00
|
|
|
func newElasticsearchWithClient() *Elasticsearch {
|
|
|
|
es := NewElasticsearch()
|
|
|
|
es.client = &http.Client{}
|
|
|
|
return es
|
|
|
|
}
|