@@ -20,7 +20,9 @@ import (
2020 "github.com/golang/mock/gomock"
2121 . "github.com/onsi/ginkgo/v2"
2222 . "github.com/onsi/gomega"
23+ "k8s.io/apimachinery/pkg/api/errors"
2324 infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
25+ "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
2426 dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
2527 "sigs.k8s.io/controller-runtime/pkg/client"
2628 "sigs.k8s.io/controller-runtime/pkg/controller"
@@ -54,4 +56,41 @@ var _ = Describe("CloudStackAffinityGroupReconciler", func() {
5456 return false
5557 }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
5658 })
59+
60+ It ("Should remove affinity group finalizer if corresponding affinity group is not present on Cloudstack." , func () {
61+ // Modify failure domain name the same way the cluster controller would.
62+ dummies .CSAffinityGroup .Spec .FailureDomainName = dummies .CSFailureDomain1 .Spec .Name
63+
64+ Ω (k8sClient .Create (ctx , dummies .CSFailureDomain1 ))
65+ Ω (k8sClient .Create (ctx , dummies .CSAffinityGroup )).Should (Succeed ())
66+
67+ mockCloudClient .EXPECT ().GetOrCreateAffinityGroup (gomock .Any ()).AnyTimes ()
68+
69+ // Test that the AffinityGroup controller sets Status.Ready to true.
70+ Eventually (func () bool {
71+ nameSpaceFilter := & client.ListOptions {Namespace : dummies .ClusterNameSpace }
72+ affinityGroups := & infrav1.CloudStackAffinityGroupList {}
73+ if err := k8sClient .List (ctx , affinityGroups , nameSpaceFilter ); err == nil {
74+ if len (affinityGroups .Items ) == 1 {
75+ return affinityGroups .Items [0 ].Status .Ready
76+ }
77+ }
78+ return false
79+ }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
80+
81+ Ω (k8sClient .Delete (ctx , dummies .CSAffinityGroup ))
82+ mockCloudClient .EXPECT ().FetchAffinityGroup (gomock .Any ()).Do (func (arg1 interface {}) {
83+ arg1 .(* cloud.AffinityGroup ).ID = ""
84+ }).AnyTimes ().Return (nil )
85+
86+ // Once the affinity group id was set to "" the controller should remove the finalizer and unblock deleting affinity group resource
87+ Eventually (func () bool {
88+ retrievedAffinityGroup := & infrav1.CloudStackAffinityGroup {}
89+ affinityGroupKey := client.ObjectKey {Namespace : dummies .ClusterNameSpace , Name : dummies .AffinityGroup .Name }
90+ if err := k8sClient .Get (ctx , affinityGroupKey , retrievedAffinityGroup ); err != nil {
91+ return errors .IsNotFound (err )
92+ }
93+ return false
94+ }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
95+ })
5796})
0 commit comments