Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crb_status_controller ut is referencing namespaced objects #5780

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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