2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-07-11 22:54:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakeMetricNoFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Nil(t, m)
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:12:36 +00:00
|
|
|
// nil fields should get dropped
|
|
|
|
func TestMakeMetricNilFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-10-07 17:12:36 +00:00
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int(101),
|
|
|
|
"nil": nil,
|
|
|
|
},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-10-07 17:12:36 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
// make an untyped, counter, & gauge metric
|
|
|
|
func TestMakeMetric(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
assert.Equal(t, "inputs.TestRunningInput", ri.Name())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Untyped,
|
|
|
|
)
|
|
|
|
|
|
|
|
m = ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Counter,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Counter,
|
|
|
|
)
|
|
|
|
|
|
|
|
m = ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Gauge,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Gauge,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricWithPluginTags(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
Tags: map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-11-07 08:34:46 +00:00
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
nil,
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest,foo=bar value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricFilteredOut(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
Tags: map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-11-07 08:34:46 +00:00
|
|
|
Filter: Filter{NamePass: []string{"foobar"}},
|
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
assert.NoError(t, ri.Config.Filter.Compile())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
nil,
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Nil(t, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricWithDaemonTags(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetDefaultTags(map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
})
|
2016-11-07 08:34:46 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest,foo=bar value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make an untyped, counter, & gauge metric
|
|
|
|
func TestMakeMetricInfFields(t *testing.T) {
|
|
|
|
inf := math.Inf(1)
|
|
|
|
ninf := math.Inf(-1)
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int(101),
|
|
|
|
"inf": inf,
|
|
|
|
"ninf": ninf,
|
|
|
|
},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricAllFieldTypes(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetTrace(true)
|
|
|
|
assert.Equal(t, true, ri.Trace())
|
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": int(10),
|
|
|
|
"b": int8(10),
|
|
|
|
"c": int16(10),
|
|
|
|
"d": int32(10),
|
|
|
|
"e": uint(10),
|
|
|
|
"f": uint8(10),
|
|
|
|
"g": uint16(10),
|
|
|
|
"h": uint32(10),
|
|
|
|
"i": uint64(10),
|
|
|
|
"j": float32(10),
|
|
|
|
"k": uint64(9223372036854775810),
|
|
|
|
"l": "foobar",
|
|
|
|
"m": true,
|
|
|
|
},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
2016-11-28 18:19:35 +00:00
|
|
|
assert.Contains(t, m.String(), "a=10i")
|
|
|
|
assert.Contains(t, m.String(), "b=10i")
|
|
|
|
assert.Contains(t, m.String(), "c=10i")
|
|
|
|
assert.Contains(t, m.String(), "d=10i")
|
|
|
|
assert.Contains(t, m.String(), "e=10i")
|
|
|
|
assert.Contains(t, m.String(), "f=10i")
|
|
|
|
assert.Contains(t, m.String(), "g=10i")
|
|
|
|
assert.Contains(t, m.String(), "h=10i")
|
|
|
|
assert.Contains(t, m.String(), "i=10i")
|
|
|
|
assert.Contains(t, m.String(), "j=10")
|
|
|
|
assert.NotContains(t, m.String(), "j=10i")
|
|
|
|
assert.Contains(t, m.String(), "k=9223372036854775807i")
|
|
|
|
assert.Contains(t, m.String(), "l=\"foobar\"")
|
|
|
|
assert.Contains(t, m.String(), "m=true")
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNameOverride(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
NameOverride: "foobar",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("foobar value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNamePrefix(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
MeasurementPrefix: "foobar_",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("foobar_RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNameSuffix(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
MeasurementSuffix: "_foobar",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest_foobar value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
}
|
2016-11-07 08:34:46 +00:00
|
|
|
|
2017-07-11 22:54:38 +00:00
|
|
|
func TestMakeMetric_TrailingSlash(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
measurement string
|
|
|
|
fields map[string]interface{}
|
|
|
|
tags map[string]string
|
|
|
|
expectedNil bool
|
|
|
|
expectedMeasurement string
|
|
|
|
expectedFields map[string]interface{}
|
|
|
|
expectedTags map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Measurement cannot have trailing slash",
|
|
|
|
measurement: `cpu\`,
|
|
|
|
fields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
tags: map[string]string{},
|
|
|
|
expectedNil: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Field key with trailing slash dropped",
|
|
|
|
measurement: `cpu`,
|
|
|
|
fields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
`bad\`: `xyzzy`,
|
|
|
|
},
|
|
|
|
tags: map[string]string{},
|
|
|
|
expectedMeasurement: `cpu`,
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
expectedTags: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2017-08-31 04:16:37 +00:00
|
|
|
name: "Field value with trailing slash okay",
|
2017-07-11 22:54:38 +00:00
|
|
|
measurement: `cpu`,
|
|
|
|
fields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
2017-08-31 04:16:37 +00:00
|
|
|
"ok": `xyzzy\`,
|
2017-07-11 22:54:38 +00:00
|
|
|
},
|
|
|
|
tags: map[string]string{},
|
|
|
|
expectedMeasurement: `cpu`,
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
2017-08-31 04:16:37 +00:00
|
|
|
"ok": `xyzzy\`,
|
2017-07-11 22:54:38 +00:00
|
|
|
},
|
|
|
|
expectedTags: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Must have one field after dropped",
|
|
|
|
measurement: `cpu`,
|
|
|
|
fields: map[string]interface{}{
|
2017-08-31 04:16:37 +00:00
|
|
|
"bad": math.NaN(),
|
2017-07-11 22:54:38 +00:00
|
|
|
},
|
|
|
|
tags: map[string]string{},
|
|
|
|
expectedNil: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Tag key with trailing slash dropped",
|
|
|
|
measurement: `cpu`,
|
|
|
|
fields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
tags: map[string]string{
|
|
|
|
`host\`: "localhost",
|
|
|
|
"a": "x",
|
|
|
|
},
|
|
|
|
expectedMeasurement: `cpu`,
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
expectedTags: map[string]string{
|
|
|
|
"a": "x",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Tag value with trailing slash dropped",
|
|
|
|
measurement: `cpu`,
|
|
|
|
fields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
tags: map[string]string{
|
|
|
|
`host`: `localhost\`,
|
|
|
|
"a": "x",
|
|
|
|
},
|
|
|
|
expectedMeasurement: `cpu`,
|
|
|
|
expectedFields: map[string]interface{}{
|
|
|
|
"value": int64(42),
|
|
|
|
},
|
|
|
|
expectedTags: map[string]string{
|
|
|
|
"a": "x",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
m := ri.MakeMetric(
|
|
|
|
tc.measurement,
|
|
|
|
tc.fields,
|
|
|
|
tc.tags,
|
|
|
|
telegraf.Untyped,
|
|
|
|
now)
|
|
|
|
|
|
|
|
if tc.expectedNil {
|
|
|
|
require.Nil(t, m)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, m)
|
|
|
|
require.Equal(t, tc.expectedMeasurement, m.Name())
|
|
|
|
require.Equal(t, tc.expectedFields, m.Fields())
|
|
|
|
require.Equal(t, tc.expectedTags, m.Tags())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
type testInput struct{}
|
|
|
|
|
|
|
|
func (t *testInput) Description() string { return "" }
|
|
|
|
func (t *testInput) SampleConfig() string { return "" }
|
|
|
|
func (t *testInput) Gather(acc telegraf.Accumulator) error { return nil }
|