Chore: Replace hand crafted mocks with mockery (#110627)
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
package folders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resourcepb"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"google.golang.org/grpc"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
)
|
||||
|
||||
type storageMock struct {
|
||||
*mock.Mock
|
||||
rest.Storage
|
||||
}
|
||||
|
||||
type searcherMock struct {
|
||||
*mock.Mock
|
||||
resourcepb.ResourceIndexClient
|
||||
}
|
||||
|
||||
func (m storageMock) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
args := m.Called(ctx, name, options)
|
||||
return args.Get(0).(runtime.Object), args.Error(1)
|
||||
}
|
||||
|
||||
func (m storageMock) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
args := m.Called(ctx, obj, tableOptions)
|
||||
return args.Get(0).(*metav1.Table), args.Error(1)
|
||||
}
|
||||
|
||||
func (m storageMock) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
|
||||
args := m.Called(ctx, obj, createValidation, options)
|
||||
return args.Get(0).(runtime.Object), args.Error(1)
|
||||
}
|
||||
|
||||
func (m storageMock) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||
args := m.Called(ctx, name, deleteValidation, options)
|
||||
return args.Get(0).(runtime.Object), args.Bool(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m storageMock) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
|
||||
args := m.Called(ctx, deleteValidation, options, listOptions)
|
||||
return args.Get(0).(runtime.Object), args.Error(1)
|
||||
}
|
||||
|
||||
func (m storageMock) GetSingularName() string {
|
||||
args := m.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (m storageMock) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
args := m.Called(ctx, options)
|
||||
return args.Get(0).(runtime.Object), args.Error(1)
|
||||
}
|
||||
|
||||
func (m storageMock) NamespaceScoped() bool {
|
||||
args := m.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (m storageMock) NewList() runtime.Object {
|
||||
args := m.Called()
|
||||
return args.Get(0).(runtime.Object)
|
||||
}
|
||||
|
||||
func (m storageMock) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
|
||||
args := m.Called(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
|
||||
if name == "object-fail" {
|
||||
return nil, false, args.Error(2)
|
||||
}
|
||||
return args.Get(0).(runtime.Object), args.Bool(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (s searcherMock) GetStats(ctx context.Context, req *resourcepb.ResourceStatsRequest, _ ...grpc.CallOption) (*resourcepb.ResourceStatsResponse, error) {
|
||||
args := s.Called(ctx, req)
|
||||
return args.Get(0).(*resourcepb.ResourceStatsResponse), args.Error(1)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
@@ -27,7 +26,7 @@ func TestParents(t *testing.T) {
|
||||
folder string
|
||||
}
|
||||
getter map[string]*folders.Folder
|
||||
setupFn func(*mock.Mock) // called after the getter is registered
|
||||
setupFn func(*grafanarest.MockStorage) // called after the getter is registered
|
||||
expected *folders.FolderInfoList
|
||||
expectedErr string
|
||||
maxDepth int // defaults to 5 unless set
|
||||
@@ -68,10 +67,10 @@ func TestParents(t *testing.T) {
|
||||
name: "test",
|
||||
folder: "parent", // NOTE that parent is not found
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
var nothing *folders.Folder // needs to be an object
|
||||
m.On("Get", context.TODO(), "parent", &metav1.GetOptions{}).Return(
|
||||
nothing, fmt.Errorf("custom error message")).Once()
|
||||
nothing, fmt.Errorf("custom error message"))
|
||||
},
|
||||
expected: &folders.FolderInfoList{Items: []folders.FolderInfo{
|
||||
{Name: "test", Parent: "parent"},
|
||||
@@ -84,7 +83,7 @@ func TestParents(t *testing.T) {
|
||||
name: "test",
|
||||
folder: "parent", // not a folder
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
m.On("Get", context.TODO(), "parent", &metav1.GetOptions{}).Return(
|
||||
&unstructured.Unstructured{}, // not a folder
|
||||
nil).Once()
|
||||
@@ -100,7 +99,7 @@ func TestParents(t *testing.T) {
|
||||
name: "test",
|
||||
folder: "test",
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
m.On("Get", context.TODO(), "test", &metav1.GetOptions{}).Return(
|
||||
&folders.Folder{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
@@ -108,7 +107,7 @@ func TestParents(t *testing.T) {
|
||||
utils.AnnoKeyFolder: "test", // invalid! this will cycle
|
||||
},
|
||||
},
|
||||
}, nil).Times(2)
|
||||
}, nil).Maybe()
|
||||
},
|
||||
expectedErr: "cyclic folder references found",
|
||||
},
|
||||
@@ -131,8 +130,7 @@ func TestParents(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
m := grafanarest.NewMockStorage(t)
|
||||
if tt.getter == nil && tt.setupFn == nil {
|
||||
// Default to filling the getter with expected results
|
||||
for _, item := range tt.expected.Items {
|
||||
@@ -148,25 +146,24 @@ func TestParents(t *testing.T) {
|
||||
Title: item.Title,
|
||||
Description: &item.Description,
|
||||
},
|
||||
}, nil) // we don't care how often they are called
|
||||
}, nil).Maybe() // we don't care how often they are called
|
||||
}
|
||||
} else {
|
||||
for k, v := range tt.getter {
|
||||
v.Name = k // set the name
|
||||
m.On("Get", context.TODO(), k, &metav1.GetOptions{}).Return(v, nil).Once()
|
||||
m.On("Get", context.TODO(), k, &metav1.GetOptions{}).Return(v, nil).Maybe()
|
||||
}
|
||||
if tt.setupFn != nil {
|
||||
tt.setupFn(m)
|
||||
}
|
||||
}
|
||||
|
||||
gm := storageMock{m, s}
|
||||
maxDepth := tt.maxDepth
|
||||
if maxDepth == 0 {
|
||||
maxDepth = 5
|
||||
}
|
||||
|
||||
getter := newParentsGetter(gm, maxDepth)
|
||||
getter := newParentsGetter(m, maxDepth)
|
||||
parents, err := getter(context.TODO(), &folders.Folder{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: tt.request.name,
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/services/folder/foldertest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resourcepb"
|
||||
)
|
||||
|
||||
@@ -30,7 +31,7 @@ func TestFolderAPIBuilder_Validate_Create(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input input
|
||||
setupFn func(*mock.Mock)
|
||||
setupFn func(*grafanarest.MockStorage)
|
||||
err error
|
||||
}{
|
||||
{
|
||||
@@ -67,21 +68,21 @@ func TestFolderAPIBuilder_Validate_Create(t *testing.T) {
|
||||
annotations: map[string]string{"grafana.app/folder": "p1"}, // already max depth
|
||||
name: "valid-name",
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
m.On("Get", mock.Anything, "p1", mock.Anything).Return(
|
||||
&folders.Folder{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "p1",
|
||||
Annotations: map[string]string{"grafana.app/folder": "p2"},
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Maybe()
|
||||
m.On("Get", mock.Anything, "p2", mock.Anything).Return(
|
||||
&folders.Folder{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "p2",
|
||||
Annotations: map[string]string{"grafana.app/folder": "p3"},
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Maybe()
|
||||
},
|
||||
err: folder.ErrMaximumDepthReached,
|
||||
},
|
||||
@@ -114,9 +115,7 @@ func TestFolderAPIBuilder_Validate_Create(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
us := storageMock{m, s}
|
||||
us := grafanarest.NewMockStorage(t)
|
||||
|
||||
b := &FolderAPIBuilder{
|
||||
gv: resourceInfo.GroupVersion(),
|
||||
@@ -131,7 +130,7 @@ func TestFolderAPIBuilder_Validate_Create(t *testing.T) {
|
||||
tt.input.obj.Annotations = tt.input.annotations
|
||||
|
||||
if tt.setupFn != nil {
|
||||
tt.setupFn(m)
|
||||
tt.setupFn(us)
|
||||
}
|
||||
|
||||
err := b.Validate(context.Background(), admission.NewAttributesRecord(
|
||||
@@ -175,11 +174,6 @@ func TestFolderAPIBuilder_Validate_Delete(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
us := storageMock{m, s}
|
||||
sm := searcherMock{Mock: m}
|
||||
|
||||
obj := &folders.Folder{
|
||||
Spec: folders.FolderSpec{
|
||||
Title: "foo",
|
||||
@@ -192,14 +186,12 @@ func TestFolderAPIBuilder_Validate_Delete(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var setupFn = func(m *mock.Mock, stats *resourcepb.ResourceStatsResponse_Stats) {
|
||||
m.On("GetStats", mock.Anything, &resourcepb.ResourceStatsRequest{Namespace: obj.Namespace, Folder: obj.Name}).Return(
|
||||
&resourcepb.ResourceStatsResponse{Stats: []*resourcepb.ResourceStatsResponse_Stats{stats}},
|
||||
nil,
|
||||
).Once()
|
||||
}
|
||||
|
||||
setupFn(m, tt.statsResponse)
|
||||
us := grafanarest.NewMockStorage(t)
|
||||
sm := resource.NewMockResourceClient(t)
|
||||
sm.On("GetStats", mock.Anything, &resourcepb.ResourceStatsRequest{Namespace: obj.Namespace, Folder: obj.Name}).Return(
|
||||
&resourcepb.ResourceStatsResponse{Stats: []*resourcepb.ResourceStatsResponse_Stats{tt.statsResponse}},
|
||||
nil,
|
||||
).Once()
|
||||
|
||||
b := &FolderAPIBuilder{
|
||||
gv: resourceInfo.GroupVersion(),
|
||||
@@ -239,7 +231,7 @@ func TestFolderAPIBuilder_Validate_Update(t *testing.T) {
|
||||
name string
|
||||
updatedObj *folders.Folder
|
||||
expected *folders.Folder
|
||||
setupFn func(*mock.Mock)
|
||||
setupFn func(*grafanarest.MockStorage)
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
@@ -291,7 +283,7 @@ func TestFolderAPIBuilder_Validate_Update(t *testing.T) {
|
||||
Annotations: map[string]string{"grafana.app/folder": "new-parent"},
|
||||
},
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
m.On("Get", mock.Anything, "new-parent", mock.Anything).Return(
|
||||
&folders.Folder{},
|
||||
nil).Once()
|
||||
@@ -309,10 +301,8 @@ func TestFolderAPIBuilder_Validate_Update(t *testing.T) {
|
||||
Annotations: map[string]string{"grafana.app/folder": accesscontrol.K6FolderUID},
|
||||
},
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
m.On("Get", mock.Anything, accesscontrol.K6FolderUID, mock.Anything).Return(
|
||||
&folders.Folder{},
|
||||
nil).Once()
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
// nothing
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
@@ -328,7 +318,7 @@ func TestFolderAPIBuilder_Validate_Update(t *testing.T) {
|
||||
Annotations: map[string]string{"grafana.app/folder": "new-parent"},
|
||||
},
|
||||
},
|
||||
setupFn: func(m *mock.Mock) {
|
||||
setupFn: func(m *grafanarest.MockStorage) {
|
||||
m.On("Get", mock.Anything, "new-parent", mock.Anything).Return(
|
||||
&folders.Folder{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
@@ -368,12 +358,10 @@ func TestFolderAPIBuilder_Validate_Update(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
us := storageMock{m, s}
|
||||
sm := searcherMock{Mock: m}
|
||||
us := grafanarest.NewMockStorage(t)
|
||||
sm := resource.NewMockResourceClient(t)
|
||||
if tt.setupFn != nil {
|
||||
tt.setupFn(m)
|
||||
tt.setupFn(us)
|
||||
}
|
||||
|
||||
b := &FolderAPIBuilder{
|
||||
@@ -470,10 +458,8 @@ func TestFolderAPIBuilder_Mutate_Create(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
us := storageMock{m, s}
|
||||
sm := searcherMock{Mock: m}
|
||||
us := grafanarest.NewMockStorage(t)
|
||||
sm := resource.NewMockResourceClient(t)
|
||||
b := &FolderAPIBuilder{
|
||||
gv: resourceInfo.GroupVersion(),
|
||||
features: nil,
|
||||
@@ -580,10 +566,8 @@ func TestFolderAPIBuilder_Mutate_Update(t *testing.T) {
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
us := storageMock{m, s}
|
||||
sm := searcherMock{Mock: m}
|
||||
us := grafanarest.NewMockStorage(t)
|
||||
sm := resource.NewMockResourceClient(t)
|
||||
b := &FolderAPIBuilder{
|
||||
gv: resourceInfo.GroupVersion(),
|
||||
features: nil,
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -204,8 +203,7 @@ func TestValidateUpdate(t *testing.T) {
|
||||
if maxDepth == 0 {
|
||||
maxDepth = 5
|
||||
}
|
||||
s := (grafanarest.Storage)(nil)
|
||||
m := &mock.Mock{}
|
||||
m := grafanarest.NewMockStorage(t)
|
||||
if tt.parents != nil {
|
||||
for _, v := range tt.parents.Items {
|
||||
m.On("Get", context.Background(), v.Name, &metav1.GetOptions{}).Return(&folders.Folder{
|
||||
@@ -214,11 +212,11 @@ func TestValidateUpdate(t *testing.T) {
|
||||
}, Spec: folders.FolderSpec{
|
||||
Title: v.Title,
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Maybe()
|
||||
}
|
||||
}
|
||||
|
||||
err := validateOnUpdate(context.Background(), tt.folder, tt.old, storageMock{m, s},
|
||||
err := validateOnUpdate(context.Background(), tt.folder, tt.old, m,
|
||||
func(ctx context.Context, folder *folders.Folder) (*folders.FolderInfoList, error) {
|
||||
return tt.parents, tt.parentsError
|
||||
}, maxDepth)
|
||||
|
||||
Reference in New Issue
Block a user