Skip to content

Commit

Permalink
Remove fake.DynamicClient in favor of using the various fake Reactors
Browse files Browse the repository at this point in the history
The functionality provided by fake.DynamicClient is replaced by the
FailingReactor et al.

Fixes #689

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis authored and sridhargaddam committed Nov 17, 2023
1 parent b6bf998 commit 0dc49f4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 314 deletions.
247 changes: 0 additions & 247 deletions pkg/fake/dynamic_client.go

This file was deleted.

41 changes: 25 additions & 16 deletions pkg/federate/federator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
"github.com/submariner-io/admiral/pkg/fake"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/syncer/test"
assert "github.com/submariner-io/admiral/pkg/test"
"github.com/submariner-io/admiral/pkg/util"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
dynamicfake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/scheme"
)

Expand Down Expand Up @@ -113,7 +116,7 @@ func testCreateOrUpdateFederator() {

Context("and create fails", func() {
BeforeEach(func() {
t.resourceClient.FailOnCreate = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "create", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand All @@ -127,8 +130,10 @@ func testCreateOrUpdateFederator() {
test.CreateResource(t.resourceClient, t.resource)
t.resource = test.NewPodWithImage(test.LocalNamespace, "apache")

t.resourceClient.FailOnGet = apierrors.NewNotFound(schema.GroupResource{}, t.resource.GetName())
t.resourceClient.FailOnCreate = apierrors.NewAlreadyExists(schema.GroupResource{}, t.resource.GetName())
fake.FailOnAction(&t.dynClient.Fake, "pods", "get",
apierrors.NewNotFound(schema.GroupResource{}, t.resource.GetName()), true)
fake.FailOnAction(&t.dynClient.Fake, "pods", "create", apierrors.NewAlreadyExists(schema.GroupResource{},
t.resource.GetName()), true)
})

It("should update the resource", func() {
Expand All @@ -152,7 +157,8 @@ func testCreateOrUpdateFederator() {

Context("and update initially fails due to conflict", func() {
BeforeEach(func() {
t.resourceClient.FailOnUpdate = apierrors.NewConflict(schema.GroupResource{}, "", errors.New("fake"))
fake.FailOnAction(&t.dynClient.Fake, "pods", "update",
apierrors.NewConflict(schema.GroupResource{}, "", errors.New("fake")), true)
})

It("should retry until it succeeds", func() {
Expand All @@ -163,7 +169,7 @@ func testCreateOrUpdateFederator() {

Context("and update fails not due to conflict", func() {
BeforeEach(func() {
t.resourceClient.FailOnUpdate = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "update", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand All @@ -174,7 +180,7 @@ func testCreateOrUpdateFederator() {

When("retrieval to find an existing resource in the datastore fails", func() {
BeforeEach(func() {
t.resourceClient.FailOnGet = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "get", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand Down Expand Up @@ -219,7 +225,7 @@ func testCreateFederator() {

Context("and create fails", func() {
BeforeEach(func() {
t.resourceClient.FailOnCreate = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "create", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand All @@ -236,7 +242,7 @@ func testCreateFederator() {

It("should succeed and not update the resource", func() {
Expect(f.Distribute(ctx, test.NewPodWithImage(test.LocalNamespace, "apache"))).To(Succeed())
t.verifyResource()
assert.EnsureNoActionsForResource(&t.dynClient.Fake, "pods", "update")
})
})
}
Expand Down Expand Up @@ -272,7 +278,8 @@ func testUpdateFederator() {

Context("and update initially fails due to conflict", func() {
BeforeEach(func() {
t.resourceClient.FailOnUpdate = apierrors.NewConflict(schema.GroupResource{}, "", errors.New("fake"))
fake.FailOnAction(&t.dynClient.Fake, "pods", "update", apierrors.NewConflict(schema.GroupResource{}, "",
errors.New("fake")), true)
})

It("should retry until it succeeds", func() {
Expand All @@ -282,7 +289,7 @@ func testUpdateFederator() {

Context("and retrieval to find the existing resource fails", func() {
BeforeEach(func() {
t.resourceClient.FailOnGet = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "get", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand All @@ -293,7 +300,7 @@ func testUpdateFederator() {

Context("and update fails not due to conflict", func() {
BeforeEach(func() {
t.resourceClient.FailOnUpdate = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "update", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand Down Expand Up @@ -419,7 +426,7 @@ func testDelete() {

Context("and delete fails", func() {
BeforeEach(func() {
t.resourceClient.FailOnDelete = apierrors.NewServiceUnavailable("fake")
fake.FailOnAction(&t.dynClient.Fake, "pods", "delete", apierrors.NewServiceUnavailable("fake"), false)
})

It("should return an error", func() {
Expand Down Expand Up @@ -455,8 +462,8 @@ type testDriver struct {
federatorNamespace string
targetNamespace string
keepMetadataFields []string
dynClient *fake.DynamicClient
resourceClient *fake.DynamicResourceClient
dynClient *dynamicfake.FakeDynamicClient
resourceClient dynamic.ResourceInterface
restMapper meta.RESTMapper
initObjs []runtime.Object
}
Expand All @@ -471,9 +478,11 @@ func newTestDriver() *testDriver {

var gvr *schema.GroupVersionResource

t.dynClient = fake.NewDynamicClient(scheme.Scheme, test.PrepInitialClientObjs("", t.localClusterID, t.initObjs...)...)
t.dynClient = dynamicfake.NewSimpleDynamicClient(scheme.Scheme, test.PrepInitialClientObjs("", t.localClusterID, t.initObjs...)...)
fake.AddBasicReactors(&t.dynClient.Fake)

t.restMapper, gvr = test.GetRESTMapperAndGroupVersionResourceFor(t.resource)
t.resourceClient, _ = t.dynClient.Resource(*gvr).Namespace(t.targetNamespace).(*fake.DynamicResourceClient)
t.resourceClient = t.dynClient.Resource(*gvr).Namespace(t.targetNamespace)

return t
}
Expand Down
Loading

0 comments on commit 0dc49f4

Please sign in to comment.