testutils

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package testutils provides testing utilities and mock implementations for the Windows Agent Collector test suite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertMetricExists

func AssertMetricExists(t TestingT, metrics []prometheus.Metric, metricName string)

AssertMetricExists checks if a metric with the given name exists in the provided metrics. This is a common assertion used in collector tests.

func AssertMetricValue

func AssertMetricValue(t TestingT, metrics []prometheus.Metric, metricName string, expectedValue float64)

AssertMetricValue checks if a metric has the expected value. This is useful for validating specific metric values in tests.

func BenchmarkCollector

func BenchmarkCollector(b BenchmarkingT, newCollector func() collector.Collector)

BenchmarkCollector provides standard benchmarking for collectors. This ensures consistent performance measurement across all collector implementations.

func CreateTestMetric

func CreateTestMetric(name, help string, value float64, labels ...string) prometheus.Metric

CreateTestMetric creates a test metric for use in mock collectors. This is a utility function to simplify test metric creation.

Types

type BenchmarkingT

type BenchmarkingT interface {
	Error(args ...interface{})
	ResetTimer()
	ReportAllocs()
	N() int
}

BenchmarkingT is a minimal interface for benchmarking.

type CollectorTestSuite

type CollectorTestSuite struct {
	// NewCollector function to create collector instances for testing
	NewCollector func() collector.Collector

	// ExpectedMetrics lists the metric names that should be collected
	ExpectedMetrics []string

	// RequiresWindows indicates if the collector requires Windows to run
	RequiresWindows bool

	// MinimumMetricCount is the minimum number of metrics expected
	MinimumMetricCount int
}

CollectorTestSuite provides a standard test suite for collector implementations. This ensures all collectors follow the same testing patterns and coverage requirements.

func (*CollectorTestSuite) RunBasicTests

func (ts *CollectorTestSuite) RunBasicTests(t TestingT)

RunBasicTests executes a standard set of tests for any collector. This ensures consistent testing across all collector implementations.

type MockCollector

type MockCollector struct {
	// Name of the collector for identification
	Name string

	// Metrics to return when Collect() is called
	Metrics []prometheus.Metric

	// Error to return when Collect() is called (if not nil)
	CollectError error

	// CollectCallCount tracks how many times Collect was called
	CollectCallCount int

	// CollectDelay simulates collection time
	CollectDelay time.Duration

	// IsEnabled controls whether the collector should be considered enabled
	IsEnabled bool
}

MockCollector provides a test implementation of the collector interface for unit testing and integration testing scenarios.

func NewMockCollector

func NewMockCollector(name string) *MockCollector

NewMockCollector creates a new mock collector with default values.

func (*MockCollector) Build added in v1.0.4

func (m *MockCollector) Build(logger *slog.Logger, miSession *mi.Session) error

Build implements the collector.Collector interface. For the mock, this is a no-op.

func (*MockCollector) Close

func (m *MockCollector) Close() error

Close implements the collector.Collector interface. For the mock, this is a no-op but tracks that it was called.

func (*MockCollector) Collect

func (m *MockCollector) Collect(ch chan<- prometheus.Metric) error

Collect implements the collector.Collector interface for testing. It returns the configured metrics or error, and tracks call counts.

func (*MockCollector) GetName

func (m *MockCollector) GetName() string

GetName returns the collector name for identification.

func (*MockCollector) WithDelay

func (m *MockCollector) WithDelay(delay time.Duration) *MockCollector

WithDelay configures the mock to simulate collection delay.

func (*MockCollector) WithError

func (m *MockCollector) WithError(err error) *MockCollector

WithError configures the mock to return an error on Collect().

func (*MockCollector) WithMetrics

func (m *MockCollector) WithMetrics(metrics ...prometheus.Metric) *MockCollector

WithMetrics configures the mock to return specific metrics.

type MockRegistry

type MockRegistry struct {
	// Metrics to return when Gather() is called
	MetricFamilies []*dto.MetricFamily

	// Error to return when Gather() is called
	GatherError error

	// GatherCallCount tracks how many times Gather was called
	GatherCallCount int
}

MockRegistry provides a test implementation of prometheus.Gatherer for testing push gateway integration.

func NewMockRegistry

func NewMockRegistry() *MockRegistry

NewMockRegistry creates a new mock registry.

func (*MockRegistry) Gather

func (m *MockRegistry) Gather() ([]*dto.MetricFamily, error)

Gather implements the prometheus.Gatherer interface for testing.

func (*MockRegistry) WithError

func (m *MockRegistry) WithError(err error) *MockRegistry

WithError configures the mock to return an error on Gather().

func (*MockRegistry) WithMetricFamilies

func (m *MockRegistry) WithMetricFamilies(families ...*dto.MetricFamily) *MockRegistry

WithMetricFamilies configures the mock to return specific metric families.

type TestingT

type TestingT interface {
	Helper()
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Run(name string, f func(TestingT)) bool
}

TestingT is a minimal interface for testing, compatible with testing.T and testing.B. This allows the test utilities to work with both unit tests and benchmarks.

Jump to

Keyboard shortcuts

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