From 3b2335464e4e3286f2cdadff08ab7474d7dffdbe Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Thu, 16 Jan 2025 13:43:32 +0100 Subject: [PATCH] fake client: preserve TypeMeta during List call with UnstructuredList MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- pkg/client/fake/client.go | 9 +++++++++ pkg/client/fake/client_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/client/fake/client.go b/pkg/client/fake/client.go index 911f1a7b05..0c4300d548 100644 --- a/pkg/client/fake/client.go +++ b/pkg/client/fake/client.go @@ -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 diff --git a/pkg/client/fake/client_test.go b/pkg/client/fake/client_test.go index b5b88f28bd..db768cca37 100644 --- a/pkg/client/fake/client_test.go +++ b/pkg/client/fake/client_test.go @@ -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)) }) @@ -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)) }) @@ -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)) } @@ -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()) }) @@ -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()) }) @@ -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"),