Skip to content

Commit

Permalink
pkg/search/controllers_test: address flake test
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Awnallah <[email protected]>
  • Loading branch information
mohamedawnallah committed Nov 25, 2024
1 parent 88c3e44 commit dcb6ab7
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions pkg/search/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"strings"
"testing"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestAddClusterEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -162,6 +163,7 @@ func TestAddClusterEventHandler(t *testing.T) {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand All @@ -188,7 +190,7 @@ func TestUpdateClusterEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -223,6 +225,7 @@ func TestUpdateClusterEventHandler(t *testing.T) {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand All @@ -249,7 +252,7 @@ func TestDeleteClusterEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -310,6 +313,7 @@ func TestDeleteClusterEventHandler(t *testing.T) {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand All @@ -336,7 +340,7 @@ func TestAddResourceRegistryEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -380,11 +384,13 @@ func TestAddResourceRegistryEventHandler(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fmt.Println("Running Test!!")
controller, informer, err := test.prep(test.client, test.restConfig, test.restMapper)
if err != nil {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand All @@ -411,7 +417,7 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -476,6 +482,7 @@ func TestUpdateResourceRegistryEventHandler(t *testing.T) {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand All @@ -502,7 +509,7 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
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)
factory := informerfactory.NewSharedInformerFactory(clientConnector, time.Second)
controller, err := createController(restConfig, factory, restMapper)
return controller, factory, err
},
Expand Down Expand Up @@ -558,6 +565,7 @@ func TestDeleteResourceRegistryEventHandler(t *testing.T) {
t.Fatalf("failed to prepare test environment for event handler setup, got: %v", err)
}
informer.Start(test.stopCh)
defer informer.Shutdown()
defer close(test.stopCh)

if err := test.verify(test.client, controller); err != nil {
Expand Down Expand Up @@ -597,6 +605,26 @@ func upsertCluster(client *fakekarmadaclient.Clientset, labels map[string]string
Status: metav1.ConditionTrue,
},
},
APIEnablements: []clusterv1alpha1.APIEnablement{
{
GroupVersion: "apps/v1",
Resources: []clusterv1alpha1.APIResource{
{
Name: "deployments",
Kind: "Deployment",
},
},
},
{
GroupVersion: "v1",
Resources: []clusterv1alpha1.APIResource{
{
Name: "pods",
Kind: "Pod",
},
},
},
},
},
}

Expand Down

0 comments on commit dcb6ab7

Please sign in to comment.