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

repair the test for rb status controller #5324

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 10 additions & 8 deletions pkg/controllers/status/crb_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
"github.com/karmada-io/karmada/pkg/util/gclient"
)
Expand Down Expand Up @@ -98,8 +99,11 @@ func TestCRBStatusController_Reconcile(t *testing.T) {
name: "failed in syncBindingStatus",
binding: &workv1alpha2.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding",
Namespace: "default",
Name: "binding",
Namespace: "default",
// 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"},
chaosi-zju marked this conversation as resolved.
Show resolved Hide resolved
DeletionTimestamp: &preTime,
},
Spec: workv1alpha2.ResourceBindingSpec{
Expand All @@ -119,6 +123,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := generateCRBStatusController()
c.ResourceInterpreter = FakeResourceInterpreter{DefaultInterpreter: native.NewDefaultInterpreter()}

// Prepare req
req := controllerruntime.Request{
Expand All @@ -130,9 +135,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) {

// Prepare binding and create it in client
if tt.binding != nil {
if err := c.Client.Create(context.Background(), tt.binding); err != nil {
t.Fatalf("Failed to create binding: %v", err)
}
c.Client = fake.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(tt.binding).WithStatusSubresource(tt.binding).Build()
}

res, err := c.Reconcile(context.Background(), req)
Expand Down Expand Up @@ -192,6 +195,7 @@ func TestCRBStatusController_syncBindingStatus(t *testing.T) {
c := generateCRBStatusController()
c.DynamicClient = dynamicfake.NewSimpleDynamicClient(scheme.Scheme,
&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: tt.podNameInDynamicClient, Namespace: "default"}})
c.ResourceInterpreter = FakeResourceInterpreter{DefaultInterpreter: native.NewDefaultInterpreter()}

binding := &workv1alpha2.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -204,9 +208,7 @@ func TestCRBStatusController_syncBindingStatus(t *testing.T) {
}

if tt.resourceExistInClient {
if err := c.Client.Create(context.Background(), binding); err != nil {
t.Fatalf("Failed to create binding: %v", err)
}
c.Client = fake.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(binding).WithStatusSubresource(binding).Build()
}

err := c.syncBindingStatus(context.Background(), binding)
Expand Down
29 changes: 21 additions & 8 deletions pkg/controllers/status/rb_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/resourceinterpreter"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/default/native"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
"github.com/karmada-io/karmada/pkg/util/gclient"
)
Expand Down Expand Up @@ -98,8 +100,11 @@ func TestRBStatusController_Reconcile(t *testing.T) {
name: "failed in syncBindingStatus",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding",
Namespace: "default",
Name: "binding",
Namespace: "default",
// 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"},
DeletionTimestamp: &preTime,
},
Spec: workv1alpha2.ResourceBindingSpec{
Expand All @@ -119,6 +124,7 @@ func TestRBStatusController_Reconcile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := generateRBStatusController()
c.ResourceInterpreter = FakeResourceInterpreter{DefaultInterpreter: native.NewDefaultInterpreter()}

// Prepare req
req := controllerruntime.Request{
Expand All @@ -130,9 +136,7 @@ func TestRBStatusController_Reconcile(t *testing.T) {

// Prepare binding and create it in client
if tt.binding != nil {
if err := c.Client.Create(context.Background(), tt.binding); err != nil {
t.Fatalf("Failed to create binding: %v", err)
}
c.Client = fake.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(tt.binding).WithStatusSubresource(tt.binding).Build()
}

res, err := c.Reconcile(context.Background(), req)
Expand Down Expand Up @@ -192,6 +196,7 @@ func TestRBStatusController_syncBindingStatus(t *testing.T) {
c := generateRBStatusController()
c.DynamicClient = dynamicfake.NewSimpleDynamicClient(scheme.Scheme,
&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: tt.podNameInDynamicClient, Namespace: "default"}})
c.ResourceInterpreter = FakeResourceInterpreter{DefaultInterpreter: native.NewDefaultInterpreter()}

binding := &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -204,9 +209,7 @@ func TestRBStatusController_syncBindingStatus(t *testing.T) {
}

if tt.resourceExistInClient {
if err := c.Client.Create(context.Background(), binding); err != nil {
t.Fatalf("Failed to create binding: %v", err)
}
c.Client = fake.NewClientBuilder().WithScheme(gclient.NewSchema()).WithObjects(binding).WithStatusSubresource(binding).Build()
}

err := c.syncBindingStatus(context.Background(), binding)
Expand All @@ -219,3 +222,13 @@ func TestRBStatusController_syncBindingStatus(t *testing.T) {
})
}
}

var _ resourceinterpreter.ResourceInterpreter = &FakeResourceInterpreter{}

type FakeResourceInterpreter struct {
*native.DefaultInterpreter
}

func (f FakeResourceInterpreter) Start(_ context.Context) (err error) {
return nil
}
chaosi-zju marked this conversation as resolved.
Show resolved Hide resolved