Skip to content

Commit 671b65b

Browse files
committed
test: add RESTMapper to client mock
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent fa9f877 commit 671b65b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pkg/test/fake.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type MockSubResourcePatchFn func(ctx context.Context, obj client.Object, patch c
6363
// A MockSchemeFn is used to mock client.Client's Scheme implementation.
6464
type MockSchemeFn func() *runtime.Scheme
6565

66+
// A MockRESTMapperFn is used to mock client.RESTMapper.
67+
type MockRESTMapperFn func() meta.RESTMapper
68+
6669
// A MockGroupVersionKindForFn is used to mock client.Client's GroupVersionKindFor implementation.
6770
type MockGroupVersionKindForFn func(runtime.Object) (schema.GroupVersionKind, error)
6871

@@ -201,13 +204,20 @@ func NewMockSubResourcePatchFn(err error, ofn ...ObjectFn) MockSubResourcePatchF
201204
}
202205
}
203206

204-
// NewMockSchemeFn returns a MockSchemeFn that returns the scheme
207+
// NewMockSchemeFn returns a MockSchemeFn that returns the scheme.
205208
func NewMockSchemeFn(scheme *runtime.Scheme) MockSchemeFn {
206209
return func() *runtime.Scheme {
207210
return scheme
208211
}
209212
}
210213

214+
// NewMockRESTMapperFn returns a MockRESTMapperFn that returns the RESTMapper.
215+
func NewMockRESTMapperFn(mapper meta.RESTMapper) MockRESTMapperFn {
216+
return func() meta.RESTMapper {
217+
return mapper
218+
}
219+
}
220+
211221
// NewMockGroupVersionKindForFn returns a MockGroupVersionKindForFn that returns the supplied GVK and error.
212222
func NewMockGroupVersionKindForFn(err error, gvk schema.GroupVersionKind, rofn ...RuntimeObjectFn) MockGroupVersionKindForFn {
213223
return func(obj runtime.Object) (schema.GroupVersionKind, error) {
@@ -257,6 +267,7 @@ type MockClient struct {
257267
MockScheme MockSchemeFn
258268
MockGroupVersionKindFor MockGroupVersionKindForFn
259269
MockIsObjectNamespaced MockIsObjectNamespacedFn
270+
MockRESTMapper MockRESTMapperFn
260271
}
261272

262273
// NewMockClient returns a MockClient that does nothing when its methods are
@@ -277,6 +288,7 @@ func NewMockClient() *MockClient {
277288
MockScheme: NewMockSchemeFn(nil),
278289
MockGroupVersionKindFor: NewMockGroupVersionKindForFn(nil, schema.GroupVersionKind{}),
279290
MockIsObjectNamespaced: NewMockIsObjectNamespacedFn(nil, false),
291+
MockRESTMapper: NewMockRESTMapperFn(nil),
280292
}
281293
}
282294

@@ -336,7 +348,7 @@ func (c *MockClient) SubResource(_ string) client.SubResourceClient {
336348

337349
// RESTMapper returns the REST mapper.
338350
func (c *MockClient) RESTMapper() meta.RESTMapper {
339-
return nil
351+
return c.MockRESTMapper()
340352
}
341353

342354
// Scheme calls MockClient's MockScheme function

0 commit comments

Comments
 (0)