telegraf/testutil/accumulator.go

169 lines
3.8 KiB
Go
Raw Normal View History

package testutil
import (
"fmt"
2015-06-20 12:32:32 +00:00
"reflect"
"time"
2015-10-16 14:54:33 +00:00
"sync"
)
2015-04-06 21:53:43 +00:00
// Point defines a single point measurement
type Point struct {
2015-05-29 20:25:48 +00:00
Measurement string
Tags map[string]string
Values map[string]interface{}
Time time.Time
}
// Accumulator defines a mocked out accumulator
type Accumulator struct {
2015-10-16 14:54:33 +00:00
sync.Mutex
Points []*Point
}
// Add adds a measurement point to the accumulator
2015-05-29 20:25:48 +00:00
func (a *Accumulator) Add(measurement string, value interface{}, tags map[string]string) {
2015-10-16 14:54:33 +00:00
a.Lock()
defer a.Unlock()
2015-06-20 12:32:32 +00:00
if tags == nil {
tags = map[string]string{}
}
a.Points = append(
a.Points,
&Point{
2015-06-18 04:43:45 +00:00
Measurement: measurement,
2015-07-07 01:20:11 +00:00
Values: map[string]interface{}{"value": value},
2015-05-29 20:25:48 +00:00
Tags: tags,
},
)
}
// AddFieldsWithTime adds a measurement point with a specified timestamp.
func (a *Accumulator) AddFieldsWithTime(
2015-05-29 20:25:48 +00:00
measurement string,
values map[string]interface{},
tags map[string]string,
timestamp time.Time,
) {
a.Points = append(
a.Points,
&Point{
2015-05-29 20:25:48 +00:00
Measurement: measurement,
Values: values,
Tags: tags,
Time: timestamp,
},
)
}
// Get gets the specified measurement point from the accumulator
2015-05-29 20:25:48 +00:00
func (a *Accumulator) Get(measurement string) (*Point, bool) {
2015-05-18 17:46:44 +00:00
for _, p := range a.Points {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2015-05-18 17:46:44 +00:00
return p, true
}
}
return nil, false
}
// CheckValue checks that the accumulators point for the given measurement
// is the same as the given value.
2015-05-29 20:25:48 +00:00
func (a *Accumulator) CheckValue(measurement string, val interface{}) bool {
for _, p := range a.Points {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2015-07-07 01:20:11 +00:00
return p.Values["value"] == val
}
}
fmt.Printf("CheckValue failed, measurement %s, value %s", measurement, val)
return false
}
2015-04-06 17:34:55 +00:00
// CheckTaggedValue calls ValidateTaggedValue
func (a *Accumulator) CheckTaggedValue(
measurement string,
val interface{},
tags map[string]string,
) bool {
2015-05-29 20:25:48 +00:00
return a.ValidateTaggedValue(measurement, val, tags) == nil
2015-04-06 21:53:43 +00:00
}
// ValidateTaggedValue validates that the given measurement and value exist
// in the accumulator and with the given tags.
func (a *Accumulator) ValidateTaggedValue(
measurement string,
val interface{},
tags map[string]string,
) error {
2015-06-20 12:32:32 +00:00
if tags == nil {
tags = map[string]string{}
}
2015-04-06 17:34:55 +00:00
for _, p := range a.Points {
2015-06-20 12:32:32 +00:00
if !reflect.DeepEqual(tags, p.Tags) {
continue
2015-04-06 17:34:55 +00:00
}
2015-06-20 12:32:32 +00:00
if p.Measurement == measurement {
2015-07-07 01:20:11 +00:00
if p.Values["value"] != val {
return fmt.Errorf("%v (%T) != %v (%T)",
p.Values["value"], p.Values["value"], val, val)
2015-04-06 21:53:43 +00:00
}
return nil
2015-04-06 17:34:55 +00:00
}
}
2015-06-20 12:32:32 +00:00
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
2015-04-06 17:34:55 +00:00
}
2015-04-07 18:54:21 +00:00
// ValidateValue calls ValidateTaggedValue
2015-05-29 20:25:48 +00:00
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {
return a.ValidateTaggedValue(measurement, val, nil)
2015-04-07 18:54:21 +00:00
}
2015-05-18 17:46:44 +00:00
// HasIntValue returns true if the measurement has an Int value
2015-05-29 20:25:48 +00:00
func (a *Accumulator) HasIntValue(measurement string) bool {
2015-05-18 17:46:44 +00:00
for _, p := range a.Points {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2015-07-07 01:20:11 +00:00
_, ok := p.Values["value"].(int64)
2015-05-18 17:46:44 +00:00
return ok
}
}
return false
}
2015-08-04 22:48:19 +00:00
// HasUIntValue returns true if the measurement has a UInt value
func (a *Accumulator) HasUIntValue(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
_, ok := p.Values["value"].(uint64)
return ok
}
}
return false
}
// HasFloatValue returns true if the given measurement has a float value
2015-05-29 20:25:48 +00:00
func (a *Accumulator) HasFloatValue(measurement string) bool {
2015-05-18 17:46:44 +00:00
for _, p := range a.Points {
2015-05-29 20:25:48 +00:00
if p.Measurement == measurement {
2015-07-07 01:20:11 +00:00
_, ok := p.Values["value"].(float64)
2015-05-18 17:46:44 +00:00
return ok
}
}
return false
}
2015-09-02 23:16:52 +00:00
// HasMeasurement returns true if the accumulator has a measurement with the
// given name
func (a *Accumulator) HasMeasurement(measurement string) bool {
for _, p := range a.Points {
if p.Measurement == measurement {
return true
}
}
return false
}