Skip to content

Commit

Permalink
✨ Improve the fake client builder with WithObjectTracker option (#1863)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Waynegates authored Apr 23, 2022
1 parent c162794 commit b1e1a4f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand Down
51 changes: 51 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
})
})

0 comments on commit b1e1a4f

Please sign in to comment.