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 3 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
142 changes: 142 additions & 0 deletions metricbeat/mb/testing/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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"
)

type Fetcher interface {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
Module() mb.Module
Name() string

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

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 {
fetcherHelper
metricSet mb.ReportingMetricSetV2
}

func NewReporterV2Fetcher(metricSet mb.ReportingMetricSetV2) *reportingMetricSetV2Fetcher {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
return &reportingMetricSetV2Fetcher{
fetcherHelper{metricSet},
metricSet,
}
}

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

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.metricSet, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}

type reportingMetricSetV2FetcherError struct {
fetcherHelper
metricSet mb.ReportingMetricSetV2Error
}

func NewReporterV2FetcherError(metricSet mb.ReportingMetricSetV2Error) *reportingMetricSetV2FetcherError {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
return &reportingMetricSetV2FetcherError{
fetcherHelper{metricSet},
metricSet,
}
}

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

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.metricSet, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}

type reportingMetricSetV2FetcherWithContext struct {
fetcherHelper
metricSet mb.ReportingMetricSetV2WithContext
}

func NewReporterV2FetcherWithContext(metricSet mb.ReportingMetricSetV2WithContext) *reportingMetricSetV2FetcherWithContext {
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
return &reportingMetricSetV2FetcherWithContext{
fetcherHelper{metricSet},
metricSet,
}
}

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

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.metricSet, t, path, cond)
if err != nil {
t.Fatal("writing events", err)
}
}

type fetcherHelper struct {
metricSet mb.MetricSet
}

func (f *fetcherHelper) Module() mb.Module {
return f.metricSet.Module()
}

func (f *fetcherHelper) Name() string {
return f.metricSet.Name()
}
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.Fetch()
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.Fetch()
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