2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-09-28 21:48:20 +00:00
|
|
|
"sort"
|
2016-09-08 14:22:10 +00:00
|
|
|
"testing"
|
2018-09-28 21:48:20 +00:00
|
|
|
"time"
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// MockProcessor is a Processor with an overrideable Apply implementation.
|
|
|
|
type MockProcessor struct {
|
|
|
|
ApplyF func(in ...telegraf.Metric) []telegraf.Metric
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) SampleConfig() string {
|
|
|
|
return ""
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) Description() string {
|
|
|
|
return ""
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
return p.ApplyF(in...)
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// TagProcessor returns a Processor whose Apply function adds the tag and
|
|
|
|
// value.
|
|
|
|
func TagProcessor(key, value string) *MockProcessor {
|
|
|
|
return &MockProcessor{
|
|
|
|
ApplyF: func(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
for _, m := range in {
|
|
|
|
m.AddTag(key, value)
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func TestRunningProcessor_Apply(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
Processor telegraf.Processor
|
|
|
|
Config *ProcessorConfig
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
input []telegraf.Metric
|
|
|
|
expected []telegraf.Metric
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "inactive filter applies metrics",
|
|
|
|
args: args{
|
|
|
|
Processor: TagProcessor("apply", "true"),
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"apply": "true",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter applies",
|
|
|
|
args: args{
|
|
|
|
Processor: TagProcessor("apply", "true"),
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"cpu"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"apply": "true",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter doesn't apply",
|
|
|
|
args: args{
|
|
|
|
Processor: TagProcessor("apply", "true"),
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{
|
|
|
|
NameDrop: []string{"cpu"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rp := &RunningProcessor{
|
|
|
|
Processor: tt.args.Processor,
|
|
|
|
Config: tt.args.Config,
|
|
|
|
}
|
|
|
|
rp.Config.Filter.Compile()
|
|
|
|
|
|
|
|
actual := rp.Apply(tt.input...)
|
|
|
|
require.Equal(t, tt.expected, actual)
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func TestRunningProcessor_Order(t *testing.T) {
|
|
|
|
rp1 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 1,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
rp2 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 2,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
rp3 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 3,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
|
|
|
procs := RunningProcessors{rp2, rp3, rp1}
|
|
|
|
sort.Sort(procs)
|
|
|
|
require.Equal(t,
|
|
|
|
RunningProcessors{rp1, rp2, rp3},
|
|
|
|
procs)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|