|
| 1 | +package e2etest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "go.opentelemetry.io/collector/confmap/provider/fileprovider" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "go.opentelemetry.io/collector/confmap" |
| 13 | + "go.opentelemetry.io/collector/confmap/converter/expandconverter" |
| 14 | + "go.opentelemetry.io/collector/confmap/provider/envprovider" |
| 15 | +) |
| 16 | + |
| 17 | +// Test_EscapedEnvVars tests that the resolver supports escaped env vars working together with expand converter. |
| 18 | +func Test_EscapedEnvVars(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + scheme string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "no_default_scheme", |
| 25 | + scheme: "", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "env", |
| 29 | + scheme: "env", |
| 30 | + }, |
| 31 | + } |
| 32 | + const expandedValue = "some expanded value" |
| 33 | + t.Setenv("ENV_VALUE", expandedValue) |
| 34 | + for _, tt := range tests { |
| 35 | + t.Run(tt.name, func(t *testing.T) { |
| 36 | + expectedMap := map[string]any{ |
| 37 | + "test_map": map[string]any{ |
| 38 | + "key1": "$ENV_VALUE", |
| 39 | + "key2": "$$ENV_VALUE", |
| 40 | + "key3": "$" + expandedValue, |
| 41 | + "key4": "some${ENV_VALUE}text", |
| 42 | + "key5": "${ONE}${TWO}", |
| 43 | + "key6": "text$", |
| 44 | + "key7": "$", |
| 45 | + "key8": "${1}${env:2}", |
| 46 | + "key9": "some${env:ENV_VALUE}text", |
| 47 | + "key10": "${env:" + expandedValue + "}", |
| 48 | + "key11": "${env:${ENV_VALUE}}", |
| 49 | + "key12": "env:MAP_VALUE_2}${ENV_VALUE}{", |
| 50 | + "key13": "$" + expandedValue, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + resolver, err := confmap.NewResolver(confmap.ResolverSettings{ |
| 55 | + URIs: []string{filepath.Join("testdata", "expand-escaped-env.yaml")}, |
| 56 | + ProviderFactories: []confmap.ProviderFactory{fileprovider.NewFactory(), envprovider.NewFactory()}, |
| 57 | + ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()}, |
| 58 | + DefaultScheme: tt.scheme, |
| 59 | + }) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // Test that expanded configs are the same with the simple config with no env vars. |
| 63 | + cfgMap, err := resolver.Resolve(context.Background()) |
| 64 | + require.NoError(t, err) |
| 65 | + m := cfgMap.ToStringMap() |
| 66 | + assert.Equal(t, expectedMap, m) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments