forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confmap_test.go
230 lines (208 loc) · 6.38 KB
/
confmap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package confmap
import (
"errors"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestToStringMapFlatten(t *testing.T) {
conf := NewFromStringMap(map[string]interface{}{"key::embedded": int64(123)})
assert.Equal(t, map[string]interface{}{"key": map[string]interface{}{"embedded": int64(123)}}, conf.ToStringMap())
}
func TestToStringMap(t *testing.T) {
tests := []struct {
name string
fileName string
stringMap map[string]interface{}
}{
{
name: "Sample Collector configuration",
fileName: filepath.Join("testdata", "config.yaml"),
stringMap: map[string]interface{}{
"receivers": map[string]interface{}{
"nop": nil,
"nop/myreceiver": nil,
},
"processors": map[string]interface{}{
"nop": nil,
"nop/myprocessor": nil,
},
"exporters": map[string]interface{}{
"nop": nil,
"nop/myexporter": nil,
},
"extensions": map[string]interface{}{
"nop": nil,
"nop/myextension": nil,
},
"service": map[string]interface{}{
"extensions": []interface{}{"nop"},
"pipelines": map[string]interface{}{
"traces": map[string]interface{}{
"receivers": []interface{}{"nop"},
"processors": []interface{}{"nop"},
"exporters": []interface{}{"nop"},
},
},
},
},
},
{
name: "Sample types",
fileName: filepath.Join("testdata", "basic_types.yaml"),
stringMap: map[string]interface{}{
"typed.options": map[string]interface{}{
"floating.point.example": 3.14,
"integer.example": 1234,
"bool.example": false,
"string.example": "this is a string",
"nil.example": nil,
},
},
},
{
name: "Embedded keys",
fileName: filepath.Join("testdata", "embedded_keys.yaml"),
stringMap: map[string]interface{}{
"typed": map[string]interface{}{"options": map[string]interface{}{
"floating": map[string]interface{}{"point": map[string]interface{}{"example": 3.14}},
"integer": map[string]interface{}{"example": 1234},
"bool": map[string]interface{}{"example": false},
"string": map[string]interface{}{"example": "this is a string"},
"nil": map[string]interface{}{"example": nil},
}},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.stringMap, newConfFromFile(t, test.fileName))
})
}
}
func TestExpandNilStructPointersHookFunc(t *testing.T) {
stringMap := map[string]interface{}{
"boolean": nil,
"struct": nil,
"map_struct": map[string]interface{}{
"struct": nil,
},
}
conf := NewFromStringMap(stringMap)
cfg := &TestConfig{}
assert.Nil(t, cfg.Struct)
assert.NoError(t, conf.UnmarshalExact(cfg))
assert.Nil(t, cfg.Boolean)
// assert.False(t, *cfg.Boolean)
assert.Nil(t, cfg.Struct)
assert.NotNil(t, cfg.MapStruct)
// TODO: Investigate this unexpected result.
assert.Equal(t, &Struct{}, cfg.MapStruct["struct"])
}
func TestExpandNilStructPointersHookFuncDefaultNotNilConfigNil(t *testing.T) {
stringMap := map[string]interface{}{
"boolean": nil,
"struct": nil,
"map_struct": map[string]interface{}{
"struct": nil,
},
}
conf := NewFromStringMap(stringMap)
varBool := true
s1 := &Struct{Name: "s1"}
s2 := &Struct{Name: "s2"}
cfg := &TestConfig{
Boolean: &varBool,
Struct: s1,
MapStruct: map[string]*Struct{"struct": s2},
}
assert.NoError(t, conf.UnmarshalExact(cfg))
assert.NotNil(t, cfg.Boolean)
assert.True(t, *cfg.Boolean)
assert.NotNil(t, cfg.Struct)
assert.Equal(t, s1, cfg.Struct)
assert.NotNil(t, cfg.MapStruct)
// TODO: Investigate this unexpected result.
assert.Equal(t, &Struct{}, cfg.MapStruct["struct"])
}
type TestConfig struct {
Boolean *bool `mapstructure:"boolean"`
Struct *Struct `mapstructure:"struct"`
MapStruct map[string]*Struct `mapstructure:"map_struct"`
}
type Struct struct {
Name string
}
type TestID string
func (tID *TestID) UnmarshalText(text []byte) error {
*tID = TestID(strings.TrimSuffix(string(text), "_"))
if *tID == "error" {
return errors.New("parsing error")
}
return nil
}
type TestIDConfig struct {
Boolean bool `mapstructure:"bool"`
Map map[TestID]string `mapstructure:"map"`
}
func TestMapKeyStringToMapKeyTextUnmarshalerHookFunc(t *testing.T) {
stringMap := map[string]interface{}{
"bool": true,
"map": map[string]interface{}{
"string": "this is a string",
},
}
conf := NewFromStringMap(stringMap)
cfg := &TestIDConfig{}
assert.NoError(t, conf.UnmarshalExact(cfg))
assert.True(t, cfg.Boolean)
assert.Equal(t, map[TestID]string{"string": "this is a string"}, cfg.Map)
}
func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncDuplicateID(t *testing.T) {
stringMap := map[string]interface{}{
"bool": true,
"map": map[string]interface{}{
"string": "this is a string",
"string_": "this is another string",
},
}
conf := NewFromStringMap(stringMap)
cfg := &TestIDConfig{}
assert.Error(t, conf.UnmarshalExact(cfg))
}
func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncErrorUnmarshal(t *testing.T) {
stringMap := map[string]interface{}{
"bool": true,
"map": map[string]interface{}{
"error": "this is a string",
},
}
conf := NewFromStringMap(stringMap)
cfg := &TestIDConfig{}
assert.Error(t, conf.UnmarshalExact(cfg))
}
// newConfFromFile creates a new Conf by reading the given file.
func newConfFromFile(t testing.TB, fileName string) map[string]interface{} {
content, err := ioutil.ReadFile(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to read the file %v", fileName)
var data map[string]interface{}
require.NoError(t, yaml.Unmarshal(content, &data), "unable to parse yaml")
return NewFromStringMap(data).ToStringMap()
}