From 6ac7de37e5bc4e1bee344dc2407da00e009bb6d8 Mon Sep 17 00:00:00 2001 From: Dmitry Romanov Date: Tue, 31 Oct 2023 16:17:23 +0700 Subject: [PATCH] fix linter errors and file.d_test (#358) --- cfg/config.go | 13 +++++++++--- cmd/file.d/file.d_test.go | 44 ++++++++++++++++++++++----------------- test/test.go | 8 +++++-- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/cfg/config.go b/cfg/config.go index 56ec1529b..d1870c44c 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -574,14 +574,21 @@ func SetDefaultValues(data interface{}) error { vFieldKind := vField.Kind() + var err error switch vFieldKind { case reflect.Struct: - SetDefaultValues(vField.Addr().Interface()) + err = SetDefaultValues(vField.Addr().Interface()) + if err != nil { + return err + } case reflect.Slice: for i := 0; i < vField.Len(); i++ { item := vField.Index(i) if item.Kind() == reflect.Struct { - SetDefaultValues(item.Addr().Interface()) + err = SetDefaultValues(item.Addr().Interface()) + if err != nil { + return err + } } } } @@ -591,7 +598,7 @@ func SetDefaultValues(data interface{}) error { switch vFieldKind { case reflect.Bool: currentValue := vField.Bool() - if currentValue != true { + if !currentValue { if defaultValue == "true" { vField.SetBool(true) } else if defaultValue == "false" { diff --git a/cmd/file.d/file.d_test.go b/cmd/file.d/file.d_test.go index f1b462ee9..3c967906a 100644 --- a/cmd/file.d/file.d_test.go +++ b/cmd/file.d/file.d_test.go @@ -21,13 +21,9 @@ import ( _ "github.com/ozontech/file.d/plugin/action/rename" _ "github.com/ozontech/file.d/plugin/action/throttle" _ "github.com/ozontech/file.d/plugin/input/fake" - "github.com/ozontech/file.d/plugin/input/file" - http2 "github.com/ozontech/file.d/plugin/input/http" k8s2 "github.com/ozontech/file.d/plugin/input/k8s" _ "github.com/ozontech/file.d/plugin/output/devnull" - "github.com/ozontech/file.d/plugin/output/gelf" _ "github.com/ozontech/file.d/plugin/output/kafka" - "github.com/ozontech/file.d/plugin/output/splunk" uuid "github.com/satori/go.uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -218,7 +214,7 @@ func TestThatPluginsAreImported(t *testing.T) { type testConfig struct { name string - factory func() (pipeline.AnyPlugin, pipeline.AnyConfig) + kind pipeline.PluginKind configJSON string } @@ -226,27 +222,27 @@ func TestConfigParseValid(t *testing.T) { testList := []testConfig{ { name: "file", - factory: file.Factory, - configJSON: `{"offsets_op":"tail","persistence_mode":"sync","watching_dir":"/var/"}`, + kind: pipeline.PluginKindInput, + configJSON: `{"offsets_op":"tail","persistence_mode":"sync","watching_dir":"/var/", "offsets_file": "./offset.yaml"}`, }, { name: "http", - factory: http2.Factory, - configJSON: `{"address": ":9001","emulate_mode":"yes"}`, + kind: pipeline.PluginKindInput, + configJSON: `{"address": ":9001","emulate_mode":"elasticsearch"}`, }, { name: "k8s", - factory: k8s2.Factory, + kind: pipeline.PluginKindInput, configJSON: `{"split_event_size":1000,"watching_dir":"/var/log/containers/","offsets_file":"/data/k8s-offsets.yaml"}`, }, { name: "gelf", - factory: gelf.Factory, + kind: pipeline.PluginKindOutput, configJSON: `{"endpoint":"graylog.svc.cluster.local:12201","reconnect_interval":"1m","default_short_message_value":"message isn't provided"}`, }, { name: "splunk", - factory: splunk.Factory, + kind: pipeline.PluginKindOutput, configJSON: `{"endpoint":"splunk_endpoint","token":"value_token"}`, }, } @@ -254,8 +250,8 @@ func TestConfigParseValid(t *testing.T) { tl := tl t.Run(tl.name, func(t *testing.T) { t.Parallel() - _, config := tl.factory() - err := fd.DecodeConfig(config, []byte(tl.configJSON)) + pluginInfo := fd.DefaultPluginRegistry.Get(tl.kind, tl.name) + _, err := cfg.GetPipelineConfig(pluginInfo, []byte(tl.configJSON), map[string]int{"gomaxprocs": 1, "capacity": 64}) assert.NoError(t, err, "shouldn't be an error") }) } @@ -265,26 +261,36 @@ func TestConfigParseInvalid(t *testing.T) { testList := []testConfig{ { name: "http", - factory: http2.Factory, + kind: pipeline.PluginKindInput, configJSON: `{"address": ":9001","emulate_mode":"yes","un_exist_field":"bla-bla"}`, }, { name: "k8s", - factory: k8s2.Factory, + kind: pipeline.PluginKindInput, configJSON: `{"split_event_size":pp,"watching_dir":"/var/log/containers/","offsets_file":"/data/k8s-offsets.yaml"}`, }, { name: "gelf", - factory: gelf.Factory, + kind: pipeline.PluginKindOutput, configJSON: `{"reconnect_interval_1":"1m","default_short_message_value":"message isn't provided"}`, }, + { + name: "http", + kind: pipeline.PluginKindInput, + configJSON: `{"address": ":9001","emulate_mode":"yes"}`, + }, + { + name: "file", + kind: pipeline.PluginKindInput, + configJSON: `{"offsets_op":"tail","persistence_mode":"sync","watching_dir":"/var/"}`, + }, } for _, tl := range testList { tl := tl t.Run(tl.name, func(t *testing.T) { t.Parallel() - _, config := tl.factory() - err := fd.DecodeConfig(config, []byte(tl.configJSON)) + pluginInfo := fd.DefaultPluginRegistry.Get(tl.kind, tl.name) + _, err := cfg.GetPipelineConfig(pluginInfo, []byte(tl.configJSON), map[string]int{"gomaxprocs": 1, "capacity": 64}) assert.Error(t, err, "should be an error") }) } diff --git a/test/test.go b/test/test.go index 3b5dc55c3..8a2e62946 100644 --- a/test/test.go +++ b/test/test.go @@ -232,8 +232,12 @@ func newDefaultParams() pipeline.PluginDefaultParams { } func NewConfig(config any, params map[string]int) any { - cfg.SetDefaultValues(config) - err := cfg.Parse(config, params) + err := cfg.SetDefaultValues(config) + if err != nil { + logger.Panicf("cannot set defaults for config: %s", err.Error()) + } + + err = cfg.Parse(config, params) if err != nil { logger.Panicf("wrong config: %s", err.Error()) }