telegraf/testutil/accumulator.go

124 lines
2.1 KiB
Go
Raw Normal View History

package testutil
import (
"fmt"
"time"
)
2015-04-06 21:53:43 +00:00
type Point struct {
Name string
Value interface{}
Tags map[string]string
Values map[string]interface{}
Time time.Time
}
type Accumulator struct {
Points []*Point
}
func (a *Accumulator) Add(name string, value interface{}, tags map[string]string) {
a.Points = append(
a.Points,
&Point{
Name: name,
Value: value,
Tags: tags,
},
)
}
func (a *Accumulator) AddValuesWithTime(
name string,
values map[string]interface{},
tags map[string]string,
timestamp time.Time,
) {
a.Points = append(
a.Points,
&Point{
Name: name,
Values: values,
Tags: tags,
Time: timestamp,
},
)
}
2015-05-18 17:46:44 +00:00
func (a *Accumulator) Get(name string) (*Point, bool) {
for _, p := range a.Points {
if p.Name == name {
return p, true
}
}
return nil, false
}
func (a *Accumulator) CheckValue(name string, val interface{}) bool {
for _, p := range a.Points {
if p.Name == name {
return p.Value == val
}
}
return false
}
2015-04-06 17:34:55 +00:00
func (a *Accumulator) CheckTaggedValue(name string, val interface{}, tags map[string]string) bool {
2015-04-06 21:53:43 +00:00
return a.ValidateTaggedValue(name, val, tags) == nil
}
func (a *Accumulator) ValidateTaggedValue(name string, val interface{}, tags map[string]string) error {
2015-04-06 17:34:55 +00:00
for _, p := range a.Points {
2015-04-06 21:53:43 +00:00
var found bool
if p.Tags == nil && tags == nil {
found = true
} else {
for k, v := range p.Tags {
if tags[k] == v {
found = true
break
}
2015-04-06 17:34:55 +00:00
}
}
2015-04-06 21:53:43 +00:00
if found && p.Name == name {
if p.Value != val {
return fmt.Errorf("%v (%T) != %v (%T)", p.Value, p.Value, val, val)
}
return nil
2015-04-06 17:34:55 +00:00
}
}
2015-04-06 21:53:43 +00:00
return fmt.Errorf("unknown value %s with tags %v", name, tags)
2015-04-06 17:34:55 +00:00
}
2015-04-07 18:54:21 +00:00
func (a *Accumulator) ValidateValue(name string, val interface{}) error {
return a.ValidateTaggedValue(name, val, nil)
}
2015-05-18 17:46:44 +00:00
func (a *Accumulator) HasIntValue(name string) bool {
for _, p := range a.Points {
if p.Name == name {
_, ok := p.Value.(int64)
return ok
}
}
return false
}
func (a *Accumulator) HasFloatValue(name string) bool {
for _, p := range a.Points {
if p.Name == name {
_, ok := p.Value.(float64)
return ok
}
}
return false
}