From 7f7879dfd37ac64f3df00caebfb4c3471c23f59a Mon Sep 17 00:00:00 2001 From: changzhen Date: Thu, 21 Nov 2024 17:14:00 +0800 Subject: [PATCH 1/2] fix flake test of TestUpdateClusterEventHandler Signed-off-by: changzhen --- pkg/search/controller.go | 5 +- pkg/search/controllers_test.go | 209 ++++++++------------------------- 2 files changed, 51 insertions(+), 163 deletions(-) diff --git a/pkg/search/controller.go b/pkg/search/controller.go index 2bfa1b1ad3db..16bedf94fbff 100644 --- a/pkg/search/controller.go +++ b/pkg/search/controller.go @@ -350,9 +350,6 @@ func (c *Controller) getRegistryBackendHandler(cluster string, matchedRegistries return handler, nil } -var controlPlaneClientBuilder = func(restConfig *rest.Config) client.Client { - return gclient.NewForConfigOrDie(restConfig) -} var clusterDynamicClientBuilder = func(cluster string, controlPlaneClient client.Client) (*util.DynamicClusterClient, error) { return util.NewClusterDynamicClientSet(cluster, controlPlaneClient) } @@ -394,7 +391,7 @@ func (c *Controller) doCacheCluster(cluster string) error { // STEP2: added/updated cluster, builds an informer manager for a specific cluster. if !c.InformerManager.IsManagerExist(cluster) { klog.Info("Try to build informer manager for cluster ", cluster) - controlPlaneClient := controlPlaneClientBuilder(c.restConfig) + controlPlaneClient := gclient.NewForConfigOrDie(c.restConfig) clusterDynamicClient, err := clusterDynamicClientBuilder(cluster, controlPlaneClient) if err != nil { diff --git a/pkg/search/controllers_test.go b/pkg/search/controllers_test.go index 6576b3d35956..c8eaea453346 100644 --- a/pkg/search/controllers_test.go +++ b/pkg/search/controllers_test.go @@ -20,9 +20,9 @@ import ( "context" "errors" "fmt" + "strings" "testing" - "time" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -31,12 +31,11 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" searchv1alpha1 "github.com/karmada-io/karmada/pkg/apis/search/v1alpha1" - versioned "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" + "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" fakekarmadaclient "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/fake" informerfactory "github.com/karmada-io/karmada/pkg/generated/informers/externalversions" "github.com/karmada-io/karmada/pkg/search/backendstore" @@ -110,12 +109,9 @@ func TestAddClusterEventHandler(t *testing.T) { apiEndpoint, labels = "10.0.0.1", map[string]string{} ) - // Wait a bit to allow addCluster - // background thread to complete its execution. if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -131,8 +127,7 @@ func TestAddClusterEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -170,22 +165,16 @@ func TestUpdateClusterEventHandler(t *testing.T) { apiEndpoint, oldLabels, newLabels = "10.0.0.1", map[string]string{"status": "old"}, map[string]string{"status": "new"} ) - // Wait a bit to allow addCluster - // background thread to complete its execution. if err := upsertCluster(clientConnector, oldLabels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow updateCluster - // background thread to complete its execution. if err := upsertCluster(clientConnector, newLabels, apiEndpoint, clusterName, updatedResourceVerison); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -201,8 +190,7 @@ func TestUpdateClusterEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -215,28 +203,26 @@ func TestUpdateClusterEventHandler(t *testing.T) { func TestDeleteClusterEventHandler(t *testing.T) { tests := []struct { - name string - restConfig *rest.Config - client *fakekarmadaclient.Clientset - controlPlaneClient client.WithWatch - restMapper meta.RESTMapper - stopCh chan struct{} - prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) - verify func(*fakekarmadaclient.Clientset, *Controller, client.Client) error + name string + restConfig *rest.Config + client *fakekarmadaclient.Clientset + restMapper meta.RESTMapper + stopCh chan struct{} + prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) + verify func(*fakekarmadaclient.Clientset, *Controller) error }{ { - name: "AddAllEventHandlers_TriggerDeleteClusterEvent_DeletedClusterAddedToWorkQueue", - restConfig: &rest.Config{}, - client: fakekarmadaclient.NewSimpleClientset(), - controlPlaneClient: fake.NewFakeClient(), - restMapper: meta.NewDefaultRESTMapper(nil), - stopCh: make(chan struct{}), + name: "AddAllEventHandlers_TriggerDeleteClusterEvent_DeletedClusterAddedToWorkQueue", + restConfig: &rest.Config{}, + client: fakekarmadaclient.NewSimpleClientset(), + restMapper: meta.NewDefaultRESTMapper(nil), + stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) controller, err := createController(restConfig, factory, restMapper) return controller, factory, err }, - verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller, controlPlaneClient client.Client) error { + verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error { var ( registryName, clusterName = "test-registry", "test-cluster" resourceVersion, apiEndpoint = "1000", "10.0.0.1" @@ -249,15 +235,6 @@ func TestDeleteClusterEventHandler(t *testing.T) { } ) - if err := clusterv1alpha1.Install(scheme.Scheme); err != nil { - return fmt.Errorf("failed to install scheme: %w", err) - } - if err := upsertClusterControllerRuntime(controlPlaneClient, labels, clusterName, apiEndpoint); err != nil { - return err - } - controlPlaneClientBuilder = func(*rest.Config) client.Client { - return controlPlaneClient - } clusterDynamicClientBuilder = func(string, client.Client) (*util.DynamicClusterClient, error) { return &util.DynamicClusterClient{ DynamicClientSet: fakedynamic.NewSimpleDynamicClient(scheme.Scheme), @@ -265,32 +242,23 @@ func TestDeleteClusterEventHandler(t *testing.T) { }, nil } - // Wait a bit to allow for addCluster background - // thread to complete its execution. if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow for addResourceRegistry background - // thread to complete its execution. if err := upsertResourceRegistry(clientConnector, resourceSelectors, registryName, resourceVersion, []string{clusterName}); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow for deleteCluster on the controller - // background thread to complete its execution. if err := deleteCluster(clientConnector, clusterName); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -311,12 +279,11 @@ func TestDeleteClusterEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) - if err := test.verify(test.client, controller, test.controlPlaneClient); err != nil { + if err := test.verify(test.client, controller); err != nil { t.Errorf("failed to verify controller, got: %v", err) } }) @@ -325,28 +292,26 @@ func TestDeleteClusterEventHandler(t *testing.T) { func TestAddResourceRegistryEventHandler(t *testing.T) { tests := []struct { - name string - restConfig *rest.Config - client *fakekarmadaclient.Clientset - controlPlaneClient client.WithWatch - restMapper meta.RESTMapper - stopCh chan struct{} - prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) - verify func(*fakekarmadaclient.Clientset, *Controller, client.Client) error + name string + restConfig *rest.Config + client *fakekarmadaclient.Clientset + restMapper meta.RESTMapper + stopCh chan struct{} + prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) + verify func(*fakekarmadaclient.Clientset, *Controller) error }{ { - name: "AddAllEventHandlers_TriggerAddResourceRegistryEvent_ResourceRegistryAddedToWorkQueue", - restConfig: &rest.Config{}, - client: fakekarmadaclient.NewSimpleClientset(), - controlPlaneClient: fake.NewFakeClient(), - restMapper: meta.NewDefaultRESTMapper(nil), - stopCh: make(chan struct{}), + name: "AddAllEventHandlers_TriggerAddResourceRegistryEvent_ResourceRegistryAddedToWorkQueue", + restConfig: &rest.Config{}, + client: fakekarmadaclient.NewSimpleClientset(), + restMapper: meta.NewDefaultRESTMapper(nil), + stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) controller, err := createController(restConfig, factory, restMapper) return controller, factory, err }, - verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller, controlPlaneClient client.Client) error { + verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error { var ( registryName, clusterName = "test-registry", "test-cluster" resourceVersion, apiEndpoint = "1000", "10.0.0.1" @@ -359,9 +324,6 @@ func TestAddResourceRegistryEventHandler(t *testing.T) { } ) - controlPlaneClientBuilder = func(*rest.Config) client.Client { - return controlPlaneClient - } clusterDynamicClientBuilder = func(string, client.Client) (*util.DynamicClusterClient, error) { return &util.DynamicClusterClient{ DynamicClientSet: fakedynamic.NewSimpleDynamicClient(scheme.Scheme), @@ -369,22 +331,16 @@ func TestAddResourceRegistryEventHandler(t *testing.T) { }, nil } - // Wait a bit to allow addCluster background - // thread to complete its execution. if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow addResourceRegistry background - // thread to complete its execution. if err := upsertResourceRegistry(clientConnector, resourceSelectors, registryName, resourceVersion, []string{clusterName}); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -400,12 +356,11 @@ func TestAddResourceRegistryEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) - if err := test.verify(test.client, controller, test.controlPlaneClient); err != nil { + if err := test.verify(test.client, controller); err != nil { t.Errorf("failed to verify controller, got: %v", err) } }) @@ -455,32 +410,23 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { } ) - // Wait a bit to allow addCluster background thread - // to complete its execution. if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow addResourceRegistry background thread - // to complete its execution. if err := upsertResourceRegistry(clientConnector, resourceSelectors, registryName, resourceVersion, []string{clusterName}); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow updateResourceRegistry background thread - // to complete its execution. if err := upsertResourceRegistry(clientConnector, resourceSelectorsUpdated, registryName, resourceVersion, []string{clusterName}); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -496,8 +442,7 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -510,28 +455,26 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { func TestDeleteResourceRegistryEventHandler(t *testing.T) { tests := []struct { - name string - restConfig *rest.Config - client *fakekarmadaclient.Clientset - controlPlaneClient client.WithWatch - restMapper meta.RESTMapper - stopCh chan struct{} - prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) - verify func(*fakekarmadaclient.Clientset, *Controller, client.Client) error + name string + restConfig *rest.Config + client *fakekarmadaclient.Clientset + restMapper meta.RESTMapper + stopCh chan struct{} + prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) + verify func(*fakekarmadaclient.Clientset, *Controller) error }{ { - name: "AddAllEventHandlers_TriggerDeleteResourceRegistryEvent_DeletedResourceRegistryAddedToWorkQueue", - restConfig: &rest.Config{}, - client: fakekarmadaclient.NewSimpleClientset(), - controlPlaneClient: fake.NewFakeClient(), - restMapper: meta.NewDefaultRESTMapper(nil), - stopCh: make(chan struct{}), + name: "AddAllEventHandlers_TriggerDeleteResourceRegistryEvent_DeletedResourceRegistryAddedToWorkQueue", + restConfig: &rest.Config{}, + client: fakekarmadaclient.NewSimpleClientset(), + restMapper: meta.NewDefaultRESTMapper(nil), + stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) controller, err := createController(restConfig, factory, restMapper) return controller, factory, err }, - verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller, controlPlaneClient client.Client) error { + verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error { var ( registryName, clusterName = "test-registry", "test-cluster" resourceVersion, apiEndpoint = "1000", "10.0.0.1" @@ -544,9 +487,6 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) { } ) - controlPlaneClientBuilder = func(*rest.Config) client.Client { - return controlPlaneClient - } clusterDynamicClientBuilder = func(string, client.Client) (*util.DynamicClusterClient, error) { return &util.DynamicClusterClient{ DynamicClientSet: fakedynamic.NewSimpleDynamicClient(scheme.Scheme), @@ -554,32 +494,23 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) { }, nil } - // Wait a bit to allow addCluster - // background thread to complete its execution. if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow addResourceRegistry - // background thread to complete its execution. if err := upsertResourceRegistry(clientConnector, resourceSelectors, registryName, resourceVersion, []string{clusterName}); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } - // Wait a bit to allow deleteResourceRegistry - // background thread to complete its execution. if err := deleteResourceRegistry(clientConnector, registryName); err != nil { return err } - time.Sleep(time.Millisecond * 250) if err := cacheNextWrapper(controller); err != nil { return err } @@ -595,12 +526,11 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - // Add event handlers and start the informer to watch for changes. - controller.addAllEventHandlers() + // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) - if err := test.verify(test.client, controller, test.controlPlaneClient); err != nil { + if err := test.verify(test.client, controller); err != nil { t.Errorf("failed to verify controller, got: %v", err) } }) @@ -660,45 +590,6 @@ func upsertCluster(client *fakekarmadaclient.Clientset, labels map[string]string return err } -// upsertClusterControllerRuntime creates or updates a Cluster resource using the controller-runtime -// client. The function takes labels, API endpoint, and cluster name to define the Cluster. -func upsertClusterControllerRuntime(controlPlaneClient client.Client, labels map[string]string, clusterName, apiEndpoint string) error { - cluster := &clusterv1alpha1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: clusterName, - Labels: labels, - }, - Spec: clusterv1alpha1.ClusterSpec{ - APIEndpoint: apiEndpoint, - }, - Status: clusterv1alpha1.ClusterStatus{ - Conditions: []metav1.Condition{ - { - Type: clusterv1alpha1.ClusterConditionReady, - Status: metav1.ConditionTrue, - }, - }, - }, - } - - // Try to create the Cluster. - err := controlPlaneClient.Create(context.TODO(), cluster) - if err == nil { - // Successfully created the Cluster. - return nil - } - - // If the Cluster already exists, update it. - if apierrors.IsAlreadyExists(err) { - if updateErr := controlPlaneClient.Update(context.TODO(), cluster); updateErr != nil { - return fmt.Errorf("failed to update cluster: %v", updateErr) - } - return nil - } - - return err -} - // upsertResourceRegistry creates or updates a ResourceRegistry resource in the Kubernetes API. // It uses the provided client, resource selectors, registry name, resource version, and target cluster names. func upsertResourceRegistry(client *fakekarmadaclient.Clientset, resourceSelectors []searchv1alpha1.ResourceSelector, registryName, resourceVersion string, clusterNames []string) error { From 7da64235a13a8650f6cdc14c6cf0256cc81584a3 Mon Sep 17 00:00:00 2001 From: changzhen Date: Thu, 21 Nov 2024 17:14:00 +0800 Subject: [PATCH 2/2] add group/values for resources Signed-off-by: Mohamed Awnallah Signed-off-by: changzhen --- pkg/search/controllers_test.go | 64 +++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/pkg/search/controllers_test.go b/pkg/search/controllers_test.go index c8eaea453346..7b0f1f5c6e87 100644 --- a/pkg/search/controllers_test.go +++ b/pkg/search/controllers_test.go @@ -20,7 +20,6 @@ import ( "context" "errors" "fmt" - "strings" "testing" @@ -30,6 +29,7 @@ import ( fakedynamic "k8s.io/client-go/dynamic/fake" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "k8s.io/client-go/restmapper" "sigs.k8s.io/controller-runtime/pkg/client" clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" @@ -42,6 +42,41 @@ import ( "github.com/karmada-io/karmada/pkg/util" ) +var apiGroupResources = []*restmapper.APIGroupResources{ + { + Group: metav1.APIGroup{ + Name: "apps", + Versions: []metav1.GroupVersionForDiscovery{ + {GroupVersion: "apps/v1", Version: "v1"}, + }, + PreferredVersion: metav1.GroupVersionForDiscovery{ + GroupVersion: "apps/v1", Version: "v1", + }, + }, + VersionedResources: map[string][]metav1.APIResource{ + "v1": { + {Name: "deployments", SingularName: "deployment", Namespaced: true, Kind: "Deployment"}, + }, + }, + }, + { + Group: metav1.APIGroup{ + Name: "", + Versions: []metav1.GroupVersionForDiscovery{ + {GroupVersion: "v1", Version: "v1"}, + }, + PreferredVersion: metav1.GroupVersionForDiscovery{ + GroupVersion: "v1", Version: "v1", + }, + }, + VersionedResources: map[string][]metav1.APIResource{ + "v1": { + {Name: "pods", SingularName: "pod", Namespaced: true, Kind: "Pod"}, + }, + }, + }, +} + func TestNewKarmadaSearchController(t *testing.T) { tests := []struct { name string @@ -126,8 +161,6 @@ func TestAddClusterEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -189,8 +222,6 @@ func TestUpdateClusterEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -215,7 +246,7 @@ func TestDeleteClusterEventHandler(t *testing.T) { name: "AddAllEventHandlers_TriggerDeleteClusterEvent_DeletedClusterAddedToWorkQueue", restConfig: &rest.Config{}, client: fakekarmadaclient.NewSimpleClientset(), - restMapper: meta.NewDefaultRESTMapper(nil), + restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources), stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) @@ -278,8 +309,6 @@ func TestDeleteClusterEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -304,7 +333,7 @@ func TestAddResourceRegistryEventHandler(t *testing.T) { name: "AddAllEventHandlers_TriggerAddResourceRegistryEvent_ResourceRegistryAddedToWorkQueue", restConfig: &rest.Config{}, client: fakekarmadaclient.NewSimpleClientset(), - restMapper: meta.NewDefaultRESTMapper(nil), + restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources), stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) @@ -355,8 +384,6 @@ func TestAddResourceRegistryEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -381,7 +408,7 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { name: "AddAllEventHandlers_TriggerUpdateResourceRegistryEvent_UpdatedResourceRegistryAddedToWorkQueue", restConfig: &rest.Config{}, client: fakekarmadaclient.NewSimpleClientset(), - restMapper: meta.NewDefaultRESTMapper(nil), + restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources), stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) @@ -410,6 +437,13 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { } ) + clusterDynamicClientBuilder = func(string, client.Client) (*util.DynamicClusterClient, error) { + return &util.DynamicClusterClient{ + DynamicClientSet: fakedynamic.NewSimpleDynamicClient(scheme.Scheme), + ClusterName: clusterName, + }, nil + } + if err := upsertCluster(clientConnector, labels, apiEndpoint, clusterName, resourceVersion); err != nil { return err } @@ -441,8 +475,6 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh) @@ -467,7 +499,7 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) { name: "AddAllEventHandlers_TriggerDeleteResourceRegistryEvent_DeletedResourceRegistryAddedToWorkQueue", restConfig: &rest.Config{}, client: fakekarmadaclient.NewSimpleClientset(), - restMapper: meta.NewDefaultRESTMapper(nil), + restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources), stopCh: make(chan struct{}), prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) { factory := informerfactory.NewSharedInformerFactory(clientConnector, 0) @@ -525,8 +557,6 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err) } - - // start the informer to watch for changes. informer.Start(test.stopCh) defer close(test.stopCh)