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"
|
|
|
|
|
|
|
|
"github.com/influxdb/telegraf/duration"
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-08-11 17:01:37 +00:00
|
|
|
// needing to load the plugins
|
2015-08-11 15:50:36 +00:00
|
|
|
_ "github.com/influxdb/telegraf/plugins/all"
|
2015-09-22 01:38:57 +00:00
|
|
|
// needing to load the outputs
|
2015-10-17 06:11:12 +00:00
|
|
|
_ "github.com/influxdb/telegraf/outputs/all"
|
2015-08-11 15:50:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_LoadPlugin(t *testing.T) {
|
|
|
|
|
|
|
|
// load a dedicated configuration file
|
|
|
|
config, _ := LoadConfig("./testdata/telegraf-agent.toml")
|
|
|
|
a, _ := NewAgent(config)
|
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
pluginsEnabled, _ := a.LoadPlugins([]string{"mysql"}, config)
|
2015-08-11 15:50:36 +00:00
|
|
|
assert.Equal(t, 1, len(pluginsEnabled))
|
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
pluginsEnabled, _ = a.LoadPlugins([]string{"foo"}, config)
|
2015-08-11 15:50:36 +00:00
|
|
|
assert.Equal(t, 0, len(pluginsEnabled))
|
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "foo"}, config)
|
2015-08-11 15:50:36 +00:00
|
|
|
assert.Equal(t, 1, len(pluginsEnabled))
|
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "redis"}, config)
|
2015-08-11 15:50:36 +00:00
|
|
|
assert.Equal(t, 2, len(pluginsEnabled))
|
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
pluginsEnabled, _ = a.LoadPlugins([]string{"mysql", "foo", "redis", "bar"}, config)
|
2015-08-11 15:50:36 +00:00
|
|
|
assert.Equal(t, 2, len(pluginsEnabled))
|
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) {
|
|
|
|
// load a dedicated configuration file
|
|
|
|
config, _ := LoadConfig("./testdata/telegraf-agent.toml")
|
|
|
|
a, _ := NewAgent(config)
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ := a.LoadOutputs([]string{"influxdb"}, config)
|
|
|
|
assert.Equal(t, 1, len(outputsEnabled))
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ = a.LoadOutputs([]string{}, config)
|
|
|
|
assert.Equal(t, 2, len(outputsEnabled))
|
2015-08-11 15:50:36 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ = a.LoadOutputs([]string{"foo"}, config)
|
|
|
|
assert.Equal(t, 0, len(outputsEnabled))
|
2015-09-22 01:38:57 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "foo"}, config)
|
|
|
|
assert.Equal(t, 1, len(outputsEnabled))
|
2015-09-22 01:38:57 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "kafka"}, config)
|
|
|
|
assert.Equal(t, 2, len(outputsEnabled))
|
2015-09-22 01:38:57 +00:00
|
|
|
|
2015-10-17 06:11:12 +00:00
|
|
|
outputsEnabled, _ = a.LoadOutputs([]string{"influxdb", "foo", "kafka", "bar"}, config)
|
|
|
|
assert.Equal(t, 2, len(outputsEnabled))
|
|
|
|
}
|
2015-10-23 17:23:08 +00:00
|
|
|
|
|
|
|
func TestAgent_ZeroJitter(t *testing.T) {
|
|
|
|
a := &Agent{
|
|
|
|
FlushInterval: duration.Duration{10 * time.Second},
|
|
|
|
FlushJitter: duration.Duration{0 * time.Second},
|
|
|
|
}
|
|
|
|
flushinterval := jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
|
|
|
|
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++ {
|
|
|
|
a := &Agent{
|
|
|
|
FlushInterval: duration.Duration{0 * time.Second},
|
|
|
|
FlushJitter: duration.Duration{5 * time.Second},
|
|
|
|
}
|
|
|
|
|
|
|
|
flushinterval := jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
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) {
|
|
|
|
a := &Agent{
|
|
|
|
FlushInterval: duration.Duration{0 * time.Second},
|
|
|
|
FlushJitter: duration.Duration{0 * time.Second},
|
|
|
|
}
|
|
|
|
|
|
|
|
flushinterval := jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
|
|
|
|
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++ {
|
|
|
|
a := &Agent{
|
|
|
|
FlushInterval: duration.Duration{30 * time.Second},
|
|
|
|
FlushJitter: duration.Duration{2 * time.Second},
|
|
|
|
}
|
|
|
|
flushinterval := jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
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++ {
|
|
|
|
a := &Agent{
|
|
|
|
FlushInterval: duration.Duration{30 * time.Second},
|
|
|
|
FlushJitter: duration.Duration{2 * time.Second},
|
|
|
|
}
|
|
|
|
flushinterval := jitterInterval(a.FlushInterval.Duration,
|
|
|
|
a.FlushJitter.Duration)
|
|
|
|
actual := flushinterval.Nanoseconds()
|
|
|
|
if actual < min {
|
|
|
|
t.Errorf("Didn't expect interval %d to be < %d", actual, min)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|