@@ -1493,56 +1493,6 @@ func TestGatherSelectedResource(t *testing.T) {
14931493 // Should select only the namespace, not its child resources
14941494 want : []* unstructured.Unstructured {testNamespace },
14951495 },
1496- {
1497- name : "should error when selecting multiple namespaces with NamespaceWithResourceSelectors mode using label selector" ,
1498- placementName : types.NamespacedName {Name : "test-placement" },
1499- selectors : []fleetv1beta1.ResourceSelectorTerm {
1500- {
1501- Group : "" ,
1502- Version : "v1" ,
1503- Kind : "Namespace" ,
1504- LabelSelector : & metav1.LabelSelector {
1505- MatchLabels : map [string ]string {
1506- "environment" : "test" ,
1507- },
1508- },
1509- SelectionScope : fleetv1beta1 .NamespaceWithResourceSelectors ,
1510- },
1511- {
1512- Group : "apps" ,
1513- Version : "v1" ,
1514- Kind : "Deployment" ,
1515- Name : "test-deployment" ,
1516- },
1517- },
1518- resourceConfig : utils .NewResourceConfig (false ), // default deny list
1519- informerManager : func () * testinformer.FakeManager {
1520- // Create a second test namespace with the same label
1521- testNamespace2 := & unstructured.Unstructured {
1522- Object : map [string ]interface {}{
1523- "apiVersion" : "v1" ,
1524- "kind" : "Namespace" ,
1525- "metadata" : map [string ]interface {}{
1526- "name" : "test-ns-2" ,
1527- "labels" : map [string ]interface {}{
1528- "environment" : "test" ,
1529- },
1530- },
1531- },
1532- }
1533- testNamespace2 .SetGroupVersionKind (utils .NamespaceGVK )
1534-
1535- return & testinformer.FakeManager {
1536- Listers : map [schema.GroupVersionResource ]* testinformer.FakeLister {
1537- utils .NamespaceGVR : {Objects : []runtime.Object {testNamespace , testNamespace2 }},
1538- utils .DeploymentGVR : {Objects : []runtime.Object {testDeployment }},
1539- },
1540- NamespaceScopedResources : []schema.GroupVersionResource {utils .DeploymentGVR },
1541- }
1542- }(),
1543- // Should error because multiple namespaces match the label selector
1544- wantError : ErrUserError ,
1545- },
15461496 {
15471497 name : "should error when selecting zero namespaces with NamespaceWithResourceSelectors mode" ,
15481498 placementName : types.NamespacedName {Name : "test-placement" },
@@ -2201,7 +2151,7 @@ func TestSortResources(t *testing.T) {
22012151 }
22022152}
22032153
2204- func TestFetchSelectedNamespaces (t * testing.T ) {
2154+ func TestFetchSelectedNamespace (t * testing.T ) {
22052155 testNs1 := & unstructured.Unstructured {
22062156 Object : map [string ]interface {}{
22072157 "apiVersion" : "v1" ,
@@ -2246,7 +2196,8 @@ func TestFetchSelectedNamespaces(t *testing.T) {
22462196 selector fleetv1beta1.ResourceSelectorTerm
22472197 skippedNamespaces map [string ]bool
22482198 informerManager * testinformer.FakeManager
2249- want []string
2199+ want string
2200+ wantFound bool
22502201 wantErr bool
22512202 }{
22522203 {
@@ -2263,43 +2214,25 @@ func TestFetchSelectedNamespaces(t *testing.T) {
22632214 utils .NamespaceGVR : {Objects : []runtime.Object {testNs1 , testNs2 }},
22642215 },
22652216 },
2266- want : []string {"test-ns-1" },
2267- },
2268- {
2269- name : "select namespaces by label selector" ,
2270- selector : fleetv1beta1.ResourceSelectorTerm {
2271- Group : "" ,
2272- Version : "v1" ,
2273- Kind : "Namespace" ,
2274- LabelSelector : & metav1.LabelSelector {
2275- MatchLabels : map [string ]string {"env" : "prod" },
2276- },
2277- },
2278- skippedNamespaces : nil ,
2279- informerManager : & testinformer.FakeManager {
2280- Listers : map [schema.GroupVersionResource ]* testinformer.FakeLister {
2281- utils .NamespaceGVR : {Objects : []runtime.Object {testNs1 , testNs2 }},
2282- },
2283- },
2284- want : []string {"test-ns-1" },
2217+ want : "test-ns-1" ,
2218+ wantFound : true ,
22852219 },
22862220 {
22872221 name : "filter out skipped namespaces" ,
22882222 selector : fleetv1beta1.ResourceSelectorTerm {
22892223 Group : "" ,
22902224 Version : "v1" ,
22912225 Kind : "Namespace" ,
2292- LabelSelector : & metav1.LabelSelector {
2293- MatchLabels : map [string ]string {},
2294- },
2226+ Name : "kube-system" , // Use name-based selection instead of label selector
22952227 },
22962228 skippedNamespaces : map [string ]bool {"kube-system" : true },
22972229 informerManager : & testinformer.FakeManager {
22982230 Listers : map [schema.GroupVersionResource ]* testinformer.FakeLister {
2299- utils .NamespaceGVR : {Objects : []runtime.Object {testNs1 , skippedNs }},
2231+ utils .NamespaceGVR : {Objects : []runtime.Object {testNs1 , testNs2 , skippedNs }},
23002232 },
23012233 },
2302- want : []string {"test-ns-1" },
2234+ want : "" , // Empty because kube-system is skipped
2235+ wantFound : false ,
23032236 },
23042237 {
23052238 name : "no namespaces match selector" ,
@@ -2315,7 +2248,8 @@ func TestFetchSelectedNamespaces(t *testing.T) {
23152248 utils .NamespaceGVR : {Objects : []runtime.Object {testNs1 , testNs2 }},
23162249 },
23172250 },
2318- want : []string {},
2251+ want : "" ,
2252+ wantFound : false ,
23192253 },
23202254 }
23212255
@@ -2328,13 +2262,16 @@ func TestFetchSelectedNamespaces(t *testing.T) {
23282262 RestMapper : newFakeRESTMapper (),
23292263 }
23302264
2331- got , err := rsr .fetchSelectedNamespaces (tt .selector , "test-placement" )
2265+ gotNamespace , gotFound , err := rsr .fetchSelectedNamespace (tt .selector , "test-placement" )
23322266 if (err != nil ) != tt .wantErr {
2333- t .Errorf ("fetchSelectedNamespaces () error = %v, wantErr %v" , err , tt .wantErr )
2267+ t .Errorf ("fetchSelectedNamespace () error = %v, wantErr %v" , err , tt .wantErr )
23342268 return
23352269 }
2336- if diff := cmp .Diff (tt .want , got ); diff != "" {
2337- t .Errorf ("fetchSelectedNamespaces() mismatch (-want +got):\n %s" , diff )
2270+ if gotNamespace != tt .want {
2271+ t .Errorf ("fetchSelectedNamespace() namespace = %v, want %v" , gotNamespace , tt .want )
2272+ }
2273+ if gotFound != tt .wantFound {
2274+ t .Errorf ("fetchSelectedNamespace() found = %v, wantFound %v" , gotFound , tt .wantFound )
23382275 }
23392276 })
23402277 }
0 commit comments