Chore: Replace hand crafted mocks with mockery (#110627)

This commit is contained in:
Ryan McKinley
2025-09-05 07:13:15 +00:00
committed by GitHub
parent 554cd6f198
commit eeb940e733
12 changed files with 1873 additions and 330 deletions
-24
View File
@@ -10,36 +10,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog/v2"
)
var (
_ rest.Storage = (DualWriter)(nil)
_ rest.Scoper = (DualWriter)(nil)
_ rest.TableConvertor = (DualWriter)(nil)
_ rest.CreaterUpdater = (DualWriter)(nil)
_ rest.CollectionDeleter = (DualWriter)(nil)
_ rest.GracefulDeleter = (DualWriter)(nil)
_ rest.SingularNameProvider = (DualWriter)(nil)
)
// Function that will create a dual writer
type DualWriteBuilder func(gr schema.GroupResource, legacy Storage, unified Storage) (Storage, error)
// Storage is a storage implementation that satisfies the same interfaces as genericregistry.Store.
type Storage interface {
rest.Storage
rest.Scoper
rest.TableConvertor
rest.SingularNameProvider
rest.Getter
rest.Lister
rest.CreaterUpdater
rest.GracefulDeleter
rest.CollectionDeleter
}
// DualWriter is a storage implementation that writes first to LegacyStorage and then to Storage.
// If writing to LegacyStorage fails, the write to Storage is skipped and the error is returned.
// Storage is used for all read operations. This is useful as a migration step from SQL based
+29 -39
View File
@@ -71,8 +71,8 @@ var storageListWith3itemsMissingFoo2 = &example.PodList{TypeMeta: metav1.TypeMet
func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
type testCase struct {
setupLegacyFn func(m *mock.Mock)
setupStorageFn func(m *mock.Mock)
setupLegacyFn func(m *MockStorage)
setupStorageFn func(m *MockStorage)
name string
expectedOutcome bool
wantErr bool
@@ -81,40 +81,40 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
[]testCase{
{
name: "both stores are in sync",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, nil)
},
expectedOutcome: true,
},
{
name: "both stores are in sync - fail to list from legacy",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, errors.New("error"))
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, nil)
},
expectedOutcome: false,
},
{
name: "both stores are in sync - fail to list from storage",
setupLegacyFn: func(m *mock.Mock) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil)
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil).Maybe()
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, errors.New("error"))
},
expectedOutcome: false,
},
{
name: "storage is missing 1 entry (foo4)",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith4items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, nil)
m.On("Update", mock.Anything, "foo4", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil)
},
@@ -122,10 +122,10 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
},
{
name: "storage needs to be update (foo2 is different)",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3itemsObj2IsDifferent, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, nil)
m.On("Update", mock.Anything, "foo2", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil)
},
@@ -133,10 +133,10 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
},
{
name: "storage is missing 1 entry (foo4) - fail to upsert",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith4items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3items, nil)
m.On("Update", mock.Anything, "foo4", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, errors.New("error"))
},
@@ -144,10 +144,10 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
},
{
name: "storage has an extra 1 entry (foo4)",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith4items, nil)
m.On("Delete", mock.Anything, "foo4", mock.Anything, mock.Anything).Return(exampleObj, false, nil)
},
@@ -155,10 +155,10 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
},
{
name: "storage has an extra 1 entry (foo4) - fail to delete",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith4items, nil)
m.On("Delete", mock.Anything, "foo4", mock.Anything, mock.Anything).Return(exampleObj, false, errors.New("error"))
},
@@ -166,10 +166,10 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
},
{
name: "storage is missing 1 entry (foo3) and has an extra 1 entry (foo4)",
setupLegacyFn: func(m *mock.Mock) {
setupLegacyFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(legacyListWith3items, nil)
},
setupStorageFn: func(m *mock.Mock) {
setupStorageFn: func(m *MockStorage) {
m.On("List", mock.Anything, mock.Anything).Return(storageListWith3itemsMissingFoo2, nil)
m.On("Update", mock.Anything, "foo2", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil)
m.On("Delete", mock.Anything, "foo4", mock.Anything, mock.Anything).Return(exampleObj, false, nil)
@@ -181,19 +181,14 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
// mode 1
for _, tt := range tests {
t.Run("Mode-1-"+tt.name, func(t *testing.T) {
l := (Storage)(nil)
s := (Storage)(nil)
lm := &mock.Mock{}
um := &mock.Mock{}
ls := storageMock{lm, l}
us := storageMock{um, s}
ls := NewMockStorage(t)
us := NewMockStorage(t)
if tt.setupLegacyFn != nil {
tt.setupLegacyFn(lm)
tt.setupLegacyFn(ls)
}
if tt.setupStorageFn != nil {
tt.setupStorageFn(um)
tt.setupStorageFn(us)
}
outcome, err := legacyToUnifiedStorageDataSyncer(context.Background(), &SyncerConfig{
@@ -220,19 +215,14 @@ func TestLegacyToUnifiedStorage_DataSyncer(t *testing.T) {
// mode 2
for _, tt := range tests {
t.Run("Mode-2-"+tt.name, func(t *testing.T) {
l := (Storage)(nil)
s := (Storage)(nil)
lm := &mock.Mock{}
um := &mock.Mock{}
ls := storageMock{lm, l}
us := storageMock{um, s}
ls := NewMockStorage(t)
us := NewMockStorage(t)
if tt.setupLegacyFn != nil {
tt.setupLegacyFn(lm)
tt.setupLegacyFn(ls)
}
if tt.setupStorageFn != nil {
tt.setupStorageFn(um)
tt.setupStorageFn(us)
}
outcome, err := legacyToUnifiedStorageDataSyncer(context.Background(), &SyncerConfig{
+13 -11
View File
@@ -14,6 +14,13 @@ import (
"k8s.io/apiserver/pkg/endpoints/request"
)
var now = time.Now()
var exampleObj = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", CreationTimestamp: metav1.Time{}, GenerateName: "foo"}, Spec: example.PodSpec{}, Status: example.PodStatus{StartTime: &metav1.Time{Time: now}}}
var anotherObj = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "2", GenerateName: "foo"}, Spec: example.PodSpec{}, Status: example.PodStatus{StartTime: &metav1.Time{Time: now}}}
var exampleList = &example.PodList{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ListMeta: metav1.ListMeta{}, Items: []example.Pod{*exampleObj}}
var anotherList = &example.PodList{Items: []example.Pod{*anotherObj}}
func TestSetDualWritingMode(t *testing.T) {
type testCase struct {
name string
@@ -80,18 +87,13 @@ func TestSetDualWritingMode(t *testing.T) {
}
for _, tt := range tests {
l := (Storage)(nil)
s := (Storage)(nil)
us := NewMockStorage(t)
us.On("List", mock.Anything, mock.Anything).Return(anotherList, nil).Maybe()
us.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil).Maybe()
us.On("Delete", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil).Maybe()
sm := &mock.Mock{}
sm.On("List", mock.Anything, mock.Anything).Return(anotherList, nil)
sm.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil)
sm.On("Delete", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(exampleObj, false, nil)
us := storageMock{sm, s}
lm := &mock.Mock{}
lm.On("List", mock.Anything, mock.Anything).Return(exampleList, nil)
ls := storageMock{lm, l}
ls := NewMockStorage(t)
ls.On("List", mock.Anything, mock.Anything).Return(exampleList, nil).Maybe()
serverLockSvc := &fakeServerLock{
err: tt.serverLockError,
+18
View File
@@ -0,0 +1,18 @@
package rest
import "k8s.io/apiserver/pkg/registry/rest"
// Storage is a storage implementation that satisfies the same interfaces as genericregistry.Store.
//
//go:generate mockery --name Storage --structname MockStorage --inpackage --filename storage_mock.go --with-expecter
type Storage interface {
rest.Storage
rest.Scoper
rest.TableConvertor
rest.SingularNameProvider
rest.Getter
rest.Lister
rest.CreaterUpdater
rest.GracefulDeleter
rest.CollectionDeleter
}
+699
View File
@@ -0,0 +1,699 @@
// Code generated by mockery v2.53.4. DO NOT EDIT.
package rest
import (
context "context"
mock "github.com/stretchr/testify/mock"
internalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
registryrest "k8s.io/apiserver/pkg/registry/rest"
runtime "k8s.io/apimachinery/pkg/runtime"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// MockStorage is an autogenerated mock type for the Storage type
type MockStorage struct {
mock.Mock
}
type MockStorage_Expecter struct {
mock *mock.Mock
}
func (_m *MockStorage) EXPECT() *MockStorage_Expecter {
return &MockStorage_Expecter{mock: &_m.Mock}
}
// ConvertToTable provides a mock function with given fields: ctx, object, tableOptions
func (_m *MockStorage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*v1.Table, error) {
ret := _m.Called(ctx, object, tableOptions)
if len(ret) == 0 {
panic("no return value specified for ConvertToTable")
}
var r0 *v1.Table
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, runtime.Object, runtime.Object) (*v1.Table, error)); ok {
return rf(ctx, object, tableOptions)
}
if rf, ok := ret.Get(0).(func(context.Context, runtime.Object, runtime.Object) *v1.Table); ok {
r0 = rf(ctx, object, tableOptions)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*v1.Table)
}
}
if rf, ok := ret.Get(1).(func(context.Context, runtime.Object, runtime.Object) error); ok {
r1 = rf(ctx, object, tableOptions)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockStorage_ConvertToTable_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ConvertToTable'
type MockStorage_ConvertToTable_Call struct {
*mock.Call
}
// ConvertToTable is a helper method to define mock.On call
// - ctx context.Context
// - object runtime.Object
// - tableOptions runtime.Object
func (_e *MockStorage_Expecter) ConvertToTable(ctx interface{}, object interface{}, tableOptions interface{}) *MockStorage_ConvertToTable_Call {
return &MockStorage_ConvertToTable_Call{Call: _e.mock.On("ConvertToTable", ctx, object, tableOptions)}
}
func (_c *MockStorage_ConvertToTable_Call) Run(run func(ctx context.Context, object runtime.Object, tableOptions runtime.Object)) *MockStorage_ConvertToTable_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(runtime.Object), args[2].(runtime.Object))
})
return _c
}
func (_c *MockStorage_ConvertToTable_Call) Return(_a0 *v1.Table, _a1 error) *MockStorage_ConvertToTable_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockStorage_ConvertToTable_Call) RunAndReturn(run func(context.Context, runtime.Object, runtime.Object) (*v1.Table, error)) *MockStorage_ConvertToTable_Call {
_c.Call.Return(run)
return _c
}
// Create provides a mock function with given fields: ctx, obj, createValidation, options
func (_m *MockStorage) Create(ctx context.Context, obj runtime.Object, createValidation registryrest.ValidateObjectFunc, options *v1.CreateOptions) (runtime.Object, error) {
ret := _m.Called(ctx, obj, createValidation, options)
if len(ret) == 0 {
panic("no return value specified for Create")
}
var r0 runtime.Object
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, runtime.Object, registryrest.ValidateObjectFunc, *v1.CreateOptions) (runtime.Object, error)); ok {
return rf(ctx, obj, createValidation, options)
}
if rf, ok := ret.Get(0).(func(context.Context, runtime.Object, registryrest.ValidateObjectFunc, *v1.CreateOptions) runtime.Object); ok {
r0 = rf(ctx, obj, createValidation, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, runtime.Object, registryrest.ValidateObjectFunc, *v1.CreateOptions) error); ok {
r1 = rf(ctx, obj, createValidation, options)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockStorage_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create'
type MockStorage_Create_Call struct {
*mock.Call
}
// Create is a helper method to define mock.On call
// - ctx context.Context
// - obj runtime.Object
// - createValidation registryrest.ValidateObjectFunc
// - options *v1.CreateOptions
func (_e *MockStorage_Expecter) Create(ctx interface{}, obj interface{}, createValidation interface{}, options interface{}) *MockStorage_Create_Call {
return &MockStorage_Create_Call{Call: _e.mock.On("Create", ctx, obj, createValidation, options)}
}
func (_c *MockStorage_Create_Call) Run(run func(ctx context.Context, obj runtime.Object, createValidation registryrest.ValidateObjectFunc, options *v1.CreateOptions)) *MockStorage_Create_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(runtime.Object), args[2].(registryrest.ValidateObjectFunc), args[3].(*v1.CreateOptions))
})
return _c
}
func (_c *MockStorage_Create_Call) Return(_a0 runtime.Object, _a1 error) *MockStorage_Create_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockStorage_Create_Call) RunAndReturn(run func(context.Context, runtime.Object, registryrest.ValidateObjectFunc, *v1.CreateOptions) (runtime.Object, error)) *MockStorage_Create_Call {
_c.Call.Return(run)
return _c
}
// Delete provides a mock function with given fields: ctx, name, deleteValidation, options
func (_m *MockStorage) Delete(ctx context.Context, name string, deleteValidation registryrest.ValidateObjectFunc, options *v1.DeleteOptions) (runtime.Object, bool, error) {
ret := _m.Called(ctx, name, deleteValidation, options)
if len(ret) == 0 {
panic("no return value specified for Delete")
}
var r0 runtime.Object
var r1 bool
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, string, registryrest.ValidateObjectFunc, *v1.DeleteOptions) (runtime.Object, bool, error)); ok {
return rf(ctx, name, deleteValidation, options)
}
if rf, ok := ret.Get(0).(func(context.Context, string, registryrest.ValidateObjectFunc, *v1.DeleteOptions) runtime.Object); ok {
r0 = rf(ctx, name, deleteValidation, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, registryrest.ValidateObjectFunc, *v1.DeleteOptions) bool); ok {
r1 = rf(ctx, name, deleteValidation, options)
} else {
r1 = ret.Get(1).(bool)
}
if rf, ok := ret.Get(2).(func(context.Context, string, registryrest.ValidateObjectFunc, *v1.DeleteOptions) error); ok {
r2 = rf(ctx, name, deleteValidation, options)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// MockStorage_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete'
type MockStorage_Delete_Call struct {
*mock.Call
}
// Delete is a helper method to define mock.On call
// - ctx context.Context
// - name string
// - deleteValidation registryrest.ValidateObjectFunc
// - options *v1.DeleteOptions
func (_e *MockStorage_Expecter) Delete(ctx interface{}, name interface{}, deleteValidation interface{}, options interface{}) *MockStorage_Delete_Call {
return &MockStorage_Delete_Call{Call: _e.mock.On("Delete", ctx, name, deleteValidation, options)}
}
func (_c *MockStorage_Delete_Call) Run(run func(ctx context.Context, name string, deleteValidation registryrest.ValidateObjectFunc, options *v1.DeleteOptions)) *MockStorage_Delete_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(registryrest.ValidateObjectFunc), args[3].(*v1.DeleteOptions))
})
return _c
}
func (_c *MockStorage_Delete_Call) Return(_a0 runtime.Object, _a1 bool, _a2 error) *MockStorage_Delete_Call {
_c.Call.Return(_a0, _a1, _a2)
return _c
}
func (_c *MockStorage_Delete_Call) RunAndReturn(run func(context.Context, string, registryrest.ValidateObjectFunc, *v1.DeleteOptions) (runtime.Object, bool, error)) *MockStorage_Delete_Call {
_c.Call.Return(run)
return _c
}
// DeleteCollection provides a mock function with given fields: ctx, deleteValidation, options, listOptions
func (_m *MockStorage) DeleteCollection(ctx context.Context, deleteValidation registryrest.ValidateObjectFunc, options *v1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
ret := _m.Called(ctx, deleteValidation, options, listOptions)
if len(ret) == 0 {
panic("no return value specified for DeleteCollection")
}
var r0 runtime.Object
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, registryrest.ValidateObjectFunc, *v1.DeleteOptions, *internalversion.ListOptions) (runtime.Object, error)); ok {
return rf(ctx, deleteValidation, options, listOptions)
}
if rf, ok := ret.Get(0).(func(context.Context, registryrest.ValidateObjectFunc, *v1.DeleteOptions, *internalversion.ListOptions) runtime.Object); ok {
r0 = rf(ctx, deleteValidation, options, listOptions)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, registryrest.ValidateObjectFunc, *v1.DeleteOptions, *internalversion.ListOptions) error); ok {
r1 = rf(ctx, deleteValidation, options, listOptions)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockStorage_DeleteCollection_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteCollection'
type MockStorage_DeleteCollection_Call struct {
*mock.Call
}
// DeleteCollection is a helper method to define mock.On call
// - ctx context.Context
// - deleteValidation registryrest.ValidateObjectFunc
// - options *v1.DeleteOptions
// - listOptions *internalversion.ListOptions
func (_e *MockStorage_Expecter) DeleteCollection(ctx interface{}, deleteValidation interface{}, options interface{}, listOptions interface{}) *MockStorage_DeleteCollection_Call {
return &MockStorage_DeleteCollection_Call{Call: _e.mock.On("DeleteCollection", ctx, deleteValidation, options, listOptions)}
}
func (_c *MockStorage_DeleteCollection_Call) Run(run func(ctx context.Context, deleteValidation registryrest.ValidateObjectFunc, options *v1.DeleteOptions, listOptions *internalversion.ListOptions)) *MockStorage_DeleteCollection_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(registryrest.ValidateObjectFunc), args[2].(*v1.DeleteOptions), args[3].(*internalversion.ListOptions))
})
return _c
}
func (_c *MockStorage_DeleteCollection_Call) Return(_a0 runtime.Object, _a1 error) *MockStorage_DeleteCollection_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockStorage_DeleteCollection_Call) RunAndReturn(run func(context.Context, registryrest.ValidateObjectFunc, *v1.DeleteOptions, *internalversion.ListOptions) (runtime.Object, error)) *MockStorage_DeleteCollection_Call {
_c.Call.Return(run)
return _c
}
// Destroy provides a mock function with no fields
func (_m *MockStorage) Destroy() {
_m.Called()
}
// MockStorage_Destroy_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Destroy'
type MockStorage_Destroy_Call struct {
*mock.Call
}
// Destroy is a helper method to define mock.On call
func (_e *MockStorage_Expecter) Destroy() *MockStorage_Destroy_Call {
return &MockStorage_Destroy_Call{Call: _e.mock.On("Destroy")}
}
func (_c *MockStorage_Destroy_Call) Run(run func()) *MockStorage_Destroy_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockStorage_Destroy_Call) Return() *MockStorage_Destroy_Call {
_c.Call.Return()
return _c
}
func (_c *MockStorage_Destroy_Call) RunAndReturn(run func()) *MockStorage_Destroy_Call {
_c.Run(run)
return _c
}
// Get provides a mock function with given fields: ctx, name, options
func (_m *MockStorage) Get(ctx context.Context, name string, options *v1.GetOptions) (runtime.Object, error) {
ret := _m.Called(ctx, name, options)
if len(ret) == 0 {
panic("no return value specified for Get")
}
var r0 runtime.Object
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string, *v1.GetOptions) (runtime.Object, error)); ok {
return rf(ctx, name, options)
}
if rf, ok := ret.Get(0).(func(context.Context, string, *v1.GetOptions) runtime.Object); ok {
r0 = rf(ctx, name, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, *v1.GetOptions) error); ok {
r1 = rf(ctx, name, options)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockStorage_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get'
type MockStorage_Get_Call struct {
*mock.Call
}
// Get is a helper method to define mock.On call
// - ctx context.Context
// - name string
// - options *v1.GetOptions
func (_e *MockStorage_Expecter) Get(ctx interface{}, name interface{}, options interface{}) *MockStorage_Get_Call {
return &MockStorage_Get_Call{Call: _e.mock.On("Get", ctx, name, options)}
}
func (_c *MockStorage_Get_Call) Run(run func(ctx context.Context, name string, options *v1.GetOptions)) *MockStorage_Get_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(*v1.GetOptions))
})
return _c
}
func (_c *MockStorage_Get_Call) Return(_a0 runtime.Object, _a1 error) *MockStorage_Get_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockStorage_Get_Call) RunAndReturn(run func(context.Context, string, *v1.GetOptions) (runtime.Object, error)) *MockStorage_Get_Call {
_c.Call.Return(run)
return _c
}
// GetSingularName provides a mock function with no fields
func (_m *MockStorage) GetSingularName() string {
ret := _m.Called()
if len(ret) == 0 {
panic("no return value specified for GetSingularName")
}
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockStorage_GetSingularName_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetSingularName'
type MockStorage_GetSingularName_Call struct {
*mock.Call
}
// GetSingularName is a helper method to define mock.On call
func (_e *MockStorage_Expecter) GetSingularName() *MockStorage_GetSingularName_Call {
return &MockStorage_GetSingularName_Call{Call: _e.mock.On("GetSingularName")}
}
func (_c *MockStorage_GetSingularName_Call) Run(run func()) *MockStorage_GetSingularName_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockStorage_GetSingularName_Call) Return(_a0 string) *MockStorage_GetSingularName_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockStorage_GetSingularName_Call) RunAndReturn(run func() string) *MockStorage_GetSingularName_Call {
_c.Call.Return(run)
return _c
}
// List provides a mock function with given fields: ctx, options
func (_m *MockStorage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
ret := _m.Called(ctx, options)
if len(ret) == 0 {
panic("no return value specified for List")
}
var r0 runtime.Object
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *internalversion.ListOptions) (runtime.Object, error)); ok {
return rf(ctx, options)
}
if rf, ok := ret.Get(0).(func(context.Context, *internalversion.ListOptions) runtime.Object); ok {
r0 = rf(ctx, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *internalversion.ListOptions) error); ok {
r1 = rf(ctx, options)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockStorage_List_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'List'
type MockStorage_List_Call struct {
*mock.Call
}
// List is a helper method to define mock.On call
// - ctx context.Context
// - options *internalversion.ListOptions
func (_e *MockStorage_Expecter) List(ctx interface{}, options interface{}) *MockStorage_List_Call {
return &MockStorage_List_Call{Call: _e.mock.On("List", ctx, options)}
}
func (_c *MockStorage_List_Call) Run(run func(ctx context.Context, options *internalversion.ListOptions)) *MockStorage_List_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(*internalversion.ListOptions))
})
return _c
}
func (_c *MockStorage_List_Call) Return(_a0 runtime.Object, _a1 error) *MockStorage_List_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockStorage_List_Call) RunAndReturn(run func(context.Context, *internalversion.ListOptions) (runtime.Object, error)) *MockStorage_List_Call {
_c.Call.Return(run)
return _c
}
// NamespaceScoped provides a mock function with no fields
func (_m *MockStorage) NamespaceScoped() bool {
ret := _m.Called()
if len(ret) == 0 {
panic("no return value specified for NamespaceScoped")
}
var r0 bool
if rf, ok := ret.Get(0).(func() bool); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// MockStorage_NamespaceScoped_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NamespaceScoped'
type MockStorage_NamespaceScoped_Call struct {
*mock.Call
}
// NamespaceScoped is a helper method to define mock.On call
func (_e *MockStorage_Expecter) NamespaceScoped() *MockStorage_NamespaceScoped_Call {
return &MockStorage_NamespaceScoped_Call{Call: _e.mock.On("NamespaceScoped")}
}
func (_c *MockStorage_NamespaceScoped_Call) Run(run func()) *MockStorage_NamespaceScoped_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockStorage_NamespaceScoped_Call) Return(_a0 bool) *MockStorage_NamespaceScoped_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockStorage_NamespaceScoped_Call) RunAndReturn(run func() bool) *MockStorage_NamespaceScoped_Call {
_c.Call.Return(run)
return _c
}
// New provides a mock function with no fields
func (_m *MockStorage) New() runtime.Object {
ret := _m.Called()
if len(ret) == 0 {
panic("no return value specified for New")
}
var r0 runtime.Object
if rf, ok := ret.Get(0).(func() runtime.Object); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
return r0
}
// MockStorage_New_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'New'
type MockStorage_New_Call struct {
*mock.Call
}
// New is a helper method to define mock.On call
func (_e *MockStorage_Expecter) New() *MockStorage_New_Call {
return &MockStorage_New_Call{Call: _e.mock.On("New")}
}
func (_c *MockStorage_New_Call) Run(run func()) *MockStorage_New_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockStorage_New_Call) Return(_a0 runtime.Object) *MockStorage_New_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockStorage_New_Call) RunAndReturn(run func() runtime.Object) *MockStorage_New_Call {
_c.Call.Return(run)
return _c
}
// NewList provides a mock function with no fields
func (_m *MockStorage) NewList() runtime.Object {
ret := _m.Called()
if len(ret) == 0 {
panic("no return value specified for NewList")
}
var r0 runtime.Object
if rf, ok := ret.Get(0).(func() runtime.Object); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
return r0
}
// MockStorage_NewList_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NewList'
type MockStorage_NewList_Call struct {
*mock.Call
}
// NewList is a helper method to define mock.On call
func (_e *MockStorage_Expecter) NewList() *MockStorage_NewList_Call {
return &MockStorage_NewList_Call{Call: _e.mock.On("NewList")}
}
func (_c *MockStorage_NewList_Call) Run(run func()) *MockStorage_NewList_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockStorage_NewList_Call) Return(_a0 runtime.Object) *MockStorage_NewList_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockStorage_NewList_Call) RunAndReturn(run func() runtime.Object) *MockStorage_NewList_Call {
_c.Call.Return(run)
return _c
}
// Update provides a mock function with given fields: ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options
func (_m *MockStorage) Update(ctx context.Context, name string, objInfo registryrest.UpdatedObjectInfo, createValidation registryrest.ValidateObjectFunc, updateValidation registryrest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *v1.UpdateOptions) (runtime.Object, bool, error) {
ret := _m.Called(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
if len(ret) == 0 {
panic("no return value specified for Update")
}
var r0 runtime.Object
var r1 bool
var r2 error
if rf, ok := ret.Get(0).(func(context.Context, string, registryrest.UpdatedObjectInfo, registryrest.ValidateObjectFunc, registryrest.ValidateObjectUpdateFunc, bool, *v1.UpdateOptions) (runtime.Object, bool, error)); ok {
return rf(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
}
if rf, ok := ret.Get(0).(func(context.Context, string, registryrest.UpdatedObjectInfo, registryrest.ValidateObjectFunc, registryrest.ValidateObjectUpdateFunc, bool, *v1.UpdateOptions) runtime.Object); ok {
r0 = rf(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(runtime.Object)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string, registryrest.UpdatedObjectInfo, registryrest.ValidateObjectFunc, registryrest.ValidateObjectUpdateFunc, bool, *v1.UpdateOptions) bool); ok {
r1 = rf(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
} else {
r1 = ret.Get(1).(bool)
}
if rf, ok := ret.Get(2).(func(context.Context, string, registryrest.UpdatedObjectInfo, registryrest.ValidateObjectFunc, registryrest.ValidateObjectUpdateFunc, bool, *v1.UpdateOptions) error); ok {
r2 = rf(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
} else {
r2 = ret.Error(2)
}
return r0, r1, r2
}
// MockStorage_Update_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Update'
type MockStorage_Update_Call struct {
*mock.Call
}
// Update is a helper method to define mock.On call
// - ctx context.Context
// - name string
// - objInfo registryrest.UpdatedObjectInfo
// - createValidation registryrest.ValidateObjectFunc
// - updateValidation registryrest.ValidateObjectUpdateFunc
// - forceAllowCreate bool
// - options *v1.UpdateOptions
func (_e *MockStorage_Expecter) Update(ctx interface{}, name interface{}, objInfo interface{}, createValidation interface{}, updateValidation interface{}, forceAllowCreate interface{}, options interface{}) *MockStorage_Update_Call {
return &MockStorage_Update_Call{Call: _e.mock.On("Update", ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)}
}
func (_c *MockStorage_Update_Call) Run(run func(ctx context.Context, name string, objInfo registryrest.UpdatedObjectInfo, createValidation registryrest.ValidateObjectFunc, updateValidation registryrest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *v1.UpdateOptions)) *MockStorage_Update_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string), args[2].(registryrest.UpdatedObjectInfo), args[3].(registryrest.ValidateObjectFunc), args[4].(registryrest.ValidateObjectUpdateFunc), args[5].(bool), args[6].(*v1.UpdateOptions))
})
return _c
}
func (_c *MockStorage_Update_Call) Return(_a0 runtime.Object, _a1 bool, _a2 error) *MockStorage_Update_Call {
_c.Call.Return(_a0, _a1, _a2)
return _c
}
func (_c *MockStorage_Update_Call) RunAndReturn(run func(context.Context, string, registryrest.UpdatedObjectInfo, registryrest.ValidateObjectFunc, registryrest.ValidateObjectUpdateFunc, bool, *v1.UpdateOptions) (runtime.Object, bool, error)) *MockStorage_Update_Call {
_c.Call.Return(run)
return _c
}
// NewMockStorage creates a new instance of MockStorage. 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 NewMockStorage(t interface {
mock.TestingT
Cleanup(func())
}) *MockStorage {
mock := &MockStorage{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
-115
View File
@@ -1,115 +0,0 @@
package rest
import (
"context"
"errors"
"time"
"github.com/stretchr/testify/mock"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/apis/example"
"k8s.io/apiserver/pkg/registry/rest"
)
var now = time.Now()
var exampleObj = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", CreationTimestamp: metav1.Time{}, GenerateName: "foo"}, Spec: example.PodSpec{}, Status: example.PodStatus{StartTime: &metav1.Time{Time: now}}}
var anotherObj = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "2", GenerateName: "foo"}, Spec: example.PodSpec{}, Status: example.PodStatus{StartTime: &metav1.Time{Time: now}}}
var exampleList = &example.PodList{TypeMeta: metav1.TypeMeta{Kind: "foo"}, ListMeta: metav1.ListMeta{}, Items: []example.Pod{*exampleObj}}
var anotherList = &example.PodList{Items: []example.Pod{*anotherObj}}
type storageMock struct {
*mock.Mock
Storage
}
// Unified Store
func (m storageMock) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
select {
case <-ctx.Done():
return nil, errors.New("context canceled")
default:
}
args := m.Called(ctx, name, options)
if err := args.Get(1); err != nil {
return nil, err.(error)
}
return args.Get(0).(runtime.Object), args.Error(1)
}
func (m storageMock) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
select {
case <-ctx.Done():
return nil, errors.New("context canceled")
default:
}
args := m.Called(ctx, obj, createValidation, options)
if err := args.Get(1); err != nil {
return nil, err.(error)
}
return args.Get(0).(runtime.Object), args.Error(1)
}
func (m storageMock) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
select {
case <-ctx.Done():
return nil, errors.New("context canceled")
default:
}
args := m.Called(ctx, options)
if err := args.Get(1); err != nil {
return nil, err.(error)
}
return args.Get(0).(runtime.Object), args.Error(1)
}
func (m storageMock) NewList() runtime.Object {
return nil
}
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) {
select {
case <-ctx.Done():
return nil, false, errors.New("context canceled")
default:
}
args := m.Called(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
if err := args.Get(2); err != nil {
return nil, false, err.(error)
}
return args.Get(0).(runtime.Object), args.Bool(1), args.Error(2)
}
func (m storageMock) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
select {
case <-ctx.Done():
return nil, false, errors.New("context canceled")
default:
}
args := m.Called(ctx, name, deleteValidation, options)
if err := args.Get(2); err != nil {
return nil, false, err.(error)
}
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 *metainternalversion.ListOptions) (runtime.Object, error) {
select {
case <-ctx.Done():
return nil, errors.New("context canceled")
default:
}
args := m.Called(ctx, deleteValidation, options, listOptions)
if err := args.Get(1); err != nil {
return nil, err.(error)
}
return args.Get(0).(runtime.Object), args.Error(1)
}
-82
View File
@@ -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)
}
+10 -13
View File
@@ -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,
+25 -41
View File
@@ -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,
+3 -5
View File
@@ -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)
+1
View File
@@ -29,6 +29,7 @@ import (
"github.com/grafana/grafana/pkg/storage/unified/resourcepb"
)
//go:generate mockery --name ResourceClient --structname MockResourceClient --inpackage --filename client_mock.go --with-expecter
type ResourceClient interface {
resourcepb.ResourceStoreClient
resourcepb.ResourceIndexClient
File diff suppressed because it is too large Load Diff