testutils

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At(ts float64) float64

At is a convenience method to allow for declarative syntax of Acceptance test definitions.

func CompareCollectors

func CompareCollectors(a, b *Collector, opts *AcceptanceOpts) (bool, error)

CompareCollectors compares two collectors based on their collected alerts.

func EqualAlerts

func EqualAlerts(a, b *models.GettableAlert, opts *AcceptanceOpts) bool

EqualAlerts compares two alerts for equality, considering the tolerance.

func EqualTime

func EqualTime(a, b time.Time, opts *AcceptanceOpts) bool

EqualTime compares two times for equality within the tolerance.

Types

type AcceptanceOpts

type AcceptanceOpts struct {
	FeatureFlags []string
	RoutePrefix  string
	Tolerance    time.Duration
	// contains filtered or unexported fields
}

AcceptanceOpts defines configuration parameters for an acceptance test.

func (*AcceptanceOpts) AlertString

func (opts *AcceptanceOpts) AlertString(a *models.GettableAlert) string

AlertString formats an alert for display with relative times.

func (*AcceptanceOpts) ExpandTime

func (opts *AcceptanceOpts) ExpandTime(rel float64) time.Time

ExpandTime returns the absolute time for the relative time calculated from the test's base time.

func (*AcceptanceOpts) RelativeTime

func (opts *AcceptanceOpts) RelativeTime(act time.Time) float64

RelativeTime returns the relative time for the given time calculated from the test's base time.

func (*AcceptanceOpts) SetBaseTime

func (opts *AcceptanceOpts) SetBaseTime(t time.Time)

SetBaseTime sets the base time for relative time calculations.

type AcceptanceTest

type AcceptanceTest struct {
	*testing.T
	// contains filtered or unexported fields
}

AcceptanceTest provides declarative definition of given inputs and expected output of an Alertmanager setup.

func NewAcceptanceTest

func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest

NewAcceptanceTest returns a new acceptance test with the base time set to the current time.

func (*AcceptanceTest) AlertmanagerCluster

func (t *AcceptanceTest) AlertmanagerCluster(conf string, size int) *AlertmanagerCluster

AlertmanagerCluster returns a new AlertmanagerCluster that allows starting a cluster of Alertmanager instances on random ports.

func (*AcceptanceTest) Collector

func (t *AcceptanceTest) Collector(name string) *Collector

Collector returns a new collector bound to the test instance.

func (*AcceptanceTest) Do

func (t *AcceptanceTest) Do(at float64, f func())

Do sets the given function to be executed at the given time.

func (*AcceptanceTest) Run

func (t *AcceptanceTest) Run(additionalArgs ...string)

Run starts all Alertmanagers and runs queries against them. It then checks whether all expected notifications have arrived at the expected receiver.

type Alertmanager

type Alertmanager struct {
	T    *AcceptanceTest
	Opts *AcceptanceOpts
	// contains filtered or unexported fields
}

Alertmanager encapsulates an Alertmanager process and allows declaring alerts being pushed to it at fixed points in time.

func (*Alertmanager) APIAddr

func (am *Alertmanager) APIAddr() string

APIAddr returns the API address for the instance.

func (*Alertmanager) Client

func (am *Alertmanager) Client() *apiclient.AlertmanagerAPI

Client returns a client to interact with the API v2 endpoint.

func (*Alertmanager) ClusterAddr

func (am *Alertmanager) ClusterAddr() string

ClusterAddr returns an address for the cluster.

func (*Alertmanager) Reload

func (am *Alertmanager) Reload()

Reload sends the reloading signal to the Alertmanager process.

func (*Alertmanager) Start

func (am *Alertmanager) Start(additionalArg []string) error

Start the alertmanager and wait until it is ready to receive.

func (*Alertmanager) Terminate

func (am *Alertmanager) Terminate()

Terminate kills the underlying Alertmanager process and remove intermediate data.

func (*Alertmanager) UpdateConfig

func (am *Alertmanager) UpdateConfig(conf string)

UpdateConfig rewrites the configuration file for the Alertmanager. It does not initiate config reloading.

func (*Alertmanager) WaitForCluster

func (am *Alertmanager) WaitForCluster(size int) error

WaitForCluster waits for the Alertmanager instance to join a cluster with the given size.

type AlertmanagerCluster

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

AlertmanagerCluster represents a group of Alertmanager instances acting as a cluster.

func (*AlertmanagerCluster) Members

func (amc *AlertmanagerCluster) Members() []*Alertmanager

Members returns the underlying slice of cluster members.

func (*AlertmanagerCluster) Reload

func (amc *AlertmanagerCluster) Reload()

Reload sends the reloading signal to the Alertmanager instances.

func (*AlertmanagerCluster) Start

func (amc *AlertmanagerCluster) Start(additionalArgs ...string) error

Start the Alertmanager cluster and wait until it is ready to receive.

func (*AlertmanagerCluster) Terminate

func (amc *AlertmanagerCluster) Terminate()

Terminate kills the underlying Alertmanager cluster processes and removes intermediate data.

func (*AlertmanagerCluster) UpdateConfig

func (amc *AlertmanagerCluster) UpdateConfig(conf string)

UpdateConfig rewrites the configuration file for the Alertmanager cluster. It does not initiate config reloading.

type Collector

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

Collector gathers alerts received by a notification receiver and verifies whether all arrived and within the correct time boundaries.

func NewCollector

func NewCollector(t *testing.T, name string, opts *AcceptanceOpts) *Collector

NewCollector creates a new Collector with the given parameters.

func (*Collector) Add

func (c *Collector) Add(alerts ...*models.GettableAlert)

Add the given alerts to the collected alerts. This is exported so it can be used by MockWebhook implementations.

func (*Collector) Check

func (c *Collector) Check() string

func (*Collector) Collected

func (c *Collector) Collected() map[float64][]models.GettableAlerts

Collected returns a map of alerts collected by the collector indexed with the receive timestamp.

func (*Collector) Latest

func (c *Collector) Latest() float64

Latest returns the latest relative point in time where a notification is expected.

func (*Collector) Opts

func (c *Collector) Opts() *AcceptanceOpts

Opts returns the acceptance options for this collector.

func (*Collector) String

func (c *Collector) String() string

func (*Collector) Want

func (c *Collector) Want(iv Interval, alerts ...*TestAlert)

Want declares that the Collector expects to receive the given alerts within the given time boundaries.

type Interval

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

func Between

func Between(start, end float64) Interval

Between is a convenience constructor for an interval for declarative syntax of Acceptance test definitions.

func (Interval) String

func (iv Interval) String() string

type MockWebhook

type MockWebhook struct {

	// Func is called early on when retrieving a notification by an
	// Alertmanager. If Func returns true, the given notification is dropped.
	// See sample usage in `send_test.go/TestRetry()`.
	Func func(timestamp float64) bool
	// contains filtered or unexported fields
}

MockWebhook provides a mock HTTP webhook receiver for testing.

func NewWebhook

func NewWebhook(t *testing.T, c *Collector) *MockWebhook

NewWebhook creates a new MockWebhook that collects alerts via HTTP.

func (*MockWebhook) Address

func (ws *MockWebhook) Address() string

Address returns the address of the mock webhook server.

func (*MockWebhook) ServeHTTP

func (ws *MockWebhook) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP handles incoming webhook requests.

type TestAlert

type TestAlert struct {
	Labels           models.LabelSet
	Annotations      models.LabelSet
	StartsAt, EndsAt float64
	Summary          string // CLI-specific field, unused in with_api_v2
}

TestAlert models a model.Alert with relative times.

func Alert

func Alert(keyval ...any) *TestAlert

Alert creates a new alert declaration with the given key/value pairs as identifying labels.

func (*TestAlert) Active

func (a *TestAlert) Active(tss ...float64) *TestAlert

Active declares the relative activity time for this alert. It must be a single starting value or two values where the second value declares the resolved time.

func (*TestAlert) Annotate

func (a *TestAlert) Annotate(keyval ...any) *TestAlert

Annotate the alert with the given key/value pairs.

func (*TestAlert) HasLabels

func (a *TestAlert) HasLabels(labels models.LabelSet) bool

HasLabels returns true if the two label sets are equivalent, otherwise false. CLI-specific method, unused in with_api_v2.

func (*TestAlert) NativeAlert

func (a *TestAlert) NativeAlert(opts *AcceptanceOpts) *models.GettableAlert

NativeAlert converts the declared test alert into a full alert based on the given parameters.

Jump to

Keyboard shortcuts

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