Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/search/controllers_test: reduce the probability of flaky test cases by waiting for cache to sync #5936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions pkg/search/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
informerfactory "github.com/karmada-io/karmada/pkg/generated/informers/externalversions"
"github.com/karmada-io/karmada/pkg/search/backendstore"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
)

var apiGroupResources = []*restmapper.APIGroupResources{
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestAddClusterEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -134,9 +135,9 @@ func TestAddClusterEventHandler(t *testing.T) {
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) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand All @@ -158,11 +159,12 @@ func TestAddClusterEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -180,7 +182,7 @@ func TestUpdateClusterEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -189,9 +191,9 @@ func TestUpdateClusterEventHandler(t *testing.T) {
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) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand Down Expand Up @@ -220,11 +222,12 @@ func TestUpdateClusterEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -242,7 +245,7 @@ func TestDeleteClusterEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -251,9 +254,9 @@ func TestDeleteClusterEventHandler(t *testing.T) {
client: fakekarmadaclient.NewSimpleClientset(),
restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources),
stopCh: make(chan struct{}),
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand Down Expand Up @@ -308,11 +311,12 @@ func TestDeleteClusterEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -330,7 +334,7 @@ func TestAddResourceRegistryEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -339,9 +343,9 @@ func TestAddResourceRegistryEventHandler(t *testing.T) {
client: fakekarmadaclient.NewSimpleClientset(),
restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources),
stopCh: make(chan struct{}),
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand Down Expand Up @@ -384,11 +388,12 @@ func TestAddResourceRegistryEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -406,7 +411,7 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -415,9 +420,9 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) {
client: fakekarmadaclient.NewSimpleClientset(),
restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources),
stopCh: make(chan struct{}),
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand Down Expand Up @@ -476,11 +481,12 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -498,7 +504,7 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
client *fakekarmadaclient.Clientset
restMapper meta.RESTMapper
stopCh chan struct{}
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error)
prep func(*fakekarmadaclient.Clientset, *rest.Config, meta.RESTMapper, chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error)
verify func(*fakekarmadaclient.Clientset, *Controller) error
}{
{
Expand All @@ -507,9 +513,9 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
client: fakekarmadaclient.NewSimpleClientset(),
restMapper: restmapper.NewDiscoveryRESTMapper(apiGroupResources),
stopCh: make(chan struct{}),
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper) (*Controller, informerfactory.SharedInformerFactory, error) {
prep: func(clientConnector *fakekarmadaclient.Clientset, restConfig *rest.Config, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, informerfactory.SharedInformerFactory, error) {
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
controller, err := createController(restConfig, factory, restMapper, stopCh)
return controller, factory, err
},
verify: func(clientConnector *fakekarmadaclient.Clientset, controller *Controller) error {
Expand Down Expand Up @@ -559,11 +565,12 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper, test.stopCh)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
informer.WaitForCacheSync(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

Expand All @@ -577,11 +584,12 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
// createController initializes a new Controller instance using the provided
// Kubernetes REST configuration, shared informer factory, and REST mapper.
// It returns the created Controller or an error if initialization fails.
func createController(restConfig *rest.Config, factory informerfactory.SharedInformerFactory, restMapper meta.RESTMapper) (*Controller, error) {
func createController(restConfig *rest.Config, factory informerfactory.SharedInformerFactory, restMapper meta.RESTMapper, stopCh chan struct{}) (*Controller, error) {
newController, err := NewController(restConfig, factory, restMapper)
if err != nil {
return nil, fmt.Errorf("failed to create new controller, got: %v", err)
}
newController.InformerManager = genericmanager.NewMultiClusterInformerManager(stopCh)
mohamedawnallah marked this conversation as resolved.
Show resolved Hide resolved
return newController, nil
}

Expand Down