2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
"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,
|
|
|
|
)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-03-28 00:30:51 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int(101),
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
expected, err := metric.New("RITest",
|
|
|
|
map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest",
|
|
|
|
map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2018-03-28 00:30:51 +00:00
|
|
|
"value": 101,
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
|
|
|
now,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
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,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("foobar",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("foobar_RITest",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest_foobar",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
2017-07-11 22:54:38 +00:00
|
|
|
},
|
2018-03-28 00:30:51 +00:00
|
|
|
now,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2017-07-11 22:54:38 +00:00
|
|
|
}
|
|
|
|
|
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 }
|