Skip to content

Commit ba9cb46

Browse files
authored
Add more test cases (#66)
1 parent 02bb510 commit ba9cb46

File tree

10 files changed

+202
-3
lines changed

10 files changed

+202
-3
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package metadata
15+
16+
import (
17+
"fmt"
18+
"io/ioutil"
19+
"path"
20+
"testing"
21+
22+
"github.com/open-telemetry/opentelemetry-log-collection/operator/helper"
23+
"github.com/stretchr/testify/require"
24+
"gopkg.in/yaml.v2"
25+
)
26+
27+
type testCase struct {
28+
name string
29+
expectErr bool
30+
expect *MetadataOperatorConfig
31+
}
32+
33+
func TestMetaDataGoldenConfig(t *testing.T) {
34+
cases := []testCase{
35+
{
36+
"default",
37+
false,
38+
defaultCfg(),
39+
},
40+
{
41+
"id",
42+
false,
43+
func() *MetadataOperatorConfig {
44+
cfg := defaultCfg()
45+
cfg.OperatorID = "newName"
46+
return cfg
47+
}(),
48+
},
49+
{
50+
"output",
51+
false,
52+
func() *MetadataOperatorConfig {
53+
cfg := defaultCfg()
54+
cfg.OutputIDs = append(cfg.OutputIDs, "nextOperator")
55+
return cfg
56+
}(),
57+
},
58+
{
59+
"attributes_single",
60+
false,
61+
func() *MetadataOperatorConfig {
62+
cfg := defaultCfg()
63+
cfg.Attributes = map[string]helper.ExprStringConfig{
64+
"key1": `val1`,
65+
}
66+
return cfg
67+
}(),
68+
},
69+
{
70+
"attributes_multi",
71+
false,
72+
func() *MetadataOperatorConfig {
73+
cfg := defaultCfg()
74+
cfg.Attributes = map[string]helper.ExprStringConfig{
75+
"key1": `val1`,
76+
"key2": `val2`,
77+
"key3": `val3`,
78+
}
79+
return cfg
80+
}(),
81+
},
82+
{
83+
"resource_single",
84+
false,
85+
func() *MetadataOperatorConfig {
86+
cfg := defaultCfg()
87+
cfg.Resource = map[string]helper.ExprStringConfig{
88+
"key1": `val1`,
89+
}
90+
return cfg
91+
}(),
92+
},
93+
{
94+
"resource_multi",
95+
false,
96+
func() *MetadataOperatorConfig {
97+
cfg := defaultCfg()
98+
cfg.Resource = map[string]helper.ExprStringConfig{
99+
"key1": `val1`,
100+
"key2": `val2`,
101+
"key3": `val3`,
102+
}
103+
return cfg
104+
}(),
105+
},
106+
{
107+
"on_error",
108+
false,
109+
func() *MetadataOperatorConfig {
110+
cfg := defaultCfg()
111+
cfg.OnError = "drop"
112+
return cfg
113+
}(),
114+
},
115+
}
116+
117+
for _, tc := range cases {
118+
t.Run("yaml/"+tc.name, func(t *testing.T) {
119+
cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
120+
if tc.expectErr {
121+
require.Error(t, yamlErr)
122+
} else {
123+
require.NoError(t, yamlErr)
124+
require.Equal(t, tc.expect, cfgFromYaml)
125+
}
126+
})
127+
t.Run("mapstructure/"+tc.name, func(t *testing.T) {
128+
cfgFromMapstructure := defaultCfg()
129+
mapErr := configFromFileViaMapstructure(
130+
path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)),
131+
cfgFromMapstructure,
132+
)
133+
if tc.expectErr {
134+
require.Error(t, mapErr)
135+
} else {
136+
require.NoError(t, mapErr)
137+
require.Equal(t, tc.expect, cfgFromMapstructure)
138+
}
139+
})
140+
}
141+
}
142+
143+
func configFromFileViaYaml(file string) (*MetadataOperatorConfig, error) {
144+
bytes, err := ioutil.ReadFile(file)
145+
if err != nil {
146+
return nil, fmt.Errorf("could not find config file: %s", err)
147+
}
148+
149+
config := defaultCfg()
150+
if err := yaml.Unmarshal(bytes, config); err != nil {
151+
return nil, fmt.Errorf("failed to read config file as yaml: %s", err)
152+
}
153+
154+
return config, nil
155+
}
156+
157+
func configFromFileViaMapstructure(file string, result *MetadataOperatorConfig) error {
158+
bytes, err := ioutil.ReadFile(file)
159+
if err != nil {
160+
return fmt.Errorf("could not find config file: %s", err)
161+
}
162+
163+
raw := map[string]interface{}{}
164+
165+
if err := yaml.Unmarshal(bytes, raw); err != nil {
166+
return fmt.Errorf("failed to read data from yaml: %s", err)
167+
}
168+
169+
err = helper.UnmarshalMapstructure(raw, result)
170+
return err
171+
}
172+
173+
func defaultCfg() *MetadataOperatorConfig {
174+
return NewMetadataOperatorConfig("metadata")
175+
}

operator/builtin/transformer/metadata/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func NewMetadataOperatorConfig(operatorID string) *MetadataOperatorConfig {
3838

3939
// MetadataOperatorConfig is the configuration of a metadata operator
4040
type MetadataOperatorConfig struct {
41-
helper.TransformerConfig `yaml:",inline"`
42-
helper.AttributerConfig `yaml:",inline"`
43-
helper.IdentifierConfig `yaml:",inline"`
41+
helper.TransformerConfig `mapstructure:",squash" yaml:",inline"`
42+
helper.AttributerConfig `mapstructure:",squash" yaml:",inline"`
43+
helper.IdentifierConfig `mapstructure:",squash" yaml:",inline"`
4444
}
4545

4646
// Build will build a metadata operator from the supplied configuration
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: metadata
2+
attributes:
3+
key1: val1
4+
key2: val2
5+
key3: val3
6+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: metadata
2+
attributes:
3+
key1: val1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type: metadata
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type: metadata
2+
id: newName
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type: metadata
2+
on_error: drop
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type: metadata
2+
output: nextOperator
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: metadata
2+
resource:
3+
key1: val1
4+
key2: val2
5+
key3: val3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type: metadata
2+
resource:
3+
key1: val1

0 commit comments

Comments
 (0)