Skip to content

Commit

Permalink
Remove ToUnstructured from syncer/test
Browse files Browse the repository at this point in the history
...in lieu of resource.MustToUnstructured.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Nov 17, 2023
1 parent 04321bc commit 5cddce5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pkg/fake/delete_colection_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/syncer/test"
"github.com/submariner-io/admiral/pkg/resource"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (r *DeleteCollectionReactor) react(action testing.Action) (bool, runtime.Ob
return true, nil, err
}

raw := test.ToUnstructured(obj)
raw := resource.MustToUnstructured(obj)
items, found, err := unstructured.NestedSlice(raw.Object, "items")
Expect(err).To(Succeed())
Expect(found).To(BeTrue())
Expand Down
5 changes: 2 additions & 3 deletions pkg/resource/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
)

Expand Down Expand Up @@ -56,9 +57,7 @@ func MustToUnstructured(from runtime.Object) *unstructured.Unstructured {

func MustToUnstructuredUsingScheme(from runtime.Object, usingScheme *runtime.Scheme) *unstructured.Unstructured {
u, err := ToUnstructuredUsingScheme(from, usingScheme)
if err != nil {
panic(err)
}
utilruntime.Must(err)

return u
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/syncer/resource_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ func testTransformFunction() {
})

verifyDistribute := func() {
d.federator.VerifyDistribute(test.SetClusterIDLabel(test.ToUnstructured(transformed), "remote"))
d.federator.VerifyDistribute(test.SetClusterIDLabel(resource.MustToUnstructured(transformed), "remote"))
}

verifyDelete := func() {
d.federator.VerifyDelete(test.SetClusterIDLabel(test.ToUnstructured(transformed), "remote"))
d.federator.VerifyDelete(test.SetClusterIDLabel(resource.MustToUnstructured(transformed), "remote"))
}

When("a resource is created in the datastore", func() {
Expand Down Expand Up @@ -399,7 +399,7 @@ func testTransformFunction() {

d.resource = test.NewPodWithImage(d.config.SourceNamespace, "updated")
test.UpdateResource(d.sourceClient, test.NewPodWithImage(d.config.SourceNamespace, "updated"))
d.federator.VerifyDistribute(test.ToUnstructured(transformed))
d.federator.VerifyDistribute(resource.MustToUnstructured(transformed))
Eventually(expOperation).Should(Receive(Equal(syncer.Update)))
})
})
Expand Down Expand Up @@ -630,7 +630,7 @@ func testOnSuccessfulSyncFunction() {

It("should invoke the OnSuccessfulSync function with the transformed resource", func() {
test.CreateResource(d.sourceClient, d.resource)
d.federator.VerifyDistribute(test.ToUnstructured(expResource))
d.federator.VerifyDistribute(resource.MustToUnstructured(expResource))
Eventually(expOperation).Should(Receive(Equal(syncer.Create)))
})
})
Expand Down Expand Up @@ -883,7 +883,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})

Expand All @@ -894,7 +894,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})
})
Expand Down Expand Up @@ -931,7 +931,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})

Expand All @@ -941,7 +941,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})
})
Expand All @@ -960,7 +960,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})
})
Expand All @@ -976,7 +976,7 @@ func testUpdateSuppression() {
})

It("should distribute it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(d.resource))
d.federator.VerifyDistribute(resource.MustToUnstructured(d.resource))
})
})

Expand Down Expand Up @@ -1152,11 +1152,11 @@ func testRequeueResource() {
})

It("should requeue it", func() {
d.federator.VerifyDistribute(test.ToUnstructured(transformed))
d.federator.VerifyDistribute(resource.MustToUnstructured(transformed))

d.syncer.RequeueResource(d.resource.Name, d.resource.Namespace)

d.federator.VerifyDistribute(test.ToUnstructured(transformed))
d.federator.VerifyDistribute(resource.MustToUnstructured(transformed))
})
})

Expand Down Expand Up @@ -1257,7 +1257,7 @@ func newTestDriver(sourceNamespace, localClusterID string, syncDirection syncer.
}

func (t *testDriver) addInitialResource(obj runtime.Object) {
t.initialResources = append(t.initialResources, test.ToUnstructured(obj))
t.initialResources = append(t.initialResources, resource.MustToUnstructured(obj))
}

func (t *testDriver) verifyDistributeOnCreateTest(clusterID string) {
Expand Down
11 changes: 2 additions & 9 deletions pkg/syncer/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object
}

func CreateResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object) *unstructured.Unstructured {
u := ToUnstructured(obj)
u := resource.MustToUnstructured(obj)
u.SetResourceVersion("")

created, err := resourceInterface.Create(context.TODO(), u, metav1.CreateOptions{})
Expand Down Expand Up @@ -181,7 +181,7 @@ func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Obje
newObjs := make([]runtime.Object, 0, len(initObjs))

for _, obj := range initObjs {
raw := ToUnstructured(obj)
raw := resource.MustToUnstructured(obj)
raw.SetUID(uuid.NewUUID())
raw.SetResourceVersion("1")

Expand All @@ -205,13 +205,6 @@ func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Obje
return newObjs
}

func ToUnstructured(obj runtime.Object) *unstructured.Unstructured {
raw, err := resource.ToUnstructured(obj)
Expect(err).To(Succeed())

return raw
}

func SetClusterIDLabel(obj runtime.Object, clusterID string) runtime.Object {
meta, err := metaapi.Accessor(obj)
Expect(err).To(Succeed())
Expand Down
9 changes: 5 additions & 4 deletions pkg/util/create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ var _ = Describe("CreateOrUpdate function", func() {
t := newCreateOrUpdateTestDiver()

createOrUpdate := func(expResult util.OperationResult) error {
result, err := util.CreateOrUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn)
result, err := util.CreateOrUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client),
resource.MustToUnstructured(t.pod), t.mutateFn)
if err != nil && expResult != util.OperationResultNone {
return err
}
Expand Down Expand Up @@ -246,7 +247,7 @@ var _ = Describe("Update function", func() {
t := newCreateOrUpdateTestDiver()

update := func() error {
return util.Update[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn)
return util.Update[runtime.Object](context.TODO(), resource.ForDynamic(t.client), resource.MustToUnstructured(t.pod), t.mutateFn)
}

When("the resource doesn't exist", func() {
Expand All @@ -268,7 +269,7 @@ var _ = Describe("MustUpdate function", func() {
t := newCreateOrUpdateTestDiver()

mustUpdate := func() error {
return util.MustUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), test.ToUnstructured(t.pod), t.mutateFn)
return util.MustUpdate[runtime.Object](context.TODO(), resource.ForDynamic(t.client), resource.MustToUnstructured(t.pod), t.mutateFn)
}

When("the resource doesn't exist", func() {
Expand Down Expand Up @@ -318,7 +319,7 @@ func newCreateOrUpdateTestDiver() *createOrUpdateTestDriver {
})

t.mutateFn = func(existing runtime.Object) (runtime.Object, error) {
obj := test.ToUnstructured(t.pod)
obj := resource.MustToUnstructured(t.pod)
obj.SetUID(resource.MustToMeta(existing).GetUID())
return util.Replace(obj)(nil)
}
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/syncer/test"
"github.com/submariner-io/admiral/pkg/resource"
testV1 "github.com/submariner-io/admiral/test/apis/admiral.submariner.io/v1"
"github.com/submariner-io/shipyard/test/e2e/framework"
metaapi "k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -48,16 +48,16 @@ func DeleteAllToasters(client dynamic.Interface, namespace, clusterName string)
DeleteAllOf(client, ToasterGVR(), namespace, clusterName)
}

func DeleteAllOf(client dynamic.Interface, gvr *schema.GroupVersionResource, namespace, clusterName string) {
func DeleteAllOf(dynClient dynamic.Interface, gvr *schema.GroupVersionResource, namespace, clusterName string) {
By(fmt.Sprintf("Deleting all %s in namespace %q from %q", gvr.Resource, namespace, clusterName))

resource := client.Resource(*gvr).Namespace(namespace)
Expect(resource.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})).To(Succeed())
client := dynClient.Resource(*gvr).Namespace(namespace)
Expect(client.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})).To(Succeed())

framework.AwaitUntil(fmt.Sprintf("list %s in namespace %q from %q", gvr.Resource, namespace, clusterName), func() (i interface{},
e error,
) {
return resource.List(context.TODO(), metav1.ListOptions{})
return client.List(context.TODO(), metav1.ListOptions{})
}, func(result interface{}) (bool, string, error) {
list, ok := result.(*unstructured.UnstructuredList)
Expect(ok).To(BeTrue())
Expand All @@ -73,7 +73,7 @@ func DeleteAllOf(client dynamic.Interface, gvr *schema.GroupVersionResource, nam
func CreateToaster(client dynamic.Interface, toaster *testV1.Toaster, clusterName string) *testV1.Toaster {
By(fmt.Sprintf("Creating Toaster %q in namespace %q in %q", toaster.Name, toaster.Namespace, clusterName))

obj, err := client.Resource(*ToasterGVR()).Namespace(toaster.Namespace).Create(context.TODO(), test.ToUnstructured(toaster),
obj, err := client.Resource(*ToasterGVR()).Namespace(toaster.Namespace).Create(context.TODO(), resource.MustToUnstructured(toaster),
metav1.CreateOptions{})
Expect(err).To(Succeed())

Expand Down

0 comments on commit 5cddce5

Please sign in to comment.