2015-04-06 16:32:10 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-05-27 05:14:42 +00:00
|
|
|
import (
|
2016-01-06 23:11:16 +00:00
|
|
|
"encoding/json"
|
2015-05-27 05:14:42 +00:00
|
|
|
"fmt"
|
2015-06-20 12:32:32 +00:00
|
|
|
"reflect"
|
2015-10-16 14:54:33 +00:00
|
|
|
"sync"
|
2016-01-05 23:58:35 +00:00
|
|
|
"testing"
|
2015-10-16 18:54:05 +00:00
|
|
|
"time"
|
2016-01-05 23:58:35 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
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
|
2016-01-06 23:11:16 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
Points []*Point
|
2016-01-06 23:11:16 +00:00
|
|
|
debug bool
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-01-06 23:11:16 +00:00
|
|
|
if a.debug {
|
|
|
|
pretty, _ := json.MarshalIndent(fields, "", " ")
|
|
|
|
prettyTags, _ := json.MarshalIndent(tags, "", " ")
|
|
|
|
msg := fmt.Sprintf("Adding Measurement [%s]\nFields:%s\nTags:%s\n",
|
|
|
|
measurement, string(pretty), string(prettyTags))
|
|
|
|
fmt.Print(msg)
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:51:37 +00:00
|
|
|
p := &Point{
|
|
|
|
Measurement: measurement,
|
|
|
|
Fields: fields,
|
|
|
|
Tags: tags,
|
|
|
|
Time: t,
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:11:16 +00:00
|
|
|
a.Points = append(a.Points, p)
|
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.
|
2016-01-06 23:11:16 +00:00
|
|
|
return a.debug
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) SetDebug(debug bool) {
|
|
|
|
// stub for implementing Accumulator interface.
|
2016-01-06 23:11:16 +00:00
|
|
|
a.debug = debug
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:58:35 +00:00
|
|
|
// NFields returns the total number of fields in the accumulator, across all
|
|
|
|
// measurements
|
|
|
|
func (a *Accumulator) NFields() int {
|
|
|
|
counter := 0
|
|
|
|
for _, pt := range a.Points {
|
|
|
|
for _, _ = range pt.Fields {
|
|
|
|
counter++
|
2015-04-06 17:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 23:58:35 +00:00
|
|
|
return counter
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
2015-05-18 17:46:44 +00:00
|
|
|
|
2016-01-06 00:28:15 +00:00
|
|
|
func (a *Accumulator) AssertContainsTaggedFields(
|
2016-01-05 23:58:35 +00:00
|
|
|
t *testing.T,
|
2015-10-28 05:32:26 +00:00
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
2016-01-05 23:58:35 +00:00
|
|
|
) {
|
2015-10-28 05:32:26 +00:00
|
|
|
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) {
|
2016-01-06 23:11:16 +00:00
|
|
|
pActual, _ := json.MarshalIndent(p.Fields, "", " ")
|
|
|
|
pExp, _ := json.MarshalIndent(fields, "", " ")
|
|
|
|
msg := fmt.Sprintf("Actual:\n%s\n(%T) \nExpected:\n%s\n(%T)",
|
|
|
|
string(pActual), p.Fields, string(pExp), fields)
|
2016-01-05 23:58:35 +00:00
|
|
|
assert.Fail(t, msg)
|
2015-10-28 05:32:26 +00:00
|
|
|
}
|
2016-01-05 23:58:35 +00:00
|
|
|
return
|
2015-10-28 05:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 23:58:35 +00:00
|
|
|
msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
|
|
|
|
assert.Fail(t, msg)
|
2015-10-28 05:32:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 00:28:15 +00:00
|
|
|
func (a *Accumulator) AssertContainsFields(
|
|
|
|
t *testing.T,
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
) {
|
|
|
|
for _, p := range a.Points {
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
if !reflect.DeepEqual(fields, p.Fields) {
|
2016-01-06 23:11:16 +00:00
|
|
|
pActual, _ := json.MarshalIndent(p.Fields, "", " ")
|
|
|
|
pExp, _ := json.MarshalIndent(fields, "", " ")
|
|
|
|
msg := fmt.Sprintf("Actual:\n%s\n(%T) \nExpected:\n%s\n(%T)",
|
|
|
|
string(pActual), p.Fields, string(pExp), fields)
|
2016-01-06 00:28:15 +00:00
|
|
|
assert.Fail(t, msg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("unknown measurement %s", measurement)
|
|
|
|
assert.Fail(t, msg)
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// HasIntValue returns true if the measurement has an Int value
|
2016-01-05 23:58:35 +00:00
|
|
|
func (a *Accumulator) HasIntField(measurement string, field 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 {
|
2016-01-05 23:58:35 +00:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
if fieldname == field {
|
|
|
|
_, ok := value.(int64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-04 22:48:19 +00:00
|
|
|
// HasUIntValue returns true if the measurement has a UInt value
|
2016-01-05 23:58:35 +00:00
|
|
|
func (a *Accumulator) HasUIntField(measurement string, field string) bool {
|
2015-08-04 22:48:19 +00:00
|
|
|
for _, p := range a.Points {
|
|
|
|
if p.Measurement == measurement {
|
2016-01-05 23:58:35 +00:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
if fieldname == field {
|
|
|
|
_, ok := value.(uint64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 22:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
// HasFloatValue returns true if the given measurement has a float value
|
2016-01-05 23:58:35 +00:00
|
|
|
func (a *Accumulator) HasFloatField(measurement string, field 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 {
|
2016-01-05 23:58:35 +00:00
|
|
|
for fieldname, value := range p.Fields {
|
|
|
|
if fieldname == field {
|
|
|
|
_, ok := value.(float64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|