@@ -18,6 +18,7 @@ package gc
1818
1919import (
2020 "context"
21+ "strings"
2122 "testing"
2223
2324 . "github.com/onsi/gomega"
@@ -34,11 +35,13 @@ import (
3435 "sigs.k8s.io/cluster-api/controllers/external"
3536)
3637
38+ const (
39+ testClusterName = "test-cluster"
40+ )
41+
3742func TestEnableGC (t * testing.T ) {
3843 RegisterTestingT (t )
3944
40- testClusterName := "test-cluster"
41-
4245 testCases := []struct {
4346 name string
4447 clusterName string
@@ -116,8 +119,6 @@ func TestEnableGC(t *testing.T) {
116119func TestDisableGC (t * testing.T ) {
117120 RegisterTestingT (t )
118121
119- testClusterName := "test-cluster"
120-
121122 testCases := []struct {
122123 name string
123124 clusterName string
@@ -186,6 +187,107 @@ func TestDisableGC(t *testing.T) {
186187 }
187188}
188189
190+ func TestConfigureGC (t * testing.T ) {
191+ RegisterTestingT (t )
192+
193+ testCases := []struct {
194+ name string
195+ clusterName string
196+ gcTasks []string
197+ existingObjs []client.Object
198+ expectError bool
199+ }{
200+ {
201+ name : "no capi cluster" ,
202+ clusterName : testClusterName ,
203+ existingObjs : []client.Object {},
204+ expectError : true ,
205+ },
206+ {
207+ name : "no infra cluster" ,
208+ clusterName : testClusterName ,
209+ existingObjs : newManagedCluster (testClusterName , true ),
210+ expectError : true ,
211+ },
212+ {
213+ name : "with managed control plane and no annotation" ,
214+ clusterName : testClusterName ,
215+ existingObjs : newManagedCluster (testClusterName , false ),
216+ gcTasks : []string {"load-balancer" , "target-group" },
217+ expectError : false ,
218+ },
219+ {
220+ name : "with awscluster and no annotation" ,
221+ clusterName : testClusterName ,
222+ existingObjs : newUnManagedCluster (testClusterName , false ),
223+ gcTasks : []string {"load-balancer" , "security-group" },
224+ expectError : false ,
225+ },
226+ {
227+ name : "with managed control plane and with annotation" ,
228+ clusterName : testClusterName ,
229+ existingObjs : newManagedClusterWithAnnotations (testClusterName , map [string ]string {infrav1 .ExternalResourceGCTasksAnnotation : "security-group" }),
230+ gcTasks : []string {"load-balancer" , "target-group" },
231+ expectError : false ,
232+ },
233+ {
234+ name : "with awscluster and with annotation" ,
235+ clusterName : testClusterName ,
236+ existingObjs : newUnManagedClusterWithAnnotations (testClusterName , map [string ]string {infrav1 .ExternalResourceGCTasksAnnotation : "security-group" }),
237+ gcTasks : []string {"load-balancer" , "target-group" },
238+ expectError : false ,
239+ },
240+ {
241+ name : "with awscluster and invalid gc tasks" ,
242+ clusterName : testClusterName ,
243+ existingObjs : newUnManagedCluster (testClusterName , false ),
244+ gcTasks : []string {"load-balancer" , "INVALID" },
245+ expectError : true ,
246+ },
247+ }
248+
249+ for _ , tc := range testCases {
250+ t .Run (tc .name , func (t * testing.T ) {
251+ g := NewWithT (t )
252+
253+ input := GCInput {
254+ ClusterName : tc .clusterName ,
255+ Namespace : "default" ,
256+ }
257+
258+ fake := newFakeClient (scheme , tc .existingObjs ... )
259+ ctx := context .TODO ()
260+
261+ proc , err := New (input , WithClient (fake ))
262+ g .Expect (err ).NotTo (HaveOccurred ())
263+
264+ resErr := proc .Configure (ctx , tc .gcTasks )
265+ if tc .expectError {
266+ g .Expect (resErr ).To (HaveOccurred ())
267+ return
268+ }
269+ g .Expect (resErr ).NotTo (HaveOccurred ())
270+
271+ cluster := tc .existingObjs [0 ].(* clusterv1.Cluster )
272+ ref := cluster .Spec .InfrastructureRef
273+
274+ obj , err := external .Get (ctx , fake , ref , "default" )
275+ g .Expect (err ).NotTo (HaveOccurred ())
276+ g .Expect (obj ).NotTo (BeNil ())
277+
278+ expected := strings .Join (tc .gcTasks , "," )
279+ annotationVal , hasAnnotation := annotations .Get (obj , infrav1 .ExternalResourceGCTasksAnnotation )
280+
281+ if expected != "" {
282+ g .Expect (hasAnnotation ).To (BeTrue ())
283+ g .Expect (annotationVal ).To (Equal (expected ))
284+ } else {
285+ g .Expect (hasAnnotation ).To (BeFalse ())
286+ }
287+ })
288+ }
289+ }
290+
189291func newFakeClient (scheme * runtime.Scheme , objs ... client.Object ) client.Client {
190292 return fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objs ... ).Build ()
191293}
0 commit comments