@@ -20,7 +20,9 @@ import (
20
20
"github.com/golang/mock/gomock"
21
21
. "github.com/onsi/ginkgo/v2"
22
22
. "github.com/onsi/gomega"
23
+ "k8s.io/apimachinery/pkg/api/errors"
23
24
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
25
+ "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
24
26
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
25
27
"sigs.k8s.io/controller-runtime/pkg/client"
26
28
)
@@ -53,4 +55,41 @@ var _ = Describe("CloudStackAffinityGroupReconciler", func() {
53
55
return false
54
56
}, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
55
57
})
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
+ })
56
95
})
0 commit comments