|
4 | 4 | package configoptional
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "fmt"
|
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/stretchr/testify/assert"
|
11 | 12 | "github.com/stretchr/testify/require"
|
12 | 13 |
|
13 | 14 | "go.opentelemetry.io/collector/confmap"
|
| 15 | + "go.opentelemetry.io/collector/confmap/confmaptest" |
| 16 | + "go.opentelemetry.io/collector/confmap/xconfmap" |
14 | 17 | )
|
15 | 18 |
|
16 | 19 | type Config[T any] struct {
|
@@ -460,3 +463,123 @@ func TestComparePointerMarshal(t *testing.T) {
|
460 | 463 | })
|
461 | 464 | }
|
462 | 465 | }
|
| 466 | + |
| 467 | +type invalid struct{} |
| 468 | + |
| 469 | +func (invalid) Validate() error { |
| 470 | + return errors.New("invalid") |
| 471 | +} |
| 472 | + |
| 473 | +var _ xconfmap.Validator = invalid{} |
| 474 | + |
| 475 | +type hasNested struct { |
| 476 | + CouldBe Optional[invalid] |
| 477 | +} |
| 478 | + |
| 479 | +func TestOptionalValidate(t *testing.T) { |
| 480 | + require.NoError(t, xconfmap.Validate(hasNested{ |
| 481 | + CouldBe: None[invalid](), |
| 482 | + })) |
| 483 | + require.NoError(t, xconfmap.Validate(hasNested{ |
| 484 | + CouldBe: Default(invalid{}), |
| 485 | + })) |
| 486 | + require.Error(t, xconfmap.Validate(hasNested{ |
| 487 | + CouldBe: Some(invalid{}), |
| 488 | + })) |
| 489 | +} |
| 490 | + |
| 491 | +type validatedConfig struct { |
| 492 | + Default Optional[optionalConfig] `mapstructure:"default"` |
| 493 | + Some Optional[someConfig] `mapstructure:"some"` |
| 494 | +} |
| 495 | + |
| 496 | +var _ xconfmap.Validator = (*optionalConfig)(nil) |
| 497 | + |
| 498 | +type optionalConfig struct { |
| 499 | + StringVal string `mapstructure:"string_val"` |
| 500 | +} |
| 501 | + |
| 502 | +func (n optionalConfig) Validate() error { |
| 503 | + if n.StringVal == "invalid" { |
| 504 | + return errors.New("field `string_val` cannot be set to `invalid`") |
| 505 | + } |
| 506 | + |
| 507 | + return nil |
| 508 | +} |
| 509 | + |
| 510 | +type someConfig struct { |
| 511 | + Nested Optional[optionalConfig] `mapstructure:"nested"` |
| 512 | +} |
| 513 | + |
| 514 | +func newDefaultValidatedConfig() validatedConfig { |
| 515 | + return validatedConfig{ |
| 516 | + Default: Default(optionalConfig{StringVal: "valid"}), |
| 517 | + } |
| 518 | +} |
| 519 | + |
| 520 | +func newInvalidDefaultConfig() validatedConfig { |
| 521 | + return validatedConfig{ |
| 522 | + Default: Default(optionalConfig{StringVal: "invalid"}), |
| 523 | + } |
| 524 | +} |
| 525 | + |
| 526 | +func TestOptionalFileValidate(t *testing.T) { |
| 527 | + cases := []struct { |
| 528 | + name string |
| 529 | + variant string |
| 530 | + cfg func() validatedConfig |
| 531 | + err error |
| 532 | + }{ |
| 533 | + { |
| 534 | + name: "valid default with just key set and no subfields", |
| 535 | + variant: "implicit", |
| 536 | + cfg: newDefaultValidatedConfig, |
| 537 | + }, |
| 538 | + { |
| 539 | + name: "valid default with keys set in default", |
| 540 | + variant: "explicit", |
| 541 | + cfg: newDefaultValidatedConfig, |
| 542 | + }, |
| 543 | + { |
| 544 | + name: "invalid config", |
| 545 | + variant: "invalid", |
| 546 | + cfg: newDefaultValidatedConfig, |
| 547 | + err: errors.New("default: field `string_val` cannot be set to `invalid`\nsome: nested: field `string_val` cannot be set to `invalid`"), |
| 548 | + }, |
| 549 | + { |
| 550 | + name: "invalid default throws an error", |
| 551 | + variant: "implicit", |
| 552 | + cfg: newInvalidDefaultConfig, |
| 553 | + err: errors.New("default: field `string_val` cannot be set to `invalid`"), |
| 554 | + }, |
| 555 | + { |
| 556 | + name: "invalid default does not throw an error when key is not set", |
| 557 | + variant: "no_default", |
| 558 | + cfg: newInvalidDefaultConfig, |
| 559 | + }, |
| 560 | + { |
| 561 | + name: "invalid default invalid default does not throw an error when the value is overridden", |
| 562 | + variant: "explicit", |
| 563 | + cfg: newInvalidDefaultConfig, |
| 564 | + }, |
| 565 | + } |
| 566 | + |
| 567 | + for _, tt := range cases { |
| 568 | + t.Run(tt.name, func(t *testing.T) { |
| 569 | + conf, err := confmaptest.LoadConf(fmt.Sprintf("testdata/validate_%s.yaml", tt.variant)) |
| 570 | + require.NoError(t, err) |
| 571 | + |
| 572 | + cfg := tt.cfg() |
| 573 | + |
| 574 | + err = conf.Unmarshal(&cfg) |
| 575 | + require.NoError(t, err) |
| 576 | + |
| 577 | + err = xconfmap.Validate(cfg) |
| 578 | + if tt.err == nil { |
| 579 | + require.NoError(t, err) |
| 580 | + } else { |
| 581 | + require.EqualError(t, err, tt.err.Error()) |
| 582 | + } |
| 583 | + }) |
| 584 | + } |
| 585 | +} |
0 commit comments