@@ -29,7 +29,7 @@ func TestNewConfigFromString(t *testing.T) {
29
29
tcs := []struct {
30
30
name string
31
31
configString func () string
32
- shouldFail bool
32
+ err string
33
33
configCheck func (* Config )
34
34
}{
35
35
{
@@ -54,22 +54,29 @@ func TestNewConfigFromString(t *testing.T) {
54
54
configString : func () string {
55
55
return `{"prometheusK8ss": {}}`
56
56
},
57
- shouldFail : true ,
57
+ err : "error unmarshaling: unknown field \" prometheusK8ss\" " ,
58
+ },
59
+ {
60
+ name : "json string with root field of the wrong case" ,
61
+ configString : func () string {
62
+ return `{"PROMETHEUSK8S": {}}`
63
+ },
64
+ err : "error unmarshaling: unknown field \" PROMETHEUSK8S\" " ,
58
65
},
59
66
{
60
67
name : "json string with unknown field" ,
61
68
configString : func () string {
62
69
return `{"prometheusK8s": {"unknown": "bar"}}`
63
70
},
64
- shouldFail : true ,
71
+ err : "error unmarshaling: unknown field \" prometheusK8s.unknown \" " ,
65
72
},
66
73
{
67
74
name : "json string with duplicated field" ,
68
75
// users should be aware of this as unmarshalling would only take one part into account.
69
76
configString : func () string {
70
77
return `{"prometheusK8s": {"foo": {}}, "prometheusK8s": {"bar": {}}}`
71
78
},
72
- shouldFail : true ,
79
+ err : "yaml: unmarshal errors: \n line 1: key \" prometheusK8s \" already set in map" ,
73
80
},
74
81
{
75
82
name : "empty json string" ,
@@ -88,7 +95,14 @@ func TestNewConfigFromString(t *testing.T) {
88
95
configString : func () string {
89
96
return `metricsServe:`
90
97
},
91
- shouldFail : true ,
98
+ err : "error unmarshaling: unknown field \" metricsServe\" " ,
99
+ },
100
+ {
101
+ name : "yaml string with root field of the wrong case" ,
102
+ configString : func () string {
103
+ return `metricserver:`
104
+ },
105
+ err : "error unmarshaling: unknown field \" metricserver\" " ,
92
106
},
93
107
{
94
108
name : "yaml string with unknown field" ,
@@ -97,7 +111,7 @@ func TestNewConfigFromString(t *testing.T) {
97
111
metricsServer:
98
112
unknown:`
99
113
},
100
- shouldFail : true ,
114
+ err : "error unmarshaling: unknown field \" metricsServer.unknown \" " ,
101
115
},
102
116
{
103
117
name : "yaml string with duplicated field" ,
@@ -108,7 +122,13 @@ metricsServer:
108
122
metricsServer:
109
123
bar:`
110
124
},
111
- shouldFail : true ,
125
+ err : "yaml: unmarshal errors:\n line 5: key \" metricsServer\" already set in map" ,
126
+ },
127
+ {
128
+ name : "empty yaml string" ,
129
+ configString : func () string {
130
+ return ``
131
+ },
112
132
},
113
133
{
114
134
name : "empty yaml string" ,
@@ -121,8 +141,8 @@ metricsServer:
121
141
for _ , tc := range tcs {
122
142
t .Run (tc .name , func (t * testing.T ) {
123
143
c , err := NewConfigFromString (tc .configString (), false )
124
- if tc .shouldFail {
125
- require .Error (t , err )
144
+ if tc .err != "" {
145
+ require .ErrorContains (t , err , tc . err )
126
146
return
127
147
}
128
148
require .NoError (t , err )
@@ -137,7 +157,7 @@ func TestNewUserConfigFromString(t *testing.T) {
137
157
tcs := []struct {
138
158
name string
139
159
configString func () string
140
- shouldFail bool
160
+ err string
141
161
configCheck func (* UserWorkloadConfiguration )
142
162
}{
143
163
{
@@ -163,22 +183,29 @@ func TestNewUserConfigFromString(t *testing.T) {
163
183
configString : func () string {
164
184
return `{"unknown": {}}`
165
185
},
166
- shouldFail : true ,
186
+ err : "error unmarshaling: unknown field \" unknown \" " ,
167
187
},
168
188
{
169
189
name : "json string with unknown field" ,
170
190
configString : func () string {
171
191
return `{"prometheusOperator": {"unknown": "bar"}}`
172
192
},
173
- shouldFail : true ,
193
+ err : "error unmarshaling: unknown field \" prometheusOperator.unknown\" " ,
194
+ },
195
+ {
196
+ name : "json string with field of wrong case" ,
197
+ configString : func () string {
198
+ return `{"prometheusOperator": {"nodeselector": ""}}`
199
+ },
200
+ err : "error unmarshaling: unknown field \" prometheusOperator.nodeselector\" " ,
174
201
},
175
202
{
176
203
name : "json string with duplicated field" ,
177
204
// users should be aware of this as unmarshalling would only take one part into account.
178
205
configString : func () string {
179
206
return `{"prometheus": {"foo": {}}, "prometheus": {"bar": {}}}`
180
207
},
181
- shouldFail : true ,
208
+ err : "yaml: unmarshal errors: \n line 1: key \" prometheus \" " ,
182
209
},
183
210
{
184
211
name : "empty json string" ,
@@ -197,7 +224,7 @@ func TestNewUserConfigFromString(t *testing.T) {
197
224
configString : func () string {
198
225
return `unknown:`
199
226
},
200
- shouldFail : true ,
227
+ err : "error unmarshaling: unknown field \" unknown \" " ,
201
228
},
202
229
{
203
230
name : "yaml string with unknown field" ,
@@ -206,7 +233,16 @@ func TestNewUserConfigFromString(t *testing.T) {
206
233
prometheusOperator:
207
234
unknown:`
208
235
},
209
- shouldFail : true ,
236
+ err : "error unmarshaling: unknown field \" prometheusOperator.unknown\" " ,
237
+ },
238
+ {
239
+ name : "yaml string with field of wrong case" ,
240
+ configString : func () string {
241
+ return `
242
+ prometheusOperator:
243
+ nodeselector:`
244
+ },
245
+ err : "error unmarshaling: unknown field \" prometheusOperator.nodeselector\" " ,
210
246
},
211
247
{
212
248
name : "yaml string with duplicated field" ,
@@ -217,7 +253,7 @@ thanosRuler:
217
253
thanosRuler:
218
254
bar:`
219
255
},
220
- shouldFail : true ,
256
+ err : "yaml: unmarshal errors: \n line 5: key \" thanosRuler \" " ,
221
257
},
222
258
{
223
259
name : "empty yaml string" ,
@@ -230,8 +266,8 @@ thanosRuler:
230
266
for _ , tc := range tcs {
231
267
t .Run (tc .name , func (t * testing.T ) {
232
268
c , err := NewUserConfigFromString (tc .configString ())
233
- if tc .shouldFail {
234
- require .Error (t , err )
269
+ if tc .err != "" {
270
+ require .ErrorContains (t , err , tc . err )
235
271
return
236
272
}
237
273
require .NoError (t , err )
@@ -729,23 +765,23 @@ func TestCollectionProfilePreCheck(t *testing.T) {
729
765
},
730
766
{
731
767
name : "full_profile" ,
732
- config : `prometheusk8s :
768
+ config : `prometheusK8s :
733
769
collectionProfile: full
734
770
` ,
735
771
expected : CollectionProfile ("full" ),
736
772
expectedError : false ,
737
773
},
738
774
{
739
775
name : "minimal_profile" ,
740
- config : `prometheusk8s :
776
+ config : `prometheusK8s :
741
777
collectionProfile: minimal
742
778
` ,
743
779
expected : CollectionProfile ("minimal" ),
744
780
expectedError : false ,
745
781
},
746
782
{
747
783
name : "incorrect_profile" ,
748
- config : `prometheusk8s :
784
+ config : `prometheusK8s :
749
785
collectionProfile: foo
750
786
` ,
751
787
expected : "" ,
@@ -831,7 +867,7 @@ func TestUnsupportedAlertmanagerVersion(t *testing.T) {
831
867
name : "using default value" ,
832
868
config : `prometheusK8s:
833
869
additionalAlertmanagerConfigs:
834
- - Scheme : foo
870
+ - scheme : foo
835
871
` ,
836
872
},
837
873
} {
0 commit comments