Skip to content

Commit

Permalink
fix linter errors and file.d_test (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Romanov authored and DmitryRomanov committed Oct 31, 2023
1 parent b7994bf commit 6ac7de3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
13 changes: 10 additions & 3 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand All @@ -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" {
Expand Down
44 changes: 25 additions & 19 deletions cmd/file.d/file.d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -218,44 +214,44 @@ func TestThatPluginsAreImported(t *testing.T) {

type testConfig struct {
name string
factory func() (pipeline.AnyPlugin, pipeline.AnyConfig)
kind pipeline.PluginKind
configJSON string
}

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"}`,
},
}
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.NoError(t, err, "shouldn't be an error")
})
}
Expand All @@ -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")
})
}
Expand Down
8 changes: 6 additions & 2 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 6ac7de3

Please sign in to comment.