2015-11-24 21:22:11 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/exec"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/memcached"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/procstat"
|
2015-11-24 21:22:11 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func TestConfig_LoadSingleInput(t *testing.T) {
|
2015-11-24 21:22:11 +00:00
|
|
|
c := NewConfig()
|
|
|
|
c.LoadConfig("./testdata/single_plugin.toml")
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
memcached := inputs.Inputs["memcached"]().(*memcached.Memcached)
|
2015-11-24 21:22:11 +00:00
|
|
|
memcached.Servers = []string{"localhost"}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
mConfig := &InputConfig{
|
2015-11-24 21:22:11 +00:00
|
|
|
Name: "memcached",
|
2015-12-01 14:15:28 +00:00
|
|
|
Filter: Filter{
|
|
|
|
Drop: []string{"other", "stuff"},
|
|
|
|
Pass: []string{"some", "strings"},
|
|
|
|
TagDrop: []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "badtag",
|
|
|
|
Filter: []string{"othertag"},
|
|
|
|
},
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
2015-12-01 14:15:28 +00:00
|
|
|
TagPass: []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "goodtag",
|
|
|
|
Filter: []string{"mytag"},
|
|
|
|
},
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
2015-12-01 14:15:28 +00:00
|
|
|
IsActive: true,
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
}
|
2016-01-07 17:23:38 +00:00
|
|
|
mConfig.Tags = make(map[string]string)
|
2015-11-24 21:22:11 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, memcached, c.Inputs[0].Input,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce a correct memcached struct.")
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, mConfig, c.Inputs[0].Config,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce correct memcached metadata.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_LoadDirectory(t *testing.T) {
|
|
|
|
c := NewConfig()
|
|
|
|
err := c.LoadConfig("./testdata/single_plugin.toml")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = c.LoadDirectory("./testdata/subconfig")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
memcached := inputs.Inputs["memcached"]().(*memcached.Memcached)
|
2015-11-24 21:22:11 +00:00
|
|
|
memcached.Servers = []string{"localhost"}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
mConfig := &InputConfig{
|
2015-11-24 21:22:11 +00:00
|
|
|
Name: "memcached",
|
2015-12-01 14:15:28 +00:00
|
|
|
Filter: Filter{
|
|
|
|
Drop: []string{"other", "stuff"},
|
|
|
|
Pass: []string{"some", "strings"},
|
|
|
|
TagDrop: []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "badtag",
|
|
|
|
Filter: []string{"othertag"},
|
|
|
|
},
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
2015-12-01 14:15:28 +00:00
|
|
|
TagPass: []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "goodtag",
|
|
|
|
Filter: []string{"mytag"},
|
|
|
|
},
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
2015-12-01 14:15:28 +00:00
|
|
|
IsActive: true,
|
2015-11-24 21:22:11 +00:00
|
|
|
},
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
}
|
2016-01-07 17:23:38 +00:00
|
|
|
mConfig.Tags = make(map[string]string)
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, memcached, c.Inputs[0].Input,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce a correct memcached struct.")
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, mConfig, c.Inputs[0].Config,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce correct memcached metadata.")
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
ex := inputs.Inputs["exec"]().(*exec.Exec)
|
2016-01-07 17:23:38 +00:00
|
|
|
ex.Command = "/usr/bin/myothercollector --foo=bar"
|
2016-01-07 20:39:43 +00:00
|
|
|
eConfig := &InputConfig{
|
|
|
|
Name: "exec",
|
|
|
|
MeasurementSuffix: "_myothercollector",
|
|
|
|
}
|
2016-01-07 17:23:38 +00:00
|
|
|
eConfig.Tags = make(map[string]string)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, ex, c.Inputs[1].Input,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Merged Testdata did not produce a correct exec struct.")
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, eConfig, c.Inputs[1].Config,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Merged Testdata did not produce correct exec metadata.")
|
|
|
|
|
|
|
|
memcached.Servers = []string{"192.168.1.1"}
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, memcached, c.Inputs[2].Input,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce a correct memcached struct.")
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, mConfig, c.Inputs[2].Config,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Testdata did not produce correct memcached metadata.")
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
pstat := inputs.Inputs["procstat"]().(*procstat.Procstat)
|
2016-01-07 17:23:38 +00:00
|
|
|
pstat.PidFile = "/var/run/grafana-server.pid"
|
2015-11-24 21:22:11 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
pConfig := &InputConfig{Name: "procstat"}
|
2016-01-07 17:23:38 +00:00
|
|
|
pConfig.Tags = make(map[string]string)
|
2015-11-24 21:22:11 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, pstat, c.Inputs[3].Input,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Merged Testdata did not produce a correct procstat struct.")
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, pConfig, c.Inputs[3].Config,
|
2015-11-24 21:22:11 +00:00
|
|
|
"Merged Testdata did not produce correct procstat metadata.")
|
|
|
|
}
|
2015-12-07 22:37:05 +00:00
|
|
|
|
|
|
|
func TestFilter_Empty(t *testing.T) {
|
|
|
|
f := Filter{}
|
|
|
|
|
|
|
|
measurements := []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"supercalifradjulisticexpialidocious",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range measurements {
|
|
|
|
if !f.ShouldPass(measurement) {
|
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_Pass(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
Pass: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
|
|
|
if !f.ShouldPass(measurement) {
|
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
|
|
|
if f.ShouldPass(measurement) {
|
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_Drop(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
Drop: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
|
|
|
if !f.ShouldPass(measurement) {
|
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
|
|
|
if f.ShouldPass(measurement) {
|
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagPass(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
TagFilter{
|
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagPass: filters,
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []map[string]string{
|
|
|
|
{"cpu": "cpu-total"},
|
|
|
|
{"cpu": "cpu-0"},
|
|
|
|
{"cpu": "cpu-1"},
|
|
|
|
{"cpu": "cpu-2"},
|
|
|
|
{"mem": "mem_free"},
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []map[string]string{
|
|
|
|
{"cpu": "cputotal"},
|
|
|
|
{"cpu": "cpu0"},
|
|
|
|
{"cpu": "cpu1"},
|
|
|
|
{"cpu": "cpu2"},
|
|
|
|
{"mem": "mem_used"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
|
|
|
if !f.ShouldTagsPass(tags) {
|
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
|
|
|
if f.ShouldTagsPass(tags) {
|
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagDrop(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
|
|
|
TagFilter{
|
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
TagFilter{
|
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filters,
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []map[string]string{
|
|
|
|
{"cpu": "cpu-total"},
|
|
|
|
{"cpu": "cpu-0"},
|
|
|
|
{"cpu": "cpu-1"},
|
|
|
|
{"cpu": "cpu-2"},
|
|
|
|
{"mem": "mem_free"},
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []map[string]string{
|
|
|
|
{"cpu": "cputotal"},
|
|
|
|
{"cpu": "cpu0"},
|
|
|
|
{"cpu": "cpu1"},
|
|
|
|
{"cpu": "cpu2"},
|
|
|
|
{"mem": "mem_used"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
|
|
|
if !f.ShouldTagsPass(tags) {
|
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
|
|
|
if f.ShouldTagsPass(tags) {
|
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|