Skip to content

🐛 Fakeclient: Don't return items on invalid selector #3022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,21 +599,22 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
return err
}
zero(obj)
if err := json.Unmarshal(j, obj); err != nil {
objCopy := obj.DeepCopyObject().(client.ObjectList)
if err := json.Unmarshal(j, objCopy); err != nil {
return err
}

objs, err := meta.ExtractList(objCopy)
if err != nil {
return err
}

if listOpts.LabelSelector == nil && listOpts.FieldSelector == nil {
return nil
return meta.SetList(obj, objs)
}

// If we're here, either a label or field selector are specified (or both), so before we return
// the list we must filter it. If both selectors are set, they are ANDed.
objs, err := meta.ExtractList(obj)
if err != nil {
return err
}

filteredList, err := c.filterList(objs, gvk, listOpts.LabelSelector, listOpts.FieldSelector)
if err != nil {
return err
Expand Down
12 changes: 9 additions & 3 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,16 +1319,20 @@ var _ = Describe("Fake client", func() {
listOpts := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("key", "val"),
}
err := cl.List(context.Background(), &corev1.ConfigMapList{}, listOpts)
list := &corev1.ConfigMapList{}
err := cl.List(context.Background(), list, listOpts)
Expect(err).To(HaveOccurred())
Expect(list.Items).To(BeEmpty())
})

It("errors when there's no Index matching the field name", func() {
listOpts := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.paused", "false"),
}
err := cl.List(context.Background(), &appsv1.DeploymentList{}, listOpts)
list := &appsv1.DeploymentList{}
err := cl.List(context.Background(), list, listOpts)
Expect(err).To(HaveOccurred())
Expect(list.Items).To(BeEmpty())
})

It("errors when field selector uses two requirements", func() {
Expand All @@ -1337,8 +1341,10 @@ var _ = Describe("Fake client", func() {
fields.OneTermEqualSelector("spec.replicas", "1"),
fields.OneTermEqualSelector("spec.strategy.type", string(appsv1.RecreateDeploymentStrategyType)),
)}
err := cl.List(context.Background(), &appsv1.DeploymentList{}, listOpts)
list := &appsv1.DeploymentList{}
err := cl.List(context.Background(), list, listOpts)
Expect(err).To(HaveOccurred())
Expect(list.Items).To(BeEmpty())
})

It("returns two deployments that match the only field selector requirement", func() {
Expand Down
Loading