@@ -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)
@@ -53,4 +55,41 @@ var _ = Describe("CloudStackAffinityGroupReconciler", func() {
5355 return false
5456 }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
5557 })
58+
59+ It ("Should remove affinity group finalizer if corresponding affinity group is not present on Cloudstack." , func () {
60+ // Modify failure domain name the same way the cluster controller would.
61+ dummies .CSAffinityGroup .Spec .FailureDomainName = dummies .CSFailureDomain1 .Spec .Name
62+
63+ Ω (k8sClient .Create (ctx , dummies .CSFailureDomain1 ))
64+ Ω (k8sClient .Create (ctx , dummies .CSAffinityGroup )).Should (Succeed ())
65+
66+ mockCloudClient .EXPECT ().GetOrCreateAffinityGroup (gomock .Any ()).AnyTimes ()
67+
68+ // Test that the AffinityGroup controller sets Status.Ready to true.
69+ Eventually (func () bool {
70+ nameSpaceFilter := & client.ListOptions {Namespace : dummies .ClusterNameSpace }
71+ affinityGroups := & infrav1.CloudStackAffinityGroupList {}
72+ if err := k8sClient .List (ctx , affinityGroups , nameSpaceFilter ); err == nil {
73+ if len (affinityGroups .Items ) == 1 {
74+ return affinityGroups .Items [0 ].Status .Ready
75+ }
76+ }
77+ return false
78+ }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
79+
80+ Ω (k8sClient .Delete (ctx , dummies .CSAffinityGroup ))
81+ mockCloudClient .EXPECT ().FetchAffinityGroup (gomock .Any ()).Do (func (arg1 interface {}) {
82+ arg1 .(* cloud.AffinityGroup ).ID = ""
83+ }).AnyTimes ().Return (nil )
84+
85+ // Once the affinity group id was set to "" the controller should remove the finalizer and unblock deleting affinity group resource
86+ Eventually (func () bool {
87+ retrievedAffinityGroup := & infrav1.CloudStackAffinityGroup {}
88+ affinityGroupKey := client.ObjectKey {Namespace : dummies .ClusterNameSpace , Name : dummies .AffinityGroup .Name }
89+ if err := k8sClient .Get (ctx , affinityGroupKey , retrievedAffinityGroup ); err != nil {
90+ return errors .IsNotFound (err )
91+ }
92+ return false
93+ }, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
94+ })
5695})
0 commit comments