| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confmap // import "go.opentelemetry.io/collector/confmap" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"path/filepath" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func TestResolverExpandEnvVars(t *testing.T) { |
| 17 |
testCases := []struct { |
| 18 |
name string // test case name (also file name containing config yaml) |
| 19 |
}{ |
| 20 |
{name: "expand-with-no-env.yaml"}, |
| 21 |
{name: "expand-with-partial-env.yaml"}, |
| 22 |
{name: "expand-with-all-env.yaml"}, |
| 23 |
} |
| 24 |
|
| 25 |
envs := map[string]string{ |
| 26 |
"EXTRA": "some string", |
| 27 |
"EXTRA_MAP_VALUE_1": "some map value_1", |
| 28 |
"EXTRA_MAP_VALUE_2": "some map value_2", |
| 29 |
"EXTRA_LIST_MAP_VALUE_1": "some list map value_1", |
| 30 |
"EXTRA_LIST_MAP_VALUE_2": "some list map value_2", |
| 31 |
"EXTRA_LIST_VALUE_1": "some list value_1", |
| 32 |
"EXTRA_LIST_VALUE_2": "some list value_2", |
| 33 |
} |
| 34 |
|
| 35 |
expectedCfgMap := newConfFromFile(t, filepath.Join("testdata", "expand-with-no-env.yaml")) |
| 36 |
fileProvider := newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) { |
| 37 |
return NewRetrieved(newConfFromFile(t, uri[5:])) |
| 38 |
}) |
| 39 |
envProvider := newFakeProvider("env", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) { |
| 40 |
return NewRetrieved(envs[uri[4:]]) |
| 41 |
}) |
| 42 |
|
| 43 |
for _, tt := range testCases { |
| 44 |
t.Run(tt.name, func(t *testing.T) { |
| 45 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{filepath.Join("testdata", tt.name)}, ProviderFactories: []ProviderFactory{fileProvider, envProvider}, ConverterFactories: nil}) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
// Test that expanded configs are the same with the simple config with no env vars. |
| 49 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 50 |
require.NoError(t, err) |
| 51 |
assert.Equal(t, expectedCfgMap, cfgMap.ToStringMap()) |
| 52 |
}) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestResolverDoneNotExpandOldEnvVars(t *testing.T) { |
| 57 |
expectedCfgMap := map[string]any{"test.1": "${EXTRA}", "test.2": "$EXTRA", "test.3": "${EXTRA}:${EXTRA}"} |
| 58 |
fileProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 59 |
return NewRetrieved(expectedCfgMap) |
| 60 |
}) |
| 61 |
envProvider := newFakeProvider("env", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 62 |
return NewRetrieved("some string") |
| 63 |
}) |
| 64 |
|
| 65 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"test:"}, ProviderFactories: []ProviderFactory{fileProvider, envProvider}, ConverterFactories: nil}) |
| 66 |
require.NoError(t, err) |
| 67 |
|
| 68 |
// Test that expanded configs are the same with the simple config with no env vars. |
| 69 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 70 |
require.NoError(t, err) |
| 71 |
assert.Equal(t, expectedCfgMap, cfgMap.ToStringMap()) |
| 72 |
} |
| 73 |
|
| 74 |
func TestResolverExpandMapAndSliceValues(t *testing.T) { |
| 75 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 76 |
return NewRetrieved(map[string]any{ |
| 77 |
"test_map": map[string]any{"recv": "${test:MAP_VALUE}"}, |
| 78 |
"test_slice": []any{"${test:MAP_VALUE}"}, |
| 79 |
}) |
| 80 |
}) |
| 81 |
|
| 82 |
const receiverExtraMapValue = "some map value" |
| 83 |
testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 84 |
return NewRetrieved(receiverExtraMapValue) |
| 85 |
}) |
| 86 |
|
| 87 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 88 |
require.NoError(t, err) |
| 89 |
|
| 90 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 91 |
require.NoError(t, err) |
| 92 |
expectedMap := map[string]any{ |
| 93 |
"test_map": map[string]any{"recv": receiverExtraMapValue}, |
| 94 |
"test_slice": []any{receiverExtraMapValue}, |
| 95 |
} |
| 96 |
assert.Equal(t, expectedMap, cfgMap.ToStringMap()) |
| 97 |
} |
| 98 |
|
| 99 |
func TestResolverExpandStringValues(t *testing.T) { |
| 100 |
tests := []struct { |
| 101 |
name string |
| 102 |
input string |
| 103 |
output any |
| 104 |
defaultProvider bool |
| 105 |
}{ |
| 106 |
// Embedded. |
| 107 |
{ |
| 108 |
name: "NoMatchOldStyle", |
| 109 |
input: "${HOST}:${PORT}", |
| 110 |
output: "${HOST}:${PORT}", |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "NoMatchOldStyleDefaultProvider", |
| 114 |
input: "${HOST}:${PORT}", |
| 115 |
output: "localhost:3044", |
| 116 |
defaultProvider: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "NoMatchOldStyleNoBrackets", |
| 120 |
input: "${HOST}:$PORT", |
| 121 |
output: "${HOST}:$PORT", |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "NoMatchOldStyleNoBracketsDefaultProvider", |
| 125 |
input: "${HOST}:$PORT", |
| 126 |
output: "localhost:$PORT", |
| 127 |
defaultProvider: true, |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "ComplexValue", |
| 131 |
input: "${env:COMPLEX_VALUE}", |
| 132 |
output: []any{"localhost:3042"}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "Embedded", |
| 136 |
input: "${env:HOST}:3043", |
| 137 |
output: "localhost:3043", |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "EmbeddedMulti", |
| 141 |
input: "${env:HOST}:${env:PORT}", |
| 142 |
output: "localhost:3044", |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "EmbeddedConcat", |
| 146 |
input: "https://${env:HOST}:3045", |
| 147 |
output: "https://localhost:3045", |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "EmbeddedNewAndOldStyle", |
| 151 |
input: "${env:HOST}:${PORT}", |
| 152 |
output: "localhost:${PORT}", |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "EmbeddedNewAndOldStyleDefaultProvider", |
| 156 |
input: "${env:HOST}:${PORT}", |
| 157 |
output: "localhost:3044", |
| 158 |
defaultProvider: true, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "Int", |
| 162 |
input: "test_${env:INT}", |
| 163 |
output: "test_1", |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "Int32", |
| 167 |
input: "test_${env:INT32}", |
| 168 |
output: "test_32", |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "Int64", |
| 172 |
input: "test_${env:INT64}", |
| 173 |
output: "test_64", |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "Float32", |
| 177 |
input: "test_${env:FLOAT32}", |
| 178 |
output: "test_3.25", |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "Float64", |
| 182 |
input: "test_${env:FLOAT64}", |
| 183 |
output: "test_6.4", |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "Bool", |
| 187 |
input: "test_${env:BOOL}", |
| 188 |
output: "test_true", |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "Timestamp", |
| 192 |
input: "test_${env:TIMESTAMP}", |
| 193 |
output: "test_2023-03-20T03:17:55.432328Z", |
| 194 |
}, |
| 195 |
{ |
| 196 |
name: "MultipleSameMatches", |
| 197 |
input: "test_${env:BOOL}_test_${env:BOOL}", |
| 198 |
output: "test_true_test_true", |
| 199 |
}, |
| 200 |
|
| 201 |
// Nested. |
| 202 |
{ |
| 203 |
name: "Nested", |
| 204 |
input: "${test:localhost:${env:PORT}}", |
| 205 |
output: "localhost:3044", |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "NestedDefaultProvider", |
| 209 |
input: "${test:localhost:${PORT}}", |
| 210 |
output: "localhost:3044", |
| 211 |
defaultProvider: true, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "EmbeddedInNested", |
| 215 |
input: "${test:${env:HOST}:${env:PORT}}", |
| 216 |
output: "localhost:3044", |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "EmbeddedInNestedDefaultProvider", |
| 220 |
input: "${test:${HOST}:${PORT}}", |
| 221 |
output: "localhost:3044", |
| 222 |
defaultProvider: true, |
| 223 |
}, |
| 224 |
{ |
| 225 |
name: "EmbeddedAndNested", |
| 226 |
input: "${test:localhost:${env:PORT}}?os=${env:OS}", |
| 227 |
output: "localhost:3044?os=ubuntu", |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "NestedMultiple", |
| 231 |
input: "${test:1${test:2${test:3${test:4${test:5${test:6}}}}}}", |
| 232 |
output: "123456", |
| 233 |
}, |
| 234 |
// No expand. |
| 235 |
{ |
| 236 |
name: "NoMatchMissingOpeningBracket", |
| 237 |
input: "env:HOST}", |
| 238 |
output: "env:HOST}", |
| 239 |
}, |
| 240 |
{ |
| 241 |
name: "NoMatchMissingOpeningBracketDefaultProvider", |
| 242 |
input: "env:HOST}", |
| 243 |
output: "env:HOST}", |
| 244 |
defaultProvider: true, |
| 245 |
}, |
| 246 |
{ |
| 247 |
name: "NoMatchMissingClosingBracket", |
| 248 |
input: "${HOST", |
| 249 |
output: "${HOST", |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "NoMatchMissingClosingBracketDefaultProvider", |
| 253 |
input: "${HOST", |
| 254 |
output: "${HOST", |
| 255 |
defaultProvider: true, |
| 256 |
}, |
| 257 |
{ |
| 258 |
name: "NoMatchBracketsWithout$", |
| 259 |
input: "HO{ST}", |
| 260 |
output: "HO{ST}", |
| 261 |
}, |
| 262 |
{ |
| 263 |
name: "NoMatchBracketsWithout$DefaultProvider", |
| 264 |
input: "HO{ST}", |
| 265 |
output: "HO{ST}", |
| 266 |
defaultProvider: true, |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "NoMatchOnlyMissingClosingBracket", |
| 270 |
input: "${env:HOST${env:PORT?os=${env:OS", |
| 271 |
output: "${env:HOST${env:PORT?os=${env:OS", |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "NoMatchOnlyMissingClosingBracketDefaultProvider", |
| 275 |
input: "${env:HOST${env:PORT?os=${env:OS", |
| 276 |
output: "${env:HOST${env:PORT?os=${env:OS", |
| 277 |
defaultProvider: true, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "NoMatchOnlyMissingOpeningBracket", |
| 281 |
input: "env:HOST}env:PORT}?os=env:OS}", |
| 282 |
output: "env:HOST}env:PORT}?os=env:OS}", |
| 283 |
}, |
| 284 |
{ |
| 285 |
name: "NoMatchOnlyMissingOpeningBracketDefaultProvider", |
| 286 |
input: "env:HOST}env:PORT}?os=env:OS}", |
| 287 |
output: "env:HOST}env:PORT}?os=env:OS}", |
| 288 |
defaultProvider: true, |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "NoMatchCloseBeforeOpen", |
| 292 |
input: "env:HOST}${env:PORT", |
| 293 |
output: "env:HOST}${env:PORT", |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "NoMatchCloseBeforeOpenDefaultProvider", |
| 297 |
input: "env:HOST}${env:PORT", |
| 298 |
output: "env:HOST}${env:PORT", |
| 299 |
defaultProvider: true, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "NoMatchOldStyleNested", |
| 303 |
input: "${test:localhost:${PORT}}", |
| 304 |
output: "${test:localhost:${PORT}}", |
| 305 |
}, |
| 306 |
// Partial expand. |
| 307 |
{ |
| 308 |
name: "PartialMatchMissingOpeningBracketFirst", |
| 309 |
input: "env:HOST}${env:PORT}", |
| 310 |
output: "env:HOST}3044", |
| 311 |
}, |
| 312 |
{ |
| 313 |
name: "PartialMatchMissingOpeningBracketFirstDefaultProvider", |
| 314 |
input: "env:HOST}${PORT}", |
| 315 |
output: "env:HOST}3044", |
| 316 |
defaultProvider: true, |
| 317 |
}, |
| 318 |
{ |
| 319 |
name: "PartialMatchMissingOpeningBracketLast", |
| 320 |
input: "${env:HOST}env:PORT}", |
| 321 |
output: "localhostenv:PORT}", |
| 322 |
}, |
| 323 |
{ |
| 324 |
name: "PartialMatchMissingClosingBracketFirst", |
| 325 |
input: "${env:HOST${env:PORT}", |
| 326 |
output: "${env:HOST3044", |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "PartialMatchMissingClosingBracketLast", |
| 330 |
input: "${env:HOST}${env:PORT", |
| 331 |
output: "localhost${env:PORT", |
| 332 |
}, |
| 333 |
{ |
| 334 |
name: "PartialMatchMultipleMissingOpen", |
| 335 |
input: "env:HOST}env:PORT}?os=${env:OS}", |
| 336 |
output: "env:HOST}env:PORT}?os=ubuntu", |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "PartialMatchMultipleMissingClosing", |
| 340 |
input: "${env:HOST${env:PORT?os=${env:OS}", |
| 341 |
output: "${env:HOST${env:PORT?os=ubuntu", |
| 342 |
}, |
| 343 |
{ |
| 344 |
name: "PartialMatchMoreClosingBrackets", |
| 345 |
input: "${env:HOST}}}}}}${env:PORT?os=${env:OS}", |
| 346 |
output: "localhost}}}}}${env:PORT?os=ubuntu", |
| 347 |
}, |
| 348 |
{ |
| 349 |
name: "PartialMatchMoreOpeningBrackets", |
| 350 |
input: "${env:HOST}${${${${${env:PORT}?os=${env:OS}", |
| 351 |
output: "localhost${${${${3044?os=ubuntu", |
| 352 |
}, |
| 353 |
{ |
| 354 |
name: "PartialMatchAlternatingMissingOpening", |
| 355 |
input: "env:HOST}${env:PORT}?os=env:OS}&pr=${env:PR}", |
| 356 |
output: "env:HOST}3044?os=env:OS}&pr=amd", |
| 357 |
}, |
| 358 |
{ |
| 359 |
name: "PartialMatchAlternatingMissingClosing", |
| 360 |
input: "${env:HOST${env:PORT}?os=${env:OS&pr=${env:PR}", |
| 361 |
output: "${env:HOST3044?os=${env:OS&pr=amd", |
| 362 |
}, |
| 363 |
{ |
| 364 |
name: "SchemeAfterNoSchemeIsExpanded", |
| 365 |
input: "${HOST}${env:PORT}", |
| 366 |
output: "${HOST}3044", |
| 367 |
}, |
| 368 |
{ |
| 369 |
name: "SchemeAfterNoSchemeIsExpandedDefaultProvider", |
| 370 |
input: "${HOST}${env:PORT}", |
| 371 |
output: "localhost3044", |
| 372 |
defaultProvider: true, |
| 373 |
}, |
| 374 |
{ |
| 375 |
name: "SchemeBeforeNoSchemeIsExpanded", |
| 376 |
input: "${env:HOST}${PORT}", |
| 377 |
output: "localhost${PORT}", |
| 378 |
}, |
| 379 |
{ |
| 380 |
name: "SchemeBeforeNoSchemeIsExpandedDefaultProvider", |
| 381 |
input: "${env:HOST}${PORT}", |
| 382 |
output: "localhost3044", |
| 383 |
defaultProvider: true, |
| 384 |
}, |
| 385 |
} |
| 386 |
|
| 387 |
for _, tt := range tests { |
| 388 |
t.Run(tt.name, func(t *testing.T) { |
| 389 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 390 |
return NewRetrieved(map[string]any{tt.name: tt.input}) |
| 391 |
}) |
| 392 |
|
| 393 |
testProvider := newFakeProvider("test", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) { |
| 394 |
return NewRetrieved(uri[5:]) |
| 395 |
}) |
| 396 |
|
| 397 |
envProvider := newEnvProvider() |
| 398 |
set := ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, envProvider, testProvider}, ConverterFactories: nil} |
| 399 |
if tt.defaultProvider { |
| 400 |
set.DefaultScheme = "env" |
| 401 |
} |
| 402 |
resolver, err := NewResolver(set) |
| 403 |
require.NoError(t, err) |
| 404 |
|
| 405 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 406 |
require.NoError(t, err) |
| 407 |
assert.Equal(t, map[string]any{tt.name: tt.output}, cfgMap.ToStringMap()) |
| 408 |
}) |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
func newEnvProvider() ProviderFactory { |
| 413 |
return newFakeProvider("env", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) { |
| 414 |
// When using `env` as the default scheme for tests, the uri will not include `env:`. |
| 415 |
// Instead of duplicating the switch cases, the scheme is added instead. |
| 416 |
if uri[0:4] != "env:" { |
| 417 |
uri = "env:" + uri |
| 418 |
} |
| 419 |
switch uri { |
| 420 |
case "env:COMPLEX_VALUE": |
| 421 |
return NewRetrievedFromYAML([]byte("[localhost:3042]")) |
| 422 |
case "env:HOST": |
| 423 |
return NewRetrievedFromYAML([]byte("localhost")) |
| 424 |
case "env:TIMESTAMP": |
| 425 |
return NewRetrievedFromYAML([]byte("2023-03-20T03:17:55.432328Z")) |
| 426 |
case "env:OS": |
| 427 |
return NewRetrievedFromYAML([]byte("ubuntu")) |
| 428 |
case "env:PR": |
| 429 |
return NewRetrievedFromYAML([]byte("amd")) |
| 430 |
case "env:PORT": |
| 431 |
return NewRetrievedFromYAML([]byte("3044")) |
| 432 |
case "env:INT": |
| 433 |
return NewRetrievedFromYAML([]byte("1")) |
| 434 |
case "env:INT32": |
| 435 |
return NewRetrieved(int32(32), withStringRepresentation("32")) |
| 436 |
case "env:INT64": |
| 437 |
return NewRetrieved(int64(64), withStringRepresentation("64")) |
| 438 |
case "env:FLOAT32": |
| 439 |
return NewRetrieved(float32(3.25), withStringRepresentation("3.25")) |
| 440 |
case "env:FLOAT64": |
| 441 |
return NewRetrieved(float64(6.4), withStringRepresentation("6.4")) |
| 442 |
case "env:BOOL": |
| 443 |
return NewRetrievedFromYAML([]byte("true")) |
| 444 |
} |
| 445 |
return nil, errors.New("impossible") |
| 446 |
}) |
| 447 |
} |
| 448 |
|
| 449 |
func TestResolverExpandReturnError(t *testing.T) { |
| 450 |
tests := []struct { |
| 451 |
name string |
| 452 |
input any |
| 453 |
}{ |
| 454 |
{ |
| 455 |
name: "string_value", |
| 456 |
input: "${test:VALUE}", |
| 457 |
}, |
| 458 |
{ |
| 459 |
name: "slice_value", |
| 460 |
input: []any{"${test:VALUE}"}, |
| 461 |
}, |
| 462 |
{ |
| 463 |
name: "map_value", |
| 464 |
input: map[string]any{"test": "${test:VALUE}"}, |
| 465 |
}, |
| 466 |
{ |
| 467 |
name: "string_embedded_value", |
| 468 |
input: "https://${test:HOST}:3045", |
| 469 |
}, |
| 470 |
} |
| 471 |
|
| 472 |
for _, tt := range tests { |
| 473 |
t.Run(tt.name, func(t *testing.T) { |
| 474 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 475 |
return NewRetrieved(map[string]any{tt.name: tt.input}) |
| 476 |
}) |
| 477 |
|
| 478 |
myErr := errors.New(tt.name) |
| 479 |
testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 480 |
return nil, myErr |
| 481 |
}) |
| 482 |
|
| 483 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 484 |
require.NoError(t, err) |
| 485 |
|
| 486 |
_, err = resolver.Resolve(context.Background()) |
| 487 |
assert.ErrorIs(t, err, myErr) |
| 488 |
}) |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func TestResolverInfiniteExpand(t *testing.T) { |
| 493 |
const receiverValue = "${test:VALUE}" |
| 494 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 495 |
return NewRetrieved(map[string]any{"test": receiverValue}) |
| 496 |
}) |
| 497 |
|
| 498 |
testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 499 |
return NewRetrieved(receiverValue) |
| 500 |
}) |
| 501 |
|
| 502 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 503 |
require.NoError(t, err) |
| 504 |
|
| 505 |
_, err = resolver.Resolve(context.Background()) |
| 506 |
assert.ErrorIs(t, err, errTooManyRecursiveExpansions) |
| 507 |
} |
| 508 |
|
| 509 |
func TestResolverExpandInvalidScheme(t *testing.T) { |
| 510 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 511 |
return NewRetrieved(map[string]any{"test": "${g_c_s:VALUE}"}) |
| 512 |
}) |
| 513 |
|
| 514 |
testProvider := newFakeProvider("g_c_s", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 515 |
panic("must not be called") |
| 516 |
}) |
| 517 |
|
| 518 |
_, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 519 |
assert.ErrorContains(t, err, "invalid 'confmap.Provider' scheme") |
| 520 |
} |
| 521 |
|
| 522 |
func TestResolverExpandInvalidOpaqueValue(t *testing.T) { |
| 523 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 524 |
return NewRetrieved(map[string]any{"test": []any{map[string]any{"test": "${test:$VALUE}"}}}) |
| 525 |
}) |
| 526 |
|
| 527 |
testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 528 |
panic("must not be called") |
| 529 |
}) |
| 530 |
|
| 531 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 532 |
require.NoError(t, err) |
| 533 |
|
| 534 |
_, err = resolver.Resolve(context.Background()) |
| 535 |
assert.EqualError(t, err, `the uri "test:$VALUE" contains unsupported characters ('$')`) |
| 536 |
} |
| 537 |
|
| 538 |
func TestResolverExpandUnsupportedScheme(t *testing.T) { |
| 539 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 540 |
return NewRetrieved(map[string]any{"test": "${unsupported:VALUE}"}) |
| 541 |
}) |
| 542 |
|
| 543 |
testProvider := newFakeProvider("test", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 544 |
panic("must not be called") |
| 545 |
}) |
| 546 |
|
| 547 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, testProvider}, ConverterFactories: nil}) |
| 548 |
require.NoError(t, err) |
| 549 |
|
| 550 |
_, err = resolver.Resolve(context.Background()) |
| 551 |
assert.EqualError(t, err, `scheme "unsupported" is not supported for uri "unsupported:VALUE"`) |
| 552 |
} |
| 553 |
|
| 554 |
func TestResolverDefaultProviderExpand(t *testing.T) { |
| 555 |
provider := newFakeProvider("input", func(context.Context, string, WatcherFunc) (*Retrieved, error) { |
| 556 |
return NewRetrieved(map[string]any{"foo": "${HOST}"}) |
| 557 |
}) |
| 558 |
|
| 559 |
resolver, err := NewResolver(ResolverSettings{URIs: []string{"input:"}, ProviderFactories: []ProviderFactory{provider, newEnvProvider()}, DefaultScheme: "env", ConverterFactories: nil}) |
| 560 |
require.NoError(t, err) |
| 561 |
|
| 562 |
cfgMap, err := resolver.Resolve(context.Background()) |
| 563 |
require.NoError(t, err) |
| 564 |
assert.Equal(t, map[string]any{"foo": "localhost"}, cfgMap.ToStringMap()) |
| 565 |
} |