@@ -17,6 +17,8 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "context"
21
+ "reflect"
20
22
"testing"
21
23
22
24
. "github.com/onsi/gomega"
@@ -26,6 +28,7 @@ import (
26
28
"k8s.io/apimachinery/pkg/runtime"
27
29
"k8s.io/apimachinery/pkg/types"
28
30
"k8s.io/client-go/kubernetes/scheme"
31
+ "k8s.io/klog/klogr"
29
32
"k8s.io/utils/pointer"
30
33
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
31
34
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -553,3 +556,125 @@ func TestRemoveMachineFinalizerAfterDeleteReconcile(t *testing.T) {
553
556
Expect (mr .Client .Get (ctx , key , m )).ToNot (HaveOccurred ())
554
557
Expect (m .ObjectMeta .Finalizers ).To (Equal ([]string {metav1 .FinalizerDeleteDependents }))
555
558
}
559
+
560
+ func TestMachineReconciler_reconcileLables (t * testing.T ) {
561
+ RegisterTestingT (t )
562
+
563
+ err := clusterv1 .AddToScheme (scheme .Scheme )
564
+ Expect (err ).NotTo (HaveOccurred ())
565
+
566
+ tests := []struct {
567
+ name string
568
+ m * clusterv1.Machine
569
+ ms * clusterv1.MachineSet
570
+ md * clusterv1.MachineDeployment
571
+ expectedLabels map [string ]string
572
+ wantErr bool
573
+ }{
574
+ {
575
+ name : "no controller, machine should not have labels" ,
576
+ m : & clusterv1.Machine {
577
+ ObjectMeta : metav1.ObjectMeta {
578
+ Name : "test-machine" ,
579
+ Namespace : corev1 .NamespaceDefault ,
580
+ },
581
+ },
582
+ expectedLabels : map [string ]string {},
583
+ wantErr : false ,
584
+ },
585
+ {
586
+ name : "controlled by machineset" ,
587
+ m : & clusterv1.Machine {
588
+ ObjectMeta : metav1.ObjectMeta {
589
+ Name : "test-machine" ,
590
+ Namespace : corev1 .NamespaceDefault ,
591
+ OwnerReferences : []metav1.OwnerReference {
592
+ {
593
+ APIVersion : clusterv1 .GroupVersion .String (),
594
+ Kind : machineSetKind .String (),
595
+ Name : "test-ms" ,
596
+ Controller : pointer .BoolPtr (true ),
597
+ BlockOwnerDeletion : nil ,
598
+ },
599
+ },
600
+ },
601
+ },
602
+ ms : & clusterv1.MachineSet {
603
+ ObjectMeta : metav1.ObjectMeta {
604
+ Name : "test-ms" ,
605
+ Namespace : corev1 .NamespaceDefault ,
606
+ },
607
+ },
608
+ expectedLabels : map [string ]string {
609
+ clusterv1 .MachineSetLabelName : "test-ms" ,
610
+ },
611
+ wantErr : false ,
612
+ },
613
+ {
614
+ name : "controlled by machinedeployment" ,
615
+ m : & clusterv1.Machine {
616
+ ObjectMeta : metav1.ObjectMeta {
617
+ Name : "test-machine" ,
618
+ Namespace : corev1 .NamespaceDefault ,
619
+ OwnerReferences : []metav1.OwnerReference {
620
+ {
621
+ APIVersion : clusterv1 .GroupVersion .String (),
622
+ Kind : machineSetKind .String (),
623
+ Name : "test-ms" ,
624
+ Controller : pointer .BoolPtr (true ),
625
+ BlockOwnerDeletion : nil ,
626
+ },
627
+ },
628
+ },
629
+ },
630
+ ms : & clusterv1.MachineSet {
631
+ ObjectMeta : metav1.ObjectMeta {
632
+ Name : "test-ms" ,
633
+ Namespace : corev1 .NamespaceDefault ,
634
+ OwnerReferences : []metav1.OwnerReference {
635
+ {
636
+ APIVersion : clusterv1 .GroupVersion .String (),
637
+ Kind : machineDeploymentKind .String (),
638
+ Name : "test-md" ,
639
+ Controller : pointer .BoolPtr (true ),
640
+ BlockOwnerDeletion : nil ,
641
+ },
642
+ },
643
+ },
644
+ },
645
+ md : & clusterv1.MachineDeployment {
646
+ ObjectMeta : metav1.ObjectMeta {
647
+ Name : "test-md" ,
648
+ Namespace : corev1 .NamespaceDefault ,
649
+ },
650
+ },
651
+ expectedLabels : map [string ]string {
652
+ clusterv1 .MachineSetLabelName : "test-ms" ,
653
+ clusterv1 .MachineDeploymentLabelName : "test-md" ,
654
+ },
655
+ wantErr : false ,
656
+ },
657
+ }
658
+ for _ , tt := range tests {
659
+ t .Run (tt .name , func (t * testing.T ) {
660
+ c := fake .NewFakeClientWithScheme (scheme .Scheme , tt .m )
661
+ if tt .ms != nil {
662
+ c = fake .NewFakeClientWithScheme (scheme .Scheme , tt .m , tt .ms )
663
+ }
664
+ if tt .md != nil {
665
+ c = fake .NewFakeClientWithScheme (scheme .Scheme , tt .m , tt .ms , tt .md )
666
+ }
667
+
668
+ r := & MachineReconciler {
669
+ Log : klogr .New (),
670
+ Client : c ,
671
+ }
672
+ if err := r .reconcileLables (context .Background (), tt .m ); (err != nil ) != tt .wantErr {
673
+ t .Errorf ("reconcileLables() error = %v, wantErr %v" , err , tt .wantErr )
674
+ }
675
+ if ! reflect .DeepEqual (tt .m .Labels , tt .expectedLabels ) {
676
+ t .Errorf ("expected labels %v, got %v" , tt .expectedLabels , tt .m .Labels )
677
+ }
678
+ })
679
+ }
680
+ }
0 commit comments