Skip to content

Commit

Permalink
Fix unit tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlaltluc committed Jun 9, 2023
1 parent 542ed0d commit 8f2fce0
Show file tree
Hide file tree
Showing 6 changed files with 545 additions and 53 deletions.
18 changes: 11 additions & 7 deletions controllers/cruisecontroltask_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/banzaicloud/koperator/pkg/scale"

Expand Down Expand Up @@ -370,8 +371,11 @@ func TestCreateCCOperation(t *testing.T) {
},
}

mockCtrl := gomock.NewController(t)

for _, testCase := range testCases {
mockClient := new(mocks.Client)
mockClient := mocks.NewMockClient(mockCtrl)
mockSubResourceClient := mocks.NewMockSubResourceClient(mockCtrl)
scheme := runtime.NewScheme()
_ = v1beta1.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
Expand All @@ -389,17 +393,17 @@ func TestCreateCCOperation(t *testing.T) {

// Mock the Create call and capture the operation
var createdOperation *banzaiv1alpha1.CruiseControlOperation
mockClient.On("Create", ctx, mock.IsType(&banzaiv1alpha1.CruiseControlOperation{})).Run(func(args mock.Arguments) {
createdOperation = args.Get(1).(*banzaiv1alpha1.CruiseControlOperation)
mockClient.EXPECT().Create(ctx, gomock.AssignableToTypeOf(&banzaiv1alpha1.CruiseControlOperation{})).Do(func(ctx context.Context, obj client.Object, opts ...client.CreateOption) {
createdOperation = obj.(*banzaiv1alpha1.CruiseControlOperation)
createdOperation.ObjectMeta.Name = "generated-name"
}).Return(nil)

// Mock the Status call
mockClient.On("Status").Return(mockClient)
mockClient.EXPECT().Status().Return(mockSubResourceClient)

// Mock the Update call
mockClient.On("Update", ctx, mock.IsType(&banzaiv1alpha1.CruiseControlOperation{})).Run(func(args mock.Arguments) {
arg := args.Get(1).(*banzaiv1alpha1.CruiseControlOperation)
mockSubResourceClient.EXPECT().Update(ctx, gomock.AssignableToTypeOf(&banzaiv1alpha1.CruiseControlOperation{})).Do(func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) {
arg := obj.(*banzaiv1alpha1.CruiseControlOperation)
createdOperation.Status = arg.Status
}).Return(nil)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/tools v0.7.0 // indirect
)

Expand Down
Loading

0 comments on commit 8f2fce0

Please sign in to comment.