Skip to content

Commit

Permalink
test: add test rest mapper for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Aug 19, 2024
1 parent dba4f6e commit 2b9d97e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/util/testing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ func NewFakeClient(scheme *runtime.Scheme, objs ...runtime.Object) *FakeIndexCli
co = append(co, o.(client.Object))
}

fakeMapper := NewFakeMapper(scheme).(*meta.DefaultRESTMapper)
for groupVersionKind := range scheme.AllKnownTypes() {
// we just assume everything is namespace scoped, if this causes issues we can
// exclude certain resources here
fakeMapper.Add(groupVersionKind, meta.RESTScopeNamespace)
}

return &FakeIndexClient{
Client: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).WithStatusSubresource(co...).Build(),
Client: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).WithRESTMapper(fakeMapper).WithStatusSubresource(co...).Build(),
scheme: scheme,
indexFuncs: map[schema.GroupVersionKind]map[string]client.IndexerFunc{},
indexes: map[schema.GroupVersionKind]map[string]map[string][]runtime.Object{},
Expand All @@ -45,6 +52,14 @@ type FakeIndexClient struct {
indexes map[schema.GroupVersionKind]map[string]map[string][]runtime.Object
}

func (fc *FakeIndexClient) AddMapping(kind schema.GroupVersionKind, scope meta.RESTScope) {
fc.Client.RESTMapper().(*meta.DefaultRESTMapper).Add(kind, scope)
}

func (fc *FakeIndexClient) AddSpecificMapping(kind schema.GroupVersionKind, plural, singular schema.GroupVersionResource, scope meta.RESTScope) {
fc.Client.RESTMapper().(*meta.DefaultRESTMapper).AddSpecific(kind, plural, singular, scope)
}

func (fc *FakeIndexClient) updateIndices(ctx context.Context, obj runtime.Object) error {
gvk, err := apiutil.GVKForObject(obj, fc.scheme)
if err != nil {
Expand Down

0 comments on commit 2b9d97e

Please sign in to comment.