Skip to content

Commit

Permalink
mockery to mockgen conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramcse committed Sep 25, 2021
1 parent 4e30c05 commit 0de4397
Show file tree
Hide file tree
Showing 34 changed files with 2,650 additions and 1,300 deletions.
61 changes: 38 additions & 23 deletions pkg/kubelet/apis/podresources/server_v1_test.go
Expand Up @@ -22,16 +22,15 @@ import (
"sort"
"testing"

"k8s.io/api/core/v1"
"github.com/golang/mock/gomock"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
podresourcesapi "k8s.io/kubelet/pkg/apis/podresources/v1"
pkgfeatures "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
"k8s.io/kubernetes/pkg/kubelet/cm/devicemanager"
"k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state"
podresourcetest "k8s.io/kubernetes/pkg/kubelet/apis/podresources/testing"
)

func TestListPodResourcesV1(t *testing.T) {
Expand All @@ -41,6 +40,9 @@ func TestListPodResourcesV1(t *testing.T) {
containerName := "container-name"
numaID := int64(1)

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

devs := []*podresourcesapi.ContainerDevices{
{
ResourceName: "resource",
Expand Down Expand Up @@ -156,16 +158,21 @@ func TestListPodResourcesV1(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
m := new(mockProvider)
m.On("GetPods").Return(tc.pods)
m.On("GetDevices", string(podUID), containerName).Return(tc.devices)
m.On("GetCPUs", string(podUID), containerName).Return(tc.cpus)
m.On("GetMemory", string(podUID), containerName).Return(tc.memory)
m.On("UpdateAllocatedDevices").Return()
m.On("GetAllocatableCPUs").Return(cpuset.CPUSet{})
m.On("GetAllocatableDevices").Return(devicemanager.NewResourceDeviceInstances())
m.On("GetAllocatableMemory").Return([]state.Block{})
server := NewV1PodResourcesServer(m, m, m, m)
mockDevicesProvider := podresourcetest.NewMockDevicesProvider(mockCtrl)
mockPodsProvider := podresourcetest.NewMockPodsProvider(mockCtrl)
mockCPUsProvider := podresourcetest.NewMockCPUsProvider(mockCtrl)
mockMemoryProvider := podresourcetest.NewMockMemoryProvider(mockCtrl)

mockPodsProvider.EXPECT().GetPods().Return(tc.pods).AnyTimes().AnyTimes()
mockDevicesProvider.EXPECT().GetDevices(string(podUID), containerName).Return(tc.devices).AnyTimes()
mockCPUsProvider.EXPECT().GetCPUs(string(podUID), containerName).Return(tc.cpus).AnyTimes()
mockMemoryProvider.EXPECT().GetMemory(string(podUID), containerName).Return(tc.memory).AnyTimes()
mockDevicesProvider.EXPECT().UpdateAllocatedDevices().Return().AnyTimes()
mockCPUsProvider.EXPECT().GetAllocatableCPUs().Return([]int64{}).AnyTimes()
mockDevicesProvider.EXPECT().GetAllocatableDevices().Return([]*podresourcesapi.ContainerDevices{}).AnyTimes()
mockMemoryProvider.EXPECT().GetAllocatableMemory().Return([]*podresourcesapi.ContainerMemory{}).AnyTimes()

server := NewV1PodResourcesServer(mockPodsProvider, mockDevicesProvider, mockCPUsProvider, mockMemoryProvider)
resp, err := server.List(context.TODO(), &podresourcesapi.ListPodResourcesRequest{})
if err != nil {
t.Errorf("want err = %v, got %q", nil, err)
Expand All @@ -180,6 +187,9 @@ func TestListPodResourcesV1(t *testing.T) {
func TestAllocatableResources(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.KubeletPodResourcesGetAllocatable, true)()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

allDevs := []*podresourcesapi.ContainerDevices{
{
ResourceName: "resource",
Expand Down Expand Up @@ -436,15 +446,20 @@ func TestAllocatableResources(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
m := new(mockProvider)
m.On("GetDevices", "", "").Return([]*podresourcesapi.ContainerDevices{})
m.On("GetCPUs", "", "").Return([]int64{})
m.On("GetMemory", "", "").Return([]*podresourcesapi.ContainerMemory{})
m.On("UpdateAllocatedDevices").Return()
m.On("GetAllocatableDevices").Return(tc.allDevices)
m.On("GetAllocatableCPUs").Return(tc.allCPUs)
m.On("GetAllocatableMemory").Return(tc.allMemory)
server := NewV1PodResourcesServer(m, m, m, m)
mockDevicesProvider := podresourcetest.NewMockDevicesProvider(mockCtrl)
mockPodsProvider := podresourcetest.NewMockPodsProvider(mockCtrl)
mockCPUsProvider := podresourcetest.NewMockCPUsProvider(mockCtrl)
mockMemoryProvider := podresourcetest.NewMockMemoryProvider(mockCtrl)

mockDevicesProvider.EXPECT().GetDevices("", "").Return([]*podresourcesapi.ContainerDevices{}).AnyTimes()
mockCPUsProvider.EXPECT().GetCPUs("", "").Return([]int64{}).AnyTimes()
mockMemoryProvider.EXPECT().GetMemory("", "").Return([]*podresourcesapi.ContainerMemory{}).AnyTimes()
mockDevicesProvider.EXPECT().UpdateAllocatedDevices().Return().AnyTimes()
mockDevicesProvider.EXPECT().GetAllocatableDevices().Return(tc.allDevices).AnyTimes()
mockCPUsProvider.EXPECT().GetAllocatableCPUs().Return(tc.allCPUs).AnyTimes()
mockMemoryProvider.EXPECT().GetAllocatableMemory().Return(tc.allMemory).AnyTimes()

server := NewV1PodResourcesServer(mockPodsProvider, mockDevicesProvider, mockCPUsProvider, mockMemoryProvider)

resp, err := server.GetAllocatableResources(context.TODO(), &podresourcesapi.AllocatableResourcesRequest{})
if err != nil {
Expand Down
65 changes: 14 additions & 51 deletions pkg/kubelet/apis/podresources/server_v1alpha1_test.go
Expand Up @@ -20,58 +20,15 @@ import (
"context"
"testing"

"github.com/stretchr/testify/mock"

"k8s.io/api/core/v1"
"github.com/golang/mock/gomock"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
podresourcesv1 "k8s.io/kubelet/pkg/apis/podresources/v1"
"k8s.io/kubelet/pkg/apis/podresources/v1alpha1"
podresourcetest "k8s.io/kubernetes/pkg/kubelet/apis/podresources/testing"
)

type mockProvider struct {
mock.Mock
}

func (m *mockProvider) GetPods() []*v1.Pod {
args := m.Called()
return args.Get(0).([]*v1.Pod)
}

func (m *mockProvider) GetDevices(podUID, containerName string) []*podresourcesv1.ContainerDevices {
args := m.Called(podUID, containerName)
return args.Get(0).([]*podresourcesv1.ContainerDevices)
}

func (m *mockProvider) GetCPUs(podUID, containerName string) []int64 {
args := m.Called(podUID, containerName)
return args.Get(0).([]int64)
}

func (m *mockProvider) GetMemory(podUID, containerName string) []*podresourcesv1.ContainerMemory {
args := m.Called(podUID, containerName)
return args.Get(0).([]*podresourcesv1.ContainerMemory)
}

func (m *mockProvider) UpdateAllocatedDevices() {
m.Called()
}

func (m *mockProvider) GetAllocatableDevices() []*podresourcesv1.ContainerDevices {
args := m.Called()
return args.Get(0).([]*podresourcesv1.ContainerDevices)
}

func (m *mockProvider) GetAllocatableCPUs() []int64 {
args := m.Called()
return args.Get(0).([]int64)
}

func (m *mockProvider) GetAllocatableMemory() []*podresourcesv1.ContainerMemory {
args := m.Called()
return args.Get(0).([]*podresourcesv1.ContainerMemory)
}

func TestListPodResourcesV1alpha1(t *testing.T) {
podName := "pod-name"
podNamespace := "pod-namespace"
Expand All @@ -85,6 +42,9 @@ func TestListPodResourcesV1alpha1(t *testing.T) {
},
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

for _, tc := range []struct {
desc string
pods []*v1.Pod
Expand Down Expand Up @@ -167,11 +127,14 @@ func TestListPodResourcesV1alpha1(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
m := new(mockProvider)
m.On("GetPods").Return(tc.pods)
m.On("GetDevices", string(podUID), containerName).Return(tc.devices)
m.On("UpdateAllocatedDevices").Return()
server := NewV1alpha1PodResourcesServer(m, m)
mockDevicesProvider := podresourcetest.NewMockDevicesProvider(mockCtrl)
mockPodsProvider := podresourcetest.NewMockPodsProvider(mockCtrl)

mockPodsProvider.EXPECT().GetPods().Return(tc.pods).AnyTimes()
mockDevicesProvider.EXPECT().GetDevices(string(podUID), containerName).Return(tc.devices).AnyTimes()
mockDevicesProvider.EXPECT().UpdateAllocatedDevices().Return().AnyTimes()

server := NewV1alpha1PodResourcesServer(mockPodsProvider, mockDevicesProvider)
resp, err := server.List(context.TODO(), &v1alpha1.ListPodResourcesRequest{})
if err != nil {
t.Errorf("want err = %v, got %q", nil, err)
Expand Down

0 comments on commit 0de4397

Please sign in to comment.