| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package e2etest |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/confmap" |
| 15 |
"go.opentelemetry.io/collector/confmap/provider/envprovider" |
| 16 |
"go.opentelemetry.io/collector/confmap/provider/fileprovider" |
| 17 |
) |
| 18 |
|
| 19 |
func Test_EscapedEnvVars_NoDefaultScheme(t *testing.T) { |
| 20 |
const expandedValue = "some expanded value" |
| 21 |
t.Setenv("ENV_VALUE", expandedValue) |
| 22 |
t.Setenv("ENV_LIST", "['$$ESCAPE_ME','$${ESCAPE_ME}','$${env:ESCAPE_ME}']") |
| 23 |
t.Setenv("ENV_MAP", "{'key1':'$$ESCAPE_ME','key2':'$${ESCAPE_ME}','key3':'$${env:ESCAPE_ME}'}") |
| 24 |
t.Setenv("ENV_NESTED_DOLLARSIGN", "here is 1 $$") |
| 25 |
t.Setenv("ENV_NESTED_DOLLARSIGN_ESCAPED", "here are 2 $$$") |
| 26 |
t.Setenv("ENV_EXPAND_NESTED", "${env:ENV_VALUE} came from nested expansion") |
| 27 |
|
| 28 |
expectedMap := map[string]any{ |
| 29 |
"test_map": map[string]any{ |
| 30 |
"key1": "$ENV_VALUE", |
| 31 |
"key2": "$$ENV_VALUE", |
| 32 |
"key3": "$${ENV_VALUE}", |
| 33 |
"key4": "some${ENV_VALUE}text", |
| 34 |
"key5": "some${ENV_VALUE}text", |
| 35 |
"key6": "${ONE}${TWO}", |
| 36 |
"key7": "text$", |
| 37 |
"key8": "$", |
| 38 |
"key9": "${1}${env:2}", |
| 39 |
"key10": "some${env:ENV_VALUE}text", |
| 40 |
"key11": "${env:${ENV_VALUE}}", |
| 41 |
"key12": "${env:${ENV_VALUE}}", |
| 42 |
"key13": "env:MAP_VALUE_2}${ENV_VALUE}{", |
| 43 |
"key14": "$" + expandedValue, |
| 44 |
"key15": "$ENV_VALUE", |
| 45 |
"key16": []any{"$ESCAPE_ME", "${ESCAPE_ME}", "${env:ESCAPE_ME}"}, |
| 46 |
"key17": map[string]any{"key1": "$ESCAPE_ME", "key2": "${ESCAPE_ME}", "key3": "${env:ESCAPE_ME}"}, |
| 47 |
"key18": "here is 1 $", |
| 48 |
"key19": "here are 2 $$", |
| 49 |
"key20": "some expanded value came from nested expansion", |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
resolver, err := confmap.NewResolver(confmap.ResolverSettings{ |
| 54 |
URIs: []string{filepath.Join("testdata", "expand-escaped-env.yaml")}, |
| 55 |
ProviderFactories: []confmap.ProviderFactory{fileprovider.NewFactory(), envprovider.NewFactory()}, |
| 56 |
DefaultScheme: "", |
| 57 |
}) |
| 58 |
require.NoError(t, err) |
| 59 |
|
| 60 |
// Test that expanded configs are the same with the simple config with no env vars. |
| 61 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 62 |
require.NoError(t, err) |
| 63 |
m := cfgMap.ToStringMap() |
| 64 |
assert.Equal(t, expectedMap, m) |
| 65 |
} |
| 66 |
|
| 67 |
func Test_EscapedEnvVars_DefaultScheme(t *testing.T) { |
| 68 |
const expandedValue = "some expanded value" |
| 69 |
t.Setenv("ENV_VALUE", expandedValue) |
| 70 |
t.Setenv("ENV_LIST", "['$$ESCAPE_ME','$${ESCAPE_ME}','$${env:ESCAPE_ME}']") |
| 71 |
t.Setenv("ENV_MAP", "{'key1':'$$ESCAPE_ME','key2':'$${ESCAPE_ME}','key3':'$${env:ESCAPE_ME}'}") |
| 72 |
t.Setenv("ENV_NESTED_DOLLARSIGN", "here is 1 $$") |
| 73 |
t.Setenv("ENV_NESTED_DOLLARSIGN_ESCAPED", "here are 2 $$$") |
| 74 |
t.Setenv("ENV_EXPAND_NESTED", "${env:ENV_VALUE} came from nested expansion") |
| 75 |
|
| 76 |
expectedMap := map[string]any{ |
| 77 |
"test_map": map[string]any{ |
| 78 |
"key1": "$ENV_VALUE", |
| 79 |
"key2": "$$ENV_VALUE", |
| 80 |
"key3": "$" + expandedValue, |
| 81 |
"key4": "some" + expandedValue + "text", |
| 82 |
"key5": "some${ENV_VALUE}text", |
| 83 |
"key6": "${ONE}${TWO}", |
| 84 |
"key7": "text$", |
| 85 |
"key8": "$", |
| 86 |
"key9": "${1}${env:2}", |
| 87 |
"key10": "some${env:ENV_VALUE}text", |
| 88 |
"key11": "${env:" + expandedValue + "}", |
| 89 |
"key12": "${env:${ENV_VALUE}}", |
| 90 |
"key13": "env:MAP_VALUE_2}${ENV_VALUE}{", |
| 91 |
"key14": "$" + expandedValue, |
| 92 |
"key15": "$ENV_VALUE", |
| 93 |
"key16": []any{"$ESCAPE_ME", "${ESCAPE_ME}", "${env:ESCAPE_ME}"}, |
| 94 |
"key17": map[string]any{"key1": "$ESCAPE_ME", "key2": "${ESCAPE_ME}", "key3": "${env:ESCAPE_ME}"}, |
| 95 |
"key18": "here is 1 $", |
| 96 |
"key19": "here are 2 $$", |
| 97 |
"key20": "some expanded value came from nested expansion", |
| 98 |
}, |
| 99 |
} |
| 100 |
|
| 101 |
resolver, err := confmap.NewResolver(confmap.ResolverSettings{ |
| 102 |
URIs: []string{filepath.Join("testdata", "expand-escaped-env.yaml")}, |
| 103 |
ProviderFactories: []confmap.ProviderFactory{fileprovider.NewFactory(), envprovider.NewFactory()}, |
| 104 |
DefaultScheme: "env", |
| 105 |
}) |
| 106 |
require.NoError(t, err) |
| 107 |
|
| 108 |
// Test that expanded configs are the same with the simple config with no env vars. |
| 109 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 110 |
require.NoError(t, err) |
| 111 |
m := cfgMap.ToStringMap() |
| 112 |
assert.Equal(t, expectedMap, m) |
| 113 |
} |