2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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 }
|