@@ -451,6 +451,59 @@ func Test_patchClusterBindingStatusCondition(t *testing.T) {
451
451
}
452
452
}
453
453
454
+ func Test_patchClusterBindingStatusWithAffinityName (t * testing.T ) {
455
+ karmadaClient := karmadafake .NewSimpleClientset ()
456
+
457
+ tests := []struct {
458
+ name string
459
+ binding * workv1alpha2.ClusterResourceBinding
460
+ affinityName string
461
+ expected * workv1alpha2.ClusterResourceBinding
462
+ }{
463
+ {
464
+ name : "add affinityName in status" ,
465
+ binding : & workv1alpha2.ClusterResourceBinding {
466
+ ObjectMeta : metav1.ObjectMeta {Name : "crb-1" , Generation : 1 },
467
+ Spec : workv1alpha2.ResourceBindingSpec {},
468
+ Status : workv1alpha2.ResourceBindingStatus {
469
+ Conditions : []metav1.Condition {util .NewCondition (workv1alpha2 .Scheduled , workv1alpha2 .BindingReasonSuccess , successfulSchedulingMessage , metav1 .ConditionTrue )},
470
+ SchedulerObservedGeneration : 1 ,
471
+ },
472
+ },
473
+ affinityName : "group1" ,
474
+ expected : & workv1alpha2.ClusterResourceBinding {
475
+ ObjectMeta : metav1.ObjectMeta {Name : "crb-1" },
476
+ Spec : workv1alpha2.ResourceBindingSpec {},
477
+ Status : workv1alpha2.ResourceBindingStatus {
478
+ SchedulerObservedAffinityName : "group1" ,
479
+ Conditions : []metav1.Condition {util .NewCondition (workv1alpha2 .Scheduled , workv1alpha2 .BindingReasonSuccess , successfulSchedulingMessage , metav1 .ConditionTrue )},
480
+ SchedulerObservedGeneration : 1 ,
481
+ },
482
+ },
483
+ },
484
+ }
485
+
486
+ for _ , test := range tests {
487
+ t .Run (test .name , func (t * testing.T ) {
488
+ _ , err := karmadaClient .WorkV1alpha2 ().ClusterResourceBindings ().Create (context .TODO (), test .binding , metav1.CreateOptions {})
489
+ if err != nil {
490
+ t .Fatal (err )
491
+ }
492
+ err = patchClusterBindingStatusWithAffinityName (karmadaClient , test .binding , test .affinityName )
493
+ if err != nil {
494
+ t .Error (err )
495
+ }
496
+ res , err := karmadaClient .WorkV1alpha2 ().ClusterResourceBindings ().Get (context .TODO (), test .binding .Name , metav1.GetOptions {})
497
+ if err != nil {
498
+ t .Fatal (err )
499
+ }
500
+ if ! reflect .DeepEqual (res .Status , test .expected .Status ) {
501
+ t .Errorf ("expected status: %v, but got: %v" , test .expected .Status , res .Status )
502
+ }
503
+ })
504
+ }
505
+ }
506
+
454
507
func Test_recordScheduleResultEventForResourceBinding (t * testing.T ) {
455
508
fakeRecorder := record .NewFakeRecorder (10 )
456
509
scheduler := & Scheduler {eventRecorder : fakeRecorder }
@@ -631,47 +684,47 @@ func Test_recordScheduleResultEventForClusterResourceBinding(t *testing.T) {
631
684
}
632
685
633
686
func Test_targetClustersToString (t * testing.T ) {
634
- tests := []struct {
635
- name string
636
- tcs []workv1alpha2.TargetCluster
637
- expectedOutput string
638
- }{
639
- {
640
- name : "empty slice" ,
641
- tcs : []workv1alpha2.TargetCluster {},
642
- expectedOutput : "" ,
643
- },
644
- {
645
- name : "single cluster" ,
646
- tcs : []workv1alpha2.TargetCluster {
647
- {Name : "cluster1" , Replicas : 1 },
648
- },
649
- expectedOutput : "cluster1:1" ,
650
- },
651
- {
652
- name : "multiple clusters" ,
653
- tcs : []workv1alpha2.TargetCluster {
654
- {Name : "cluster1" , Replicas : 1 },
655
- {Name : "cluster2" , Replicas : 2 },
656
- },
657
- expectedOutput : "cluster1:1, cluster2:2" ,
658
- },
659
- {
660
- name : "clusters with zero replicas" ,
661
- tcs : []workv1alpha2.TargetCluster {
662
- {Name : "cluster1" , Replicas : 0 },
663
- {Name : "cluster2" , Replicas : 2 },
664
- },
665
- expectedOutput : "cluster1:0, cluster2:2" ,
666
- },
667
- }
668
-
669
- for _ , test := range tests {
670
- t .Run (test .name , func (t * testing.T ) {
671
- result := targetClustersToString (test .tcs )
672
- if result != test .expectedOutput {
673
- t .Errorf ("expected %q, got %q" , test .expectedOutput , result )
674
- }
675
- })
676
- }
677
- }
687
+ tests := []struct {
688
+ name string
689
+ tcs []workv1alpha2.TargetCluster
690
+ expectedOutput string
691
+ }{
692
+ {
693
+ name : "empty slice" ,
694
+ tcs : []workv1alpha2.TargetCluster {},
695
+ expectedOutput : "" ,
696
+ },
697
+ {
698
+ name : "single cluster" ,
699
+ tcs : []workv1alpha2.TargetCluster {
700
+ {Name : "cluster1" , Replicas : 1 },
701
+ },
702
+ expectedOutput : "cluster1:1" ,
703
+ },
704
+ {
705
+ name : "multiple clusters" ,
706
+ tcs : []workv1alpha2.TargetCluster {
707
+ {Name : "cluster1" , Replicas : 1 },
708
+ {Name : "cluster2" , Replicas : 2 },
709
+ },
710
+ expectedOutput : "cluster1:1, cluster2:2" ,
711
+ },
712
+ {
713
+ name : "clusters with zero replicas" ,
714
+ tcs : []workv1alpha2.TargetCluster {
715
+ {Name : "cluster1" , Replicas : 0 },
716
+ {Name : "cluster2" , Replicas : 2 },
717
+ },
718
+ expectedOutput : "cluster1:0, cluster2:2" ,
719
+ },
720
+ }
721
+
722
+ for _ , test := range tests {
723
+ t .Run (test .name , func (t * testing.T ) {
724
+ result := targetClustersToString (test .tcs )
725
+ if result != test .expectedOutput {
726
+ t .Errorf ("expected %q, got %q" , test .expectedOutput , result )
727
+ }
728
+ })
729
+ }
730
+ }
0 commit comments