@@ -17,14 +17,17 @@ package metautils_test
17
17
import (
18
18
"reflect"
19
19
20
+ "github.com/golang/mock/gomock"
20
21
. "github.com/onmetal/controller-utils/metautils"
22
+ mockmetautils "github.com/onmetal/controller-utils/mock/controller-utils/metautils"
21
23
. "github.com/onsi/ginkgo/v2"
22
24
. "github.com/onsi/gomega"
23
25
appsv1 "k8s.io/api/apps/v1"
24
26
corev1 "k8s.io/api/core/v1"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
29
"k8s.io/apimachinery/pkg/runtime"
30
+ "k8s.io/apimachinery/pkg/runtime/schema"
28
31
"k8s.io/apimachinery/pkg/types"
29
32
"k8s.io/client-go/kubernetes/scheme"
30
33
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -44,6 +47,14 @@ func (b *BadList) DeepCopyObject() runtime.Object {
44
47
}
45
48
46
49
var _ = Describe ("Metautils" , func () {
50
+ var (
51
+ ctrl * gomock.Controller
52
+ )
53
+ BeforeEach (func () {
54
+ ctrl = gomock .NewController (GinkgoT ())
55
+ DeferCleanup (ctrl .Finish )
56
+ })
57
+
47
58
Describe ("ListElementType" , func () {
48
59
It ("should return the element type of an object list" , func () {
49
60
t , err := ListElementType (& appsv1.DeploymentList {})
@@ -451,4 +462,97 @@ var _ = Describe("Metautils", func() {
451
462
Expect (func () { MustSetObjectSlice (1 , nil ) }).To (Panic ())
452
463
})
453
464
})
465
+
466
+ Describe ("NewListForGVK" , func () {
467
+ It ("should create a new list for the given gvk" , func () {
468
+ list , err := NewListForGVK (scheme .Scheme , corev1 .SchemeGroupVersion .WithKind ("Secret" ))
469
+ Expect (err ).NotTo (HaveOccurred ())
470
+
471
+ Expect (list ).To (Equal (& corev1.SecretList {}))
472
+ })
473
+
474
+ It ("should error if it cannot instantiate the list" , func () {
475
+ _ , err := NewListForGVK (scheme .Scheme , schema.GroupVersionKind {})
476
+ Expect (err ).To (HaveOccurred ())
477
+ })
478
+ })
479
+
480
+ Describe ("NewListForObject" , func () {
481
+ It ("should create a new list for the given object" , func () {
482
+ list , err := NewListForObject (scheme .Scheme , & corev1.Secret {})
483
+ Expect (err ).NotTo (HaveOccurred ())
484
+
485
+ Expect (list ).To (Equal (& corev1.SecretList {}))
486
+ })
487
+
488
+ It ("should error if it cannot determine the gvk for the object" , func () {
489
+ _ , err := NewListForObject (scheme .Scheme , & unstructured.Unstructured {})
490
+ Expect (err ).To (HaveOccurred ())
491
+ })
492
+ })
493
+
494
+ Describe ("EachListItem" , func () {
495
+ It ("should traverse over each list item" , func () {
496
+ list := & corev1.SecretList {
497
+ Items : []corev1.Secret {
498
+ {ObjectMeta : metav1.ObjectMeta {Name : "foo" }},
499
+ {ObjectMeta : metav1.ObjectMeta {Name : "bar" }},
500
+ },
501
+ }
502
+
503
+ f := mockmetautils .NewMockEachListItemFunc (ctrl )
504
+ gomock .InOrder (
505
+ f .EXPECT ().Call (& list .Items [0 ]),
506
+ f .EXPECT ().Call (& list .Items [1 ]),
507
+ )
508
+
509
+ Expect (EachListItem (list , f .Call )).To (Succeed ())
510
+ })
511
+ })
512
+
513
+ DescribeTable ("SetLabel" ,
514
+ func (initLabels map [string ]string , key , value string , expected map [string ]string ) {
515
+ obj := & metav1.ObjectMeta {Labels : initLabels }
516
+ SetLabel (obj , key , value )
517
+ Expect (obj .Labels ).To (Equal (expected ))
518
+ },
519
+ Entry ("nil labels" , nil , "foo" , "bar" , map [string ]string {"foo" : "bar" }),
520
+ Entry ("key present w/ different value" , map [string ]string {"foo" : "baz" }, "foo" , "bar" , map [string ]string {"foo" : "bar" }),
521
+ Entry ("other keys present" , map [string ]string {"bar" : "baz" }, "foo" , "bar" , map [string ]string {"bar" : "baz" , "foo" : "bar" }),
522
+ )
523
+
524
+ DescribeTable ("SetLabels" ,
525
+ func (initLabels map [string ]string , set , expected map [string ]string ) {
526
+ obj := & metav1.ObjectMeta {Labels : initLabels }
527
+ SetLabels (obj , set )
528
+ Expect (obj .Labels ).To (Equal (expected ))
529
+ },
530
+ Entry ("nil labels" , nil , map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" }),
531
+ Entry ("key present w/ different value" , map [string ]string {"foo" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" }),
532
+ Entry ("other keys present" , map [string ]string {"bar" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"bar" : "baz" , "foo" : "bar" }),
533
+ Entry ("partial other keys, same key" , map [string ]string {"foo" : "baz" , "bar" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" , "bar" : "baz" }),
534
+ )
535
+
536
+ DescribeTable ("SetAnnotation" ,
537
+ func (initAnnotations map [string ]string , key , value string , expected map [string ]string ) {
538
+ obj := & metav1.ObjectMeta {Annotations : initAnnotations }
539
+ SetAnnotation (obj , key , value )
540
+ Expect (obj .Annotations ).To (Equal (expected ))
541
+ },
542
+ Entry ("nil annotations" , nil , "foo" , "bar" , map [string ]string {"foo" : "bar" }),
543
+ Entry ("key present w/ different value" , map [string ]string {"foo" : "baz" }, "foo" , "bar" , map [string ]string {"foo" : "bar" }),
544
+ Entry ("other keys present" , map [string ]string {"bar" : "baz" }, "foo" , "bar" , map [string ]string {"bar" : "baz" , "foo" : "bar" }),
545
+ )
546
+
547
+ DescribeTable ("SetAnnotations" ,
548
+ func (initAnnotations map [string ]string , set , expected map [string ]string ) {
549
+ obj := & metav1.ObjectMeta {Annotations : initAnnotations }
550
+ SetAnnotations (obj , set )
551
+ Expect (obj .Annotations ).To (Equal (expected ))
552
+ },
553
+ Entry ("nil annotations" , nil , map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" }),
554
+ Entry ("key present w/ different value" , map [string ]string {"foo" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" }),
555
+ Entry ("other keys present" , map [string ]string {"bar" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"bar" : "baz" , "foo" : "bar" }),
556
+ Entry ("partial other keys, same key" , map [string ]string {"foo" : "baz" , "bar" : "baz" }, map [string ]string {"foo" : "bar" }, map [string ]string {"foo" : "bar" , "bar" : "baz" }),
557
+ )
454
558
})
0 commit comments