Skip to content

Commit

Permalink
testing: add reader and cache mock
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Feb 23, 2024
1 parent 9f2041d commit ffc8dce
Showing 1 changed file with 92 additions and 14 deletions.
106 changes: 92 additions & 14 deletions pkg/test/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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...)
Expand Down Expand Up @@ -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 {

Check warning on line 397 in pkg/test/fake.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type MockCache should have comment or be unexported (revive)
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 {

Check warning on line 417 in pkg/test/fake.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type MockInformers should have comment or be unexported (revive)
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 {

Check warning on line 455 in pkg/test/fake.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type MockFieldIndexer should have comment or be unexported (revive)
MockIndexField MockIndexFieldFn
}

func (m MockCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {

Check warning on line 459 in pkg/test/fake.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method MockCache.IndexField should have comment or be unexported (revive)
return m.MockIndexField(ctx, obj, field, extractValue)
}

0 comments on commit ffc8dce

Please sign in to comment.