2015-05-22 23:45:14 +00:00
|
|
|
package telegraf
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-11 15:50:36 +00:00
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-08-11 17:01:37 +00:00
|
|
|
"testing"
|
2015-10-23 17:23:08 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-23 00:45:31 +00:00
|
|
|
"github.com/influxdb/telegraf/internal/config"
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-08-11 17:01:37 +00:00
|
|
|
// needing to load the plugins
|
2016-01-23 00:45:31 +00:00
|
|
|
_ "github.com/influxdb/telegraf/plugins/inputs/all"
|
2015-09-22 01:38:57 +00:00
|
|
|
// needing to load the outputs
|
2016-01-23 00:45:31 +00:00
|
|
|
_ "github.com/influxdb/telegraf/plugins/outputs/all"
|
2015-08-11 15:50:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_LoadPlugin(t *testing.T) {
|
2015-11-24 21:22:11 +00:00
|
|
|
c := config.NewConfig()
|
2016-01-07 20:39:43 +00:00
|
|
|
c.InputFilters = []string{"mysql"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ := NewAgent(c)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, 1, len(a.Config.Inputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
c = config.NewConfig()
|
2016-01-07 20:39:43 +00:00
|
|
|
c.InputFilters = []string{"foo"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, 0, len(a.Config.Inputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
c = config.NewConfig()
|
2016-01-07 20:39:43 +00:00
|
|
|
c.InputFilters = []string{"mysql", "foo"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, 1, len(a.Config.Inputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
c = config.NewConfig()
|
2016-01-07 20:39:43 +00:00
|
|
|
c.InputFilters = []string{"mysql", "redis"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, 2, len(a.Config.Inputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
|
|
|
|
c = config.NewConfig()
|
2016-01-07 20:39:43 +00:00
|
|
|
c.InputFilters = []string{"mysql", "foo", "redis", "bar"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
2016-01-07 20:39:43 +00:00
|
|
|
assert.Equal(t, 2, len(a.Config.Inputs))
|
2015-09-22 01:38:57 +00:00
|
|
|
}
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
func TestAgent_LoadOutput(t *testing.T) {
|
2015-11-24 21:22:11 +00:00
|
|
|
c := config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"influxdb"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ := NewAgent(c)
|
|
|
|
assert.Equal(t, 2, len(a.Config.Outputs))
|
|
|
|
|
2016-01-07 17:52:46 +00:00
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"kafka"}
|
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 1, len(a.Config.Outputs))
|
|
|
|
|
2015-11-24 21:22:11 +00:00
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
|
|
|
|
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"foo"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 0, len(a.Config.Outputs))
|
|
|
|
|
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"influxdb", "foo"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 2, len(a.Config.Outputs))
|
|
|
|
|
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"influxdb", "kafka"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, len(c.Outputs))
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
|
|
|
|
|
|
|
c = config.NewConfig()
|
|
|
|
c.OutputFilters = []string{"influxdb", "foo", "kafka", "bar"}
|
2016-01-07 17:52:46 +00:00
|
|
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
|
|
|
assert.NoError(t, err)
|
2015-11-24 21:22:11 +00:00
|
|
|
a, _ = NewAgent(c)
|
|
|
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
2015-10-17 06:11:12 +00:00
|
|
|
}
|
2015-10-23 17:23:08 +00:00
|
|
|
|
|
|
|
func TestAgent_ZeroJitter(t *testing.T) {
|
2015-11-26 01:42:07 +00:00
|
|
|
flushinterval := jitterInterval(time.Duration(10*time.Second),
|
|
|
|
time.Duration(0*time.Second))
|
2015-10-23 17:23:08 +00:00
|
|
|
|
|
|
|
actual := flushinterval.Nanoseconds()
|
|
|
|
exp := time.Duration(10 * time.Second).Nanoseconds()
|
|
|
|
|
|
|
|
if actual != exp {
|
|
|
|
t.Errorf("Actual %v, expected %v", actual, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ZeroInterval(t *testing.T) {
|
|
|
|
min := time.Duration(500 * time.Millisecond).Nanoseconds()
|
|
|
|
max := time.Duration(5 * time.Second).Nanoseconds()
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2015-11-26 01:42:07 +00:00
|
|
|
flushinterval := jitterInterval(time.Duration(0*time.Second),
|
|
|
|
time.Duration(5*time.Second))
|
2015-10-23 17:23:08 +00:00
|
|
|
actual := flushinterval.Nanoseconds()
|
|
|
|
|
|
|
|
if actual > max {
|
|
|
|
t.Errorf("Didn't expect interval %d to be > %d", actual, max)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if actual < min {
|
|
|
|
t.Errorf("Didn't expect interval %d to be < %d", actual, min)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_ZeroBoth(t *testing.T) {
|
2015-11-26 01:42:07 +00:00
|
|
|
flushinterval := jitterInterval(time.Duration(0*time.Second),
|
|
|
|
time.Duration(0*time.Second))
|
2015-10-23 17:23:08 +00:00
|
|
|
|
|
|
|
actual := flushinterval
|
|
|
|
exp := time.Duration(500 * time.Millisecond)
|
|
|
|
|
|
|
|
if actual != exp {
|
|
|
|
t.Errorf("Actual %v, expected %v", actual, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_JitterMax(t *testing.T) {
|
|
|
|
max := time.Duration(32 * time.Second).Nanoseconds()
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2015-11-26 01:42:07 +00:00
|
|
|
flushinterval := jitterInterval(time.Duration(30*time.Second),
|
|
|
|
time.Duration(2*time.Second))
|
2015-10-23 17:23:08 +00:00
|
|
|
actual := flushinterval.Nanoseconds()
|
|
|
|
if actual > max {
|
|
|
|
t.Errorf("Didn't expect interval %d to be > %d", actual, max)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_JitterMin(t *testing.T) {
|
|
|
|
min := time.Duration(30 * time.Second).Nanoseconds()
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2015-11-26 01:42:07 +00:00
|
|
|
flushinterval := jitterInterval(time.Duration(30*time.Second),
|
|
|
|
time.Duration(2*time.Second))
|
2015-10-23 17:23:08 +00:00
|
|
|
actual := flushinterval.Nanoseconds()
|
|
|
|
if actual < min {
|
|
|
|
t.Errorf("Didn't expect interval %d to be < %d", actual, min)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|