Skip to content

Commit

Permalink
add new suite test
Browse files Browse the repository at this point in the history
Signed-off-by: sivchari <[email protected]>
  • Loading branch information
sivchari committed Sep 17, 2024
1 parent 5b51210 commit 5aa3f3e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ func HasControllerReference(object metav1.Object) bool {

// HasOwnerReference returns true if the owners list contains an owner reference
// that matches the object's group, kind, and name.
func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) bool {
func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) (bool, error) {
gvk, err := apiutil.GVKForObject(obj, scheme)
if err != nil {
return false
return false, err
}
idx := indexOwnerRef(ownerRefs, metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Name: obj.GetName(),
Kind: gvk.Kind,
})
return idx != -1
return idx != -1, nil
}

// RemoveControllerReference removes an owner reference where the controller
Expand Down
36 changes: 36 additions & 0 deletions pkg/controller/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,42 @@ var _ = Describe("Controllerutil", func() {
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(BeFalse())
})
})

Describe("HasOwnerReference", func() {
It("should return true if the object has the owner reference", func() {
rs := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
dep := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
Expect(controllerutil.SetOwnerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred())

b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expect(b).To(BeTrue())
})

It("should return false if the object does not have the owner reference", func() {
rs := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
dep := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expect(b).To(BeFalse())
})

It("should return error if the object can not create group-version-kind", func() {
rs := &appsv1.ReplicaSet{}
dep := &errMetaObj{}
b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, runtime.NewScheme())
Expect(err).To(HaveOccurred())
Expect(b).To(BeFalse())
})
})
})
})

Expand Down

0 comments on commit 5aa3f3e

Please sign in to comment.