2016-01-27 21:21:36 +00:00
|
|
|
package agent
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-08-11 15:50:36 +00:00
|
|
|
import (
|
2015-08-11 17:01:37 +00:00
|
|
|
"testing"
|
2019-03-29 22:40:33 +00:00
|
|
|
"time"
|
2015-10-23 17:23:08 +00:00
|
|
|
|
2020-05-04 18:09:10 +00:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2016-01-20 18:57:35 +00:00
|
|
|
_ "github.com/influxdata/telegraf/plugins/inputs/all"
|
|
|
|
_ "github.com/influxdata/telegraf/plugins/outputs/all"
|
2016-03-21 21:33:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-03-29 22:40:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-11 15:50:36 +00:00
|
|
|
)
|
|
|
|
|
2016-03-21 21:33:19 +00:00
|
|
|
func TestAgent_OmitHostname(t *testing.T) {
|
|
|
|
c := config.NewConfig()
|
|
|
|
c.Agent.OmitHostname = true
|
|
|
|
_, err := NewAgent(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotContains(t, c.Tags, "host")
|
|
|
|
}
|
|
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err := c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err := c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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{}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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"}
|
2020-05-04 18:09:10 +00:00
|
|
|
err = c.LoadConfig("../config/testdata/telegraf-agent.toml")
|
2016-01-07 17:52:46 +00:00
|
|
|
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
|
|
|
}
|
2019-03-29 22:40:33 +00:00
|
|
|
|
|
|
|
func TestWindow(t *testing.T) {
|
|
|
|
parse := func(s string) time.Time {
|
|
|
|
tm, err := time.Parse(time.RFC3339, s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return tm
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
start time.Time
|
|
|
|
roundInterval bool
|
|
|
|
period time.Duration
|
|
|
|
since time.Time
|
|
|
|
until time.Time
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "round with exact alignment",
|
|
|
|
start: parse("2018-03-27T00:00:00Z"),
|
|
|
|
roundInterval: true,
|
|
|
|
period: 30 * time.Second,
|
|
|
|
since: parse("2018-03-27T00:00:00Z"),
|
|
|
|
until: parse("2018-03-27T00:00:30Z"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "round with alignment needed",
|
|
|
|
start: parse("2018-03-27T00:00:05Z"),
|
|
|
|
roundInterval: true,
|
|
|
|
period: 30 * time.Second,
|
|
|
|
since: parse("2018-03-27T00:00:00Z"),
|
|
|
|
until: parse("2018-03-27T00:00:30Z"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no round with exact alignment",
|
|
|
|
start: parse("2018-03-27T00:00:00Z"),
|
|
|
|
roundInterval: false,
|
|
|
|
period: 30 * time.Second,
|
|
|
|
since: parse("2018-03-27T00:00:00Z"),
|
|
|
|
until: parse("2018-03-27T00:00:30Z"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no found with alignment needed",
|
|
|
|
start: parse("2018-03-27T00:00:05Z"),
|
|
|
|
roundInterval: false,
|
|
|
|
period: 30 * time.Second,
|
|
|
|
since: parse("2018-03-27T00:00:05Z"),
|
|
|
|
until: parse("2018-03-27T00:00:35Z"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
since, until := updateWindow(tt.start, tt.roundInterval, tt.period)
|
|
|
|
require.Equal(t, tt.since, since, "since")
|
|
|
|
require.Equal(t, tt.until, until, "until")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|