@@ -611,6 +611,196 @@ func TestFindOrphanWorks(t *testing.T) {
611
611
}
612
612
}
613
613
614
+ func TestFindWorksInClusters (t * testing.T ) {
615
+ type args struct {
616
+ c client.Client
617
+ bindingNamespace string
618
+ bindingName string
619
+ bindingID string
620
+ targetClusters sets.Set [string ]
621
+ }
622
+ tests := []struct {
623
+ name string
624
+ args args
625
+ want []workv1alpha1.Work
626
+ wantErr bool
627
+ }{
628
+ {
629
+ name : "namespace scope: no works in target clusters" ,
630
+ args : args {
631
+ c : fake .NewClientBuilder ().WithScheme (gclient .NewSchema ()).WithObjects (
632
+ & workv1alpha1.Work {
633
+ ObjectMeta : metav1.ObjectMeta {
634
+ Name : "work1" ,
635
+ Namespace : names .ExecutionSpacePrefix + "cluster1" ,
636
+ ResourceVersion : "999" ,
637
+ Labels : map [string ]string {
638
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
639
+ },
640
+ },
641
+ },
642
+ & workv1alpha1.Work {
643
+ ObjectMeta : metav1.ObjectMeta {
644
+ Name : "work2" ,
645
+ Namespace : names .ExecutionSpacePrefix + "cluster2" ,
646
+ ResourceVersion : "999" ,
647
+ Labels : map [string ]string {
648
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
649
+ },
650
+ },
651
+ },
652
+ ).WithIndex (
653
+ & workv1alpha1.Work {},
654
+ indexregistry .WorkIndexByLabelResourceBindingID ,
655
+ indexregistry .GenLabelIndexerFunc (workv1alpha2 .ResourceBindingPermanentIDLabel ),
656
+ ).Build (),
657
+ bindingNamespace : "default" ,
658
+ bindingName : "test-binding" ,
659
+ bindingID : "binding-id" ,
660
+ targetClusters : sets .New ("cluster3" ),
661
+ },
662
+ want : nil ,
663
+ wantErr : false ,
664
+ },
665
+ {
666
+ name : "namespace scope: some works in target clusters" ,
667
+ args : args {
668
+ c : fake .NewClientBuilder ().WithScheme (gclient .NewSchema ()).WithObjects (
669
+ & workv1alpha1.Work {
670
+ ObjectMeta : metav1.ObjectMeta {
671
+ Name : "work1" ,
672
+ Namespace : names .ExecutionSpacePrefix + "cluster1" ,
673
+ ResourceVersion : "999" ,
674
+ Labels : map [string ]string {
675
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
676
+ },
677
+ },
678
+ },
679
+ & workv1alpha1.Work {
680
+ ObjectMeta : metav1.ObjectMeta {
681
+ Name : "work2" ,
682
+ Namespace : names .ExecutionSpacePrefix + "cluster2" ,
683
+ ResourceVersion : "999" ,
684
+ Labels : map [string ]string {
685
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
686
+ },
687
+ },
688
+ },
689
+ ).WithIndex (
690
+ & workv1alpha1.Work {},
691
+ indexregistry .WorkIndexByLabelResourceBindingID ,
692
+ indexregistry .GenLabelIndexerFunc (workv1alpha2 .ResourceBindingPermanentIDLabel ),
693
+ ).Build (),
694
+ bindingNamespace : "default" ,
695
+ bindingName : "test-binding" ,
696
+ bindingID : "binding-id" ,
697
+ targetClusters : sets .New ("cluster1" ),
698
+ },
699
+ want : []workv1alpha1.Work {
700
+ {
701
+ ObjectMeta : metav1.ObjectMeta {
702
+ Name : "work1" ,
703
+ Namespace : names .ExecutionSpacePrefix + "cluster1" ,
704
+ ResourceVersion : "999" ,
705
+ Labels : map [string ]string {
706
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
707
+ },
708
+ },
709
+ },
710
+ },
711
+ wantErr : false ,
712
+ },
713
+ {
714
+ name : "namespace scope: error getting cluster name" ,
715
+ args : args {
716
+ c : fake .NewClientBuilder ().WithScheme (gclient .NewSchema ()).WithObjects (
717
+ & workv1alpha1.Work {
718
+ ObjectMeta : metav1.ObjectMeta {
719
+ Name : "work1" ,
720
+ Namespace : "invalid-namespace" ,
721
+ ResourceVersion : "999" ,
722
+ Labels : map [string ]string {
723
+ workv1alpha2 .ResourceBindingPermanentIDLabel : "binding-id" ,
724
+ },
725
+ },
726
+ },
727
+ ).WithIndex (
728
+ & workv1alpha1.Work {},
729
+ indexregistry .WorkIndexByLabelResourceBindingID ,
730
+ indexregistry .GenLabelIndexerFunc (workv1alpha2 .ResourceBindingPermanentIDLabel ),
731
+ ).Build (),
732
+ bindingNamespace : "default" ,
733
+ bindingName : "test-binding" ,
734
+ bindingID : "binding-id" ,
735
+ targetClusters : sets .New ("cluster1" ),
736
+ },
737
+ want : nil ,
738
+ wantErr : true ,
739
+ },
740
+ {
741
+ name : "cluster scope: some works in target clusters" ,
742
+ args : args {
743
+ c : fake .NewClientBuilder ().WithScheme (gclient .NewSchema ()).WithObjects (
744
+ & workv1alpha1.Work {
745
+ ObjectMeta : metav1.ObjectMeta {
746
+ Name : "work3" ,
747
+ Namespace : names .ExecutionSpacePrefix + "clusterA" ,
748
+ ResourceVersion : "999" ,
749
+ Labels : map [string ]string {
750
+ workv1alpha2 .ClusterResourceBindingPermanentIDLabel : "cluster-binding-id" ,
751
+ },
752
+ },
753
+ },
754
+ & workv1alpha1.Work {
755
+ ObjectMeta : metav1.ObjectMeta {
756
+ Name : "work4" ,
757
+ Namespace : names .ExecutionSpacePrefix + "clusterB" ,
758
+ ResourceVersion : "999" ,
759
+ Labels : map [string ]string {
760
+ workv1alpha2 .ClusterResourceBindingPermanentIDLabel : "cluster-binding-id" ,
761
+ },
762
+ },
763
+ },
764
+ ).WithIndex (
765
+ & workv1alpha1.Work {},
766
+ indexregistry .WorkIndexByLabelClusterResourceBindingID ,
767
+ indexregistry .GenLabelIndexerFunc (workv1alpha2 .ClusterResourceBindingPermanentIDLabel ),
768
+ ).Build (),
769
+ bindingNamespace : "" ,
770
+ bindingName : "cluster-binding" ,
771
+ bindingID : "cluster-binding-id" ,
772
+ targetClusters : sets .New ("clusterA" ),
773
+ },
774
+ want : []workv1alpha1.Work {
775
+ {
776
+ ObjectMeta : metav1.ObjectMeta {
777
+ Name : "work3" ,
778
+ Namespace : names .ExecutionSpacePrefix + "clusterA" ,
779
+ ResourceVersion : "999" ,
780
+ Labels : map [string ]string {
781
+ workv1alpha2 .ClusterResourceBindingPermanentIDLabel : "cluster-binding-id" ,
782
+ },
783
+ },
784
+ },
785
+ },
786
+ wantErr : false ,
787
+ },
788
+ }
789
+
790
+ for _ , tt := range tests {
791
+ t .Run (tt .name , func (t * testing.T ) {
792
+ got , err := FindWorksInClusters (context .TODO (), tt .args .c , tt .args .bindingNamespace , tt .args .bindingName , tt .args .bindingID , tt .args .targetClusters )
793
+ if (err != nil ) != tt .wantErr {
794
+ t .Errorf ("FindWorksInClusters() error = %v, wantErr %v" , err , tt .wantErr )
795
+ return
796
+ }
797
+ if ! reflect .DeepEqual (got , tt .want ) {
798
+ t .Errorf ("FindWorksInClusters() got = %v, want %v" , got , tt .want )
799
+ }
800
+ })
801
+ }
802
+ }
803
+
614
804
func TestRemoveOrphanWorks (t * testing.T ) {
615
805
makeWork := func (name string ) * workv1alpha1.Work {
616
806
return & workv1alpha1.Work {
@@ -1468,3 +1658,48 @@ func TestFindTargetStatusItemByCluster(t *testing.T) {
1468
1658
})
1469
1659
}
1470
1660
}
1661
+
1662
+ func TestObtainClustersWithPurgeModeDirectly (t * testing.T ) {
1663
+ tests := []struct {
1664
+ name string
1665
+ bindingSpec workv1alpha2.ResourceBindingSpec
1666
+ wantClusters sets.Set [string ]
1667
+ }{
1668
+ {
1669
+ name : "Multiple clusters with different PurgeModes" ,
1670
+ bindingSpec : workv1alpha2.ResourceBindingSpec {
1671
+ GracefulEvictionTasks : []workv1alpha2.GracefulEvictionTask {
1672
+ {
1673
+ FromCluster : "cluster1" ,
1674
+ PurgeMode : policyv1alpha1 .PurgeModeDirectly ,
1675
+ },
1676
+ {
1677
+ FromCluster : "cluster2" ,
1678
+ PurgeMode : policyv1alpha1 .PurgeModeDirectly ,
1679
+ },
1680
+ {
1681
+ FromCluster : "cluster3" ,
1682
+ PurgeMode : policyv1alpha1 .PurgeModeGracefully ,
1683
+ },
1684
+ },
1685
+ },
1686
+ wantClusters : sets .New ("cluster1" , "cluster2" ),
1687
+ },
1688
+ {
1689
+ name : "Empty GracefulEvictionTasks" ,
1690
+ bindingSpec : workv1alpha2.ResourceBindingSpec {
1691
+ GracefulEvictionTasks : []workv1alpha2.GracefulEvictionTask {},
1692
+ },
1693
+ wantClusters : sets .New [string ](),
1694
+ },
1695
+ }
1696
+
1697
+ for _ , tt := range tests {
1698
+ t .Run (tt .name , func (t * testing.T ) {
1699
+ gotClusters := ObtainClustersWithPurgeModeDirectly (tt .bindingSpec )
1700
+ if ! gotClusters .Equal (tt .wantClusters ) {
1701
+ t .Errorf ("ObtainClustersWithPurgeModeDirectly() = %v, want %v" , gotClusters .UnsortedList (), tt .wantClusters .UnsortedList ())
1702
+ }
1703
+ })
1704
+ }
1705
+ }
0 commit comments