Skip to content

Commit

Permalink
fake client: preserve TypeMeta during List call with UnstructuredList
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Jan 16, 2025
1 parent 1ec7c1b commit 3b23354
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
return err
}

if _, isUnstructured := obj.(runtime.Unstructured); isUnstructured {
ta, err := meta.TypeAccessor(obj)
if err != nil {
return err
}
ta.SetKind(originalKind)
ta.SetAPIVersion(gvk.GroupVersion().String())
}

objs, err := meta.ExtractList(objCopy)
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("DeploymentList")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("apps/v1"))
Expect(list.GetKind()).To(Equal("DeploymentList"))
Expect(list.Items).To(HaveLen(2))
})

Expand All @@ -167,6 +169,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("Deployment")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("apps/v1"))
Expect(list.GetKind()).To(Equal("Deployment"))
Expect(list.Items).To(HaveLen(2))
})

Expand All @@ -178,6 +182,8 @@ var _ = Describe("Fake client", func() {
list.SetKind("EndpointsList")
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).ToNot(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("v1"))
Expect(list.GetKind()).To(Equal("EndpointsList"))
Expect(list.Items).To(HaveLen(1))
}

Expand Down Expand Up @@ -247,6 +253,8 @@ var _ = Describe("Fake client", func() {
list.SetAPIVersion("custom/v3")
list.SetKind("ImageList")
err := cl.List(context.Background(), list)
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("custom/v3"))
Expect(list.GetKind()).To(Equal("ImageList"))
Expect(err).ToNot(HaveOccurred())
})

Expand All @@ -255,6 +263,8 @@ var _ = Describe("Fake client", func() {
list.SetAPIVersion("custom/v4")
list.SetKind("Image")
err := cl.List(context.Background(), list)
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("custom/v4"))
Expect(list.GetKind()).To(Equal("Image"))
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -1325,6 +1335,20 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(BeEmpty())
})

It("errors when there's no Index for the GroupVersionResource with UnstructuredList", func() {
listOpts := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("key", "val"),
}
list := &unstructured.UnstructuredList{}
list.SetAPIVersion("v1")
list.SetKind("ConfigMapList")
err := cl.List(context.Background(), list, listOpts)
Expect(err).To(HaveOccurred())
Expect(list.GroupVersionKind().GroupVersion().String()).To(Equal("v1"))
Expect(list.GetKind()).To(Equal("ConfigMapList"))
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"),
Expand Down

0 comments on commit 3b23354

Please sign in to comment.