testing

package
v0.0.0-...-be347a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2019 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package testing provides utility functions for testing Module and MetricSet implementations.

MetricSet Example

This is an example showing how to use this package to test a MetricSet. By using these methods you ensure the MetricSet is instantiated in the same way that Metricbeat does it and with the same validations.

package mymetricset_test

import (
	mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)

func TestFetch(t *testing.T) {
	f := mbtest.NewEventFetcher(t, getConfig())
	event, err := f.Fetch()
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)

	// Test event attributes...
}

func getConfig() map[string]interface{} {
	return map[string]interface{}{
		"module":     "mymodule",
		"metricsets": []string{"status"},
		"hosts":      []string{mymodule.GetHostFromEnv()},
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateFullEvent

func CreateFullEvent(ms mb.MetricSet, metricSetData common.MapStr) beat.Event

CreateFullEvent builds a full event given the data generated by a MetricSet. This simulates the output of Metricbeat as if it were 2016-05-23T08:05:34.853Z and the hostname is host.example.com.

func NewEventFetcher

func NewEventFetcher(t testing.TB, config interface{}) mb.EventFetcher

NewEventFetcher instantiates a new EventFetcher using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewEventsFetcher

func NewEventsFetcher(t testing.TB, config interface{}) mb.EventsFetcher

NewEventsFetcher instantiates a new EventsFetcher using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewMetricSet

func NewMetricSet(t testing.TB, config interface{}) mb.MetricSet

NewMetricSet instantiates a new MetricSet using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewPushMetricSet

func NewPushMetricSet(t testing.TB, config interface{}) mb.PushMetricSet

NewPushMetricSet instantiates a new PushMetricSet using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewPushMetricSetV2

func NewPushMetricSetV2(t testing.TB, config interface{}) mb.PushMetricSetV2

NewPushMetricSetV2 instantiates a new PushMetricSetV2 using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewPushMetricSetV2WithContext

func NewPushMetricSetV2WithContext(t testing.TB, config interface{}) mb.PushMetricSetV2WithContext

NewPushMetricSetV2WithContext instantiates a new PushMetricSetV2WithContext using the given configuration. The ModuleFactory and MetricSetFactory are obtained from the global Registry.

func NewReportingMetricSet

func NewReportingMetricSet(t testing.TB, config interface{}) mb.ReportingMetricSet

func NewReportingMetricSetV2

func NewReportingMetricSetV2(t testing.TB, config interface{}) mb.ReportingMetricSetV2

NewReportingMetricSetV2 returns a new ReportingMetricSetV2 instance. Then you can use ReportingFetchV2 to perform a Fetch operation with the MetricSet.

func NewReportingMetricSetV2Error

func NewReportingMetricSetV2Error(t testing.TB, config interface{}) mb.ReportingMetricSetV2Error

NewReportingMetricSetV2Error returns a new ReportingMetricSetV2 instance. Then you can use ReportingFetchV2 to perform a Fetch operation with the MetricSet.

func NewReportingMetricSetV2WithContext

func NewReportingMetricSetV2WithContext(t testing.TB, config interface{}) mb.ReportingMetricSetV2WithContext

NewReportingMetricSetV2WithContext returns a new ReportingMetricSetV2WithContext instance. Then you can use ReportingFetchV2 to perform a Fetch operation with the MetricSet.

func ReportingFetch

func ReportingFetch(metricSet mb.ReportingMetricSet) ([]common.MapStr, []error)

ReportingFetch runs the given reporting metricset and returns all of the events and errors that occur during that period.

func ReportingFetchV2

func ReportingFetchV2(metricSet mb.ReportingMetricSetV2) ([]mb.Event, []error)

ReportingFetchV2 runs the given reporting metricset and returns all of the events and errors that occur during that period.

func ReportingFetchV2Error

func ReportingFetchV2Error(metricSet mb.ReportingMetricSetV2Error) ([]mb.Event, []error)

ReportingFetchV2Error runs the given reporting metricset and returns all of the events and errors that occur during that period.

func ReportingFetchV2WithContext

func ReportingFetchV2WithContext(metricSet mb.ReportingMetricSetV2WithContext) ([]mb.Event, []error)

ReportingFetchV2WithContext runs the given reporting metricset and returns all of the events and errors that occur during that period.

func RunPushMetricSet

func RunPushMetricSet(duration time.Duration, metricSet mb.PushMetricSet) ([]common.MapStr, []error)

RunPushMetricSet run the given push metricset for the specific amount of time and returns all of the events and errors that occur during that period.

func RunPushMetricSetV2

func RunPushMetricSetV2(timeout time.Duration, waitEvents int, metricSet mb.PushMetricSetV2) []mb.Event

RunPushMetricSetV2 run the given push metricset for the specific amount of time and returns all of the events and errors that occur during that period.

func RunPushMetricSetV2WithContext

func RunPushMetricSetV2WithContext(timeout time.Duration, waitEvents int, metricSet mb.PushMetricSetV2WithContext) []mb.Event

RunPushMetricSetV2WithContext run the given push metricset for the specific amount of time and returns all of the events that occur during that period.

func SelectEvent

func SelectEvent(events []common.MapStr, cond func(e common.MapStr) bool) (common.MapStr, error)

SelectEvent selects the first event that matches an specific condition

func SelectEventV2

func SelectEventV2(f mb.MetricSet, events []mb.Event, cond func(e common.MapStr) bool) (mb.Event, error)

SelectEventV2 selects the first event that matches an specific condition

func StandardizeEvent

func StandardizeEvent(ms mb.MetricSet, e mb.Event, modifiers ...mb.EventModifier) beat.Event

StandardizeEvent builds a beat.Event given the data generated by a MetricSet. This simulates the output as if it were 2016-05-23T08:05:34.853Z and the hostname is host.example.com and the RTT is 155us.

func TestDataFiles

func TestDataFiles(t *testing.T, module, metricSet string)

TestDataFiles run tests with config from the usual path (`_meta/testdata`)

func TestDataFilesWithConfig

func TestDataFilesWithConfig(t *testing.T, module, metricSet string, config DataConfig)

TestDataFilesWithConfig run tests for a testdata config

func WriteEvent

func WriteEvent(f mb.EventFetcher, t testing.TB) error

WriteEvent fetches a single event writes the output to a ./_meta/data.json file.

func WriteEventToDataJSON

func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event, postfixPath string)

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 WriteEvents

func WriteEvents(f mb.EventsFetcher, t testing.TB) error

WriteEvents fetches events and writes the first event to a ./_meta/data.json file.

func WriteEventsCond

func WriteEventsCond(f mb.EventsFetcher, t testing.TB, cond func(e common.MapStr) bool) error

WriteEventsCond fetches events and writes the first event that matches the condition to a ./_meta/data.json file.

func WriteEventsReporterV2

func WriteEventsReporterV2(f mb.ReportingMetricSetV2, t testing.TB, path string) error

WriteEventsReporterV2 fetches events and writes the first event to a ./_meta/data.json file.

func WriteEventsReporterV2Cond

func WriteEventsReporterV2Cond(f mb.ReportingMetricSetV2, t testing.TB, path string, cond func(common.MapStr) bool) error

WriteEventsReporterV2Cond fetches events and writes the first event that matches the condition to a file.

func WriteEventsReporterV2Error

func WriteEventsReporterV2Error(f mb.ReportingMetricSetV2Error, t testing.TB, path string) error

WriteEventsReporterV2Error fetches events and writes the first event to a ./_meta/data.json file.

func WriteEventsReporterV2ErrorCond

func WriteEventsReporterV2ErrorCond(f mb.ReportingMetricSetV2Error, t testing.TB, path string, cond func(common.MapStr) bool) error

WriteEventsReporterV2ErrorCond fetches events and writes the first event that matches the condition to a file.

func WriteEventsReporterV2WithContext

func WriteEventsReporterV2WithContext(f mb.ReportingMetricSetV2WithContext, t testing.TB, path string) error

WriteEventsReporterV2WithContext fetches events and writes the first event to a ./_meta/data.json file.

func WriteEventsReporterV2WithContextCond

func WriteEventsReporterV2WithContextCond(f mb.ReportingMetricSetV2WithContext, t testing.TB, path string, cond func(common.MapStr) bool) error

WriteEventsReporterV2WithContextCond fetches events and writes the first event that matches the condition to a file.

Types

type CapturingReporterV2

type CapturingReporterV2 struct {
	// contains filtered or unexported fields
}

CapturingReporterV2 is a reporter used for testing which stores all events and errors

func (*CapturingReporterV2) Error

func (r *CapturingReporterV2) Error(err error) bool

Error is used to report an error

func (*CapturingReporterV2) Event

func (r *CapturingReporterV2) Event(event mb.Event) bool

Event is used to report an event

func (*CapturingReporterV2) GetErrors

func (r *CapturingReporterV2) GetErrors() []error

GetErrors returns all reported errors

func (*CapturingReporterV2) GetEvents

func (r *CapturingReporterV2) GetEvents() []mb.Event

GetEvents returns all reported events

type DataConfig

type DataConfig struct {
	// Path is the directory containing this configuration
	Path string

	// WritePath is the path where to write the generated files
	WritePath string

	// The type of the test to run, usually `http`.
	Type string

	// URL of the endpoint that must be tested depending on each module
	URL string

	// Suffix is the extension of the source file with the input contents. Defaults to `json`, `plain` is also a common use.
	Suffix string

	// Module is a map of specific configs that will be appended to a module configuration prior initializing it.
	// For example, the following config in yaml:
	//   module:
	//     namespace: test
	//     foo: bar
	//
	// Will produce the following module config:
	//   - module: http
	//     metricsets:
	//       - json
	//     period: 10s
	//     hosts: ["localhost:80"]
	//     path: "/"
	//     namespace: "test"
	//     foo: bar
	//
	// (notice last two lines)
	Module map[string]interface{} `yaml:"module"`

	// OmitDocumentedFieldsCheck is a list of fields that must be omitted from the function that checks if the field
	// is contained in {metricset}/_meta/fields.yml
	OmitDocumentedFieldsCheck []string `yaml:"omit_documented_fields_check"`

	// RemoveFieldsForComparison
	RemoveFieldsForComparison []string `yaml:"remove_fields_from_comparison"`
}

DataConfig is the configuration for testdata tests

For example for an http service that mimics the apache status page the following configuration could be used: ``` type: http url: "/server-status?auto=" suffix: plain omit_documented_fields_check:

  • "apache.status.hostname"

remove_fields_from_comparison: - "apache.status.hostname" module:

namespace: test

``` A test will be run for each file with the `plain` extension in the same directory where a file with this configuration is placed.

func ReadDataConfig

func ReadDataConfig(t *testing.T, f string) DataConfig

ReadDataConfig reads the testdataconfig from a path

func TestDataConfig

func TestDataConfig(t *testing.T) DataConfig

TestDataConfig is a convenience helper function to read the testdata config from the usual path

type Fetcher

type Fetcher interface {
	Module() mb.Module
	Name() string

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

Fetcher is an interface implemented by all fetchers for testing purpouses

func NewFetcher

func NewFetcher(t testing.TB, config interface{}) Fetcher

NewFetcher returns a test fetcher from a Metricset configuration

type TestModule

type TestModule struct {
	ModName   string
	ModConfig mb.ModuleConfig
	RawConfig *common.Config
}

func NewTestModule

func NewTestModule(t testing.TB, config interface{}) *TestModule

func (*TestModule) Config

func (m *TestModule) Config() mb.ModuleConfig

func (*TestModule) Name

func (m *TestModule) Name() string

func (*TestModule) UnpackConfig

func (m *TestModule) UnpackConfig(to interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL