@@ -25,6 +25,7 @@ import (
25
25
workinformers "open-cluster-management.io/api/client/work/informers/externalversions"
26
26
27
27
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
28
+ testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing"
28
29
)
29
30
30
31
func TestReconcile (t * testing.T ) {
@@ -268,3 +269,123 @@ func TestRunController(t *testing.T) {
268
269
}
269
270
}
270
271
}
272
+
273
+ func TestStopUnusedManagers (t * testing.T ) {
274
+ cases := []struct {
275
+ name string
276
+ addonName string
277
+ managedClusterAddons []runtime.Object
278
+ existingManagers map [string ]context.CancelFunc
279
+ expectedManagerStopped bool
280
+ expectedRequeue bool
281
+ }{
282
+ {
283
+ name : "no managed cluster addons, manager should be stopped" ,
284
+ addonName : "test-addon" ,
285
+ managedClusterAddons : []runtime.Object {},
286
+ existingManagers : map [string ]context.CancelFunc {
287
+ "test-addon" : func () {},
288
+ },
289
+ expectedManagerStopped : true ,
290
+ expectedRequeue : false ,
291
+ },
292
+ {
293
+ name : "managed cluster addons exist, manager should not be stopped" ,
294
+ addonName : "test-addon" ,
295
+ managedClusterAddons : []runtime.Object {
296
+ testinghelpers .NewManagedClusterAddons ("test-addon" , "cluster1" , nil , nil ),
297
+ },
298
+ existingManagers : map [string ]context.CancelFunc {
299
+ "test-addon" : func () {},
300
+ },
301
+ expectedManagerStopped : false ,
302
+ expectedRequeue : true ,
303
+ },
304
+ {
305
+ name : "multiple managed cluster addons exist, manager should not be stopped" ,
306
+ addonName : "test-addon" ,
307
+ managedClusterAddons : []runtime.Object {
308
+ testinghelpers .NewManagedClusterAddons ("test-addon" , "cluster1" , nil , nil ),
309
+ testinghelpers .NewManagedClusterAddons ("test-addon" , "cluster2" , nil , nil ),
310
+ },
311
+ existingManagers : map [string ]context.CancelFunc {
312
+ "test-addon" : func () {},
313
+ },
314
+ expectedManagerStopped : false ,
315
+ expectedRequeue : true ,
316
+ },
317
+ {
318
+ name : "no manager exists, should not error" ,
319
+ addonName : "test-addon" ,
320
+ managedClusterAddons : []runtime.Object {},
321
+ existingManagers : map [string ]context.CancelFunc {},
322
+ expectedManagerStopped : false ,
323
+ expectedRequeue : false ,
324
+ },
325
+ }
326
+
327
+ for _ , c := range cases {
328
+ t .Run (c .name , func (t * testing.T ) {
329
+ fakeAddonClient := fakeaddon .NewSimpleClientset (c .managedClusterAddons ... )
330
+ addonInformers := addoninformers .NewSharedInformerFactory (fakeAddonClient , 10 * time .Minute )
331
+
332
+ fakeDynamicClient := dynamicfake .NewSimpleDynamicClient (runtime .NewScheme ())
333
+ dynamicInformerFactory := dynamicinformer .NewDynamicSharedInformerFactory (fakeDynamicClient , 0 )
334
+ fakeClusterClient := fakecluster .NewSimpleClientset ()
335
+ clusterInformers := clusterv1informers .NewSharedInformerFactory (fakeClusterClient , 10 * time .Minute )
336
+ fakeWorkClient := fakework .NewSimpleClientset ()
337
+ workInformers := workinformers .NewSharedInformerFactory (fakeWorkClient , 10 * time .Minute )
338
+ hubKubeClient := fakekube .NewSimpleClientset ()
339
+
340
+ managerStopped := false
341
+ existingManagers := make (map [string ]context.CancelFunc )
342
+ for name , stopFunc := range c .existingManagers {
343
+ existingManagers [name ] = func () {
344
+ if name == c .addonName {
345
+ managerStopped = true
346
+ }
347
+ stopFunc ()
348
+ }
349
+ }
350
+
351
+ controller := & addonTemplateController {
352
+ kubeConfig : & rest.Config {},
353
+ kubeClient : hubKubeClient ,
354
+ addonClient : fakeAddonClient ,
355
+ workClient : fakeWorkClient ,
356
+ cmaLister : addonInformers .Addon ().V1alpha1 ().ClusterManagementAddOns ().Lister (),
357
+ addonManagers : existingManagers ,
358
+ addonInformers : addonInformers ,
359
+ clusterInformers : clusterInformers ,
360
+ dynamicInformers : dynamicInformerFactory ,
361
+ workInformers : workInformers ,
362
+ eventRecorder : eventstesting .NewTestingEventRecorder (t ),
363
+ }
364
+
365
+ ctx := context .TODO ()
366
+ syncContext := testingcommon .NewFakeSyncContext (t , c .addonName )
367
+
368
+ controller .stopUnusedManagers (ctx , syncContext , c .addonName )
369
+
370
+ // Check if manager was stopped
371
+ if c .expectedManagerStopped {
372
+ assert .True (t , managerStopped , "expected manager to be stopped" )
373
+ _ , exists := controller .addonManagers [c .addonName ]
374
+ assert .False (t , exists , "expected manager to be removed from map" )
375
+ } else {
376
+ assert .False (t , managerStopped , "expected manager not to be stopped" )
377
+ if len (c .existingManagers ) > 0 {
378
+ _ , exists := controller .addonManagers [c .addonName ]
379
+ assert .True (t , exists , "expected manager to still exist in map" )
380
+ }
381
+ }
382
+
383
+ // Check if requeue was called
384
+ if c .expectedRequeue {
385
+ // We can't easily test the exact requeue behavior with the fake sync context
386
+ // but we can verify the manager wasn't stopped when it should be requeued
387
+ assert .False (t , managerStopped , "manager should not be stopped when requeue is expected" )
388
+ }
389
+ })
390
+ }
391
+ }
0 commit comments