From d48504c13984e76555882dc101bd1a2b9b158bfc Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 16 Nov 2023 22:56:15 -0500 Subject: [PATCH] Genericize (Get|Update|Create)Resource functions in syncer/test ...so they return an instance of the given runtime.Object type instead of *Unstructured. Signed-off-by: Tom Pantelis --- pkg/resource/util.go | 12 ++++++++++ pkg/syncer/broker/syncer_test.go | 2 +- pkg/syncer/test/util.go | 34 ++++++++++++---------------- pkg/util/create_or_update_test.go | 10 ++++---- pkg/watcher/resource_watcher_test.go | 11 ++------- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/pkg/resource/util.go b/pkg/resource/util.go index 991d79e6..54054fcc 100644 --- a/pkg/resource/util.go +++ b/pkg/resource/util.go @@ -87,6 +87,18 @@ func MustToUnstructuredUsingDefaultConverter(from runtime.Object) *unstructured. return u } +func MustFromUnstructured[T runtime.Object](u *unstructured.Unstructured, objType T) T { + return MustFromUnstructuredUsingScheme(u, objType, scheme.Scheme) +} + +func MustFromUnstructuredUsingScheme[T runtime.Object](u *unstructured.Unstructured, objType T, usingScheme *runtime.Scheme) T { + actual := objType.DeepCopyObject().(T) + err := usingScheme.Convert(u, actual, nil) + utilruntime.Must(err) + + return actual +} + func MustToMeta(obj interface{}) metav1.Object { objMeta, err := meta.Accessor(obj) if err != nil { diff --git a/pkg/syncer/broker/syncer_test.go b/pkg/syncer/broker/syncer_test.go index a6cb46a5..1fa838e5 100644 --- a/pkg/syncer/broker/syncer_test.go +++ b/pkg/syncer/broker/syncer_test.go @@ -256,7 +256,7 @@ var _ = Describe("Broker Syncer", func() { It("should sync to the broker datastore", func() { test.AwaitResource(brokerClient, resource.GetName()) - actual := test.GetPod(brokerClient, resource) + actual := test.GetResource(brokerClient, resource) Expect(actual.Labels).To(HaveKeyWithValue(sync.OrigNamespaceLabelKey, metav1.NamespaceDefault)) }) }) diff --git a/pkg/syncer/test/util.go b/pkg/syncer/test/util.go index fa05f544..0be440dd 100644 --- a/pkg/syncer/test/util.go +++ b/pkg/syncer/test/util.go @@ -43,28 +43,33 @@ const ( LocalNamespace = "local-ns" ) -func GetResourceAndError(resourceInterface dynamic.ResourceInterface, obj runtime.Object) (*unstructured.Unstructured, error) { - return resourceInterface.Get(context.TODO(), resource.MustToMeta(obj).GetName(), metav1.GetOptions{}) +func GetResourceAndError[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) (T, error) { + u, err := resourceInterface.Get(context.TODO(), resource.MustToMeta(obj).GetName(), metav1.GetOptions{}) + if err != nil { + return *new(T), err + } + + return resource.MustFromUnstructured(u, obj), nil } -func GetResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object) *unstructured.Unstructured { +func GetResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T { ret, err := GetResourceAndError(resourceInterface, obj) Expect(err).To(Succeed()) return ret } -func CreateResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object) *unstructured.Unstructured { +func CreateResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T { u := resource.MustToUnstructured(obj) u.SetResourceVersion("") created, err := resourceInterface.Create(context.TODO(), u, metav1.CreateOptions{}) Expect(err).To(Succeed()) - return created + return resource.MustFromUnstructured(created, obj) } -func UpdateResource(resourceInterface dynamic.ResourceInterface, obj runtime.Object) *unstructured.Unstructured { +func UpdateResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T { u := resource.MustToUnstructured(obj) err := util.Update[*unstructured.Unstructured](context.Background(), resource.ForDynamic(resourceInterface), u, util.Replace(u)) @@ -74,7 +79,7 @@ func UpdateResource(resourceInterface dynamic.ResourceInterface, obj runtime.Obj } func VerifyResource(resourceInterface dynamic.ResourceInterface, expected *corev1.Pod, expNamespace, clusterID string) { - actual := GetPod(resourceInterface, expected) + actual := GetResource(resourceInterface, expected) Expect(actual.GetName()).To(Equal(expected.GetName())) Expect(actual.GetNamespace()).To(Equal(expNamespace)) @@ -95,16 +100,6 @@ func VerifyResource(resourceInterface dynamic.ResourceInterface, expected *corev Expect(actual.GetLabels()).To(Equal(duplicate)) } -func GetPod(resourceInterface dynamic.ResourceInterface, from *corev1.Pod) *corev1.Pod { - actual := &corev1.Pod{} - - raw := GetResource(resourceInterface, from) - err := scheme.Scheme.Convert(raw, actual, nil) - Expect(err).To(Succeed()) - - return actual -} - func NewPod(namespace string) *corev1.Pod { return NewPodWithImage(namespace, "nginx") } @@ -196,9 +191,8 @@ func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Obje return newObjs } -func SetClusterIDLabel(obj runtime.Object, clusterID string) runtime.Object { - meta, err := metaapi.Accessor(obj) - Expect(err).To(Succeed()) +func SetClusterIDLabel[T runtime.Object](obj T, clusterID string) T { + meta := resource.MustToMeta(obj) labels := meta.GetLabels() if labels == nil { diff --git a/pkg/util/create_or_update_test.go b/pkg/util/create_or_update_test.go index e7232bde..17952e5a 100644 --- a/pkg/util/create_or_update_test.go +++ b/pkg/util/create_or_update_test.go @@ -426,13 +426,13 @@ func (t *createOrUpdateTestDriver) testUpdate(doUpdate func(util.OperationResult }) JustBeforeEach(func() { - t.pod = test.GetPod(t.client, t.pod) + t.pod = test.GetResource(t.client, t.pod) t.pod.Status = corev1.PodStatus{Phase: corev1.PodRunning} }) It("should only update the status", func() { Expect(doUpdate(util.OperationResultUpdated)).To(Succeed()) - Expect(test.GetPod(t.client, t.pod).Status).To(Equal(t.pod.Status)) + Expect(test.GetResource(t.client, t.pod).Status).To(Equal(t.pod.Status)) tests.EnsureNoActionsForResource(t.testingFake, "pods", "update") }) @@ -443,7 +443,7 @@ func (t *createOrUpdateTestDriver) testUpdate(doUpdate func(util.OperationResult It("should update the status", func() { Expect(doUpdate(util.OperationResultUpdated)).To(Succeed()) - Expect(test.GetPod(t.client, t.pod).Status).To(Equal(t.pod.Status)) + Expect(test.GetResource(t.client, t.pod).Status).To(Equal(t.pod.Status)) }) }) }) @@ -454,7 +454,7 @@ func (t *createOrUpdateTestDriver) testUpdate(doUpdate func(util.OperationResult }) JustBeforeEach(func() { - t.pod = test.GetPod(t.client, t.pod) + t.pod = test.GetResource(t.client, t.pod) t.pod.Status = corev1.PodStatus{} }) @@ -529,7 +529,7 @@ func (t *createOrUpdateTestDriver) verifyPod() *corev1.Pod { Expect(scheme.Scheme.Convert(&list.Items[0], pod, nil)).To(Succeed()) } - actual := test.GetPod(t.client, pod) + actual := test.GetResource(t.client, pod) t.compareWithPod(actual) return actual diff --git a/pkg/watcher/resource_watcher_test.go b/pkg/watcher/resource_watcher_test.go index d5a86dc9..1cccc0d4 100644 --- a/pkg/watcher/resource_watcher_test.go +++ b/pkg/watcher/resource_watcher_test.go @@ -124,10 +124,7 @@ var _ = Describe("Resource Watcher", func() { When("a Pod is created, updated and deleted", func() { It("should notify the appropriate handler of each event", func() { - obj := test.CreateResource(pods, pod) - pod.Namespace = obj.GetNamespace() - pod.ResourceVersion = obj.GetResourceVersion() - pod.UID = obj.GetUID() + pod := test.CreateResource(pods, pod) Eventually(createdPods).Should(Receive(Equal(pod))) Consistently(createdPods).ShouldNot(Receive()) @@ -153,11 +150,7 @@ var _ = Describe("Resource Watcher", func() { }, } - obj := test.CreateResource(services, service) - service.Namespace = obj.GetNamespace() - service.ResourceVersion = obj.GetResourceVersion() - service.UID = obj.GetUID() - + service = test.CreateResource(services, service) Eventually(createdServices).Should(Receive(Equal(service))) }) })