Debian Codegraph @

component/componenttest/configtest_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package componenttest
5
6 import (
7 "io"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestCheckConfigStructPointerAndValue(t *testing.T) {
15 config := struct {
16 SomeFiled string `mapstructure:"test"`
17 }{}
18 assert.NoError(t, CheckConfigStruct(config))
19 assert.NoError(t, CheckConfigStruct(&config))
20 }
21
22 func TestCheckConfigStruct(t *testing.T) {
23 type BadConfigTag struct {
24 BadTagField int `mapstructure:"test-dash"`
25 }
26
27 tests := []struct {
28 name string
29 config any
30 wantErrMsgSubStr string
31 }{
32 {
33 name: "typical_config",
34 config: struct {
35 MyPublicString string `mapstructure:"string"`
36 }{},
37 },
38 {
39 name: "private_fields_ignored",
40 config: struct {
41 // A public type with proper tag.
42 MyPublicString string `mapstructure:"string"`
43 // A public type with proper tag.
44 MyPublicInt string `mapstructure:"int"`
45 // A public type that should be ignored.
46 MyFunc func() error
47 // A public type that should be ignored.
48 Reader io.Reader
49 // private type not tagged.
50 myPrivateString string
51 _someInt int
52 }{},
53 },
54 {
55 name: "remain_mapstructure_tag",
56 config: struct {
57 AdditionalProperties map[string]any `mapstructure:",remain"`
58 }{},
59 },
60 {
61 name: "remain_with_interface_type",
62 config: struct {
63 AdditionalProperties any `mapstructure:",remain"`
64 }{},
65 },
66 {
67 name: "omitempty_mapstructure_tag",
68 config: struct {
69 MyPublicString string `mapstructure:",omitempty"`
70 }{},
71 },
72 {
73 name: "named_omitempty_mapstructure_tag",
74 config: struct {
75 MyPublicString string `mapstructure:"my_public_string,omitempty"`
76 }{},
77 },
78 {
79 name: "not_struct_nor_pointer",
80 config: func(x int) int {
81 return x * x
82 },
83 wantErrMsgSubStr: "config must be a struct or a pointer to one, the passed object is a func",
84 },
85 {
86 name: "squash_on_non_struct",
87 config: struct {
88 MyInt int `mapstructure:",squash"`
89 }{},
90 wantErrMsgSubStr: "attempt to squash non-struct type on field \"MyInt\"",
91 },
92 {
93 name: "remain_on_non_map",
94 config: struct {
95 AdditionalProperties string `mapstructure:",remain"`
96 }{},
97 wantErrMsgSubStr: `attempt to use "remain" on non-map or interface type field "AdditionalProperties"`,
98 },
99 {
100 name: "bad_custom_field_name",
101 config: struct {
102 AdditionalProperties any `mapstructure:"Additional_Properties"`
103 }{},
104 wantErrMsgSubStr: `field "AdditionalProperties" has config tag "Additional_Properties" which doesn't satisfy "^[a-z0-9][a-z0-9_]*$"`,
105 },
106 {
107 name: "invalid_tag_detected",
108 config: BadConfigTag{},
109 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
110 },
111 {
112 name: "public_field_must_have_tag",
113 config: struct {
114 PublicFieldWithoutMapstructureTag string
115 }{},
116 wantErrMsgSubStr: "mapstructure tag not present on field \"PublicFieldWithoutMapstructureTag\"",
117 },
118 {
119 name: "public_field_must_have_nonempty_tag",
120 config: struct {
121 PublicFieldWithoutMapstructureTag string `mapstructure:""`
122 }{},
123 wantErrMsgSubStr: "mapstructure tag on field \"PublicFieldWithoutMapstructureTag\" is empty",
124 },
125 {
126 name: "invalid_map_item",
127 config: struct {
128 Map map[string]BadConfigTag `mapstructure:"test_map"`
129 }{},
130 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
131 },
132 {
133 name: "invalid_slice_item",
134 config: struct {
135 Slice []BadConfigTag `mapstructure:"test_slice"`
136 }{},
137 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
138 },
139 {
140 name: "invalid_array_item",
141 config: struct {
142 Array [2]BadConfigTag `mapstructure:"test_array"`
143 }{},
144 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
145 },
146 {
147 name: "invalid_map_item_ptr",
148 config: struct {
149 Map map[string]*BadConfigTag `mapstructure:"test_map"`
150 }{},
151 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
152 },
153 {
154 name: "invalid_slice_item_ptr",
155 config: struct {
156 Slice []*BadConfigTag `mapstructure:"test_slice"`
157 }{},
158 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
159 },
160 {
161 name: "invalid_array_item_ptr",
162 config: struct {
163 Array [2]*BadConfigTag `mapstructure:"test_array"`
164 }{},
165 wantErrMsgSubStr: "field \"BadTagField\" has config tag \"test-dash\" which doesn't satisfy",
166 },
167 {
168 name: "valid_map_item",
169 config: struct {
170 Map map[string]int `mapstructure:"test_map"`
171 }{},
172 },
173 {
174 name: "valid_slice_item",
175 config: struct {
176 Slice []string `mapstructure:"test_slice"`
177 }{},
178 },
179 }
180
181 for _, tt := range tests {
182 t.Run(tt.name, func(t *testing.T) {
183 err := CheckConfigStruct(tt.config)
184 if tt.wantErrMsgSubStr == "" {
185 assert.NoError(t, err)
186 } else {
187 require.ErrorContains(t, err, tt.wantErrMsgSubStr)
188 }
189 })
190 }
191 }