diff --git a/operator/builtin/transformer/metadata/config_test.go b/operator/builtin/transformer/metadata/config_test.go new file mode 100644 index 000000000000..ffc035f5e531 --- /dev/null +++ b/operator/builtin/transformer/metadata/config_test.go @@ -0,0 +1,175 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package metadata + +import ( + "fmt" + "io/ioutil" + "path" + "testing" + + "github.com/open-telemetry/opentelemetry-log-collection/operator/helper" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" +) + +type testCase struct { + name string + expectErr bool + expect *MetadataOperatorConfig +} + +func TestMetaDataGoldenConfig(t *testing.T) { + cases := []testCase{ + { + "default", + false, + defaultCfg(), + }, + { + "id", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.OperatorID = "newName" + return cfg + }(), + }, + { + "output", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.OutputIDs = append(cfg.OutputIDs, "nextOperator") + return cfg + }(), + }, + { + "attributes_single", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.Attributes = map[string]helper.ExprStringConfig{ + "key1": `val1`, + } + return cfg + }(), + }, + { + "attributes_multi", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.Attributes = map[string]helper.ExprStringConfig{ + "key1": `val1`, + "key2": `val2`, + "key3": `val3`, + } + return cfg + }(), + }, + { + "resource_single", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.Resource = map[string]helper.ExprStringConfig{ + "key1": `val1`, + } + return cfg + }(), + }, + { + "resource_multi", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.Resource = map[string]helper.ExprStringConfig{ + "key1": `val1`, + "key2": `val2`, + "key3": `val3`, + } + return cfg + }(), + }, + { + "on_error", + false, + func() *MetadataOperatorConfig { + cfg := defaultCfg() + cfg.OnError = "drop" + return cfg + }(), + }, + } + + for _, tc := range cases { + t.Run("yaml/"+tc.name, func(t *testing.T) { + cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name))) + if tc.expectErr { + require.Error(t, yamlErr) + } else { + require.NoError(t, yamlErr) + require.Equal(t, tc.expect, cfgFromYaml) + } + }) + t.Run("mapstructure/"+tc.name, func(t *testing.T) { + cfgFromMapstructure := defaultCfg() + mapErr := configFromFileViaMapstructure( + path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)), + cfgFromMapstructure, + ) + if tc.expectErr { + require.Error(t, mapErr) + } else { + require.NoError(t, mapErr) + require.Equal(t, tc.expect, cfgFromMapstructure) + } + }) + } +} + +func configFromFileViaYaml(file string) (*MetadataOperatorConfig, error) { + bytes, err := ioutil.ReadFile(file) + if err != nil { + return nil, fmt.Errorf("could not find config file: %s", err) + } + + config := defaultCfg() + if err := yaml.Unmarshal(bytes, config); err != nil { + return nil, fmt.Errorf("failed to read config file as yaml: %s", err) + } + + return config, nil +} + +func configFromFileViaMapstructure(file string, result *MetadataOperatorConfig) error { + bytes, err := ioutil.ReadFile(file) + if err != nil { + return fmt.Errorf("could not find config file: %s", err) + } + + raw := map[string]interface{}{} + + if err := yaml.Unmarshal(bytes, raw); err != nil { + return fmt.Errorf("failed to read data from yaml: %s", err) + } + + err = helper.UnmarshalMapstructure(raw, result) + return err +} + +func defaultCfg() *MetadataOperatorConfig { + return NewMetadataOperatorConfig("metadata") +} diff --git a/operator/builtin/transformer/metadata/metadata.go b/operator/builtin/transformer/metadata/metadata.go index cb7b2be74fba..ffe2fbeefe2a 100644 --- a/operator/builtin/transformer/metadata/metadata.go +++ b/operator/builtin/transformer/metadata/metadata.go @@ -38,9 +38,9 @@ func NewMetadataOperatorConfig(operatorID string) *MetadataOperatorConfig { // MetadataOperatorConfig is the configuration of a metadata operator type MetadataOperatorConfig struct { - helper.TransformerConfig `yaml:",inline"` - helper.AttributerConfig `yaml:",inline"` - helper.IdentifierConfig `yaml:",inline"` + helper.TransformerConfig `mapstructure:",squash" yaml:",inline"` + helper.AttributerConfig `mapstructure:",squash" yaml:",inline"` + helper.IdentifierConfig `mapstructure:",squash" yaml:",inline"` } // Build will build a metadata operator from the supplied configuration diff --git a/operator/builtin/transformer/metadata/testdata/attributes_multi.yaml b/operator/builtin/transformer/metadata/testdata/attributes_multi.yaml new file mode 100644 index 000000000000..53ce53e8528c --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/attributes_multi.yaml @@ -0,0 +1,6 @@ +type: metadata +attributes: + key1: val1 + key2: val2 + key3: val3 + diff --git a/operator/builtin/transformer/metadata/testdata/attributes_single.yaml b/operator/builtin/transformer/metadata/testdata/attributes_single.yaml new file mode 100644 index 000000000000..8b9134b61f7c --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/attributes_single.yaml @@ -0,0 +1,3 @@ +type: metadata +attributes: + key1: val1 \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/default.yaml b/operator/builtin/transformer/metadata/testdata/default.yaml new file mode 100644 index 000000000000..9b6232572079 --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/default.yaml @@ -0,0 +1 @@ +type: metadata \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/id.yaml b/operator/builtin/transformer/metadata/testdata/id.yaml new file mode 100644 index 000000000000..8807014ffe9b --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/id.yaml @@ -0,0 +1,2 @@ +type: metadata +id: newName \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/on_error.yaml b/operator/builtin/transformer/metadata/testdata/on_error.yaml new file mode 100644 index 000000000000..60a4a728cff8 --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/on_error.yaml @@ -0,0 +1,2 @@ +type: metadata +on_error: drop \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/output.yaml b/operator/builtin/transformer/metadata/testdata/output.yaml new file mode 100644 index 000000000000..1218c40ecaf8 --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/output.yaml @@ -0,0 +1,2 @@ +type: metadata +output: nextOperator \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/resource_multi.yaml b/operator/builtin/transformer/metadata/testdata/resource_multi.yaml new file mode 100644 index 000000000000..9836bef5520d --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/resource_multi.yaml @@ -0,0 +1,5 @@ +type: metadata +resource: + key1: val1 + key2: val2 + key3: val3 \ No newline at end of file diff --git a/operator/builtin/transformer/metadata/testdata/resource_single.yaml b/operator/builtin/transformer/metadata/testdata/resource_single.yaml new file mode 100644 index 000000000000..0f5a137c5f62 --- /dev/null +++ b/operator/builtin/transformer/metadata/testdata/resource_single.yaml @@ -0,0 +1,3 @@ +type: metadata +resource: + key1: val1 \ No newline at end of file