Skip to content

Commit 78663f9

Browse files
authored
[chore][confmap] Add tests for handling of slices (#12871)
<!--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 a test for #12661 and another test present on #11882. I verified that the first test fails in v0.123.0: on this version the slice has `[]S{A: "A", B: "B"}` instead.
1 parent d6dc135 commit 78663f9

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

confmap/confmap_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,59 @@ func TestZeroSliceHookFunc(t *testing.T) {
689689
}
690690
}
691691

692+
// Tests for issue that happened in https://github.com/open-telemetry/opentelemetry-collector/issues/12661.
693+
func TestStructValuesReplaced(t *testing.T) {
694+
type S struct {
695+
A string `mapstructure:"A,omitempty"`
696+
B string `mapstructure:"B,omitempty"`
697+
}
698+
699+
type structWithSlices struct {
700+
Structs []S `mapstructure:"structs"`
701+
}
702+
703+
slicesStruct := structWithSlices{
704+
Structs: []S{
705+
{A: "A"},
706+
},
707+
}
708+
709+
bCfg := map[string]any{
710+
"structs": []any{
711+
map[string]any{
712+
"B": "B",
713+
},
714+
},
715+
}
716+
bConf := NewFromStringMap(bCfg)
717+
err := bConf.Unmarshal(&slicesStruct)
718+
require.NoError(t, err)
719+
720+
assert.Equal(t, []S{{B: "B"}}, slicesStruct.Structs)
721+
}
722+
723+
func TestNilValuesUnchanged(t *testing.T) {
724+
type structWithSlices struct {
725+
Strings []string `mapstructure:"strings"`
726+
}
727+
728+
slicesStruct := &structWithSlices{}
729+
730+
nilCfg := map[string]any{
731+
"strings": []any(nil),
732+
}
733+
nilConf := NewFromStringMap(nilCfg)
734+
err := nilConf.Unmarshal(slicesStruct)
735+
require.NoError(t, err)
736+
737+
confFromStruct := New()
738+
err = confFromStruct.Marshal(slicesStruct)
739+
require.NoError(t, err)
740+
741+
require.Equal(t, nilCfg, nilConf.ToStringMap())
742+
require.Equal(t, confFromStruct.ToStringMap(), nilConf.ToStringMap())
743+
}
744+
692745
type c struct {
693746
Modifiers []string `mapstructure:"modifiers"`
694747
}

faulty_config.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
receivers:
2+
nop:
3+
exporters:
4+
nop:
5+
service:
6+
pipelines:
7+
logs:
8+
receivers: [nop]
9+
processors: []
10+
exporters: [nop]
11+
telemetry:
12+
metrics:
13+
readers:
14+
- periodic:
15+
exporter:
16+
otlp:
17+
protocol: http/protobuf
18+
endpoint: https://backend:4318

0 commit comments

Comments
 (0)