From e2d5ba1a2b8f92c1a043a4b3208cbcdfa118755b Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Tue, 18 Apr 2023 10:59:49 +0300 Subject: [PATCH] pkg/podres: update mocked PodResourcesListerClient Update mocked implementation of k8s.io/kubelet/pkg/apis/podresources/v1.PodResourcesListerClient. The mocked implementation is moved to a separate "mocks" subpackage as it's for an external interface. This patch also adds code for auto-generation for the mocked interface. --- pkg/podres/client.go | 2 + pkg/podres/mock_PodResourcesListerClient.go | 78 ----------- pkg/podres/mocks/PodResourcesListerClient.go | 132 ++++++++++++++++++ .../podresourcesscanner_test.go | 6 +- 4 files changed, 137 insertions(+), 81 deletions(-) delete mode 100644 pkg/podres/mock_PodResourcesListerClient.go create mode 100644 pkg/podres/mocks/PodResourcesListerClient.go diff --git a/pkg/podres/client.go b/pkg/podres/client.go index dec0b87024..7d08191828 100644 --- a/pkg/podres/client.go +++ b/pkg/podres/client.go @@ -16,6 +16,8 @@ limitations under the License. package podres +//go:generate mockery --srcpkg=k8s.io/kubelet/pkg/apis/podresources/v1 --name PodResourcesListerClient + import ( "fmt" "log" diff --git a/pkg/podres/mock_PodResourcesListerClient.go b/pkg/podres/mock_PodResourcesListerClient.go deleted file mode 100644 index fe694ed511..0000000000 --- a/pkg/podres/mock_PodResourcesListerClient.go +++ /dev/null @@ -1,78 +0,0 @@ -// Code generated by mockery v2.4.0-beta. DO NOT EDIT. - -package podres - -import ( - context "context" - - grpc "google.golang.org/grpc" - - mock "github.com/stretchr/testify/mock" - - v1 "k8s.io/kubelet/pkg/apis/podresources/v1" -) - -// MockPodResourcesListerClient is an autogenerated mock type for the PodResourcesListerClient type -type MockPodResourcesListerClient struct { - mock.Mock -} - -// GetAllocatableResources provides a mock function with given fields: ctx, in, opts -func (_m *MockPodResourcesListerClient) GetAllocatableResources(ctx context.Context, in *v1.AllocatableResourcesRequest, opts ...grpc.CallOption) (*v1.AllocatableResourcesResponse, error) { - _va := make([]interface{}, len(opts)) - for _i := range opts { - _va[_i] = opts[_i] - } - var _ca []interface{} - _ca = append(_ca, ctx, in) - _ca = append(_ca, _va...) - ret := _m.Called(_ca...) - - var r0 *v1.AllocatableResourcesResponse - if rf, ok := ret.Get(0).(func(context.Context, *v1.AllocatableResourcesRequest, ...grpc.CallOption) *v1.AllocatableResourcesResponse); ok { - r0 = rf(ctx, in, opts...) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*v1.AllocatableResourcesResponse) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(context.Context, *v1.AllocatableResourcesRequest, ...grpc.CallOption) error); ok { - r1 = rf(ctx, in, opts...) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// List provides a mock function with given fields: ctx, in, opts -func (_m *MockPodResourcesListerClient) List(ctx context.Context, in *v1.ListPodResourcesRequest, opts ...grpc.CallOption) (*v1.ListPodResourcesResponse, error) { - _va := make([]interface{}, len(opts)) - for _i := range opts { - _va[_i] = opts[_i] - } - var _ca []interface{} - _ca = append(_ca, ctx, in) - _ca = append(_ca, _va...) - ret := _m.Called(_ca...) - - var r0 *v1.ListPodResourcesResponse - if rf, ok := ret.Get(0).(func(context.Context, *v1.ListPodResourcesRequest, ...grpc.CallOption) *v1.ListPodResourcesResponse); ok { - r0 = rf(ctx, in, opts...) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*v1.ListPodResourcesResponse) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(context.Context, *v1.ListPodResourcesRequest, ...grpc.CallOption) error); ok { - r1 = rf(ctx, in, opts...) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} diff --git a/pkg/podres/mocks/PodResourcesListerClient.go b/pkg/podres/mocks/PodResourcesListerClient.go new file mode 100644 index 0000000000..2180db1c40 --- /dev/null +++ b/pkg/podres/mocks/PodResourcesListerClient.go @@ -0,0 +1,132 @@ +// Code generated by mockery v2.20.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + + grpc "google.golang.org/grpc" + + mock "github.com/stretchr/testify/mock" + + v1 "k8s.io/kubelet/pkg/apis/podresources/v1" +) + +// PodResourcesListerClient is an autogenerated mock type for the PodResourcesListerClient type +type PodResourcesListerClient struct { + mock.Mock +} + +// Get provides a mock function with given fields: ctx, in, opts +func (_m *PodResourcesListerClient) Get(ctx context.Context, in *v1.GetPodResourcesRequest, opts ...grpc.CallOption) (*v1.GetPodResourcesResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, in) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 *v1.GetPodResourcesResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *v1.GetPodResourcesRequest, ...grpc.CallOption) (*v1.GetPodResourcesResponse, error)); ok { + return rf(ctx, in, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, *v1.GetPodResourcesRequest, ...grpc.CallOption) *v1.GetPodResourcesResponse); ok { + r0 = rf(ctx, in, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*v1.GetPodResourcesResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *v1.GetPodResourcesRequest, ...grpc.CallOption) error); ok { + r1 = rf(ctx, in, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetAllocatableResources provides a mock function with given fields: ctx, in, opts +func (_m *PodResourcesListerClient) GetAllocatableResources(ctx context.Context, in *v1.AllocatableResourcesRequest, opts ...grpc.CallOption) (*v1.AllocatableResourcesResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, in) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 *v1.AllocatableResourcesResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *v1.AllocatableResourcesRequest, ...grpc.CallOption) (*v1.AllocatableResourcesResponse, error)); ok { + return rf(ctx, in, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, *v1.AllocatableResourcesRequest, ...grpc.CallOption) *v1.AllocatableResourcesResponse); ok { + r0 = rf(ctx, in, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*v1.AllocatableResourcesResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *v1.AllocatableResourcesRequest, ...grpc.CallOption) error); ok { + r1 = rf(ctx, in, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// List provides a mock function with given fields: ctx, in, opts +func (_m *PodResourcesListerClient) List(ctx context.Context, in *v1.ListPodResourcesRequest, opts ...grpc.CallOption) (*v1.ListPodResourcesResponse, error) { + _va := make([]interface{}, len(opts)) + for _i := range opts { + _va[_i] = opts[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, in) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 *v1.ListPodResourcesResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *v1.ListPodResourcesRequest, ...grpc.CallOption) (*v1.ListPodResourcesResponse, error)); ok { + return rf(ctx, in, opts...) + } + if rf, ok := ret.Get(0).(func(context.Context, *v1.ListPodResourcesRequest, ...grpc.CallOption) *v1.ListPodResourcesResponse); ok { + r0 = rf(ctx, in, opts...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*v1.ListPodResourcesResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *v1.ListPodResourcesRequest, ...grpc.CallOption) error); ok { + r1 = rf(ctx, in, opts...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type mockConstructorTestingTNewPodResourcesListerClient interface { + mock.TestingT + Cleanup(func()) +} + +// NewPodResourcesListerClient creates a new instance of PodResourcesListerClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewPodResourcesListerClient(t mockConstructorTestingTNewPodResourcesListerClient) *PodResourcesListerClient { + mock := &PodResourcesListerClient{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/resourcemonitor/podresourcesscanner_test.go b/pkg/resourcemonitor/podresourcesscanner_test.go index 1a0a3d0915..7bb4736136 100644 --- a/pkg/resourcemonitor/podresourcesscanner_test.go +++ b/pkg/resourcemonitor/podresourcesscanner_test.go @@ -33,7 +33,7 @@ import ( v1 "k8s.io/kubelet/pkg/apis/podresources/v1" "sigs.k8s.io/node-feature-discovery/pkg/apihelper" - "sigs.k8s.io/node-feature-discovery/pkg/podres" + mockv1 "sigs.k8s.io/node-feature-discovery/pkg/podres/mocks" ) func TestPodScanner(t *testing.T) { @@ -54,7 +54,7 @@ func TestPodScanner(t *testing.T) { } Convey("When I scan for pod resources using fake client and no namespace", t, func() { - mockPodResClient := new(podres.MockPodResourcesListerClient) + mockPodResClient := new(mockv1.PodResourcesListerClient) mockAPIHelper := new(apihelper.MockAPIHelpers) mockClient := &k8sclient.Clientset{} computePodFingerprint := true @@ -676,7 +676,7 @@ func TestPodScanner(t *testing.T) { }) Convey("When I scan for pod resources using fake client and given namespace", t, func() { - mockPodResClient := new(podres.MockPodResourcesListerClient) + mockPodResClient := new(mockv1.PodResourcesListerClient) mockAPIHelper := new(apihelper.MockAPIHelpers) mockClient := &k8sclient.Clientset{} computePodFingerprint := false