Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper with a common interface for all test reporters #11998

Merged
merged 7 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions metricbeat/mb/testing/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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.

package testing

import (
"testing"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
)

// Fetcher is an interface implemented by all fetchers for testing purpouses
type Fetcher interface {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
Module() mb.Module
Name() string

FetchEvents() ([]mb.Event, []error)
WriteEvents(testing.TB, string)
WriteEventsCond(testing.TB, string, func(common.MapStr) bool)
}

// NewFetcher returns a test fetcher from a Metricset configuration
func NewFetcher(t testing.TB, config interface{}) Fetcher {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
metricSet := NewMetricSet(t, config)
switch metricSet := metricSet.(type) {
case mb.ReportingMetricSetV2:
return newReporterV2Fetcher(metricSet)
case mb.ReportingMetricSetV2Error:
return newReporterV2FetcherError(metricSet)
case mb.ReportingMetricSetV2WithContext:
return newReporterV2FetcherWithContext(metricSet)
default:
t.Fatalf("Failed to create a Fetcher for metricset of type %T", metricSet)
}
return nil
}

type reportingMetricSetV2Fetcher struct {
mb.ReportingMetricSetV2
}

func newReporterV2Fetcher(metricSet mb.ReportingMetricSetV2) *reportingMetricSetV2Fetcher {
return &reportingMetricSetV2Fetcher{metricSet}
}

func (f *reportingMetricSetV2Fetcher) FetchEvents() ([]mb.Event, []error) {
return ReportingFetchV2(f)
}

func (f *reportingMetricSetV2Fetcher) WriteEvents(t testing.TB, path string) {
f.WriteEventsCond(t, path, nil)
}

func (f *reportingMetricSetV2Fetcher) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) {
err := WriteEventsReporterV2Cond(f, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}

type reportingMetricSetV2FetcherError struct {
mb.ReportingMetricSetV2Error
}

func newReporterV2FetcherError(metricSet mb.ReportingMetricSetV2Error) *reportingMetricSetV2FetcherError {
return &reportingMetricSetV2FetcherError{metricSet}
}

func (f *reportingMetricSetV2FetcherError) FetchEvents() ([]mb.Event, []error) {
return ReportingFetchV2Error(f)
}

func (f *reportingMetricSetV2FetcherError) WriteEvents(t testing.TB, path string) {
f.WriteEventsCond(t, path, nil)
}

func (f *reportingMetricSetV2FetcherError) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) {
err := WriteEventsReporterV2ErrorCond(f, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}

type reportingMetricSetV2FetcherWithContext struct {
mb.ReportingMetricSetV2WithContext
}

func newReporterV2FetcherWithContext(metricSet mb.ReportingMetricSetV2WithContext) *reportingMetricSetV2FetcherWithContext {
return &reportingMetricSetV2FetcherWithContext{metricSet}
}

func (f *reportingMetricSetV2FetcherWithContext) FetchEvents() ([]mb.Event, []error) {
return ReportingFetchV2WithContext(f)
}

func (f *reportingMetricSetV2FetcherWithContext) WriteEvents(t testing.TB, path string) {
f.WriteEventsCond(t, path, nil)
}

func (f *reportingMetricSetV2FetcherWithContext) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) {
err := WriteEventsReporterV2WithContextCond(f, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
)

func TestData(t *testing.T) {
f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig())
if err := mbtest.WriteEventsReporterV2WithContext(f, t, ""); err != nil {
t.Fatal("write", err)
}
f := mbtest.NewFetcher(t, getConfig())
f.WriteEvents(t, "")
}

func getConfig() map[string]interface{} {
Expand Down
14 changes: 10 additions & 4 deletions metricbeat/module/etcd/metrics/metrics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/logp"

"github.com/elastic/beats/libbeat/tests/compose"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)
Expand All @@ -36,13 +35,20 @@ func TestFetch(t *testing.T) {

compose.EnsureUp(t, "etcd")

f := mbtest.NewReportingMetricSetV2(t, getConfig())
events, errs := mbtest.ReportingFetchV2(f)
m := mbtest.NewFetcher(t, getConfig())
events, errs := m.FetchEvents()
if len(errs) > 0 {
t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs)
}
assert.NotEmpty(t, events)
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0])
t.Logf("%s/%s event: %+v", m.Module().Name(), m.Name(), events[0])
}

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

m := mbtest.NewFetcher(t, getConfig())
m.WriteEvents(t, "")
}

func getConfig() map[string]interface{} {
Expand Down
16 changes: 4 additions & 12 deletions metricbeat/module/etcd/store/store_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestFetch(t *testing.T) {
logp.TestingSetup()
compose.EnsureUp(t, "etcd")

f := mbtest.NewReportingMetricSetV2Error(t, getConfig())
events, errs := mbtest.ReportingFetchV2Error(f)
ms := mbtest.NewFetcher(t, getConfig())
events, errs := ms.FetchEvents()
if len(errs) > 0 {
t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs)
}
Expand All @@ -45,16 +45,8 @@ func TestFetch(t *testing.T) {
func TestData(t *testing.T) {
compose.EnsureUp(t, "etcd")

f := mbtest.NewReportingMetricSetV2Error(t, getConfig())
events, errs := mbtest.ReportingFetchV2Error(f)
if len(errs) > 0 {
t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs)
}
assert.NotEmpty(t, events)

if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil {
t.Fatal("write", err)
}
f := mbtest.NewFetcher(t, getConfig())
f.WriteEvents(t, "")
}

func getConfig() map[string]interface{} {
Expand Down