Skip to content

Commit

Permalink
Genericize (Get|Update|Create)Resource functions in syncer/test
Browse files Browse the repository at this point in the history
...so they return an instance of the given runtime.Object type instead
of *Unstructured.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Nov 20, 2023
1 parent 2d9a79a commit d48504c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
12 changes: 12 additions & 0 deletions pkg/resource/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/syncer/broker/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
Expand Down
34 changes: 14 additions & 20 deletions pkg/syncer/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand All @@ -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))
})
})
})
Expand All @@ -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{}
})

Expand Down Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions pkg/watcher/resource_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)))
})
})
Expand Down

0 comments on commit d48504c

Please sign in to comment.