From b1e1a4f79554037734771d7cf758ac057c3a6e38 Mon Sep 17 00:00:00 2001 From: Waynegates Date: Sat, 23 Apr 2022 08:45:36 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Improve=20the=20fake=20client=20bui?= =?UTF-8?q?lder=20with=20WithObjectTracker=20option=20(#1863)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve the fake client builder with WithObjectTracker option * Update unit test to cover WithObjectTracker * Update unit test to cover WithObjectTracker * Update unit test with goimports --- pkg/client/fake/client.go | 16 ++++++++++- pkg/client/fake/client_test.go | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/pkg/client/fake/client.go b/pkg/client/fake/client.go index 94ff93acc8..cabc4ee7cf 100644 --- a/pkg/client/fake/client.go +++ b/pkg/client/fake/client.go @@ -92,6 +92,7 @@ type ClientBuilder struct { initObject []client.Object initLists []client.ObjectList initRuntimeObjects []runtime.Object + objectTracker testing.ObjectTracker } // WithScheme sets this builder's internal scheme. @@ -128,6 +129,12 @@ func (f *ClientBuilder) WithRuntimeObjects(initRuntimeObjs ...runtime.Object) *C return f } +// WithObjectTracker can be optionally used to initialize this fake client with testing.ObjectTracker. +func (f *ClientBuilder) WithObjectTracker(ot testing.ObjectTracker) *ClientBuilder { + f.objectTracker = ot + return f +} + // Build builds and returns a new fake client. func (f *ClientBuilder) Build() client.WithWatch { if f.scheme == nil { @@ -137,7 +144,14 @@ func (f *ClientBuilder) Build() client.WithWatch { f.restMapper = meta.NewDefaultRESTMapper([]schema.GroupVersion{}) } - tracker := versionedTracker{ObjectTracker: testing.NewObjectTracker(f.scheme, scheme.Codecs.UniversalDecoder()), scheme: f.scheme} + var tracker versionedTracker + + if f.objectTracker == nil { + tracker = versionedTracker{ObjectTracker: testing.NewObjectTracker(f.scheme, scheme.Codecs.UniversalDecoder()), scheme: f.scheme} + } else { + tracker = versionedTracker{ObjectTracker: f.objectTracker, scheme: f.scheme} + } + for _, obj := range f.initObject { if err := tracker.Add(obj); err != nil { panic(fmt.Errorf("failed to add object %v to fake client: %w", obj, err)) diff --git a/pkg/client/fake/client_test.go b/pkg/client/fake/client_test.go index 3ee32d68fc..5ce93a8cdc 100644 --- a/pkg/client/fake/client_test.go +++ b/pkg/client/fake/client_test.go @@ -22,6 +22,8 @@ import ( "fmt" "time" + "k8s.io/client-go/kubernetes/fake" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -1002,4 +1004,53 @@ var _ = Describe("Fake client", func() { } Expect(retrieved).To(Equal(reference)) }) + + It("should be able to build with given tracker and get resource", func() { + clientSet := fake.NewSimpleClientset(dep) + cl := NewClientBuilder().WithRuntimeObjects(dep2).WithObjectTracker(clientSet.Tracker()).Build() + + By("Getting a deployment") + namespacedName := types.NamespacedName{ + Name: "test-deployment", + Namespace: "ns1", + } + obj := &appsv1.Deployment{} + err := cl.Get(context.Background(), namespacedName, obj) + Expect(err).To(BeNil()) + Expect(obj).To(Equal(dep)) + + By("Getting a deployment from clientSet") + csDep2, err := clientSet.AppsV1().Deployments("ns1").Get(context.Background(), "test-deployment-2", metav1.GetOptions{}) + Expect(err).To(BeNil()) + Expect(csDep2).To(Equal(dep2)) + + By("Getting a new deployment") + namespacedName3 := types.NamespacedName{ + Name: "test-deployment-3", + Namespace: "ns1", + } + + dep3 := &appsv1.Deployment{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "apps/v1", + Kind: "Deployment", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "test-deployment-3", + Namespace: "ns1", + Labels: map[string]string{ + "test-label": "label-value", + }, + ResourceVersion: trackerAddResourceVersion, + }, + } + + _, err = clientSet.AppsV1().Deployments("ns1").Create(context.Background(), dep3, metav1.CreateOptions{}) + Expect(err).To(BeNil()) + + obj = &appsv1.Deployment{} + err = cl.Get(context.Background(), namespacedName3, obj) + Expect(err).To(BeNil()) + Expect(obj).To(Equal(dep3)) + }) })