Documentation
¶
Overview ¶
Package touchtest exposes common prometheus testing utilities.
Example ¶
Example shows how to assert on metrics by describing the expected state in a separate registry. Expect loads that registry, after which the comparison methods check an actual Gatherer or Collector against it.
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/touchstone/touchtest"
)
// exampleT stands in for a *testing.T so the example can run outside a
// test. In real tests, pass the *testing.T directly to touchtest.New.
type exampleT struct{}
func (exampleT) Errorf(string, ...any) { fmt.Println("assertion failed") }
func (exampleT) FailNow() {}
func newCounter(r prometheus.Registerer, value float64) prometheus.Counter {
c := prometheus.NewCounter(prometheus.CounterOpts{
Name: "requests",
Help: "the total number of requests",
})
r.MustRegister(c)
c.Add(value)
return c
}
// Example shows how to assert on metrics by describing the expected state
// in a separate registry. Expect loads that registry, after which the
// comparison methods check an actual Gatherer or Collector against it.
func main() {
expected := prometheus.NewRegistry()
newCounter(expected, 3)
actual := prometheus.NewRegistry()
counter := newCounter(actual, 3)
a := touchtest.New(exampleT{})
a.Expect(expected)
fmt.Println(a.Registered("requests"))
fmt.Println(a.NotRegistered("other"))
fmt.Println(a.GatherAndCompare(actual, "requests"))
fmt.Println(a.CollectAndCompare(counter, "requests"))
// a change to the actual metric no longer matches the expectation
counter.Inc()
fmt.Println(a.CollectAndCompare(counter, "requests"))
}
Output: true true true true assertion failed false
Index ¶
- type Assertions
- func (a *Assertions) CollectAndCompare(actual prometheus.Collector, metricNames ...string) bool
- func (a *Assertions) Expect(g prometheus.Gatherer) *Assertions
- func (a *Assertions) GatherAndCompare(actual prometheus.Gatherer, metricNames ...string) bool
- func (a *Assertions) NotRegistered(metricNames ...string) bool
- func (a *Assertions) Registered(metricNames ...string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Assertions ¶
type Assertions struct {
// contains filtered or unexported fields
}
Assertions is a set of test verifications for metrics. Principally, this involves comparisons against an expected Gatherer.
func New ¶
func New(t require.TestingT) *Assertions
New creates an Assertions for the given testing environment.
func NewSuite ¶
func NewSuite(s suite.TestingSuite) *Assertions
NewSuite creates an Assertions for the given testing suite.
func (*Assertions) CollectAndCompare ¶
func (a *Assertions) CollectAndCompare(actual prometheus.Collector, metricNames ...string) bool
CollectAndCompare compares the actual collector against the current expectation previously set with Expect. This method fails the test on any error, then returns false. This method returns true if the expectation was met.
Use this method to run an assertion against a single metric that optionally has multiple submetrics.
func (*Assertions) Expect ¶
func (a *Assertions) Expect(g prometheus.Gatherer) *Assertions
Expect loads a given set of metrics into this set of assertions. Multiple assertions can be made against this expected set without reencoding.
If any errors occur while encoding the given expected Gatherer, the enclosing test is failed.
func (*Assertions) GatherAndCompare ¶
func (a *Assertions) GatherAndCompare(actual prometheus.Gatherer, metricNames ...string) bool
GatherAndCompare compares the actual Gatherer against the current expectation previously set with Expect. This method fails the test on any error, then returns false. This method returns true if the expectation was met.
Use this method to run an assertion against an entire prometheus registry.
func (*Assertions) NotRegistered ¶
func (a *Assertions) NotRegistered(metricNames ...string) bool
NotRegistered asserts that the given metric names are absent in the current expectation previously set with Expect.
func (*Assertions) Registered ¶
func (a *Assertions) Registered(metricNames ...string) bool
Registered asserts that the given metric names are present in the current expectation previously set with Expect.