testutil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelError = 'E'
	LevelWarn  = 'W'
	LevelInfo  = 'I'
	LevelDebug = 'D'
	LevelTrace = 'T'
)
View Source
const (
	DefaultDelta   = 0.001
	DefaultEpsilon = 0.1
)

Variables

This section is empty.

Functions

func DefaultSampleConfig

func DefaultSampleConfig(sampleConfig string) []byte

DefaultSampleConfig returns the sample config with the default parameters uncommented to also be able to test the validity of default setting.

func FromTestMetric

func FromTestMetric(met *Metric) telegraf.Metric

func GetLocalHost

func GetLocalHost() string

GetLocalHost returns the DOCKER_HOST environment variable, parsing out any scheme or ports so that only the IP address is returned.

func GetRandomString

func GetRandomString(chars int) string

GetRandomString returns a random alphanumerical string of the given length. Please note, this function is different to `internal.RandomString` as it will not use `crypto.Rand` and will therefore not rely on the entropy-pool of the host which might be drained e.g. in CI pipelines. This is useful to e.g. create random passwords for tests where security is not a concern.

func IgnoreFields

func IgnoreFields(names ...string) cmp.Option

IgnoreFields disables comparison of the fields with the given names. The field-names are case-sensitive!

func IgnoreTags

func IgnoreTags(names ...string) cmp.Option

IgnoreTags disables comparison of the tags with the given names. The tag-names are case-sensitive!

func IgnoreTime

func IgnoreTime() cmp.Option

IgnoreTime disables comparison of timestamp.

func IgnoreType

func IgnoreType() cmp.Option

func MetricEqual

func MetricEqual(expected, actual telegraf.Metric, opts ...cmp.Option) bool

MetricEqual returns true if the metrics are equal.

func MockMetrics

func MockMetrics() []telegraf.Metric

MockMetrics returns a mock []telegraf.Metric object for using in unit tests of telegraf output sinks.

func MockMetricsWithValue

func MockMetricsWithValue(value float64) []telegraf.Metric

func MustMetric

func MustMetric(
	name string,
	tags map[string]string,
	fields map[string]interface{},
	tm time.Time,
	tp ...telegraf.ValueType,
) telegraf.Metric

MustMetric creates a new metric.

func NewPKI

func NewPKI(keyPath string) *pki

func OnlyTags

func OnlyTags() cmp.Option

OnlyTags returns an option for keeping only "Tags" for a given Metric

func ParseLinesFromFile

func ParseLinesFromFile(filename string) ([]string, error)

ParseLinesFromFile returns the lines of the file as strings

func ParseMetricsFrom

func ParseMetricsFrom(lines []string, header string, parser LineParser) ([]telegraf.Metric, error)

ParseMetricsFrom parses metrics from the given lines in line-protocol following a header, with a trailing blank line

func ParseMetricsFromFile

func ParseMetricsFromFile(filename string, parser telegraf.Parser) ([]telegraf.Metric, error)

ParseMetricsFromFile parses metrics from the given file in line-protocol

func ParseRawLinesFrom

func ParseRawLinesFrom(lines []string, header string) ([]string, error)

ParseRawLinesFrom returns the raw lines between the given header and a trailing blank line

func PrintMetrics

func PrintMetrics(m []telegraf.Metric)

func RequireMetricEqual

func RequireMetricEqual(t testing.TB, expected, actual telegraf.Metric, opts ...cmp.Option)

RequireMetricEqual halts the test with an error if the metrics are not equal.

func RequireMetricsEqual

func RequireMetricsEqual(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option)

RequireMetricsEqual halts the test with an error if the array of metrics are not equal.

func RequireMetricsStructureEqual

func RequireMetricsStructureEqual(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option)

RequireMetricsStructureEqual halts the test with an error if the array of metrics is structural different. Structure means that the metric differs in either name, tag key/values, time (if not ignored) or fields. For fields ONLY the name and type are compared NOT the value.

func RequireMetricsStructureSubset

func RequireMetricsStructureSubset(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option)

RequireMetricsStructureSubset halts the test with an error if the expected array of metrics is not a subset of the actual metrics. The equality here is only based on the structure (i.e. key name and value types) and NOT on the actual value.

func RequireMetricsSubset

func RequireMetricsSubset(t testing.TB, expected, actual []telegraf.Metric, opts ...cmp.Option)

RequireMetricsSubset halts the test with an error if the expected array of metrics is not a subset of the actual metrics.

func SortMetrics

func SortMetrics() cmp.Option

SortMetrics enables sorting metrics before comparison.

func TempSocket

func TempSocket(tb testing.TB) string

func TestMetric

func TestMetric(value interface{}, name ...string) telegraf.Metric

TestMetric Returns a simple test point:

measurement -> "test1" or name
tags -> "tag1":"value1"
value -> value
time -> time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

func WithinDefaultDelta

func WithinDefaultDelta(dt float64) bool

Types

type Accumulator

type Accumulator struct {
	Metrics []*Metric

	Discard bool
	Errors  []error

	TimeFunc func() time.Time

	sync.Mutex
	*sync.Cond
	// contains filtered or unexported fields
}

Accumulator defines a mocked out accumulator

func (*Accumulator) AddCounter

func (a *Accumulator) AddCounter(
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
	timestamp ...time.Time,
)

func (*Accumulator) AddError

func (a *Accumulator) AddError(err error)

AddError appends the given error to Accumulator.Errors.

func (*Accumulator) AddFields

func (a *Accumulator) AddFields(
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
	timestamp ...time.Time,
)

AddFields adds a measurement point with a specified timestamp.

func (*Accumulator) AddGauge

func (a *Accumulator) AddGauge(
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
	timestamp ...time.Time,
)

func (*Accumulator) AddHistogram

func (a *Accumulator) AddHistogram(
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
	timestamp ...time.Time,
)

func (*Accumulator) AddMetric

func (a *Accumulator) AddMetric(m telegraf.Metric)

func (*Accumulator) AddMetrics

func (a *Accumulator) AddMetrics(metrics []telegraf.Metric)

func (*Accumulator) AddSummary

func (a *Accumulator) AddSummary(
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
	timestamp ...time.Time,
)

func (*Accumulator) AddTrackingMetric

func (a *Accumulator) AddTrackingMetric(m telegraf.Metric) telegraf.TrackingID

func (*Accumulator) AddTrackingMetricGroup

func (a *Accumulator) AddTrackingMetricGroup(group []telegraf.Metric) telegraf.TrackingID

func (*Accumulator) AssertContainsFields

func (a *Accumulator) AssertContainsFields(
	t *testing.T,
	measurement string,
	fields map[string]interface{},
)

func (*Accumulator) AssertContainsTaggedFields

func (a *Accumulator) AssertContainsTaggedFields(
	t *testing.T,
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
)

func (*Accumulator) AssertDoesNotContainMeasurement

func (a *Accumulator) AssertDoesNotContainMeasurement(t *testing.T, measurement string)

func (*Accumulator) AssertDoesNotContainsTaggedFields

func (a *Accumulator) AssertDoesNotContainsTaggedFields(
	t *testing.T,
	measurement string,
	fields map[string]interface{},
	tags map[string]string,
)

func (*Accumulator) BoolField

func (a *Accumulator) BoolField(measurement, field string) (v, ok bool)

BoolField returns the bool value of the given measurement and field or false.

func (*Accumulator) ClearMetrics

func (a *Accumulator) ClearMetrics()

func (*Accumulator) Debug

func (a *Accumulator) Debug() bool

func (*Accumulator) Delivered

func (a *Accumulator) Delivered() <-chan telegraf.DeliveryInfo

func (*Accumulator) DisablePrecision

func (*Accumulator) DisablePrecision()

func (*Accumulator) FirstError

func (a *Accumulator) FirstError() error

func (*Accumulator) FloatField

func (a *Accumulator) FloatField(measurement, field string) (float64, bool)

FloatField returns the float64 value of the given measurement and field or false.

func (*Accumulator) GatherError

func (a *Accumulator) GatherError(gf func(telegraf.Accumulator) error) error

GatherError calls the given Gather function and returns the first error found.

func (*Accumulator) Get

func (a *Accumulator) Get(measurement string) (*Metric, bool)

Get gets the specified measurement point from the accumulator

func (*Accumulator) GetDeliveries

func (a *Accumulator) GetDeliveries() []telegraf.DeliveryInfo

func (*Accumulator) GetTelegrafMetrics

func (a *Accumulator) GetTelegrafMetrics() []telegraf.Metric

GetTelegrafMetrics returns all the metrics collected by the accumulator If you are getting race conditions here then you are not waiting for all of your metrics to arrive: see Wait()

func (*Accumulator) HasField

func (a *Accumulator) HasField(measurement, field string) bool

HasField returns true if the given measurement has a field with the given name

func (*Accumulator) HasFloatField

func (a *Accumulator) HasFloatField(measurement, field string) bool

HasFloatField returns true if the given measurement has a float value

func (*Accumulator) HasInt32Field

func (a *Accumulator) HasInt32Field(measurement, field string) bool

HasInt32Field returns true if the measurement has an Int value

func (*Accumulator) HasInt64Field

func (a *Accumulator) HasInt64Field(measurement, field string) bool

HasInt64Field returns true if the measurement has an Int64 value

func (*Accumulator) HasIntField

func (a *Accumulator) HasIntField(measurement, field string) bool

HasIntField returns true if the measurement has an Int value

func (*Accumulator) HasMeasurement

func (a *Accumulator) HasMeasurement(measurement string) bool

HasMeasurement returns true if the accumulator has a measurement with the given name

func (*Accumulator) HasPoint

func (a *Accumulator) HasPoint(
	measurement string,
	tags map[string]string,
	fieldKey string,
	fieldValue interface{},
) bool

func (*Accumulator) HasStringField

func (a *Accumulator) HasStringField(measurement, field string) bool

HasStringField returns true if the measurement has a String value

func (*Accumulator) HasTag

func (a *Accumulator) HasTag(measurement, key string) bool

func (*Accumulator) HasTimestamp

func (a *Accumulator) HasTimestamp(measurement string, timestamp time.Time) bool

HasTimestamp returns true if the measurement has a matching Time value

func (*Accumulator) HasUIntField

func (a *Accumulator) HasUIntField(measurement, field string) bool

HasUIntField returns true if the measurement has a UInt value

func (*Accumulator) Int32Field

func (a *Accumulator) Int32Field(measurement, field string) (int32, bool)

Int32Field returns the int32 value of the given measurement and field or false.

func (*Accumulator) Int64Field

func (a *Accumulator) Int64Field(measurement, field string) (int64, bool)

Int64Field returns the int64 value of the given measurement and field or false.

func (*Accumulator) IntField

func (a *Accumulator) IntField(measurement, field string) (int, bool)

IntField returns the int value of the given measurement and field or false.

func (*Accumulator) NDelivered

func (a *Accumulator) NDelivered() int

func (*Accumulator) NFields

func (a *Accumulator) NFields() int

NFields returns the total number of fields in the accumulator, across all measurements

func (*Accumulator) NMetrics

func (a *Accumulator) NMetrics() uint64

func (*Accumulator) SetDebug

func (a *Accumulator) SetDebug(debug bool)

func (*Accumulator) SetPrecision

func (*Accumulator) SetPrecision(time.Duration)

func (*Accumulator) StringField

func (a *Accumulator) StringField(measurement, field string) (string, bool)

StringField returns the string value of the given measurement and field or false.

func (*Accumulator) TagSetValue

func (a *Accumulator) TagSetValue(measurement, key string) string

func (*Accumulator) TagValue

func (a *Accumulator) TagValue(measurement, key string) string

func (*Accumulator) Uint64Field

func (a *Accumulator) Uint64Field(measurement, field string) (uint64, bool)

Uint64Field returns the int64 value of the given measurement and field or false.

func (*Accumulator) Wait

func (a *Accumulator) Wait(n int)

Wait waits for the given number of metrics to be added to the accumulator.

func (*Accumulator) WaitError

func (a *Accumulator) WaitError(n int)

WaitError waits for the given number of errors to be added to the accumulator.

func (*Accumulator) WithTracking

func (a *Accumulator) WithTracking(maxTracked int) telegraf.TrackingAccumulator

type CaptureLogger

type CaptureLogger struct {
	Name string // Name is the plugin name, will be printed in the `[]`.

	sync.Mutex
	// contains filtered or unexported fields
}

CaptureLogger defines a logging structure for plugins.

func (*CaptureLogger) AddAttribute

func (*CaptureLogger) AddAttribute(string, interface{})

AddAttribute is not supported by the test-logger

func (*CaptureLogger) Clear

func (l *CaptureLogger) Clear()

func (*CaptureLogger) Debug

func (l *CaptureLogger) Debug(args ...interface{})

func (*CaptureLogger) Debugf

func (l *CaptureLogger) Debugf(format string, args ...interface{})

func (*CaptureLogger) Error

func (l *CaptureLogger) Error(args ...interface{})

func (*CaptureLogger) Errorf

func (l *CaptureLogger) Errorf(format string, args ...interface{})

func (*CaptureLogger) Errors

func (l *CaptureLogger) Errors() []string

func (*CaptureLogger) Info

func (l *CaptureLogger) Info(args ...interface{})

func (*CaptureLogger) Infof

func (l *CaptureLogger) Infof(format string, args ...interface{})

func (*CaptureLogger) LastError

func (l *CaptureLogger) LastError() string

func (*CaptureLogger) Level

func (*CaptureLogger) Level() telegraf.LogLevel

func (*CaptureLogger) Messages

func (l *CaptureLogger) Messages() []Entry

func (*CaptureLogger) NMessages

func (l *CaptureLogger) NMessages() int

func (*CaptureLogger) Trace

func (l *CaptureLogger) Trace(args ...interface{})

func (*CaptureLogger) Tracef

func (l *CaptureLogger) Tracef(format string, args ...interface{})

func (*CaptureLogger) Warn

func (l *CaptureLogger) Warn(args ...interface{})

func (*CaptureLogger) Warnf

func (l *CaptureLogger) Warnf(format string, args ...interface{})

func (*CaptureLogger) Warnings

func (l *CaptureLogger) Warnings() []string

type Container

type Container struct {
	Entrypoint         []string
	Env                map[string]string
	Files              map[string]string
	HostAccessPorts    []int
	HostConfigModifier func(*container.HostConfig)
	ExposedPorts       []string
	Cmd                []string
	Image              string
	Name               string
	Hostname           string
	Networks           []string
	WaitingFor         wait.Strategy

	Address string
	Ports   map[string]string
	Logs    TestLogConsumer
	// contains filtered or unexported fields
}

func (*Container) Exec

func (c *Container) Exec(cmds []string) (int, io.Reader, error)

func (*Container) GetInfo

func (c *Container) GetInfo() (string, error)

func (*Container) LookupMappedPorts

func (c *Container) LookupMappedPorts() error

LookupMappedPorts creates a lookup table of exposed ports to mapped ports

func (*Container) Pause

func (c *Container) Pause() error

func (*Container) PrintLogs

func (c *Container) PrintLogs()

func (*Container) Resume

func (c *Container) Resume() error

func (*Container) Start

func (c *Container) Start() error

func (*Container) Terminate

func (c *Container) Terminate()

type Entry

type Entry struct {
	Level byte
	Name  string
	Text  string
}

func (*Entry) String

func (e *Entry) String() string

type LineParser

type LineParser interface {
	ParseLine(line string) (telegraf.Metric, error)
}

type Logger

type Logger struct {
	Name  string // Name is the plugin name, will be printed in the `[]`.
	Quiet bool
}

func (Logger) AddAttribute

func (Logger) AddAttribute(string, interface{})

AddAttribute is not supported by the test-logger

func (Logger) Debug

func (l Logger) Debug(args ...interface{})

func (Logger) Debugf

func (l Logger) Debugf(format string, args ...interface{})

func (Logger) Error

func (l Logger) Error(args ...interface{})

func (Logger) Errorf

func (l Logger) Errorf(format string, args ...interface{})

func (Logger) Info

func (l Logger) Info(args ...interface{})

func (Logger) Infof

func (l Logger) Infof(format string, args ...interface{})

func (Logger) Level

func (Logger) Level() telegraf.LogLevel

func (Logger) Trace

func (l Logger) Trace(args ...interface{})

Trace logs a trace message, patterned after log.Print.

func (Logger) Tracef

func (l Logger) Tracef(format string, args ...interface{})

func (Logger) Warn

func (l Logger) Warn(args ...interface{})

func (Logger) Warnf

func (l Logger) Warnf(format string, args ...interface{})

type Metric

type Metric struct {
	Measurement string
	Tags        map[string]string
	Fields      map[string]interface{}
	Time        time.Time
	Type        telegraf.ValueType
}

Metric defines a single point measurement

func ToTestMetric

func ToTestMetric(tm telegraf.Metric) *Metric

func (*Metric) String

func (p *Metric) String() string

type NopAccumulator

type NopAccumulator struct{}

NopAccumulator is used for benchmarking to isolate the plugin from the internal telegraf accumulator machinery.

func (*NopAccumulator) AddCounter

func (*NopAccumulator) AddCounter(string, map[string]interface{}, map[string]string, ...time.Time)

func (*NopAccumulator) AddError

func (*NopAccumulator) AddError(error)

func (*NopAccumulator) AddFields

func (*NopAccumulator) AddFields(string, map[string]interface{}, map[string]string, ...time.Time)

func (*NopAccumulator) AddGauge

func (*NopAccumulator) AddGauge(string, map[string]interface{}, map[string]string, ...time.Time)

func (*NopAccumulator) AddHistogram

func (*NopAccumulator) AddHistogram(string, map[string]interface{}, map[string]string, ...time.Time)

func (*NopAccumulator) AddMetric

func (*NopAccumulator) AddMetric(telegraf.Metric)

func (*NopAccumulator) AddSummary

func (*NopAccumulator) AddSummary(string, map[string]interface{}, map[string]string, ...time.Time)

func (*NopAccumulator) SetPrecision

func (*NopAccumulator) SetPrecision(time.Duration)

func (*NopAccumulator) WithTracking

type PKIPaths

type PKIPaths struct {
	ServerPem  string
	ServerCert string
	ServerKey  string
	ClientCert string
}

type TestLogConsumer

type TestLogConsumer struct {
	Msgs []string
}

func (*TestLogConsumer) Accept

func (g *TestLogConsumer) Accept(l testcontainers.Log)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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