Skip to content

Commit

Permalink
repair the test for rb status controller
Browse files Browse the repository at this point in the history
Signed-off-by: chaosi-zju <[email protected]>
  • Loading branch information
chaosi-zju committed Aug 8, 2024
1 parent 37dfa37 commit e4138c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
12 changes: 6 additions & 6 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 @@ -100,6 +101,7 @@ func TestCRBStatusController_Reconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "binding",
Namespace: "default",
Finalizers: []string{"test"},
DeletionTimestamp: &preTime,
},
Spec: workv1alpha2.ResourceBindingSpec{
Expand All @@ -119,6 +121,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 +133,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 +193,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 +206,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
23 changes: 17 additions & 6 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 @@ -100,6 +102,7 @@ func TestRBStatusController_Reconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "binding",
Namespace: "default",
Finalizers: []string{"test"},
DeletionTimestamp: &preTime,
},
Spec: workv1alpha2.ResourceBindingSpec{
Expand All @@ -119,6 +122,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 +134,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 +194,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 +207,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 +220,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
}

0 comments on commit e4138c9

Please sign in to comment.