Skip to content

Commit c5352f7

Browse files
committed
add new suite test
Signed-off-by: sivchari <[email protected]>
1 parent 364384a commit c5352f7

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,17 @@ func HasControllerReference(object metav1.Object) bool {
183183

184184
// HasOwnerReference returns true if the owners list contains an owner reference
185185
// that matches the object's group, kind, and name.
186-
func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) bool {
186+
func HasOwnerReference(ownerRefs []metav1.OwnerReference, obj client.Object, scheme *runtime.Scheme) (bool, error) {
187187
gvk, err := apiutil.GVKForObject(obj, scheme)
188188
if err != nil {
189-
return false
189+
return false, err
190190
}
191191
idx := indexOwnerRef(ownerRefs, metav1.OwnerReference{
192192
APIVersion: gvk.GroupVersion().String(),
193193
Name: obj.GetName(),
194194
Kind: gvk.Kind,
195195
})
196-
return idx != -1
196+
return idx != -1, nil
197197
}
198198

199199
// RemoveControllerReference removes an owner reference where the controller

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,38 @@ var _ = Describe("Controllerutil", func() {
957957
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(BeFalse())
958958
})
959959
})
960+
961+
Describe("HasOwnerReference", func() {
962+
It("should return true if the object has the owner reference", func() {
963+
rs := &appsv1.ReplicaSet{}
964+
dep := &extensionsv1beta1.Deployment{
965+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
966+
}
967+
Expect(controllerutil.SetOwnerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred())
968+
969+
b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, scheme.Scheme)
970+
Expect(err).NotTo(HaveOccurred())
971+
Expect(b).To(BeTrue())
972+
})
973+
974+
It("should return error if the object can not create group-version-kind", func() {
975+
rs := &appsv1.ReplicaSet{}
976+
dep := &errMetaObj{}
977+
b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, runtime.NewScheme())
978+
Expect(err).To(HaveOccurred())
979+
Expect(b).To(BeFalse())
980+
})
981+
982+
It("should return false if the object does not have the owner reference", func() {
983+
rs := &appsv1.ReplicaSet{}
984+
dep := &extensionsv1beta1.Deployment{
985+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
986+
}
987+
b, err := controllerutil.HasOwnerReference(dep.GetOwnerReferences(), rs, scheme.Scheme)
988+
Expect(err).NotTo(HaveOccurred())
989+
Expect(b).To(BeFalse())
990+
})
991+
})
960992
})
961993
})
962994

0 commit comments

Comments
 (0)