@@ -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
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -54,4 +56,41 @@ var _ = Describe("CloudStackAffinityGroupReconciler", func() {
54
56
return false
55
57
}, timeout ).WithPolling (pollInterval ).Should (BeTrue ())
56
58
})
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
+ })
57
96
})
0 commit comments