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"
|
2019-06-25 23:16:15 +00:00
|
|
|
|
2016-12-20 16:30:03 +00:00
|
|
|
"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",
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 23:16:15 +00:00
|
|
|
func defaultServerInfo() serverInfo {
|
|
|
|
return serverInfo{nodeID: "", masterID: "SDFsfSDFsdfFSDSDfSFDSDF"}
|
|
|
|
}
|
2017-10-06 23:16:32 +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) {
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
func checkIsMaster(es *Elasticsearch, server string, expected bool, t *testing.T) {
|
|
|
|
if es.serverInfo[server].isMaster() != expected {
|
2016-12-20 16:30:03 +00:00
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2016-12-20 16:30:03 +00:00
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
2017-10-06 23:16:32 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
if err := acc.GatherError(es.Gather); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2017-10-06 23:16:32 +00:00
|
|
|
|
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
2016-12-20 16:30:03 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
if err := es.gatherNodeStats("junk", &acc); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-07-08 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2016-12-20 16:30:03 +00:00
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
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))
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], 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"})
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2017-10-04 22:29:32 +00:00
|
|
|
v1IndexExpected,
|
|
|
|
map[string]string{"index": "v1"})
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2017-10-04 22:29:32 +00:00
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
2017-10-04 22:29:32 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
require.NoError(t, es.gatherClusterHealth("junk", &acc))
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2017-10-04 22:29:32 +00:00
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health",
|
|
|
|
clusterHealthExpected,
|
|
|
|
map[string]string{"name": "elasticsearch_telegraf"})
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2017-10-04 22:29:32 +00:00
|
|
|
v1IndexExpected,
|
|
|
|
map[string]string{"index": "v1"})
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertDoesNotContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2017-10-04 22:29:32 +00:00
|
|
|
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)
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = defaultServerInfo()
|
2017-10-04 22:29:32 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
require.NoError(t, es.gatherClusterHealth("junk", &acc))
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2017-10-04 22:29:32 +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
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2016-01-06 01:06:30 +00:00
|
|
|
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
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "elasticsearch_cluster_health_indices",
|
2016-01-06 01:06:30 +00:00
|
|
|
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"}
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
info := serverInfo{nodeID: "SDFsfSDFsdfFSDSDfSFDSDF", masterID: ""}
|
2016-12-20 16:30:03 +00:00
|
|
|
|
|
|
|
// first get catMaster
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, IsMasterResult)
|
2019-06-25 23:16:15 +00:00
|
|
|
masterID, err := es.getCatMaster("junk")
|
|
|
|
require.NoError(t, err)
|
|
|
|
info.masterID = masterID
|
|
|
|
es.serverInfo["http://example.com:9200"] = info
|
2016-12-20 16:30:03 +00:00
|
|
|
|
|
|
|
IsMasterResultTokens := strings.Split(string(IsMasterResult), " ")
|
2019-06-25 23:16:15 +00:00
|
|
|
if masterID != IsMasterResultTokens[0] {
|
2016-12-20 16:30:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], true, t)
|
2016-12-20 16:30:03 +00:00
|
|
|
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"}
|
2019-06-25 23:16:15 +00:00
|
|
|
es.serverInfo = make(map[string]serverInfo)
|
|
|
|
es.serverInfo["http://example.com:9200"] = serverInfo{nodeID: "SDFsfSDFsdfFSDSDfSFDSDF", masterID: ""}
|
2016-12-20 16:30:03 +00:00
|
|
|
|
|
|
|
// first get catMaster
|
|
|
|
es.client.Transport = newTransportMock(http.StatusOK, IsNotMasterResult)
|
2019-06-25 23:16:15 +00:00
|
|
|
masterID, err := es.getCatMaster("junk")
|
|
|
|
require.NoError(t, err)
|
2016-12-20 16:30:03 +00:00
|
|
|
|
|
|
|
IsNotMasterResultTokens := strings.Split(string(IsNotMasterResult), " ")
|
2019-06-25 23:16:15 +00:00
|
|
|
if masterID != IsNotMasterResultTokens[0] {
|
2016-12-20 16:30:03 +00:00
|
|
|
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
|
2019-06-25 23:16:15 +00:00
|
|
|
checkIsMaster(es, es.Servers[0], false, t)
|
2016-12-20 16:30:03 +00:00
|
|
|
checkNodeStatsResult(t, &acc)
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:23:49 +00:00
|
|
|
func newElasticsearchWithClient() *Elasticsearch {
|
|
|
|
es := NewElasticsearch()
|
|
|
|
es.client = &http.Client{}
|
|
|
|
return es
|
|
|
|
}
|