From ffc8dce868fbea00f10de8172acf484e95298efd Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Fri, 23 Feb 2024 09:44:16 +0100 Subject: [PATCH] testing: add reader and cache mock Signed-off-by: Dr. Stefan Schimanski --- pkg/test/fake.go | 106 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/pkg/test/fake.go b/pkg/test/fake.go index c9f4b1c1b..f168c7e2b 100644 --- a/pkg/test/fake.go +++ b/pkg/test/fake.go @@ -22,6 +22,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -232,13 +233,30 @@ func NewMockIsObjectNamespacedFn(err error, isNamespaced bool, rofn ...RuntimeOb } } +// MockClientReader implements controller-runtime's client.Reader interface, +// allowing each method to be overridden for testing. +type MockClientReader struct { + MockGet MockGetFn + MockList MockListFn +} + +// Get calls MockClient's MockGet function. +func (c *MockClientReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error { + return c.MockGet(ctx, key, obj) +} + +// List calls MockClient's MockList function. +func (c *MockClientReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { + return c.MockList(ctx, list, opts...) +} + // MockClient implements controller-runtime's Client interface, allowing each // method to be overridden for testing. The controller-runtime provides a fake // client, but it is has surprising side effects (e.g. silently calling // os.Exit(1)) and does not allow us control over the errors it returns. type MockClient struct { - MockGet MockGetFn - MockList MockListFn + MockClientReader + MockCreate MockCreateFn MockDelete MockDeleteFn MockDeleteAllOf MockDeleteAllOfFn @@ -263,8 +281,10 @@ type MockClient struct { // called. func NewMockClient() *MockClient { return &MockClient{ - MockGet: NewMockGetFn(nil), - MockList: NewMockListFn(nil), + MockClientReader: MockClientReader{ + MockGet: NewMockGetFn(nil), + MockList: NewMockListFn(nil), + }, MockCreate: NewMockCreateFn(nil), MockDelete: NewMockDeleteFn(nil), MockDeleteAllOf: NewMockDeleteAllOfFn(nil), @@ -280,16 +300,6 @@ func NewMockClient() *MockClient { } } -// Get calls MockClient's MockGet function. -func (c *MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error { - return c.MockGet(ctx, key, obj) -} - -// List calls MockClient's MockList function. -func (c *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { - return c.MockList(ctx, list, opts...) -} - // Create calls MockClient's MockCreate function. func (c *MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { return c.MockCreate(ctx, obj, opts...) @@ -381,3 +391,71 @@ func (m *MockSubResourceClient) Update(ctx context.Context, obj client.Object, o func (m *MockSubResourceClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error { return m.MockPatch(ctx, obj, patch, opts...) } + +var _ cache.Cache = &MockCache{} + +type MockCache struct { + MockClientReader + MockInformers +} + +// MockGetInformerFn is used to mock cache.Informers' GetInformer implementation. +type MockGetInformerFn func(ctx context.Context, obj client.Object, opts ...cache.InformerGetOption) (cache.Informer, error) + +// MockGetInformerForKindFn is used to mock cache.Informers' GetInformerForKind implementation. +type MockGetInformerForKindFn func(ctx context.Context, gvk schema.GroupVersionKind, opts ...cache.InformerGetOption) (cache.Informer, error) + +// MockRemoveInformersFn is used to mock cache.Informers' RemoveInformer implementation. +type MockRemoveInformersFn func(ctx context.Context, obj client.Object) error + +// MockStartFn is used to mock cache.Informers' Start implementation. +type MockStartFn func(ctx context.Context) error + +// MockWaitForCacheSyncFn is used to mock cache.Informers' WaitForCacheSync implementation. +type MockWaitForCacheSyncFn func(ctx context.Context) bool + +type MockInformers struct { + MockFieldIndexer + + MockGetInformer MockGetInformerFn + MockGetInformerForKind MockGetInformerForKindFn + MockRemoveInformer MockRemoveInformersFn + MockStart MockStartFn + MockWaitForCacheSync MockWaitForCacheSyncFn +} + +// GetInformer calls MockInformers' MockGetInformer function. +func (m MockCache) GetInformer(ctx context.Context, obj client.Object, opts ...cache.InformerGetOption) (cache.Informer, error) { + return m.MockGetInformer(ctx, obj, opts...) +} + +// GetInformerForKind calls MockInformers' MockGetInformerForKind function. +func (m MockCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...cache.InformerGetOption) (cache.Informer, error) { + return m.MockGetInformerForKind(ctx, gvk, opts...) +} + +// RemoveInformer calls MockInformers' MockRemoveInformer function. +func (m MockCache) RemoveInformer(ctx context.Context, obj client.Object) error { + return m.MockRemoveInformer(ctx, obj) +} + +// Start calls MockInformers' MockStart function. +func (m MockCache) Start(ctx context.Context) error { + return m.MockStart(ctx) +} + +// WaitForCacheSync calls MockInformers' MockWaitForCacheSync function. +func (m MockCache) WaitForCacheSync(ctx context.Context) bool { + return m.MockWaitForCacheSync(ctx) +} + +// MockIndexFieldFn is used to mock cache.Cache's IndexField implementation. +type MockIndexFieldFn func(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error + +type MockFieldIndexer struct { + MockIndexField MockIndexFieldFn +} + +func (m MockCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error { + return m.MockIndexField(ctx, obj, field, extractValue) +}