Skip to content

Commit

Permalink
[chore] Add end to end tests for type casting (#10399)
Browse files Browse the repository at this point in the history
<!--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
mx-psi authored Jun 13, 2024
1 parent 05bcfc0 commit 6ca551e
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
1 change: 1 addition & 0 deletions confmap/internal/e2e/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../Makefile.Common
30 changes: 30 additions & 0 deletions confmap/internal/e2e/go.mod
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
28 changes: 28 additions & 0 deletions confmap/internal/e2e/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions confmap/internal/e2e/testdata/types_expand.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
field: ${env:ENV}
1 change: 1 addition & 0 deletions confmap/internal/e2e/testdata/types_expand_inline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
field: "inline field with ${env:ENV} expansion"
168 changes: 168 additions & 0 deletions confmap/internal/e2e/types_test.go
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)
}

})
}
}
1 change: 1 addition & 0 deletions versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ excluded-modules:
- go.opentelemetry.io/collector/cmd/otelcorecol
- go.opentelemetry.io/collector/internal/e2e
- go.opentelemetry.io/collector/internal/tools
- go.opentelemetry.io/collector/confmap/internal/e2e

0 comments on commit 6ca551e

Please sign in to comment.