2018-07-25 02:29:00 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2019-05-15 21:46:28 +00:00
|
|
|
"reflect"
|
2018-09-05 21:50:32 +00:00
|
|
|
"sort"
|
2018-07-25 02:29:00 +00:00
|
|
|
"testing"
|
2018-09-05 21:50:32 +00:00
|
|
|
"time"
|
2018-07-25 02:29:00 +00:00
|
|
|
|
2018-09-05 21:50:32 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-05-15 21:46:28 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-07-25 02:29:00 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-09-05 21:50:32 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2018-07-25 02:29:00 +00:00
|
|
|
)
|
|
|
|
|
2018-09-05 21:50:32 +00:00
|
|
|
type metricDiff struct {
|
|
|
|
Measurement string
|
|
|
|
Tags []*telegraf.Tag
|
|
|
|
Fields []*telegraf.Field
|
|
|
|
Type telegraf.ValueType
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:46:28 +00:00
|
|
|
func lessFunc(lhs, rhs *metricDiff) bool {
|
|
|
|
if lhs.Measurement != rhs.Measurement {
|
|
|
|
return lhs.Measurement < rhs.Measurement
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
if i >= len(lhs.Tags) && i >= len(rhs.Tags) {
|
|
|
|
break
|
|
|
|
} else if i >= len(lhs.Tags) {
|
|
|
|
return true
|
|
|
|
} else if i >= len(rhs.Tags) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhs.Tags[i].Key != rhs.Tags[i].Key {
|
|
|
|
return lhs.Tags[i].Key < rhs.Tags[i].Key
|
|
|
|
}
|
|
|
|
if lhs.Tags[i].Value != rhs.Tags[i].Value {
|
|
|
|
return lhs.Tags[i].Value < rhs.Tags[i].Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
if i >= len(lhs.Fields) && i >= len(rhs.Fields) {
|
|
|
|
break
|
|
|
|
} else if i >= len(lhs.Fields) {
|
|
|
|
return true
|
|
|
|
} else if i >= len(rhs.Fields) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhs.Fields[i].Key != rhs.Fields[i].Key {
|
|
|
|
return lhs.Fields[i].Key < rhs.Fields[i].Key
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhs.Fields[i].Value != rhs.Fields[i].Value {
|
|
|
|
ltype := reflect.TypeOf(lhs.Fields[i].Value)
|
|
|
|
rtype := reflect.TypeOf(lhs.Fields[i].Value)
|
|
|
|
|
|
|
|
if ltype.Kind() != rtype.Kind() {
|
|
|
|
return ltype.Kind() < rtype.Kind()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := lhs.Fields[i].Value.(type) {
|
|
|
|
case int64:
|
|
|
|
return v < lhs.Fields[i].Value.(int64)
|
|
|
|
case uint64:
|
|
|
|
return v < lhs.Fields[i].Value.(uint64)
|
|
|
|
case float64:
|
|
|
|
return v < lhs.Fields[i].Value.(float64)
|
|
|
|
case string:
|
|
|
|
return v < lhs.Fields[i].Value.(string)
|
|
|
|
case bool:
|
|
|
|
return !v
|
|
|
|
default:
|
|
|
|
panic("unknown type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhs.Type != rhs.Type {
|
|
|
|
return lhs.Type < rhs.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhs.Time.UnixNano() != rhs.Time.UnixNano() {
|
|
|
|
return lhs.Time.UnixNano() < rhs.Time.UnixNano()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-05 21:50:32 +00:00
|
|
|
func newMetricDiff(metric telegraf.Metric) *metricDiff {
|
2019-01-15 19:48:52 +00:00
|
|
|
if metric == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 21:50:32 +00:00
|
|
|
m := &metricDiff{}
|
|
|
|
m.Measurement = metric.Name()
|
|
|
|
|
|
|
|
for _, tag := range metric.TagList() {
|
|
|
|
m.Tags = append(m.Tags, tag)
|
|
|
|
}
|
|
|
|
sort.Slice(m.Tags, func(i, j int) bool {
|
|
|
|
return m.Tags[i].Key < m.Tags[j].Key
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, field := range metric.FieldList() {
|
|
|
|
m.Fields = append(m.Fields, field)
|
|
|
|
}
|
|
|
|
sort.Slice(m.Fields, func(i, j int) bool {
|
|
|
|
return m.Fields[i].Key < m.Fields[j].Key
|
|
|
|
})
|
|
|
|
|
|
|
|
m.Type = metric.Type()
|
|
|
|
m.Time = metric.Time()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:46:28 +00:00
|
|
|
// SortMetrics enables sorting metrics before comparison.
|
|
|
|
func SortMetrics() cmp.Option {
|
|
|
|
return cmpopts.SortSlices(lessFunc)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:54:12 +00:00
|
|
|
// IgnoreTime disables comparison of timestamp.
|
|
|
|
func IgnoreTime() cmp.Option {
|
|
|
|
return cmpopts.IgnoreFields(metricDiff{}, "Time")
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:46:28 +00:00
|
|
|
// MetricEqual returns true if the metrics are equal.
|
2019-09-20 03:03:10 +00:00
|
|
|
func MetricEqual(expected, actual telegraf.Metric, opts ...cmp.Option) bool {
|
2018-11-05 21:34:28 +00:00
|
|
|
var lhs, rhs *metricDiff
|
|
|
|
if expected != nil {
|
|
|
|
lhs = newMetricDiff(expected)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
rhs = newMetricDiff(actual)
|
|
|
|
}
|
|
|
|
|
2019-09-20 03:03:10 +00:00
|
|
|
opts = append(opts, cmpopts.EquateNaNs())
|
|
|
|
return cmp.Equal(lhs, rhs, opts...)
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 21:46:28 +00:00
|
|
|
// RequireMetricEqual halts the test with an error if the metrics are not
|
|
|
|
// equal.
|
2019-06-25 19:04:39 +00:00
|
|
|
func RequireMetricEqual(t *testing.T, expected, actual telegraf.Metric, opts ...cmp.Option) {
|
2018-09-05 21:50:32 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var lhs, rhs *metricDiff
|
|
|
|
if expected != nil {
|
|
|
|
lhs = newMetricDiff(expected)
|
|
|
|
}
|
|
|
|
if actual != nil {
|
|
|
|
rhs = newMetricDiff(actual)
|
|
|
|
}
|
|
|
|
|
2019-09-20 03:03:10 +00:00
|
|
|
opts = append(opts, cmpopts.EquateNaNs())
|
2019-06-25 19:04:39 +00:00
|
|
|
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
|
2018-09-05 21:50:32 +00:00
|
|
|
t.Fatalf("telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:46:28 +00:00
|
|
|
// RequireMetricsEqual halts the test with an error if the array of metrics
|
|
|
|
// are not equal.
|
|
|
|
func RequireMetricsEqual(t *testing.T, expected, actual []telegraf.Metric, opts ...cmp.Option) {
|
2018-09-05 21:50:32 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
lhs := make([]*metricDiff, 0, len(expected))
|
2018-09-05 21:50:32 +00:00
|
|
|
for _, m := range expected {
|
|
|
|
lhs = append(lhs, newMetricDiff(m))
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
rhs := make([]*metricDiff, 0, len(actual))
|
2018-09-05 21:50:32 +00:00
|
|
|
for _, m := range actual {
|
|
|
|
rhs = append(rhs, newMetricDiff(m))
|
|
|
|
}
|
2019-09-20 03:03:10 +00:00
|
|
|
|
|
|
|
opts = append(opts, cmpopts.EquateNaNs())
|
2019-05-15 21:46:28 +00:00
|
|
|
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
|
2018-09-05 21:50:32 +00:00
|
|
|
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metric creates a new metric or panics on error.
|
|
|
|
func MustMetric(
|
|
|
|
name string,
|
|
|
|
tags map[string]string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tm time.Time,
|
|
|
|
tp ...telegraf.ValueType,
|
|
|
|
) telegraf.Metric {
|
|
|
|
m, err := metric.New(name, tags, fields, tm, tp...)
|
|
|
|
if err != nil {
|
|
|
|
panic("MustMetric")
|
|
|
|
}
|
|
|
|
return m
|
2018-07-25 02:29:00 +00:00
|
|
|
}
|
2019-02-20 21:23:59 +00:00
|
|
|
|
|
|
|
func FromTestMetric(met *Metric) telegraf.Metric {
|
2019-11-27 01:31:36 +00:00
|
|
|
m, err := metric.New(met.Measurement, met.Tags, met.Fields, met.Time, met.Type)
|
2019-02-20 21:23:59 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("MustMetric")
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|