Skip to content

Commit

Permalink
Add more test cases (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrod1598 authored Mar 22, 2021
1 parent 02bb510 commit ba9cb46
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 3 deletions.
175 changes: 175 additions & 0 deletions operator/builtin/transformer/metadata/config_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 3 additions & 3 deletions operator/builtin/transformer/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: metadata
attributes:
key1: val1
key2: val2
key3: val3

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: metadata
attributes:
key1: val1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: metadata
2 changes: 2 additions & 0 deletions operator/builtin/transformer/metadata/testdata/id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: metadata
id: newName
2 changes: 2 additions & 0 deletions operator/builtin/transformer/metadata/testdata/on_error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: metadata
on_error: drop
2 changes: 2 additions & 0 deletions operator/builtin/transformer/metadata/testdata/output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: metadata
output: nextOperator
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: metadata
resource:
key1: val1
key2: val2
key3: val3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: metadata
resource:
key1: val1

0 comments on commit ba9cb46

Please sign in to comment.