Skip to content

Commit

Permalink
Combine Elasticsearch module integration tests (elastic#7049)
Browse files Browse the repository at this point in the history
This combines all Elasticsearch module integration tests on the module level. This has the advantage that it removes duplicated code and makes adding a metricset to the test very simple. Also data generators was move to the module level to have it one place. It also should speed up tests as Elasticsearch has to be started only once.

As package name `elasticsearch_test` is used to make sure tests happen inside it's own package.

For the data generation a path was introduced to still write the data files into the correct directory.
  • Loading branch information
ruflin authored and stevea78 committed May 20, 2018
1 parent a36e484 commit 24e9362
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 216 deletions.
2 changes: 1 addition & 1 deletion auditbeat/module/auditd/audit_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestData(t *testing.T) {
}

beatEvent := mbtest.StandardizeEvent(ms, events[0], core.AddDatasetToEvent)
mbtest.WriteEventToDataJSON(t, beatEvent)
mbtest.WriteEventToDataJSON(t, beatEvent, "")
}

func getConfig() map[string]interface{} {
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/module/file_integrity/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestData(t *testing.T) {
}

fullEvent := mbtest.StandardizeEvent(ms, events[len(events)-1], core.AddDatasetToEvent)
mbtest.WriteEventToDataJSON(t, fullEvent)
mbtest.WriteEventToDataJSON(t, fullEvent, "")
}

func TestDetectDeletedFiles(t *testing.T) {
Expand Down
12 changes: 7 additions & 5 deletions metricbeat/mb/testing/data_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func WriteEvent(f mb.EventFetcher, t testing.TB) error {
}

fullEvent := CreateFullEvent(f, event)
WriteEventToDataJSON(t, fullEvent)
WriteEventToDataJSON(t, fullEvent, "")
return nil
}

Expand Down Expand Up @@ -75,13 +75,13 @@ func WriteEventsCond(f mb.EventsFetcher, t testing.TB, cond func(e common.MapStr
}

fullEvent := CreateFullEvent(f, *event)
WriteEventToDataJSON(t, fullEvent)
WriteEventToDataJSON(t, fullEvent, "")
return nil
}

// WriteEventsReporterV2 fetches events and writes the first event to a ./_meta/data.json
// file.
func WriteEventsReporterV2(f mb.ReportingMetricSetV2, t testing.TB) error {
func WriteEventsReporterV2(f mb.ReportingMetricSetV2, t testing.TB, path string) error {
if !*dataFlag {
t.Skip("skip data generation tests")
}
Expand All @@ -97,7 +97,7 @@ func WriteEventsReporterV2(f mb.ReportingMetricSetV2, t testing.TB) error {

e := StandardizeEvent(f, events[0], mb.AddMetricSetInfo)

WriteEventToDataJSON(t, e)
WriteEventToDataJSON(t, e, path)
return nil
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func StandardizeEvent(ms mb.MetricSet, e mb.Event, modifiers ...mb.EventModifier
// WriteEventToDataJSON writes the given event as "pretty" JSON to
// a ./_meta/data.json file. If the -data CLI flag is unset or false then the
// method is a no-op.
func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event) {
func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event, pathPostfix string) {
if !*dataFlag {
return
}
Expand All @@ -151,6 +151,8 @@ func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event) {
t.Fatal(err)
}

path = path + pathPostfix

fields := fullEvent.Fields
fields["@timestamp"] = fullEvent.Timestamp

Expand Down
119 changes: 119 additions & 0 deletions metricbeat/module/elasticsearch/elasticsearch_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// +build integration

package elasticsearch_test

import (
"fmt"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"

_ "github.com/elastic/beats/metricbeat/module/elasticsearch/index"
_ "github.com/elastic/beats/metricbeat/module/elasticsearch/node"
_ "github.com/elastic/beats/metricbeat/module/elasticsearch/node_stats"
)

var metricSets = []string{
"index",
"node",
"node_stats",
}

func TestFetch(t *testing.T) {
compose.EnsureUp(t, "elasticsearch")

err := createIndex(getEnvHost() + ":" + getEnvPort())
assert.NoError(t, err)

for _, metricSet := range metricSets {
t.Run(metricSet, func(t *testing.T) {
f := mbtest.NewReportingMetricSetV2(t, getConfig(metricSet))
events, errs := mbtest.ReportingFetchV2(f)

assert.NotNil(t, events)
assert.Nil(t, errs)
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0].BeatEvent("elasticsearch", metricSet).Fields.StringToPrint())
})
}
}

func TestData(t *testing.T) {
compose.EnsureUp(t, "elasticsearch")

for _, metricSet := range metricSets {
t.Run(metricSet, func(t *testing.T) {
f := mbtest.NewReportingMetricSetV2(t, getConfig(metricSet))
err := mbtest.WriteEventsReporterV2(f, t, "/"+metricSet)
if err != nil {
t.Fatal("write", err)
}
})
}
}

// GetEnvHost returns host for Elasticsearch
func getEnvHost() string {
host := os.Getenv("ES_HOST")

if len(host) == 0 {
host = "127.0.0.1"
}
return host
}

// GetEnvPort returns port for Elasticsearch
func getEnvPort() string {
port := os.Getenv("ES_PORT")

if len(port) == 0 {
port = "9200"
}
return port
}

// GetConfig returns config for elasticsearch module
func getConfig(metricset string) map[string]interface{} {
return map[string]interface{}{
"module": "elasticsearch",
"metricsets": []string{metricset},
"hosts": []string{getEnvHost() + ":" + getEnvPort()},
}
}

// createIndex creates and elasticsearch index in case it does not exit yet
func createIndex(host string) error {
client := &http.Client{}

resp, err := http.Get("http://" + host + "/testindex")
if err != nil {
return err
}
resp.Body.Close()

// This means index already exists
if resp.StatusCode == 200 {
return nil
}

req, err := http.NewRequest("PUT", "http://"+host+"/testindex", nil)
if err != nil {
return err
}

resp, err = client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("HTTP error %d: %s", resp.StatusCode, resp.Status)
}

return nil
}
14 changes: 7 additions & 7 deletions metricbeat/module/elasticsearch/index/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
},
"elasticsearch": {
"cluster": {
"id": "AI8XrG4wTLKQg3lbRGgQYQ",
"id": "EFdYsDSoSZmU7fXE2wvJ0g",
"name": "docker-cluster"
},
"index": {
"name": ".monitoring-es-6-2018.04.22",
"name": ".ml-anomalies-shared",
"total": {
"docs": {
"count": 241933,
"deleted": 525
"count": 7,
"deleted": 2
},
"segments": {
"count": 16,
"count": 6,
"memory": {
"bytes": 565573
"bytes": 10886
}
},
"store": {
"size": {
"bytes": 135541290
"bytes": 44505
}
}
}
Expand Down
75 changes: 0 additions & 75 deletions metricbeat/module/elasticsearch/index/index_integration_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions metricbeat/module/elasticsearch/node/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"memory": {
"heap": {
"init": {
"bytes": 1073741824
"bytes": 536870912
},
"max": {
"bytes": 1056309248
"bytes": 519438336
}
},
"nonheap": {
Expand All @@ -30,7 +30,7 @@
},
"version": "1.8.0_161"
},
"name": "xMLdnuCzSoempTmcPqdtfw",
"name": "q7ya3ngBTS2cxzYi0duQdA",
"process": {
"mlockall": false
},
Expand Down
34 changes: 0 additions & 34 deletions metricbeat/module/elasticsearch/node/node_integration_test.go

This file was deleted.

Loading

0 comments on commit 24e9362

Please sign in to comment.