@@ -29,7 +29,7 @@ func TestNewConfigFromString(t *testing.T) {
2929 tcs := []struct {
3030 name string
3131 configString func () string
32- shouldFail bool
32+ err string
3333 configCheck func (* Config )
3434 }{
3535 {
@@ -54,22 +54,29 @@ func TestNewConfigFromString(t *testing.T) {
5454 configString : func () string {
5555 return `{"prometheusK8ss": {}}`
5656 },
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\" " ,
5865 },
5966 {
6067 name : "json string with unknown field" ,
6168 configString : func () string {
6269 return `{"prometheusK8s": {"unknown": "bar"}}`
6370 },
64- shouldFail : true ,
71+ err : "error unmarshaling: unknown field \" prometheusK8s.unknown \" " ,
6572 },
6673 {
6774 name : "json string with duplicated field" ,
6875 // users should be aware of this as unmarshalling would only take one part into account.
6976 configString : func () string {
7077 return `{"prometheusK8s": {"foo": {}}, "prometheusK8s": {"bar": {}}}`
7178 },
72- shouldFail : true ,
79+ err : "yaml: unmarshal errors: \n line 1: key \" prometheusK8s \" already set in map" ,
7380 },
7481 {
7582 name : "empty json string" ,
@@ -88,7 +95,14 @@ func TestNewConfigFromString(t *testing.T) {
8895 configString : func () string {
8996 return `metricsServe:`
9097 },
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\" " ,
92106 },
93107 {
94108 name : "yaml string with unknown field" ,
@@ -97,7 +111,7 @@ func TestNewConfigFromString(t *testing.T) {
97111metricsServer:
98112 unknown:`
99113 },
100- shouldFail : true ,
114+ err : "error unmarshaling: unknown field \" metricsServer.unknown \" " ,
101115 },
102116 {
103117 name : "yaml string with duplicated field" ,
@@ -108,7 +122,13 @@ metricsServer:
108122metricsServer:
109123 bar:`
110124 },
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+ },
112132 },
113133 {
114134 name : "empty yaml string" ,
@@ -121,8 +141,8 @@ metricsServer:
121141 for _ , tc := range tcs {
122142 t .Run (tc .name , func (t * testing.T ) {
123143 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 )
126146 return
127147 }
128148 require .NoError (t , err )
@@ -137,7 +157,7 @@ func TestNewUserConfigFromString(t *testing.T) {
137157 tcs := []struct {
138158 name string
139159 configString func () string
140- shouldFail bool
160+ err string
141161 configCheck func (* UserWorkloadConfiguration )
142162 }{
143163 {
@@ -163,22 +183,29 @@ func TestNewUserConfigFromString(t *testing.T) {
163183 configString : func () string {
164184 return `{"unknown": {}}`
165185 },
166- shouldFail : true ,
186+ err : "error unmarshaling: unknown field \" unknown \" " ,
167187 },
168188 {
169189 name : "json string with unknown field" ,
170190 configString : func () string {
171191 return `{"prometheusOperator": {"unknown": "bar"}}`
172192 },
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\" " ,
174201 },
175202 {
176203 name : "json string with duplicated field" ,
177204 // users should be aware of this as unmarshalling would only take one part into account.
178205 configString : func () string {
179206 return `{"prometheus": {"foo": {}}, "prometheus": {"bar": {}}}`
180207 },
181- shouldFail : true ,
208+ err : "yaml: unmarshal errors: \n line 1: key \" prometheus \" " ,
182209 },
183210 {
184211 name : "empty json string" ,
@@ -197,7 +224,7 @@ func TestNewUserConfigFromString(t *testing.T) {
197224 configString : func () string {
198225 return `unknown:`
199226 },
200- shouldFail : true ,
227+ err : "error unmarshaling: unknown field \" unknown \" " ,
201228 },
202229 {
203230 name : "yaml string with unknown field" ,
@@ -206,7 +233,16 @@ func TestNewUserConfigFromString(t *testing.T) {
206233prometheusOperator:
207234 unknown:`
208235 },
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\" " ,
210246 },
211247 {
212248 name : "yaml string with duplicated field" ,
@@ -217,7 +253,7 @@ thanosRuler:
217253thanosRuler:
218254 bar:`
219255 },
220- shouldFail : true ,
256+ err : "yaml: unmarshal errors: \n line 5: key \" thanosRuler \" " ,
221257 },
222258 {
223259 name : "empty yaml string" ,
@@ -230,8 +266,8 @@ thanosRuler:
230266 for _ , tc := range tcs {
231267 t .Run (tc .name , func (t * testing.T ) {
232268 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 )
235271 return
236272 }
237273 require .NoError (t , err )
@@ -729,23 +765,23 @@ func TestCollectionProfilePreCheck(t *testing.T) {
729765 },
730766 {
731767 name : "full_profile" ,
732- config : `prometheusk8s :
768+ config : `prometheusK8s :
733769 collectionProfile: full
734770 ` ,
735771 expected : CollectionProfile ("full" ),
736772 expectedError : false ,
737773 },
738774 {
739775 name : "minimal_profile" ,
740- config : `prometheusk8s :
776+ config : `prometheusK8s :
741777 collectionProfile: minimal
742778 ` ,
743779 expected : CollectionProfile ("minimal" ),
744780 expectedError : false ,
745781 },
746782 {
747783 name : "incorrect_profile" ,
748- config : `prometheusk8s :
784+ config : `prometheusK8s :
749785 collectionProfile: foo
750786 ` ,
751787 expected : "" ,
@@ -831,7 +867,7 @@ func TestUnsupportedAlertmanagerVersion(t *testing.T) {
831867 name : "using default value" ,
832868 config : `prometheusK8s:
833869 additionalAlertmanagerConfigs:
834- - Scheme : foo
870+ - scheme : foo
835871 ` ,
836872 },
837873 } {
0 commit comments