2020-06-05 07:09:22 +08:00
|
|
|
package shim
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
2020-08-19 02:20:31 +08:00
|
|
|
"time"
|
2020-06-05 07:09:22 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-08-19 02:20:31 +08:00
|
|
|
tgConfig "github.com/influxdata/telegraf/config"
|
2020-06-05 07:09:22 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2020-10-16 01:46:13 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
2020-06-05 07:09:22 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
|
|
|
os.Setenv("SECRET_TOKEN", "xxxxxxxxxx")
|
|
|
|
|
os.Setenv("SECRET_VALUE", `test"\test`)
|
|
|
|
|
|
|
|
|
|
inputs.Add("test", func() telegraf.Input {
|
|
|
|
|
return &serviceInput{}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c := "./testdata/plugin.conf"
|
|
|
|
|
conf, err := LoadConfig(&c)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
inp := conf.Input.(*serviceInput)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, "awesome name", inp.ServiceName)
|
|
|
|
|
require.Equal(t, "xxxxxxxxxx", inp.SecretToken)
|
|
|
|
|
require.Equal(t, `test"\test`, inp.SecretValue)
|
|
|
|
|
}
|
2020-07-11 03:05:26 +08:00
|
|
|
|
|
|
|
|
func TestDefaultImportedPluginsSelfRegisters(t *testing.T) {
|
|
|
|
|
inputs.Add("test", func() telegraf.Input {
|
|
|
|
|
return &testInput{}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
cfg, err := LoadConfig(nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, "test", cfg.Input.Description())
|
|
|
|
|
}
|
2020-08-19 02:20:31 +08:00
|
|
|
|
|
|
|
|
func TestLoadingSpecialTypes(t *testing.T) {
|
|
|
|
|
inputs.Add("test", func() telegraf.Input {
|
|
|
|
|
return &testDurationInput{}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c := "./testdata/special.conf"
|
|
|
|
|
conf, err := LoadConfig(&c)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
inp := conf.Input.(*testDurationInput)
|
|
|
|
|
|
|
|
|
|
require.EqualValues(t, 3*time.Second, inp.Duration)
|
|
|
|
|
require.EqualValues(t, 3*1000*1000, inp.Size)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 01:46:13 +08:00
|
|
|
func TestLoadingProcessorWithConfig(t *testing.T) {
|
|
|
|
|
proc := &testConfigProcessor{}
|
|
|
|
|
processors.Add("test_config_load", func() telegraf.Processor {
|
|
|
|
|
return proc
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c := "./testdata/processor.conf"
|
|
|
|
|
_, err := LoadConfig(&c)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.EqualValues(t, "yep", proc.Loaded)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-19 02:20:31 +08:00
|
|
|
type testDurationInput struct {
|
|
|
|
|
Duration tgConfig.Duration `toml:"duration"`
|
|
|
|
|
Size tgConfig.Size `toml:"size"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *testDurationInput) SampleConfig() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *testDurationInput) Description() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-03-23 01:21:36 +08:00
|
|
|
func (i *testDurationInput) Gather(_ telegraf.Accumulator) error {
|
2020-08-19 02:20:31 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2020-10-16 01:46:13 +08:00
|
|
|
|
|
|
|
|
type testConfigProcessor struct {
|
|
|
|
|
Loaded string `toml:"loaded"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *testConfigProcessor) SampleConfig() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *testConfigProcessor) Description() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
func (p *testConfigProcessor) Apply(metrics ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
|
return metrics
|
|
|
|
|
}
|