forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine Elasticsearch module integration tests (elastic#7049)
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
Showing
16 changed files
with
162 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
metricbeat/module/elasticsearch/elasticsearch_integration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 0 additions & 75 deletions
75
metricbeat/module/elasticsearch/index/index_integration_test.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
metricbeat/module/elasticsearch/node/node_integration_test.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.