From 0b5df8e176cec320c6b8236c7cdc279f6bc5a6b0 Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Tue, 3 Sep 2024 17:35:21 +0800 Subject: [PATCH] add plugin SDK interface and mock --- .mockery.yaml | 1 + controller/konnect/ops/ops.go | 6 +- .../konnect/ops/ops_kongpluginbinding.go | 13 +- controller/konnect/ops/plugin.go | 14 + controller/konnect/ops/plugin_mock_test.go | 264 ++++++++++++++++++ 5 files changed, 288 insertions(+), 10 deletions(-) create mode 100644 controller/konnect/ops/plugin.go create mode 100644 controller/konnect/ops/plugin_mock_test.go diff --git a/.mockery.yaml b/.mockery.yaml index 36d799f63..8b2c5408b 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -13,3 +13,4 @@ packages: interfaces: ControlPlaneSDK: ServicesSDK: + PluginSDK: diff --git a/controller/konnect/ops/ops.go b/controller/konnect/ops/ops.go index a5158a574..549261697 100644 --- a/controller/konnect/ops/ops.go +++ b/controller/konnect/ops/ops.go @@ -64,7 +64,7 @@ func Create[ case *configurationv1beta1.KongConsumerGroup: return e, createConsumerGroup(ctx, sdk.ConsumerGroups, ent) case *configurationv1alpha1.KongPluginBinding: - return e, createPlugin(ctx, cl, sdk, ent) + return e, createPlugin(ctx, cl, sdk.Plugins, ent) // --------------------------------------------------------------------- // TODO: add other Konnect types @@ -102,7 +102,7 @@ func Delete[ case *configurationv1beta1.KongConsumerGroup: return deleteConsumerGroup(ctx, sdk.ConsumerGroups, ent) case *configurationv1alpha1.KongPluginBinding: - return deletePlugin(ctx, sdk, ent) + return deletePlugin(ctx, sdk.Plugins, ent) // --------------------------------------------------------------------- // TODO: add other Konnect types @@ -165,7 +165,7 @@ func Update[ case *configurationv1beta1.KongConsumerGroup: return ctrl.Result{}, updateConsumerGroup(ctx, sdk.ConsumerGroups, cl, ent) case *configurationv1alpha1.KongPluginBinding: - return ctrl.Result{}, updatePlugin(ctx, sdk, cl, ent) + return ctrl.Result{}, updatePlugin(ctx, sdk.Plugins, cl, ent) // --------------------------------------------------------------------- // TODO: add other Konnect types diff --git a/controller/konnect/ops/ops_kongpluginbinding.go b/controller/konnect/ops/ops_kongpluginbinding.go index d04c6faab..0fc814897 100644 --- a/controller/konnect/ops/ops_kongpluginbinding.go +++ b/controller/konnect/ops/ops_kongpluginbinding.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" - sdkkonnectgo "github.com/Kong/sdk-konnect-go" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors" @@ -32,7 +31,7 @@ import ( func createPlugin( ctx context.Context, cl client.Client, - sdk *sdkkonnectgo.SDK, + sdk PluginSDK, pluginBinding *configurationv1alpha1.KongPluginBinding, ) error { controlPlaneID := pluginBinding.GetControlPlaneID() @@ -44,7 +43,7 @@ func createPlugin( return err } - resp, err := sdk.Plugins.CreatePlugin(ctx, + resp, err := sdk.CreatePlugin(ctx, controlPlaneID, *pluginInput, ) @@ -87,7 +86,7 @@ func createPlugin( // if the operation fails. func updatePlugin( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk PluginSDK, cl client.Client, pb *configurationv1alpha1.KongPluginBinding, ) error { @@ -121,7 +120,7 @@ func updatePlugin( return err } - resp, err := sdk.Plugins.UpsertPlugin(ctx, + resp, err := sdk.UpsertPlugin(ctx, sdkkonnectops.UpsertPluginRequest{ ControlPlaneID: controlPlaneID, PluginID: pb.GetKonnectID(), @@ -167,11 +166,11 @@ func updatePlugin( // It returns an error if the operation fails. func deletePlugin( ctx context.Context, - sdk *sdkkonnectgo.SDK, + sdk PluginSDK, pb *configurationv1alpha1.KongPluginBinding, ) error { id := pb.GetKonnectID() - _, err := sdk.Plugins.DeletePlugin(ctx, pb.GetControlPlaneID(), id) + _, err := sdk.DeletePlugin(ctx, pb.GetControlPlaneID(), id) if errWrapped := wrapErrIfKonnectOpFailed[configurationv1alpha1.KongPluginBinding](err, DeleteOp, pb); errWrapped != nil { // plugin delete operation returns an SDKError instead of a NotFoundError. var sdkError *sdkkonnecterrs.SDKError diff --git a/controller/konnect/ops/plugin.go b/controller/konnect/ops/plugin.go new file mode 100644 index 000000000..c6834948e --- /dev/null +++ b/controller/konnect/ops/plugin.go @@ -0,0 +1,14 @@ +package ops + +import ( + "context" + + sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" + sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" +) + +type PluginSDK interface { + CreatePlugin(ctx context.Context, controlPlaneID string, plugin sdkkonnectcomp.PluginInput, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreatePluginResponse, error) + UpsertPlugin(ctx context.Context, request sdkkonnectops.UpsertPluginRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertPluginResponse, error) + DeletePlugin(ctx context.Context, controlPlaneID string, pluginID string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeletePluginResponse, error) +} diff --git a/controller/konnect/ops/plugin_mock_test.go b/controller/konnect/ops/plugin_mock_test.go new file mode 100644 index 000000000..08262cd87 --- /dev/null +++ b/controller/konnect/ops/plugin_mock_test.go @@ -0,0 +1,264 @@ +// Code generated by mockery. DO NOT EDIT. + +package ops + +import ( + context "context" + + components "github.com/Kong/sdk-konnect-go/models/components" + + mock "github.com/stretchr/testify/mock" + + operations "github.com/Kong/sdk-konnect-go/models/operations" +) + +// MockPluginSDK is an autogenerated mock type for the PluginSDK type +type MockPluginSDK struct { + mock.Mock +} + +type MockPluginSDK_Expecter struct { + mock *mock.Mock +} + +func (_m *MockPluginSDK) EXPECT() *MockPluginSDK_Expecter { + return &MockPluginSDK_Expecter{mock: &_m.Mock} +} + +// CreatePlugin provides a mock function with given fields: ctx, controlPlaneID, plugin, opts +func (_m *MockPluginSDK) CreatePlugin(ctx context.Context, controlPlaneID string, plugin components.PluginInput, opts ...operations.Option) (*operations.CreatePluginResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, controlPlaneID, plugin) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for CreatePlugin") + } + + var r0 *operations.CreatePluginResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, components.PluginInput, ...operations.Option) (*operations.CreatePluginResponse, error)); ok { + return rf(ctx, controlPlaneID, plugin, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, string, components.PluginInput, ...operations.Option) *operations.CreatePluginResponse); ok { + r0 = rf(ctx, controlPlaneID, plugin, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*operations.CreatePluginResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, components.PluginInput, ...operations.Option) error); ok { + r1 = rf(ctx, controlPlaneID, plugin, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPluginSDK_CreatePlugin_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreatePlugin' +type MockPluginSDK_CreatePlugin_Call struct { + *mock.Call +} + +// CreatePlugin is a helper method to define mock.On call +// - ctx context.Context +// - controlPlaneID string +// - plugin components.PluginInput +// - opts ...operations.Option +func (_e *MockPluginSDK_Expecter) CreatePlugin(ctx interface{}, controlPlaneID interface{}, plugin interface{}, opts ...interface{}) *MockPluginSDK_CreatePlugin_Call { + return &MockPluginSDK_CreatePlugin_Call{Call: _e.mock.On("CreatePlugin", + append([]interface{}{ctx, controlPlaneID, plugin}, opts...)...)} +} + +func (_c *MockPluginSDK_CreatePlugin_Call) Run(run func(ctx context.Context, controlPlaneID string, plugin components.PluginInput, opts ...operations.Option)) *MockPluginSDK_CreatePlugin_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]operations.Option, len(args)-3) + for i, a := range args[3:] { + if a != nil { + variadicArgs[i] = a.(operations.Option) + } + } + run(args[0].(context.Context), args[1].(string), args[2].(components.PluginInput), variadicArgs...) + }) + return _c +} + +func (_c *MockPluginSDK_CreatePlugin_Call) Return(_a0 *operations.CreatePluginResponse, _a1 error) *MockPluginSDK_CreatePlugin_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPluginSDK_CreatePlugin_Call) RunAndReturn(run func(context.Context, string, components.PluginInput, ...operations.Option) (*operations.CreatePluginResponse, error)) *MockPluginSDK_CreatePlugin_Call { + _c.Call.Return(run) + return _c +} + +// DeletePlugin provides a mock function with given fields: ctx, controlPlaneID, pluginID, opts +func (_m *MockPluginSDK) DeletePlugin(ctx context.Context, controlPlaneID string, pluginID string, opts ...operations.Option) (*operations.DeletePluginResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, controlPlaneID, pluginID) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for DeletePlugin") + } + + var r0 *operations.DeletePluginResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, ...operations.Option) (*operations.DeletePluginResponse, error)); ok { + return rf(ctx, controlPlaneID, pluginID, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, ...operations.Option) *operations.DeletePluginResponse); ok { + r0 = rf(ctx, controlPlaneID, pluginID, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*operations.DeletePluginResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, ...operations.Option) error); ok { + r1 = rf(ctx, controlPlaneID, pluginID, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPluginSDK_DeletePlugin_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeletePlugin' +type MockPluginSDK_DeletePlugin_Call struct { + *mock.Call +} + +// DeletePlugin is a helper method to define mock.On call +// - ctx context.Context +// - controlPlaneID string +// - pluginID string +// - opts ...operations.Option +func (_e *MockPluginSDK_Expecter) DeletePlugin(ctx interface{}, controlPlaneID interface{}, pluginID interface{}, opts ...interface{}) *MockPluginSDK_DeletePlugin_Call { + return &MockPluginSDK_DeletePlugin_Call{Call: _e.mock.On("DeletePlugin", + append([]interface{}{ctx, controlPlaneID, pluginID}, opts...)...)} +} + +func (_c *MockPluginSDK_DeletePlugin_Call) Run(run func(ctx context.Context, controlPlaneID string, pluginID string, opts ...operations.Option)) *MockPluginSDK_DeletePlugin_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]operations.Option, len(args)-3) + for i, a := range args[3:] { + if a != nil { + variadicArgs[i] = a.(operations.Option) + } + } + run(args[0].(context.Context), args[1].(string), args[2].(string), variadicArgs...) + }) + return _c +} + +func (_c *MockPluginSDK_DeletePlugin_Call) Return(_a0 *operations.DeletePluginResponse, _a1 error) *MockPluginSDK_DeletePlugin_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPluginSDK_DeletePlugin_Call) RunAndReturn(run func(context.Context, string, string, ...operations.Option) (*operations.DeletePluginResponse, error)) *MockPluginSDK_DeletePlugin_Call { + _c.Call.Return(run) + return _c +} + +// UpsertPlugin provides a mock function with given fields: ctx, request, opts +func (_m *MockPluginSDK) UpsertPlugin(ctx context.Context, request operations.UpsertPluginRequest, opts ...operations.Option) (*operations.UpsertPluginResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, request) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for UpsertPlugin") + } + + var r0 *operations.UpsertPluginResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, operations.UpsertPluginRequest, ...operations.Option) (*operations.UpsertPluginResponse, error)); ok { + return rf(ctx, request, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, operations.UpsertPluginRequest, ...operations.Option) *operations.UpsertPluginResponse); ok { + r0 = rf(ctx, request, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*operations.UpsertPluginResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, operations.UpsertPluginRequest, ...operations.Option) error); ok { + r1 = rf(ctx, request, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPluginSDK_UpsertPlugin_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpsertPlugin' +type MockPluginSDK_UpsertPlugin_Call struct { + *mock.Call +} + +// UpsertPlugin is a helper method to define mock.On call +// - ctx context.Context +// - request operations.UpsertPluginRequest +// - opts ...operations.Option +func (_e *MockPluginSDK_Expecter) UpsertPlugin(ctx interface{}, request interface{}, opts ...interface{}) *MockPluginSDK_UpsertPlugin_Call { + return &MockPluginSDK_UpsertPlugin_Call{Call: _e.mock.On("UpsertPlugin", + append([]interface{}{ctx, request}, opts...)...)} +} + +func (_c *MockPluginSDK_UpsertPlugin_Call) Run(run func(ctx context.Context, request operations.UpsertPluginRequest, opts ...operations.Option)) *MockPluginSDK_UpsertPlugin_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]operations.Option, len(args)-2) + for i, a := range args[2:] { + if a != nil { + variadicArgs[i] = a.(operations.Option) + } + } + run(args[0].(context.Context), args[1].(operations.UpsertPluginRequest), variadicArgs...) + }) + return _c +} + +func (_c *MockPluginSDK_UpsertPlugin_Call) Return(_a0 *operations.UpsertPluginResponse, _a1 error) *MockPluginSDK_UpsertPlugin_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPluginSDK_UpsertPlugin_Call) RunAndReturn(run func(context.Context, operations.UpsertPluginRequest, ...operations.Option) (*operations.UpsertPluginResponse, error)) *MockPluginSDK_UpsertPlugin_Call { + _c.Call.Return(run) + return _c +} + +// NewMockPluginSDK creates a new instance of MockPluginSDK. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockPluginSDK(t interface { + mock.TestingT + Cleanup(func()) +}) *MockPluginSDK { + mock := &MockPluginSDK{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +}