@@ -27,6 +27,7 @@ import (
2727 "github.com/onmetal/controller-utils/testdata"
2828 . "github.com/onsi/ginkgo"
2929 . "github.com/onsi/gomega"
30+ "github.com/stretchr/testify/mock"
3031 corev1 "k8s.io/api/core/v1"
3132 apierrors "k8s.io/apimachinery/pkg/api/errors"
3233 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,7 @@ import (
4142var _ = Describe ("Clientutils" , func () {
4243 const (
4344 objectsPath = "../testdata/bases/objects.yaml"
45+ finalizer = "my-finalizer"
4446 )
4547
4648 var (
@@ -820,4 +822,128 @@ var _ = Describe("Clientutils", func() {
820822 }))
821823 })
822824 })
825+
826+ Describe ("DeleteIfExists" , func () {
827+ It ("should delete the existing object and return true" , func () {
828+ c .EXPECT ().Delete (ctx , cm )
829+ existed , err := DeleteIfExists (ctx , c , cm )
830+ Expect (err ).NotTo (HaveOccurred ())
831+ Expect (existed ).To (BeTrue (), "object should have existed" )
832+ })
833+
834+ It ("should catch the not-found error when deleting and return false" , func () {
835+ c .EXPECT ().Delete (ctx , cm ).Return (apierrors .NewNotFound (schema.GroupResource {}, "" ))
836+ existed , err := DeleteIfExists (ctx , c , cm )
837+ Expect (err ).NotTo (HaveOccurred ())
838+ Expect (existed ).To (BeFalse (), "object should not have" )
839+ })
840+
841+ It ("should forward any unknown errors" , func () {
842+ expectedErr := fmt .Errorf ("custom" )
843+ c .EXPECT ().Delete (ctx , cm ).Return (expectedErr )
844+ _ , err := DeleteIfExists (ctx , c , cm )
845+ Expect (err ).To (Equal (expectedErr ))
846+ })
847+ })
848+
849+ Describe ("DeleteMultipleIfExist" , func () {
850+ It ("should delete the multiple objects and return the ones that existed" , func () {
851+ gomock .InOrder (
852+ c .EXPECT ().Delete (ctx , cm ),
853+ c .EXPECT ().Delete (ctx , secret ).Return (apierrors .NewNotFound (schema.GroupResource {}, "" )),
854+ )
855+
856+ existed , err := DeleteMultipleIfExist (ctx , c , []client.Object {cm , secret })
857+ Expect (err ).NotTo (HaveOccurred ())
858+ Expect (existed ).To (Equal ([]client.Object {cm }))
859+ })
860+
861+ It ("should forward any unknown errors but still return the objects that existed" , func () {
862+ expectedErr := fmt .Errorf ("custom error" )
863+ gomock .InOrder (
864+ c .EXPECT ().Delete (ctx , cm ),
865+ c .EXPECT ().Delete (ctx , secret ).Return (expectedErr ),
866+ )
867+
868+ existed , err := DeleteMultipleIfExist (ctx , c , []client.Object {cm , secret })
869+ Expect (err ).To (SatisfyAll (
870+ HaveOccurred (),
871+ WithTransform (func (err error ) bool {
872+ return errors .Is (err , expectedErr )
873+ }, BeTrue ()),
874+ ))
875+ Expect (existed ).To (Equal ([]client.Object {cm }))
876+ })
877+ })
878+
879+ Context ("Finalizer utilities" , func () {
880+ var (
881+ addFinalizerPatchData []byte
882+ removeFinalizerPatchData []byte
883+ cmWithFinalizer * corev1.ConfigMap
884+ )
885+ BeforeEach (func () {
886+ cmWithFinalizer = cm .DeepCopy ()
887+ cmWithFinalizer .Finalizers = []string {finalizer }
888+
889+ var err error
890+ addFinalizerPatchData , err = client .MergeFrom (cm ).Data (cmWithFinalizer )
891+ Expect (err ).NotTo (HaveOccurred ())
892+
893+ removeFinalizerPatchData , err = client .MergeFrom (cmWithFinalizer ).Data (cm )
894+ Expect (err ).NotTo (HaveOccurred ())
895+ })
896+
897+ Describe ("PatchAddFinalizer" , func () {
898+ It ("should issue a patch adding the finalizer" , func () {
899+ c .EXPECT ().Patch (ctx , cm , mock .MatchedBy (func (p client.Patch ) bool {
900+ return Expect (p .Data (cm )).To (Equal (addFinalizerPatchData ))
901+ }))
902+ Expect (PatchAddFinalizer (ctx , c , cm , finalizer )).To (Succeed ())
903+ })
904+ })
905+
906+ Describe ("PatchRemoveFinalizer" , func () {
907+ It ("should issue a patch removing the finalizer" , func () {
908+ c .EXPECT ().Patch (ctx , cmWithFinalizer , mock .MatchedBy (func (p client.Patch ) bool {
909+ return Expect (p .Data (cm )).To (Equal (removeFinalizerPatchData ))
910+ }))
911+ Expect (PatchRemoveFinalizer (ctx , c , cmWithFinalizer , finalizer )).To (Succeed ())
912+ })
913+ })
914+
915+ Describe ("PatchEnsureFinalizer" , func () {
916+ It ("should add the finalizer if it is not present and report that it was modified" , func () {
917+ c .EXPECT ().Patch (ctx , cm , mock .MatchedBy (func (p client.Patch ) bool {
918+ return Expect (p .Data (cm )).To (Equal (addFinalizerPatchData ))
919+ }))
920+ modified , err := PatchEnsureFinalizer (ctx , c , cm , finalizer )
921+ Expect (err ).NotTo (HaveOccurred ())
922+ Expect (modified ).To (BeTrue (), "cm should be modified: %v" , cm )
923+ })
924+
925+ It ("should not add the finalizer if it is already present and report that it was not modified" , func () {
926+ modified , err := PatchEnsureFinalizer (ctx , c , cmWithFinalizer , finalizer )
927+ Expect (err ).NotTo (HaveOccurred ())
928+ Expect (modified ).To (BeFalse (), "cm should not be modified" )
929+ })
930+ })
931+
932+ Describe ("PatchEnsureNoFinalizer" , func () {
933+ It ("should remove the finalizer if it is present and report that it was modified" , func () {
934+ c .EXPECT ().Patch (ctx , cmWithFinalizer , mock .MatchedBy (func (p client.Patch ) bool {
935+ return Expect (p .Data (cmWithFinalizer )).To (Equal (removeFinalizerPatchData ))
936+ }))
937+ modified , err := PatchEnsureNoFinalizer (ctx , c , cmWithFinalizer , finalizer )
938+ Expect (err ).NotTo (HaveOccurred ())
939+ Expect (modified ).To (BeTrue (), "cm should be modified: %v" , cm )
940+ })
941+
942+ It ("should not remove the finalizer if it is already not present and report that it was not modified" , func () {
943+ modified , err := PatchEnsureNoFinalizer (ctx , c , cm , finalizer )
944+ Expect (err ).NotTo (HaveOccurred ())
945+ Expect (modified ).To (BeFalse (), "cm should not be modified" )
946+ })
947+ })
948+ })
823949})
0 commit comments