From 0e64e7ed051cef13a82745871848c1c1a7a92d95 Mon Sep 17 00:00:00 2001 From: zach593 Date: Sun, 3 Nov 2024 17:34:28 +0800 Subject: [PATCH] fix crb_status_controller ut is referencing namespaced objects Signed-off-by: zach593 --- .../status/crb_status_controller_test.go | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/pkg/controllers/status/crb_status_controller_test.go b/pkg/controllers/status/crb_status_controller_test.go index 9c44efa45364..1bf0b33dfa4e 100644 --- a/pkg/controllers/status/crb_status_controller_test.go +++ b/pkg/controllers/status/crb_status_controller_test.go @@ -43,9 +43,9 @@ func generateCRBStatusController() *CRBStatusController { stopCh := make(chan struct{}) defer close(stopCh) dynamicClient := dynamicfake.NewSimpleDynamicClient(scheme.Scheme, - &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}}) + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns1"}}) m := genericmanager.NewSingleClusterInformerManager(dynamicClient, 0, stopCh) - m.Lister(corev1.SchemeGroupVersion.WithResource("pods")) + m.Lister(corev1.SchemeGroupVersion.WithResource("namespaces")) m.Start() m.WaitForCacheSync() @@ -55,7 +55,7 @@ func generateCRBStatusController() *CRBStatusController { InformerManager: m, RESTMapper: func() meta.RESTMapper { m := meta.NewDefaultRESTMapper([]schema.GroupVersion{corev1.SchemeGroupVersion}) - m.Add(corev1.SchemeGroupVersion.WithKind("Pod"), meta.RESTScopeNamespace) + m.Add(corev1.SchemeGroupVersion.WithKind("Namespace"), meta.RESTScopeNamespace) return m }(), EventRecorder: &record.FakeRecorder{}, @@ -75,15 +75,13 @@ func TestCRBStatusController_Reconcile(t *testing.T) { name: "failed in syncBindingStatus", binding: &workv1alpha2.ClusterResourceBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: "binding", - Namespace: "default", + Name: "binding", }, Spec: workv1alpha2.ResourceBindingSpec{ Resource: workv1alpha2.ObjectReference{ APIVersion: "v1", - Kind: "Pod", - Namespace: "default", - Name: "pod", + Kind: "Namespace", + Name: "ns", }, }, }, @@ -99,8 +97,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) { name: "failed in syncBindingStatus", binding: &workv1alpha2.ClusterResourceBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: "binding", - Namespace: "default", + Name: "binding", // finalizers field is required when deletionTimestamp is defined, otherwise will encounter the // error: `refusing to create obj binding with metadata.deletionTimestamp but no finalizers`. Finalizers: []string{"test"}, @@ -109,9 +106,8 @@ func TestCRBStatusController_Reconcile(t *testing.T) { Spec: workv1alpha2.ResourceBindingSpec{ Resource: workv1alpha2.ObjectReference{ APIVersion: "v1", - Kind: "Pod", - Namespace: "default", - Name: "pod", + Kind: "Namespace", + Name: "ns", }, }, }, @@ -128,8 +124,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) { // Prepare req req := controllerruntime.Request{ NamespacedName: types.NamespacedName{ - Name: "binding", - Namespace: "default", + Name: "binding", }, } @@ -151,42 +146,40 @@ func TestCRBStatusController_Reconcile(t *testing.T) { func TestCRBStatusController_syncBindingStatus(t *testing.T) { tests := []struct { - name string - resource workv1alpha2.ObjectReference - podNameInDynamicClient string - resourceExistInClient bool - expectedError bool + name string + resource workv1alpha2.ObjectReference + nsNameInDynamicClient string + resourceExistInClient bool + expectedError bool }{ { name: "failed in FetchResourceTemplate, err is NotFound", resource: workv1alpha2.ObjectReference{ APIVersion: "v1", - Kind: "Pod", - Namespace: "default", - Name: "pod", + Kind: "Namespace", + Name: "ns", }, - podNameInDynamicClient: "pod1", - resourceExistInClient: true, - expectedError: false, + nsNameInDynamicClient: "ns1", + resourceExistInClient: true, + expectedError: false, }, { - name: "failed in FetchResourceTemplate, err is not NotFound", - resource: workv1alpha2.ObjectReference{}, - podNameInDynamicClient: "pod", - resourceExistInClient: true, - expectedError: true, + name: "failed in FetchResourceTemplate, err is not NotFound", + resource: workv1alpha2.ObjectReference{}, + nsNameInDynamicClient: "ns", + resourceExistInClient: true, + expectedError: true, }, { name: "failed in AggregateClusterResourceBindingWorkStatus", resource: workv1alpha2.ObjectReference{ APIVersion: "v1", - Kind: "Pod", - Namespace: "default", - Name: "pod", + Kind: "Namespace", + Name: "ns", }, - podNameInDynamicClient: "pod", - resourceExistInClient: false, - expectedError: true, + nsNameInDynamicClient: "ns", + resourceExistInClient: false, + expectedError: true, }, } @@ -194,13 +187,12 @@ func TestCRBStatusController_syncBindingStatus(t *testing.T) { t.Run(tt.name, func(t *testing.T) { c := generateCRBStatusController() c.DynamicClient = dynamicfake.NewSimpleDynamicClient(scheme.Scheme, - &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: tt.podNameInDynamicClient, Namespace: "default"}}) + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tt.nsNameInDynamicClient}}) c.ResourceInterpreter = FakeResourceInterpreter{DefaultInterpreter: native.NewDefaultInterpreter()} binding := &workv1alpha2.ClusterResourceBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: "binding", - Namespace: "default", + Name: "binding", }, Spec: workv1alpha2.ResourceBindingSpec{ Resource: tt.resource,