@@ -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,124 @@ 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
+ wantErr : false ,
583
+ },
584
+ {
585
+ name : "controlled by machineset" ,
586
+ m : & clusterv1.Machine {
587
+ ObjectMeta : metav1.ObjectMeta {
588
+ Name : "test-machine" ,
589
+ Namespace : corev1 .NamespaceDefault ,
590
+ OwnerReferences : []metav1.OwnerReference {
591
+ {
592
+ APIVersion : clusterv1 .GroupVersion .String (),
593
+ Kind : machineSetKind .String (),
594
+ Name : "test-ms" ,
595
+ Controller : pointer .BoolPtr (true ),
596
+ BlockOwnerDeletion : nil ,
597
+ },
598
+ },
599
+ },
600
+ },
601
+ ms : & clusterv1.MachineSet {
602
+ ObjectMeta : metav1.ObjectMeta {
603
+ Name : "test-ms" ,
604
+ Namespace : corev1 .NamespaceDefault ,
605
+ },
606
+ },
607
+ expectedLabels : map [string ]string {
608
+ clusterv1 .MachineSetLabelName : "test-ms" ,
609
+ },
610
+ wantErr : false ,
611
+ },
612
+ {
613
+ name : "controlled by machinedeployment" ,
614
+ m : & clusterv1.Machine {
615
+ ObjectMeta : metav1.ObjectMeta {
616
+ Name : "test-machine" ,
617
+ Namespace : corev1 .NamespaceDefault ,
618
+ OwnerReferences : []metav1.OwnerReference {
619
+ {
620
+ APIVersion : clusterv1 .GroupVersion .String (),
621
+ Kind : machineSetKind .String (),
622
+ Name : "test-ms" ,
623
+ Controller : pointer .BoolPtr (true ),
624
+ BlockOwnerDeletion : nil ,
625
+ },
626
+ },
627
+ },
628
+ },
629
+ ms : & clusterv1.MachineSet {
630
+ ObjectMeta : metav1.ObjectMeta {
631
+ Name : "test-ms" ,
632
+ Namespace : corev1 .NamespaceDefault ,
633
+ OwnerReferences : []metav1.OwnerReference {
634
+ {
635
+ APIVersion : clusterv1 .GroupVersion .String (),
636
+ Kind : machineDeploymentKind .String (),
637
+ Name : "test-md" ,
638
+ Controller : pointer .BoolPtr (true ),
639
+ BlockOwnerDeletion : nil ,
640
+ },
641
+ },
642
+ },
643
+ },
644
+ md : & clusterv1.MachineDeployment {
645
+ ObjectMeta : metav1.ObjectMeta {
646
+ Name : "test-md" ,
647
+ Namespace : corev1 .NamespaceDefault ,
648
+ },
649
+ },
650
+ expectedLabels : map [string ]string {
651
+ clusterv1 .MachineSetLabelName : "test-ms" ,
652
+ clusterv1 .MachineDeploymentLabelName : "test-md" ,
653
+ },
654
+ wantErr : false ,
655
+ },
656
+ }
657
+ for _ , tt := range tests {
658
+ t .Run (tt .name , func (t * testing.T ) {
659
+ c := fake .NewFakeClientWithScheme (scheme .Scheme , tt .m )
660
+ if tt .ms != nil {
661
+ c = fake .NewFakeClientWithScheme (scheme .Scheme , tt .m , tt .ms )
662
+ }
663
+ if tt .md != nil {
664
+ c = fake .NewFakeClientWithScheme (scheme .Scheme , tt .m , tt .ms , tt .md )
665
+ }
666
+
667
+ r := & MachineReconciler {
668
+ Log : klogr .New (),
669
+ Client : c ,
670
+ }
671
+ if err := r .reconcileLables (context .Background (), tt .m ); (err != nil ) != tt .wantErr {
672
+ t .Errorf ("reconcileLables() error = %v, wantErr %v" , err , tt .wantErr )
673
+ }
674
+ if ! reflect .DeepEqual (tt .m .Labels , tt .expectedLabels ) {
675
+ t .Errorf ("expected labels %v, got %v" , tt .expectedLabels , tt .m .Labels )
676
+ }
677
+ })
678
+ }
679
+ }
0 commit comments