@@ -18,6 +18,7 @@ package store
1818
1919import (
2020 "reflect"
21+ "slices"
2122 "testing"
2223
2324 "k8s.io/kube-state-metrics/v2/pkg/options"
@@ -196,3 +197,64 @@ func TestWithAllowAnnotations(t *testing.T) {
196197 }
197198 }
198199}
200+
201+ func TestWithEnabledResources (t * testing.T ) {
202+
203+ tests := []struct {
204+ Desc string
205+ EnabledResources []string
206+ Wanted []string
207+ err expectedError
208+ }{
209+ {
210+ Desc : "sorts enabled resources" ,
211+ EnabledResources : []string {"pods" , "cronjobs" , "deployments" },
212+ Wanted : []string {
213+ "cronjobs" ,
214+ "deployments" ,
215+ "pods" ,
216+ },
217+ },
218+ {
219+ Desc : "de-duplicates enabled resources" ,
220+ EnabledResources : []string {"pods" , "cronjobs" , "deployments" , "pods" },
221+ Wanted : []string {
222+ "cronjobs" ,
223+ "deployments" ,
224+ "pods" ,
225+ },
226+ },
227+ {
228+ Desc : "error if not exist" ,
229+ EnabledResources : []string {"pods" , "cronjobs" , "deployments" , "foo" },
230+ Wanted : []string {},
231+ err : expectedError {
232+ expectedResourceError : true ,
233+ },
234+ },
235+ }
236+ for _ , test := range tests {
237+ b := NewBuilder ()
238+
239+ // Set the enabled resources.
240+ err := b .WithEnabledResources (test .EnabledResources )
241+ if test .err .expectedResourceError {
242+ if err == nil {
243+ t .Log ("Did not expect error while setting resources (--resources)." )
244+ t .Fatalf ("Test error for Desc: %s. Got Error: %v" , test .Desc , err )
245+ } else {
246+ return
247+ }
248+ }
249+ if err != nil {
250+ t .Log ("..." )
251+ t .Fatal ("..." , test .Desc , err )
252+ }
253+
254+ // Evaluate.
255+ if ! slices .Equal (b .enabledResources , test .Wanted ) {
256+ t .Log ("Expected enabled resources to be equal." )
257+ t .Errorf ("Test error for Desc: %s\n Want: \n %+v\n Got: \n %#+v" , test .Desc , test .Wanted , b .enabledResources )
258+ }
259+ }
260+ }
0 commit comments