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
|
|
|
)
|
|
|
|
|
2017-10-06 23:16:32 +00:00
|
|
|
func defaultTags() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"cluster_name": "es-testcluster",
|
|
|
|
"node_attribute_master": "true",
|
|
|
|
"node_id": "SDFsfSDFsdfFSDSDfSFDSDF",
|
|
|
|
"node_name": "test.host.com",
|
|
|
|
"node_host": "test",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:16:32 +00:00
|
|
|
func checkNodeStatsResult(t *testing.T, acc *testutil.Accumulator) {
|
|
|
|
tags := defaultTags()
|
2016-12-20 16:30:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:16:32 +00:00
|
|
|
func TestGatherIndividualStats(t *testing.T) {
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.NodeStats = []string{"jvm", "process"}
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, nodeStatsResponseJVMProcess)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
if err := acc.GatherError(es.Gather); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
|
|
|
|
tags := defaultTags()
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_indices", nodestatsIndicesExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_os", nodestatsOsExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_process", nodestatsProcessExpected, tags)
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_jvm", nodestatsJvmExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_thread_pool", nodestatsThreadPoolExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_fs", nodestatsFsExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_transport", nodestatsTransportExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_http", nodestatsHttpExpected, tags)
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_breakers", nodestatsBreakersExpected, tags)
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
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
|
|
|
|
2017-10-04 22:29:32 +00:00
|
|
|
func TestGatherClusterHealthEmptyClusterHealth(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
|
2017-10-04 22:29:32 +00:00
|
|
|
es.ClusterHealthLevel = ""
|
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
|
|
|
|
2017-10-04 22:29:32 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health",
|
|
|
|
clusterHealthExpected,
|
|
|
|
map[string]string{"name": "elasticsearch_telegraf"})
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v1IndexExpected,
|
|
|
|
map[string]string{"index": "v1"})
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v2IndexExpected,
|
|
|
|
map[string]string{"index": "v2"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherClusterHealthSpecificClusterHealth(t *testing.T) {
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.ClusterHealth = true
|
|
|
|
es.ClusterHealthLevel = "cluster"
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, clusterHealthResponse)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
require.NoError(t, es.gatherClusterHealth("junk", &acc))
|
|
|
|
|
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health",
|
|
|
|
clusterHealthExpected,
|
|
|
|
map[string]string{"name": "elasticsearch_telegraf"})
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v1IndexExpected,
|
|
|
|
map[string]string{"index": "v1"})
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v2IndexExpected,
|
|
|
|
map[string]string{"index": "v2"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatherClusterHealthAlsoIndicesHealth(t *testing.T) {
|
|
|
|
es := newElasticsearchWithClient()
|
|
|
|
es.Servers = []string{"http://example.com:9200"}
|
|
|
|
es.ClusterHealth = true
|
|
|
|
es.ClusterHealthLevel = "indices"
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, clusterHealthResponseWithIndices)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
require.NoError(t, es.gatherClusterHealth("junk", &acc))
|
|
|
|
|
|
|
|
checkIsMaster(es, false, t)
|
|
|
|
|
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,
|
2019-06-17 20:31:15 +00:00
|
|
|
map[string]string{"index": "v1", "name": "elasticsearch_telegraf"})
|
2016-01-06 01:06:30 +00:00
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_indices",
|
|
|
|
v2IndexExpected,
|
2019-06-17 20:31:15 +00:00
|
|
|
map[string]string{"index": "v2", "name": "elasticsearch_telegraf"})
|
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
|
|
|
|
}
|