Skip to content

Commit 537ffeb

Browse files
authored
Add missing WithCreateFunction to the test harness (#15)
This does not make any changes to the autoscaler, but the test harness that is used as dependency in other projects.
1 parent 4df7fc4 commit 537ffeb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/harness/fake_client.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type GetFn func(ctx context.Context, key client.ObjectKey, obj client.Object, op
2727
// ListFn is the client.Client.List function signature
2828
type ListFn func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
2929

30+
// CreateFn is the client.Client.Create function signature
31+
type CreateFn func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
32+
3033
// UpdateFn is the client.Client.Update function signature
3134
type UpdateFn func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
3235

@@ -48,6 +51,7 @@ type FakeClient struct {
4851
client.Client
4952
getFunctions map[reflect.Type]map[string]GetFn
5053
listFunctions map[reflect.Type]ListFn
54+
createFunctions map[reflect.Type]map[string]CreateFn
5155
updateFunctions map[reflect.Type]map[string]UpdateFn
5256
statusUpdateFunctions map[reflect.Type]map[string]UpdateSubResourceFn
5357
deleteFn map[reflect.Type]map[string]DeleteFn
@@ -112,6 +116,31 @@ func (f *FakeClient) List(ctx context.Context, list client.ObjectList, opts ...c
112116
return f.Client.List(ctx, list, opts...)
113117
}
114118

119+
// WithCreateFunction registers a custom Create function for the given object.
120+
func (f *FakeClient) WithCreateFunction(container GenericObjectContainer, fn CreateFn) *FakeClient {
121+
if f.createFunctions == nil {
122+
f.createFunctions = make(map[reflect.Type]map[string]CreateFn)
123+
}
124+
if f.createFunctions[reflect.TypeOf(container.ClientObject())] == nil {
125+
f.createFunctions[reflect.TypeOf(container.ClientObject())] = make(map[string]CreateFn)
126+
}
127+
f.createFunctions[reflect.TypeOf(container.ClientObject())][container.ObjectKey().String()] = fn
128+
return f
129+
}
130+
131+
// Create calls the custom Create function if it was registered for the given object.
132+
// Otherwise, it calls the original client.Client.Create function.
133+
func (f *FakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
134+
if f.createFunctions != nil {
135+
if functions, ok := f.createFunctions[reflect.TypeOf(obj)]; ok {
136+
if fn, ok := functions[client.ObjectKeyFromObject(obj).String()]; ok {
137+
return fn(ctx, obj, opts...)
138+
}
139+
}
140+
}
141+
return f.Client.Create(ctx, obj, opts...)
142+
}
143+
115144
// WithUpdateFunction registers a custom Update function for the given object.
116145
func (f *FakeClient) WithUpdateFunction(container GenericObjectContainer, fn UpdateFn) *FakeClient {
117146
if f.updateFunctions == nil {

0 commit comments

Comments
 (0)