Skip to content

Commit

Permalink
Merge pull request #5780 from ctripcloud/crb-status-ut
Browse files Browse the repository at this point in the history
fix crb_status_controller ut is referencing namespaced objects
  • Loading branch information
karmada-bot authored Nov 4, 2024
2 parents 6f2f3eb + 0e64e7e commit e90ff63
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions pkg/controllers/status/crb_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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{},
Expand All @@ -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",
},
},
},
Expand All @@ -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"},
Expand All @@ -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",
},
},
},
Expand All @@ -128,8 +124,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) {
// Prepare req
req := controllerruntime.Request{
NamespacedName: types.NamespacedName{
Name: "binding",
Namespace: "default",
Name: "binding",
},
}

Expand All @@ -151,56 +146,53 @@ 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,
},
}

for _, tt := range tests {
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,
Expand Down

0 comments on commit e90ff63

Please sign in to comment.