code-generator icon indicating copy to clipboard operation
code-generator copied to clipboard

fake clients return nil on failed Get, real clients return an empty object

Open danwinship opened this issue 2 years ago • 2 comments

If you use a fake client to Get() an object that doesn't exist, it returns nil and an error:

func (c *FakeEndpoints) Get(ctx context.Context, name string, options metav1.GetOptions) (result *v1.Endpoints, err error) {
	obj, err := c.Fake.
		Invokes(testing.NewGetAction(endpointsResource, c.ns, name), &v1.Endpoints{})

	if obj == nil {
		return nil, err
	}
	return obj.(*v1.Endpoints), err
}

but if you use a real client, it returns an empty object in that case:

func (c *endpoints) Get(ctx context.Context, name string, options metav1.GetOptions) (result *v1.Endpoints, err error) {
	result = &v1.Endpoints{}
	err = c.client.Get().
		Namespace(c.ns).
		Resource("endpoints").
		Name(name).
		VersionedParams(&options, scheme.ParameterCodec).
		Do(ctx).
		Into(result)
	return
}

meaning code may behave one way under unit tests but then behave differently in e2e tests / production

danwinship avatar May 02 '23 17:05 danwinship