Skip to content

Instantly share code, notes, and snippets.

@jsoriano
Last active November 22, 2018 13:25
Show Gist options
  • Save jsoriano/5012214f5c3578489f1d800d2f0a9aa3 to your computer and use it in GitHub Desktop.
Save jsoriano/5012214f5c3578489f1d800d2f0a9aa3 to your computer and use it in GitHub Desktop.
Metricbeat integration tests revamp, code style
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build integration
package status
import (
"testing"
"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/apache/mtest"
"github.com/stretchr/testify/assert"
)
func TestStatus(t *testing.T) {
mtest.Runner.Run(t, compose.Suite{
"Fetch": func(t *testing.T, r compose.R) {
f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
event, err := f.Fetch()
if !assert.NoError(t, err) {
t.FailNow()
}
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)
// Check number of fields.
if len(event) < 11 {
t.Fatal("Too few top-level elements in the event")
}
},
"Data": func(t *testing.T, r compose.R) {
f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
err := mbtest.WriteEvent(f, t)
if err != nil {
t.Fatal("write", err)
}
},
})
}
func getConfig(host string) map[string]interface{} {
return map[string]interface{}{
"module": "apache",
"metricsets": []string{"status"},
"hosts": []string{host},
}
}
diff --git a/metricbeat/module/apache/status/status_integration_test.go b/metricbeat/module/apache/status/status_integration_test.go
index 01e7f70b5..8554183d9 100644
--- a/metricbeat/module/apache/status/status_integration_test.go
+++ b/metricbeat/module/apache/status/status_integration_test.go
@@ -24,43 +24,42 @@ import (
"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
- "github.com/elastic/beats/metricbeat/module/apache"
+ "github.com/elastic/beats/metricbeat/module/apache/mtest"
"github.com/stretchr/testify/assert"
)
-func TestFetch(t *testing.T) {
- compose.EnsureUp(t, "apache")
+func TestStatus(t *testing.T) {
+ mtest.Runner.Run(t, compose.Suite{
+ "Fetch": func(t *testing.T, r compose.R) {
+ f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
+ event, err := f.Fetch()
+ if !assert.NoError(t, err) {
+ t.FailNow()
+ }
- f := mbtest.NewEventFetcher(t, getConfig())
- event, err := f.Fetch()
- if !assert.NoError(t, err) {
- t.FailNow()
- }
-
- t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)
+ t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)
- // Check number of fields.
- if len(event) < 11 {
- t.Fatal("Too few top-level elements in the event")
- }
-}
+ // Check number of fields.
+ if len(event) < 11 {
+ t.Fatal("Too few top-level elements in the event")
+ }
+ },
+ "Data": func(t *testing.T, r compose.R) {
+ f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
-func TestData(t *testing.T) {
- compose.EnsureUp(t, "apache")
-
- f := mbtest.NewEventFetcher(t, getConfig())
-
- err := mbtest.WriteEvent(f, t)
- if err != nil {
- t.Fatal("write", err)
- }
+ err := mbtest.WriteEvent(f, t)
+ if err != nil {
+ t.Fatal("write", err)
+ }
+ },
+ })
}
-func getConfig() map[string]interface{} {
+func getConfig(host string) map[string]interface{} {
return map[string]interface{}{
"module": "apache",
"metricsets": []string{"status"},
- "hosts": []string{apache.GetApacheEnvHost()},
+ "hosts": []string{host},
}
}
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build integration
package status
import (
"testing"
"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
"github.com/elastic/beats/metricbeat/module/apache/mtest"
"github.com/stretchr/testify/assert"
)
func TestStatus(t *testing.T) {
mtest.Runner.Run(t, compose.Suite{
"Fetch": testFetch,
"Data": testData,
})
}
func testFetch(t *testing.T, r compose.R) {
f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
event, err := f.Fetch()
if !assert.NoError(t, err) {
t.FailNow()
}
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)
// Check number of fields.
if len(event) < 11 {
t.Fatal("Too few top-level elements in the event")
}
}
func testData(t *testing.T, r compose.R) {
f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
err := mbtest.WriteEvent(f, t)
if err != nil {
t.Fatal("write", err)
}
}
func getConfig(host string) map[string]interface{} {
return map[string]interface{}{
"module": "apache",
"metricsets": []string{"status"},
"hosts": []string{host},
}
}
diff --git a/metricbeat/module/apache/status/status_integration_test.go b/metricbeat/module/apache/status/status_integration_test.go
index 01e7f70b5..d31340d42 100644
--- a/metricbeat/module/apache/status/status_integration_test.go
+++ b/metricbeat/module/apache/status/status_integration_test.go
@@ -24,15 +24,20 @@ import (
"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
- "github.com/elastic/beats/metricbeat/module/apache"
+ "github.com/elastic/beats/metricbeat/module/apache/mtest"
"github.com/stretchr/testify/assert"
)
-func TestFetch(t *testing.T) {
- compose.EnsureUp(t, "apache")
+func TestStatus(t *testing.T) {
+ mtest.Runner.Run(t, compose.Suite{
+ "Fetch": testFetch,
+ "Data": testData,
+ })
+}
- f := mbtest.NewEventFetcher(t, getConfig())
+func testFetch(t *testing.T, r compose.R) {
+ f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
event, err := f.Fetch()
if !assert.NoError(t, err) {
t.FailNow()
@@ -46,10 +51,8 @@ func TestFetch(t *testing.T) {
}
}
-func TestData(t *testing.T) {
- compose.EnsureUp(t, "apache")
-
- f := mbtest.NewEventFetcher(t, getConfig())
+func testData(t *testing.T, r compose.R) {
+ f := mbtest.NewEventFetcher(t, getConfig(r.Host()))
err := mbtest.WriteEvent(f, t)
if err != nil {
@@ -57,10 +60,10 @@ func TestData(t *testing.T) {
}
}
-func getConfig() map[string]interface{} {
+func getConfig(host string) map[string]interface{} {
return map[string]interface{}{
"module": "apache",
"metricsets": []string{"status"},
- "hosts": []string{apache.GetApacheEnvHost()},
+ "hosts": []string{host},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment