Add final aggregator (#5820)

This commit is contained in:
Daniel Nelson
2019-05-15 14:46:28 -07:00
committed by GitHub
parent d645e0303f
commit a724bf487f
7 changed files with 402 additions and 4 deletions

View File

@@ -1,11 +1,13 @@
package testutil
import (
"reflect"
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
@@ -18,6 +20,77 @@ type metricDiff struct {
Time time.Time
}
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
}
func newMetricDiff(metric telegraf.Metric) *metricDiff {
if metric == nil {
return nil
@@ -45,6 +118,12 @@ func newMetricDiff(metric telegraf.Metric) *metricDiff {
return m
}
// SortMetrics enables sorting metrics before comparison.
func SortMetrics() cmp.Option {
return cmpopts.SortSlices(lessFunc)
}
// MetricEqual returns true if the metrics are equal.
func MetricEqual(expected, actual telegraf.Metric) bool {
var lhs, rhs *metricDiff
if expected != nil {
@@ -57,6 +136,8 @@ func MetricEqual(expected, actual telegraf.Metric) bool {
return cmp.Equal(lhs, rhs)
}
// RequireMetricEqual halts the test with an error if the metrics are not
// equal.
func RequireMetricEqual(t *testing.T, expected, actual telegraf.Metric) {
t.Helper()
@@ -73,7 +154,9 @@ func RequireMetricEqual(t *testing.T, expected, actual telegraf.Metric) {
}
}
func RequireMetricsEqual(t *testing.T, expected, actual []telegraf.Metric) {
// 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) {
t.Helper()
lhs := make([]*metricDiff, 0, len(expected))
@@ -84,7 +167,7 @@ func RequireMetricsEqual(t *testing.T, expected, actual []telegraf.Metric) {
for _, m := range actual {
rhs = append(rhs, newMetricDiff(m))
}
if diff := cmp.Diff(lhs, rhs); diff != "" {
if diff := cmp.Diff(lhs, rhs, opts...); diff != "" {
t.Fatalf("[]telegraf.Metric\n--- expected\n+++ actual\n%s", diff)
}
}

View File

@@ -4,18 +4,19 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
func TestRequireMetricsEqual(t *testing.T) {
func TestRequireMetricEqual(t *testing.T) {
tests := []struct {
name string
got telegraf.Metric
want telegraf.Metric
}{
{
name: "telegraf and testutil metrics should be equal",
name: "equal metrics should be equal",
got: func() telegraf.Metric {
m, _ := metric.New(
"test",
@@ -56,3 +57,50 @@ func TestRequireMetricsEqual(t *testing.T) {
})
}
}
func TestRequireMetricsEqual(t *testing.T) {
tests := []struct {
name string
got []telegraf.Metric
want []telegraf.Metric
opts []cmp.Option
}{
{
name: "sort metrics option sorts by name",
got: []telegraf.Metric{
MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Unix(0, 0),
),
MustMetric(
"net",
map[string]string{},
map[string]interface{}{},
time.Unix(0, 0),
),
},
want: []telegraf.Metric{
MustMetric(
"net",
map[string]string{},
map[string]interface{}{},
time.Unix(0, 0),
),
MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Unix(0, 0),
),
},
opts: []cmp.Option{SortMetrics()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RequireMetricsEqual(t, tt.want, tt.got, tt.opts...)
})
}
}