-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] Add end to end tests for type casting (#10399)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description <!-- Issue number if applicable --> Adds tests of current type casting behavior when using env and file provider. #### Link to tracking issue Relates to #9854, #8565, #9532
- Loading branch information
Showing
7 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module go.opentelemetry.io/collector/confmap/internal/e2e | ||
|
||
go 1.21.0 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.9.0 | ||
go.opentelemetry.io/collector/confmap v0.102.1 | ||
go.opentelemetry.io/collector/confmap/provider/envprovider v0.102.1 | ||
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.102.1 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect | ||
github.com/knadh/koanf/maps v0.1.1 // indirect | ||
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect | ||
github.com/knadh/koanf/v2 v2.1.1 // indirect | ||
github.com/mitchellh/copystructure v1.2.0 // indirect | ||
github.com/mitchellh/reflectwalk v1.0.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
go.uber.org/zap v1.27.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
replace go.opentelemetry.io/collector/confmap => ../../ | ||
|
||
replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../provider/fileprovider | ||
|
||
replace go.opentelemetry.io/collector/confmap/provider/envprovider => ../../provider/envprovider |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
field: ${env:ENV} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
field: "inline field with ${env:ENV} expansion" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/confmap/provider/envprovider" | ||
"go.opentelemetry.io/collector/confmap/provider/fileprovider" | ||
) | ||
|
||
type TargetField string | ||
|
||
const ( | ||
TargetFieldInt TargetField = "int_field" | ||
TargetFieldString TargetField = "string_field" | ||
TargetFieldBool TargetField = "bool_field" | ||
TargetFieldInlineString TargetField = "inline_string_field" | ||
) | ||
|
||
type Test struct { | ||
value string | ||
targetField TargetField | ||
expected any | ||
expectedErr string | ||
} | ||
|
||
type TargetConfig[T any] struct { | ||
Field T `mapstructure:"field"` | ||
} | ||
|
||
func AssertExpectedMatch[T any](t *testing.T, tt Test, conf *confmap.Conf, cfg *TargetConfig[T]) { | ||
err := conf.Unmarshal(cfg) | ||
if tt.expectedErr != "" { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expected, cfg.Field) | ||
} | ||
|
||
func TestTypeCasting(t *testing.T) { | ||
values := []Test{ | ||
{ | ||
value: "123", | ||
targetField: TargetFieldInt, | ||
expected: 123, | ||
}, | ||
{ | ||
value: "123", | ||
targetField: TargetFieldString, | ||
expected: "123", | ||
}, | ||
{ | ||
value: "123", | ||
targetField: TargetFieldInlineString, | ||
expected: "inline field with 123 expansion", | ||
}, | ||
{ | ||
value: "0123", | ||
targetField: TargetFieldInt, | ||
expected: 83, | ||
}, | ||
{ | ||
value: "0123", | ||
targetField: TargetFieldString, | ||
expected: "83", | ||
}, | ||
{ | ||
value: "0123", | ||
targetField: TargetFieldInlineString, | ||
expected: "inline field with 83 expansion", | ||
}, | ||
{ | ||
value: "0xdeadbeef", | ||
targetField: TargetFieldInt, | ||
expected: 3735928559, | ||
}, | ||
{ | ||
value: "0xdeadbeef", | ||
targetField: TargetFieldString, | ||
expected: "3735928559", | ||
}, | ||
{ | ||
value: "0xdeadbeef", | ||
targetField: TargetFieldInlineString, | ||
expected: "inline field with 3735928559 expansion", | ||
}, | ||
{ | ||
value: "\"0123\"", | ||
targetField: TargetFieldString, | ||
expected: "0123", | ||
}, | ||
{ | ||
value: "\"0123\"", | ||
targetField: TargetFieldInt, | ||
expected: 83, | ||
}, | ||
{ | ||
value: "\"0123\"", | ||
targetField: TargetFieldInlineString, | ||
expected: "inline field with 0123 expansion", | ||
}, | ||
{ | ||
value: "!!str 0123", | ||
targetField: TargetFieldString, | ||
expected: "0123", | ||
}, | ||
{ | ||
value: "!!str 0123", | ||
targetField: TargetFieldInlineString, | ||
expected: "inline field with 0123 expansion", | ||
}, | ||
{ | ||
value: "t", | ||
targetField: TargetFieldBool, | ||
expected: true, | ||
}, | ||
{ | ||
value: "23", | ||
targetField: TargetFieldBool, | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, tt := range values { | ||
t.Run(tt.value+"/"+string(tt.targetField), func(t *testing.T) { | ||
testFile := "types_expand.yaml" | ||
if tt.targetField == TargetFieldInlineString { | ||
testFile = "types_expand_inline.yaml" | ||
} | ||
|
||
resolver, err := confmap.NewResolver(confmap.ResolverSettings{ | ||
URIs: []string{filepath.Join("testdata", testFile)}, | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
fileprovider.NewFactory(), | ||
envprovider.NewFactory(), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
t.Setenv("ENV", tt.value) | ||
|
||
conf, err := resolver.Resolve(context.Background()) | ||
require.NoError(t, err) | ||
|
||
switch tt.targetField { | ||
case TargetFieldInt: | ||
var cfg TargetConfig[int] | ||
AssertExpectedMatch(t, tt, conf, &cfg) | ||
case TargetFieldString, TargetFieldInlineString: | ||
var cfg TargetConfig[string] | ||
AssertExpectedMatch(t, tt, conf, &cfg) | ||
case TargetFieldBool: | ||
var cfg TargetConfig[bool] | ||
AssertExpectedMatch(t, tt, conf, &cfg) | ||
default: | ||
t.Fatalf("unexpected target field %q", tt.targetField) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters