Documentation
¶
Overview ¶
Package fake provides a fake client for testing.
A fake client is backed by its simple object store indexed by GroupVersionResource. You can create a fake client with optional objects.
client := NewClientBuilder().WithScheme(scheme).WithObjects(initObjs...).Build()
You can invoke the methods defined in the Client interface.
When in doubt, it's almost always better not to use this package and instead use envtest.Environment with a real client and API server.
WARNING: ⚠️ Current Limitations / Known Issues with the fake Client ⚠️
- This client does not have a way to inject specific errors to test handled vs. unhandled errors.
- There is some support for sub resources which can cause issues with tests if you're trying to update e.g. metadata and status in the same reconcile.
- No OpenAPI validation is performed when creating or updating objects.
- ObjectMeta's `Generation` and `ResourceVersion` don't behave properly, Patch or Update operations that rely on these fields will fail, or give false positives.
Index ¶
- func AddIndex(c client.Client, obj runtime.Object, field string, ...) error
- func NewFakeClient(initObjs ...runtime.Object) client.WithWatch
- type ClientBuilder
- func (f *ClientBuilder) Build() client.WithWatch
- func (f *ClientBuilder) WithIndex(obj runtime.Object, field string, extractValue client.IndexerFunc) *ClientBuilder
- func (f *ClientBuilder) WithInterceptorFuncs(interceptorFuncs interceptor.Funcs) *ClientBuilder
- func (f *ClientBuilder) WithLists(initLists ...client.ObjectList) *ClientBuilder
- func (f *ClientBuilder) WithObjectTracker(ot testing.ObjectTracker) *ClientBuilder
- func (f *ClientBuilder) WithObjects(initObjs ...client.Object) *ClientBuilder
- func (f *ClientBuilder) WithRESTMapper(restMapper meta.RESTMapper) *ClientBuilder
- func (f *ClientBuilder) WithRuntimeObjects(initRuntimeObjs ...runtime.Object) *ClientBuilder
- func (f *ClientBuilder) WithScheme(scheme *runtime.Scheme) *ClientBuilder
- func (f *ClientBuilder) WithStatusSubresource(o ...client.Object) *ClientBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddIndex ¶ added in v0.20.0
func AddIndex(c client.Client, obj runtime.Object, field string, extractValue client.IndexerFunc) error
AddIndex adds an index to a fake client. It will panic if used with a client that is not a fake client. It will error if there is already an index for given object with the same name as field.
It can be used to test code that adds indexes to the cache at runtime.
Types ¶
type ClientBuilder ¶ added in v0.7.0
type ClientBuilder struct {
// contains filtered or unexported fields
}
ClientBuilder builds a fake client.
func NewClientBuilder ¶ added in v0.7.0
func NewClientBuilder() *ClientBuilder
NewClientBuilder returns a new builder to create a fake client.
func (*ClientBuilder) Build ¶ added in v0.7.0
func (f *ClientBuilder) Build() client.WithWatch
Build builds and returns a new fake client.
func (*ClientBuilder) WithIndex ¶ added in v0.14.0
func (f *ClientBuilder) WithIndex(obj runtime.Object, field string, extractValue client.IndexerFunc) *ClientBuilder
WithIndex can be optionally used to register an index with name `field` and indexer `extractValue` for API objects of the same GroupVersionKind (GVK) as `obj` in the fake client. It can be invoked multiple times, both with objects of the same GVK or different ones. Invoking WithIndex twice with the same `field` and GVK (via `obj`) arguments will panic. WithIndex retrieves the GVK of `obj` using the scheme registered via WithScheme if WithScheme was previously invoked, the default scheme otherwise.
func (*ClientBuilder) WithInterceptorFuncs ¶ added in v0.15.0
func (f *ClientBuilder) WithInterceptorFuncs(interceptorFuncs interceptor.Funcs) *ClientBuilder
WithInterceptorFuncs configures the client methods to be intercepted using the provided interceptor.Funcs.
func (*ClientBuilder) WithLists ¶ added in v0.7.0
func (f *ClientBuilder) WithLists(initLists ...client.ObjectList) *ClientBuilder
WithLists can be optionally used to initialize this fake client with client.ObjectList(s).
func (*ClientBuilder) WithObjectTracker ¶ added in v0.12.0
func (f *ClientBuilder) WithObjectTracker(ot testing.ObjectTracker) *ClientBuilder
WithObjectTracker can be optionally used to initialize this fake client with testing.ObjectTracker.
func (*ClientBuilder) WithObjects ¶ added in v0.7.0
func (f *ClientBuilder) WithObjects(initObjs ...client.Object) *ClientBuilder
WithObjects can be optionally used to initialize this fake client with client.Object(s).
func (*ClientBuilder) WithRESTMapper ¶ added in v0.10.3
func (f *ClientBuilder) WithRESTMapper(restMapper meta.RESTMapper) *ClientBuilder
WithRESTMapper sets this builder's restMapper. The restMapper is directly set as mapper in the Client. This can be used for example with a meta.DefaultRESTMapper to provide a static rest mapping. If not set, defaults to an empty meta.DefaultRESTMapper.
func (*ClientBuilder) WithRuntimeObjects ¶ added in v0.7.0
func (f *ClientBuilder) WithRuntimeObjects(initRuntimeObjs ...runtime.Object) *ClientBuilder
WithRuntimeObjects can be optionally used to initialize this fake client with runtime.Object(s).
func (*ClientBuilder) WithScheme ¶ added in v0.7.0
func (f *ClientBuilder) WithScheme(scheme *runtime.Scheme) *ClientBuilder
WithScheme sets this builder's internal scheme. If not set, defaults to client-go's global scheme.Scheme.
func (*ClientBuilder) WithStatusSubresource ¶ added in v0.15.0
func (f *ClientBuilder) WithStatusSubresource(o ...client.Object) *ClientBuilder
WithStatusSubresource configures the passed object with a status subresource, which means calls to Update and Patch will not alter its status.