2015-04-06 16:32:10 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-05-27 05:14:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-20 12:32:32 +00:00
|
|
|
"reflect"
|
2015-10-16 14:54:33 +00:00
|
|
|
"sync"
|
2015-10-16 18:54:05 +00:00
|
|
|
"time"
|
2015-05-27 05:14:42 +00:00
|
|
|
)
|
2015-04-06 21:53:43 +00:00
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Point defines a single point measurement
|
2015-04-06 16:32:10 +00:00
|
|
|
type Point struct {
|
2015-05-29 20:25:48 +00:00
|
|
|
Measurement string
|
|
|
|
Tags map[string]string
|
2015-11-13 19:51:37 +00:00
|
|
|
Fields map[string]interface{}
|
2015-05-29 20:25:48 +00:00
|
|
|
Time time.Time
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 19:51:37 +00:00
|
|
|
func (p *Point) String() string {
|
|
|
|
return fmt.Sprintf("%s %v", p.Measurement, p.Fields)
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Accumulator defines a mocked out accumulator
|
2015-04-06 16:32:10 +00:00
|
|
|
type Accumulator struct {
|
2015-10-16 14:54:33 +00:00
|
|
|
sync.Mutex
|
2015-04-06 16:32:10 +00:00
|
|
|
Points []*Point
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// Add adds a measurement point to the accumulator
|
2015-10-16 22:13:32 +00:00
|
|
|
func (a *Accumulator) Add(
|
|
|
|
measurement string,
|
|
|
|
value interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
t ...time.Time,
|
|
|
|
) {
|
2015-11-13 19:51:37 +00:00
|
|
|
fields := map[string]interface{}{"value": value}
|
|
|
|
a.AddFields(measurement, fields, tags, t...)
|
2015-05-27 05:14:42 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
// AddFields adds a measurement point with a specified timestamp.
|
|
|
|
func (a *Accumulator) AddFields(
|
2015-05-29 20:25:48 +00:00
|
|
|
measurement string,
|
2015-11-13 19:51:37 +00:00
|
|
|
fields map[string]interface{},
|
2015-05-27 05:14:42 +00:00
|
|
|
tags map[string]string,
|
2015-10-16 22:13:32 +00:00
|
|
|
timestamp ...time.Time,
|
2015-05-27 05:14:42 +00:00
|
|
|
) {
|
2015-10-16 22:13:32 +00:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
2015-11-13 19:51:37 +00:00
|
|
|
if tags == nil {
|
|
|
|
tags = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
var t time.Time
|
|
|
|
if len(timestamp) > 0 {
|
|
|
|
t = timestamp[0]
|
|
|
|
} else {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
2015-11-13 19:51:37 +00:00
|
|
|
|
|
|
|
p := &Point{
|
|
|
|
Measurement: measurement,
|
|
|
|
Fields: fields,
|
|
|
|
Tags: tags,
|
|
|
|
Time: t,
|
|
|
|
}
|
|
|
|
|
2015-05-27 05:14:42 +00:00
|
|
|
a.Points = append(
|
|
|
|
a.Points,
|
2015-11-13 19:51:37 +00:00
|
|
|
p,
|
2015-05-27 05:14:42 +00:00
|
|
|
)
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
func (a *Accumulator) SetDefaultTags(tags map[string]string) {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) AddDefaultTag(key, value string) {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) Prefix() string {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) SetPrefix(prefix string) {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) Debug() bool {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) SetDebug(debug bool) {
|
|
|
|
// stub for implementing Accumulator interface.
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-11-02 11:09:30 +00:00
|
|
|
// CheckValue calls CheckFieldsValue passing a single-value map as fields
|
|
|
|
func (a *Accumulator) CheckValue(measurement string, val interface{}) bool {
|
|
|
|
return a.CheckFieldsValue(measurement, map[string]interface{}{"value": val})
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// CheckValue checks that the accumulators point for the given measurement
|
|
|
|
// is the same as the given value.
|
2015-11-02 11:09:30 +00:00
|
|
|
func (a *Accumulator) CheckFieldsValue(measurement string, fields map[string]interface{}) bool {
|
2015-04-06 16:32:10 +00:00
|
|
|
for _, p := range a.Points {
|
2015-05-29 20:25:48 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-11-13 19:51:37 +00:00
|
|
|
if reflect.DeepEqual(fields, p.Fields) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Measurement %s Failure, expected: %v, got %v\n",
|
|
|
|
measurement, fields, p.Fields)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-13 19:51:37 +00:00
|
|
|
fmt.Printf("Measurement %s, fields %s not found\n", measurement, fields)
|
2015-04-06 16:32:10 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-04-06 17:34:55 +00:00
|
|
|
|
2015-08-04 14:58:32 +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
|
|
|
}
|
|
|
|
|
2015-11-02 11:09:30 +00:00
|
|
|
// ValidateTaggedValue calls ValidateTaggedFieldsValue passing a single-value map as fields
|
2015-08-04 14:58:32 +00:00
|
|
|
func (a *Accumulator) ValidateTaggedValue(
|
|
|
|
measurement string,
|
|
|
|
val interface{},
|
|
|
|
tags map[string]string,
|
2015-11-02 11:09:30 +00:00
|
|
|
) error {
|
|
|
|
return a.ValidateTaggedFieldsValue(measurement, map[string]interface{}{"value": val}, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateValue calls ValidateTaggedValue
|
|
|
|
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {
|
|
|
|
return a.ValidateTaggedValue(measurement, val, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckTaggedFieldsValue calls ValidateTaggedFieldsValue
|
|
|
|
func (a *Accumulator) CheckTaggedFieldsValue(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
) bool {
|
|
|
|
return a.ValidateTaggedFieldsValue(measurement, fields, tags) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateTaggedValue validates that the given measurement and value exist
|
|
|
|
// in the accumulator and with the given tags.
|
|
|
|
func (a *Accumulator) ValidateTaggedFieldsValue(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
2015-08-04 14:58:32 +00:00
|
|
|
) 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-11-13 19:51:37 +00:00
|
|
|
if !reflect.DeepEqual(fields, p.Fields) {
|
|
|
|
return fmt.Errorf("%v != %v ", fields, p.Fields)
|
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
|
|
|
|
2015-11-02 11:09:30 +00:00
|
|
|
// ValidateFieldsValue calls ValidateTaggedFieldsValue
|
|
|
|
func (a *Accumulator) ValidateFieldsValue(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
) error {
|
|
|
|
return a.ValidateTaggedValue(measurement, fields, nil)
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
2015-05-18 17:46:44 +00:00
|
|
|
|
2015-10-28 05:32:26 +00:00
|
|
|
func (a *Accumulator) ValidateTaggedFields(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
) error {
|
|
|
|
if tags == nil {
|
|
|
|
tags = map[string]string{}
|
|
|
|
}
|
|
|
|
for _, p := range a.Points {
|
|
|
|
if !reflect.DeepEqual(tags, p.Tags) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Measurement == measurement {
|
2015-11-13 19:51:37 +00:00
|
|
|
if !reflect.DeepEqual(fields, p.Fields) {
|
2015-10-28 05:32:26 +00:00
|
|
|
return fmt.Errorf("%v (%T) != %v (%T)",
|
2015-11-13 19:51:37 +00:00
|
|
|
p.Fields, p.Fields, fields, fields)
|
2015-10-28 05:32:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +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-11-13 19:51:37 +00:00
|
|
|
_, ok := p.Fields["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 {
|
2015-11-13 19:51:37 +00:00
|
|
|
_, ok := p.Fields["value"].(uint64)
|
2015-08-04 22:48:19 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// 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-11-13 19:51:37 +00:00
|
|
|
_, ok := p.Fields["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
|
|
|
|
}
|